Skip to content

Commit 1a3888b

Browse files
committed
Update test scripts for Bzlmod compatibility
Part of bazel-contrib#1482. These are mostly small changes to make test assertions more flexible between `WORKSPACE` and Bzlmod runs. For Bzlmod runs: - Fixed `test_scala_config_content` from `test_scala_config.sh` by changing a path from `external/io_bazel_rules_scala_config` to `external/*io_bazel_rules_scala_config`. - Fixed a number of tests by updating expected output messages to allow them to start with either `@//` or `@@//`. - Fixed `test_stamped_target_label_loading` from `test/shell/test_strict_dependency.sh` by accommodating the canonical `io_bazel_rules_scala_guava` repo name. Also allows for the optional current Scala version suffix. Also made these other important changes: - Updated all the assertions in `test_helper.sh` to use Bash builtin regex matching via `_expect_failure_with_messages` instead of `grep`. This allows the expected message patterns to use full regular expressions while avoiding forking a new process. This new function helped reduce duplication in that file at the same time. - Added `--repo_env="SCALA_VERSION=..."` to each test script called from `./test_coverage.sh`, and set `SCALA_VERSION` to 2.12.19 in each of these files. Using other Scala versions technically works, but the output is slightly different, causing the `diff` commands in the test cases to fail. - Updated `test_version.sh` to copy the top level `.bazelversion` file into its test repo. - Changed how `test_version.sh` handles injecting `twitter_scrooge` repos into the `WORKSPACE` file. This will make it easy to do the equivalent for `MODULE.bazel` when the time comes. Also includes a few minor, opportunistic formatting cleanups.
1 parent cd22d88 commit 1a3888b

11 files changed

+107
-97
lines changed

test/shell/test_coverage_equals_in_target.sh

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
44
. "${dir}"/test_helper.sh
55
runner=$(get_test_runner "${1:-local}")
66

7+
SCALA_VERSION="${SCALA_VERSION:-2.12.19}"
8+
79
test_coverage_target_name_contains_equals_sign() {
8-
bazel coverage //test/coverage_filename_encoding:name-with-equals
10+
bazel coverage \
11+
--repo_env="SCALA_VERSION=${SCALA_VERSION}" \
12+
//test/coverage_filename_encoding:name-with-equals
913
diff test/coverage_filename_encoding/expected-coverage.dat $(bazel info bazel-testlogs)/test/coverage_filename_encoding/name-with-equals/coverage.dat
1014
}
1115

test/shell/test_coverage_scalatest.sh

+7-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@ dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
44
. "${dir}"/test_helper.sh
55
runner=$(get_test_runner "${1:-local}")
66

7+
SCALA_VERSION="${SCALA_VERSION:-2.12.19}"
8+
79
test_coverage_on() {
8-
bazel coverage //test/coverage_scalatest:test-scalatest
10+
bazel coverage \
11+
--repo_env="SCALA_VERSION=${SCALA_VERSION}" \
12+
//test/coverage_scalatest:test-scalatest
913
diff test/coverage_scalatest/expected-coverage.dat $(bazel info bazel-testlogs)/test/coverage_scalatest/test-scalatest/coverage.dat
1014
}
1115

1216
test_coverage_includes_test_targets() {
1317
bazel coverage \
14-
--instrument_test_targets=True \
15-
//test/coverage_scalatest:test-scalatest
18+
--instrument_test_targets=True \
19+
//test/coverage_scalatest:test-scalatest
1620
grep -q "SF:test/coverage_scalatest/TestWithScalaTest.scala" $(bazel info bazel-testlogs)/test/coverage_scalatest/test-scalatest/coverage.dat
1721
}
1822

test/shell/test_coverage_scalatest_resources.sh

+8-4
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@ dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
44
. "${dir}"/test_helper.sh
55
runner=$(get_test_runner "${1:-local}")
66

