Skip to content

Commit 85d118f

Browse files
authored
Consolidate Bazel flags in a single place for tests (#391)
* Test flags are now stored in .test-bazelrc. This is referenced by .travis.yml and by all non-Bazel test scripts. * Common functionality for non-Bazel test scripts is now in non_bazel_tests_common.bash. This includes Bash functions that help run Bazel with appropriate options. All test scripts source this. * Scripts that create new workspaces now run in batch mode. This prevents Bazel servers from hanging around in memory.
1 parent 4acbe9a commit 85d118f

File tree

9 files changed

+59
-30
lines changed

9 files changed

+59
-30
lines changed

.test-bazelrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# This file contains options passed to Bazel when running tests.
2+
# They are used by Travis CI and by non-Bazel test scripts.
3+
build --verbose_failures --sandbox_debug --test_output=errors --spawn_strategy=standalone --genrule_strategy=standalone
4+
test --test_strategy=standalone

.travis.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,9 @@ script:
4141
--batch \
4242
--host_jvm_args=-Xmx500m \
4343
--host_jvm_args=-Xms500m \
44+
--bazelrc=.test-bazelrc \
4445
test \
45-
--verbose_failures \
46-
--sandbox_debug \
47-
--test_output=errors \
48-
--test_strategy=standalone \
49-
--spawn_strategy=standalone \
50-
--genrule_strategy=standalone \
46+
--experimental_repository_cache="$HOME/.bazel_repository_cache" \
5147
--local_resources=400,1,1.0 \
5248
//...
5349
- tests/run_non_bazel_tests.bash

tests/cgo_library_root_dir/cgo_library_root_dir.bash

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
set -euo pipefail
2121

2222
TEST_DIR=$(cd $(dirname "$0"); pwd)
23-
RULES_DIR=$(cd "$TEST_DIR/../.."; pwd)
23+
source "$TEST_DIR/../non_bazel_tests_common.bash"
24+
2425
WORKSPACE_DIR=$(mktemp -d)
2526

2627
function cleanup {
@@ -31,7 +32,4 @@ trap cleanup EXIT
3132
cp -r "$TEST_DIR"/* "$WORKSPACE_DIR"
3233
cd "$WORKSPACE_DIR"
3334
sed -e "s|@@RULES_DIR@@|$RULES_DIR|" <WORKSPACE.in >WORKSPACE
34-
bazel test \
35-
--genrule_strategy=standalone \
36-
--spawn_strategy=standalone \
37-
//:go_default_test
35+
bazel_batch_test //:go_default_test

tests/custom_go_toolchain/custom_go_toolchain.bash

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
set -euo pipefail
2626

2727
TEST_DIR=$(cd $(dirname "$0"); pwd)
28-
RULES_DIR=$(cd "$TEST_DIR/../.."; pwd)
28+
source "$TEST_DIR/../non_bazel_tests_common.bash"
2929

3030
GO_VERSION=1.7.5
3131
WORKSPACE_DIR=$(mktemp -d)
@@ -79,11 +79,7 @@ EOF
7979
cp "$TEST_DIR"/BUILD "$WORKSPACE_DIR"
8080
cp "$TEST_DIR"/print_version.go "$WORKSPACE_DIR"
8181
pushd "$WORKSPACE_DIR"
82-
ACTUAL_VERSION=$(bazel \
83-
run \
84-
--genrule_strategy=standalone \
85-
--spawn_strategy=standalone \
86-
//:print_version)
82+
ACTUAL_VERSION=$(bazel_batch_run //:print_version)
8783
popd
8884
if [ "$ACTUAL_VERSION" != "go$GO_VERSION" ]; then
8985
echo "bad version; got $ACTUAL_VERSION, want $GO_VERSION" >&1

tests/gc_opts_unsafe/gc_opts_unsafe.bash

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
# error message.
1111

1212
cd $(dirname "$0")
13+
source ../non_bazel_tests_common.bash
1314

1415
result=0
1516

1617
function check_build_fails {
1718
local target=$1
1819
local message=$2
1920
local outfile=$(mktemp)
20-
bazel build "$target" 2>&1 | tee "$outfile"
21+
bazel_build "$target" 2>&1 | tee "$outfile"
2122
local target_result=${PIPESTATUS[0]}
2223
if [ $target_result -eq 0 ]; then
2324
echo "build of $target succeeded but should have failed" >&2

tests/new_go_repository_build_name/new_go_repository_build_name.bash

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
set -euo pipefail
2525

2626
TEST_DIR=$(cd $(dirname "$0"); pwd)
27-
RULES_DIR=$(cd "$TEST_DIR/../.."; pwd)
27+
source "$TEST_DIR/../non_bazel_tests_common.bash"
28+
2829
WORKSPACE_DIR=$(mktemp -d)
2930

3031
function cleanup {
@@ -45,7 +46,4 @@ sed \
4546
-e "s|@@RULES_DIR@@|$RULES_DIR|" \
4647
-e "s|@@WORKSPACE_DIR@@|$WORKSPACE_DIR|" \
4748
<WORKSPACE.in >WORKSPACE
48-
bazel test \
49-
--genrule_strategy=standalone \
50-
--spawn_strategy=standalone \
51-
//:go_default_test
49+
bazel_batch_test //:go_default_test

tests/non_bazel_tests_common.bash

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2017 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
export RULES_DIR=$(cd $(dirname "${BASH_SOURCE[0]}")/..; pwd)
16+
export BAZELRC=$RULES_DIR/.test-bazelrc
17+
export BAZEL_BUILD_OPTS="--experimental_repository_cache=$HOME/.bazel_repository_cache"
18+
19+
function bazel_build {
20+
bazel --bazelrc="$BAZELRC" build $BAZEL_BUILD_OPTS "$@"
21+
}
22+
function bazel_batch_build {
23+
bazel --bazelrc="$BAZELRC" --batch build $BAZEL_BUILD_OPTS "$@"
24+
}
25+
function bazel_test {
26+
bazel --bazelrc="$BAZELRC" test $BAZEL_BUILD_OPTS "$@"
27+
}
28+
function bazel_batch_test {
29+
bazel --bazelrc="$BAZELRC" --batch test $BAZEL_BUILD_OPTS "$@"
30+
}
31+
function bazel_run {
32+
bazel --bazelrc="$BAZELRC" run $BAZEL_BUILD_OPTS "$@"
33+
}
34+
function bazel_batch_run {
35+
bazel --bazelrc="$BAZELRC" --batch run $BAZEL_BUILD_OPTS "$@"
36+
}
37+
export -f bazel_build bazel_test bazel_run
38+
export -f bazel_batch_build bazel_batch_test bazel_batch_run

tests/test_filter_test/test_filter_test.bash

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22

33
# Check that --test_filter can be used to avoid a failing test case.
44
cd $(dirname "$0")
5-
exec bazel test --test_filter=Pass :go_default_test
5+
source ../non_bazel_tests_common.bash
6+
7+
bazel_test --test_filter=Pass :go_default_test

tests/test_filter_test_1.7.5/test_filter_test_1.7.5.bash

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
set -euo pipefail
2525

2626
TEST_DIR=$(cd $(dirname "$0"); pwd)
27-
RULES_DIR=$(cd "$TEST_DIR/../.."; pwd)
27+
source "$TEST_DIR/../non_bazel_tests_common.bash"
2828
WORKSPACE_DIR=$(mktemp -d)
2929
TEST_FILES=(
3030
BUILD
@@ -50,8 +50,4 @@ for file in "${TEST_FILES[@]}"; do
5050
done
5151

5252
cd "$WORKSPACE_DIR"
53-
bazel test \
54-
--test_filter=Pass \
55-
--genrule_strategy=standalone \
56-
--spawn_strategy=standalone \
57-
:go_default_test
53+
bazel_batch_test --test_filter=Pass //:go_default_test

0 commit comments

Comments
 (0)