7+
SCALA_VERSION="${SCALA_VERSION:-2.12.19}"
8+
79
test_coverage_succeeds_resource_call() {
810
bazel coverage \
9-
--instrumentation_filter=^//test/coverage_scalatest_resources[:/] \
10-
//test/coverage_scalatest_resources/consumer:tests
11+
--repo_env="SCALA_VERSION=${SCALA_VERSION}" \
12+
--instrumentation_filter=^//test/coverage_scalatest_resources[:/] \
13+
//test/coverage_scalatest_resources/consumer:tests
1114
diff test/coverage_scalatest_resources/expected-coverage.dat $(bazel info bazel-testlogs)/test/coverage_scalatest_resources/consumer/tests/coverage.dat
1215
}
1316

1417
test_coverage_includes_resource_test_targets() {
1518
bazel coverage \
16-
--instrument_test_targets=True \
17-
//test/coverage_scalatest_resources/consumer:tests
19+
--repo_env="SCALA_VERSION=${SCALA_VERSION}" \
20+
--instrument_test_targets=True \
21+
//test/coverage_scalatest_resources/consumer:tests
1822
grep -q "SF:test/coverage_scalatest_resources/consumer/src/test/scala/com/example/consumer/ConsumerSpec.scala" $(bazel info bazel-testlogs)/test/coverage_scalatest_resources/consumer/tests/coverage.dat
1923
}
2024

test/shell/test_coverage_specs2_with_junit.sh

+8-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@ dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
44
. "${dir}"/test_helper.sh
55
runner=$(get_test_runner "${1:-local}")
66

7+
SCALA_VERSION="${SCALA_VERSION:-2.12.19}"
8+
79
test_coverage_on() {
8-
bazel coverage //test/coverage_specs2_with_junit:test-specs2-with-junit
10+
bazel coverage \
11+
--repo_env="SCALA_VERSION=${SCALA_VERSION}" \
12+
//test/coverage_specs2_with_junit:test-specs2-with-junit
913
diff test/coverage_specs2_with_junit/expected-coverage.dat $(bazel info bazel-testlogs)/test/coverage_specs2_with_junit/test-specs2-with-junit/coverage.dat
1014
}
1115

1216
test_coverage_includes_test_targets() {
1317
bazel coverage \
14-
--instrument_test_targets=True \
15-
//test/coverage_specs2_with_junit:test-specs2-with-junit
18+
--repo_env="SCALA_VERSION=${SCALA_VERSION}" \
19+
--instrument_test_targets=True \
20+
//test/coverage_specs2_with_junit:test-specs2-with-junit
1621
grep -q "SF:test/coverage_specs2_with_junit/TestWithSpecs2WithJUnit.scala" $(bazel info bazel-testlogs)/test/coverage_specs2_with_junit/test-specs2-with-junit/coverage.dat
1722
}
1823

test/shell/test_helper.sh

+23-36
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ action_should_fail() {
2424
fi
2525
}
2626

27+
_expect_failure_with_messages() {
28+
local bazel_cmd="$1"
29+
shift
30+
local expected_message=""
31+
32+
while [[ "$#" -ne 0 ]]; do
33+
expected_message="$1"
34+
shift
35+
36+
if [[ -n "$expected_message" && ! "$output" =~ $expected_message ]]; then
37+
echo ${output}
38+
echo "'${bazel_cmd}' should have logged \"${expected_message}\"."
39+
exit 1
40+
fi
41+
done
42+
}
43+
2744
test_expect_failure_with_message() {
2845
set +e
2946

@@ -34,21 +51,8 @@ test_expect_failure_with_message() {
3451
command="bazel test --nocache_test_results --test_output=streamed ${test_filter} ${test_command}"
3552
output=$(${command} 2>&1)
3653

37-
echo ${output} | grep "$expected_message"
38-
if [ $? -ne 0 ]; then
39-
echo ${output}
40-
echo "'bazel test ${test_command}' should have logged \"${expected_message}\"."
41-
exit 1
42-
fi
43-
if [ "${additional_expected_message}" != "" ]; then
44-
echo ${output} | grep "$additional_expected_message"
45-
if [ $? -ne 0 ]; then
46-
echo ${output}
47-
echo "'bazel test ${test_command}' should have logged \"${additional_expected_message}\"."
48-
exit 1
49-
fi
50-
fi
51-
54+
_expect_failure_with_messages "bazel test ${test_command}" \
55+
"$expected_message" "$additional_expected_message"
5256
set -e
5357
}
5458

@@ -58,13 +62,11 @@ action_should_fail_with_message() {
5862
TEST_ARG=${@:2}
5963
RES=$(bazel $TEST_ARG 2>&1)
6064
RESPONSE_CODE=$?
61-
echo $RES | grep -- "$MSG"
62-
GREP_RES=$?
6365
if [ $RESPONSE_CODE -eq 0 ]; then
6466
echo $RES
6567
echo -e "${RED} \"bazel $TEST_ARG\" should have failed but passed. $NC"
6668
exit 1
67-
elif [ $GREP_RES -ne 0 ]; then
69+
elif [[ ! "$RES" =~ $MSG ]]; then
6870
echo $RES
6971
echo -e "${RED} \"bazel $TEST_ARG\" should have failed with message \"$MSG\" but did not. $NC"
7072
exit 1
@@ -79,13 +81,11 @@ action_should_fail_without_message() {
7981
TEST_ARG=${@:2}
8082
RES=$(bazel $TEST_ARG 2>&1)
8183
RESPONSE_CODE=$?
82-
echo $RES | grep -- "$MSG"
83-
GREP_RES=$?
8484
if [ $RESPONSE_CODE -eq 0 ]; then
8585
echo $RES
8686
echo -e "${RED} \"bazel $TEST_ARG\" should have failed but passed. $NC"
8787
exit 1
88-
elif [ $GREP_RES -eq 0 ]; then
88+
elif [[ "$RES" =~ $MSG ]]; then
8989
echo $RES
9090
echo -e "${RED} \"bazel $TEST_ARG\" should have failed with message not containing \"$MSG\" but it did. $NC"
9191
exit 1
@@ -120,21 +120,8 @@ test_expect_failure_or_warning_on_missing_direct_deps_with_expected_message() {
120120
exit 1
121121
fi
122122

123-
echo ${output} | grep "$expected_message"
124-
if [ $? -ne 0 ]; then
125-
echo ${output}
126-
echo "'bazel build ${test_target}' should have logged \"${expected_message}\"."
127-
exit 1
128-
fi
129-
if [ "${additional_expected_message}" != "" ]; then
130-
echo ${output} | grep "$additional_expected_message"
131-
if [ $? -ne 0 ]; then
132-
echo ${output}
133-
echo "'bazel build ${test_target}' should have logged \"${additional_expected_message}\"."
134-
exit 1
135-
fi
136-
fi
137-
123+
_expect_failure_with_messages "bazel build ${test_target}" \
124+
"$expected_message" "$additional_expected_message"
138125
set -e
139126
}
140127

test/shell/test_scala_config.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ test_classpath_contains_2_13() {
1818

1919
test_scala_config_content() {
2020
bazel build --repo_env=SCALA_VERSION=0.0.0 @io_bazel_rules_scala_config//:all 2> /dev/null
21-
grep "SCALA_MAJOR_VERSION='0.0'" $(bazel info output_base)/external/io_bazel_rules_scala_config/config.bzl
21+
grep "SCALA_MAJOR_VERSION='0.0'" \
22+
"$(bazel info output_base)"/external/*io_bazel_rules_scala_config/config.bzl
2223
}
2324

2425
$runner test_classpath_contains_2_12

test/shell/test_scala_library.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ test_scala_library_expect_better_failure_with_target_label_from_stamped_jar_on_m
177177

178178
test_scala_library_expect_better_failure_message_on_missing_transitive_dependency_labels_from_other_jvm_rules() {
179179
transitive_target='.*transitive_dependency_without_manifest.jar'
180-
direct_target='@//test_expect_failure/missing_direct_deps/internal_deps:unstamped_direct_java_provider_dependency'
180+
direct_target='@@?//test_expect_failure/missing_direct_deps/internal_deps:unstamped_direct_java_provider_dependency'
181181
test_target='//test_expect_failure/missing_direct_deps/internal_deps:unstamped_jar_dependent_on_some_java_provider'
182182

183183
expected_message="Unknown label of file $transitive_target which came from $direct_target"

test/shell/test_semanticdb.sh

+20-20
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ test_produces_semanticdb(){
2424

2525

2626
if [ $is_bundle -eq 1 ]; then
27-
local toolchain="--extra_toolchains=//test/semanticdb:semanticdb_bundle_toolchain"
27+
local toolchain="--extra_toolchains=//test/semanticdb:semanticdb_bundle_toolchain"
2828
else
2929
local toolchain="--extra_toolchains=//test/semanticdb:semanticdb_nobundle_toolchain"
3030
fi
@@ -36,7 +36,7 @@ test_produces_semanticdb(){
3636

3737
bazel build //test/semanticdb:semantic_provider_vars_all ${toolchain} ${version_opt}
3838

39-
#semantic_provider_vars.sh contains the SemanticdbInfo data
39+
#semantic_provider_vars.sh contains the SemanticdbInfo data
4040
. $(bazel info bazel-bin)/test/semanticdb/semantic_provider_vars_all.sh
4141

4242
#Check the Provider variables
@@ -61,7 +61,7 @@ test_produces_semanticdb(){
6161
echo "Error: SemanticdbInfo.target_root expected to be empty string"
6262
exit 1
6363
fi
64-
fi
64+
fi
6565

6666
if [[ $scala_majver == 3 ]] && [[ $semanticdb_pluginjarpath != "" ]]; then
6767
echo "Error: SemanticdbInfo.pluginjarpath expected to be empty for scala 3"
@@ -73,7 +73,7 @@ test_produces_semanticdb(){
7373
fi
7474

7575
if [ $is_bundle -eq 0 ]; then
76-
76+
7777
semanticdb_path="$(bazel info execution_root)/${semanticdb_target_root}/META-INF/semanticdb/test/semanticdb/"
7878

7979
for arg in $FILES
@@ -85,8 +85,8 @@ test_produces_semanticdb(){
8585
fi
8686
done
8787
fi
88-
89-
local JAR="$(bazel info bazel-bin)/test/semanticdb/all_lib.jar"
88+
89+
local JAR="$(bazel info bazel-bin)/test/semanticdb/all_lib.jar"
9090

9191
if [ $is_bundle -eq 0 ]; then
9292
if jar_contains_files $JAR $FILES; then
@@ -110,11 +110,11 @@ test_empty_semanticdb(){
110110
}
111111

112112
test_no_semanticdb() {
113-
#verify no semanticdb files have been generated in the bin dir or bundled in the jar
113+
#verify no semanticdb files have been generated in the bin dir or bundled in the jar
114114

115115
set -e
116116

117-
local jar="$(bazel info bazel-bin)/test/semanticdb/all_lib.jar"
117+
local jar="$(bazel info bazel-bin)/test/semanticdb/all_lib.jar"
118118
local targetout_path="$(bazel info bazel-bin)/test/semanticdb"
119119

120120
rm -rf $targetout_path #clean out the output dir for clean slate
@@ -137,7 +137,7 @@ test_no_semanticdb() {
137137

138138
test_semanticdb_handles_removed_sourcefiles() {
139139
#Ensure absense of bug where bazel failed with 'access denied' on Windows when a source file was removed.
140-
140+
141141
#First add the new scala file, build it, then remove the new scala file, and ensure it builds.
142142
set -e
143143

@@ -151,7 +151,7 @@ test_semanticdb_handles_removed_sourcefiles() {
151151
mkdir -p $newfile_dir && echo "class D{ val a = 1; }" > $newfilepath
152152

153153

154-
#make sure D.scala was added to the target (sanity check)
154+
#make sure D.scala was added to the target (sanity check)
155155
local query_result1=$(bazel query "labels(srcs, $rule_label)")
156156
if [[ $query_result1 != *"$newfilename"* ]] ; then
157157
echo "$newfilename was not properly added as src for target $rule_label"
@@ -162,30 +162,30 @@ test_semanticdb_handles_removed_sourcefiles() {
162162

163163
#remove the new source file and build it
164164
rm $newfilepath
165-
165+
166166
#make sure D.scala was properly removed from the target(sanity check)
167167
local query_result2=$(bazel query "labels(srcs, $rule_label)")
168-
if [[ $query_result2 == *"$newfilename"* ]] ; then
168+
if [[ $query_result2 == *"$newfilename"* ]] ; then
169169
echo "$newfilename was not properly removed as src for target $rule_label"
170170
exit 1
171171
fi
172-
172+
173173
bazel build $rule_label $toolchainArg
174174

175-
175+
176176
}
177177

178178
run_semanticdb_tests() {
179179
local bundle=1; local nobundle=0
180180
local scala3=3; local scala2=2
181-
182-
$runner test_produces_semanticdb $scala2 $bundle
183-
$runner test_produces_semanticdb $scala2 $nobundle
184-
181+
182+
$runner test_produces_semanticdb $scala2 $bundle
183+
$runner test_produces_semanticdb $scala2 $nobundle
184+
185185
$runner test_empty_semanticdb
186186

187-
$runner test_produces_semanticdb $scala3 $bundle
188-
$runner test_produces_semanticdb $scala3 $nobundle
187+
$runner test_produces_semanticdb $scala3 $bundle
188+
$runner test_produces_semanticdb $scala3 $nobundle
189189

190190
$runner test_no_semanticdb
191191
$runner test_semanticdb_handles_removed_sourcefiles

test/shell/test_strict_dependency.sh

+5-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ test_plus_one_ast_analyzer_strict_deps() {
4444

4545
test_stamped_target_label_loading() {
4646
local test_target="//test_expect_failure/missing_direct_deps/external_deps:java_lib_with_a_transitive_external_dep"
47-
local expected_message="buildozer 'add deps @io_bazel_rules_scala_guava//:io_bazel_rules_scala_guava' ${test_target}"
47+
local missing_dep="@@?[a-z_.~+-]*io_bazel_rules_scala_guava[_0-9]*//:io_bazel_rules_scala_guava[_0-9]*"
48+
local expected_message="buildozer 'add deps ${missing_dep}' ${test_target}"
4849

4950
test_expect_failure_or_warning_on_missing_direct_deps_with_expected_message \
5051
"${expected_message}" ${test_target} \
@@ -69,7 +70,7 @@ test_strict_deps_filter_included_target() {
6970

7071
test_demonstrate_INCORRECT_scala_proto_library_stamp() {
7172
local test_target="//test_expect_failure/missing_direct_deps/scala_proto_deps:uses_transitive_scala_proto"
72-
local incorrectly_stamped_expected_message="buildozer 'add deps @//test_expect_failure/missing_direct_deps/scala_proto_deps:some_proto' ${test_target}"
73+
local incorrectly_stamped_expected_message="buildozer 'add deps @@?//test_expect_failure/missing_direct_deps/scala_proto_deps:some_proto' ${test_target}"
7374

7475
# When stamping is fixed, expected stamp is:
7576
# local correctly_stamped_expected_message="buildozer 'add deps //test_expect_failure/missing_direct_deps/scala_proto_deps:some_scala_proto' ${test_target}"
@@ -82,7 +83,7 @@ test_demonstrate_INCORRECT_scala_proto_library_stamp() {
8283

8384
test_scala_proto_library_stamp_by_convention() {
8485
local test_target="//test_expect_failure/missing_direct_deps/scala_proto_deps:uses_transitive_scala_proto"
85-
local expected_message="buildozer 'add deps @//test_expect_failure/missing_direct_deps/scala_proto_deps:some_scala_proto' ${test_target}"
86+
local expected_message="buildozer 'add deps @@?//test_expect_failure/missing_direct_deps/scala_proto_deps:some_scala_proto' ${test_target}"
8687

8788
test_expect_failure_or_warning_on_missing_direct_deps_with_expected_message \
8889
"${expected_message}" ${test_target} \
@@ -92,7 +93,7 @@ test_scala_proto_library_stamp_by_convention() {
9293

9394
test_scala_proto_library_custom_phase_stamping() {
9495
local test_target="//test_expect_failure/missing_direct_deps/scala_proto_deps:uses_transitive_some_proto_custom_suffix"
95-
local expected_message="buildozer 'add deps @//test_expect_failure/missing_direct_deps/scala_proto_deps:some_proto_custom_suffix' ${test_target}"
96+
local expected_message="buildozer 'add deps @@?//test_expect_failure/missing_direct_deps/scala_proto_deps:some_proto_custom_suffix' ${test_target}"
9697

9798
test_expect_failure_or_warning_on_missing_direct_deps_with_expected_message \
9899
"${expected_message}" ${test_target} \

0 commit comments

Comments
 (0)