forked from bazelbuild/rules_scala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_run.sh
executable file
·490 lines (416 loc) · 15.9 KB
/
test_run.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#!/bin/bash
set -e
test_disappearing_class() {
git checkout test_expect_failure/disappearing_class/ClassProvider.scala
bazel build test_expect_failure/disappearing_class:uses_class
echo -e "package scala.test\n\nobject BackgroundNoise{}" > test_expect_failure/disappearing_class/ClassProvider.scala
set +e
bazel build test_expect_failure/disappearing_class:uses_class
RET=$?
git checkout test_expect_failure/disappearing_class/ClassProvider.scala
if [ $RET -eq 0 ]; then
echo "Class caching at play. This should fail"
exit 1
fi
set -e
}
md5_util() {
if [[ "$OSTYPE" == "darwin"* ]]; then
_md5_util="md5"
else
_md5_util="md5sum"
fi
echo "$_md5_util"
}
test_build_is_identical() {
bazel build test/...
$(md5_util) bazel-bin/test/*.jar > hash1
bazel clean
bazel build test/...
$(md5_util) bazel-bin/test/*.jar > hash2
diff hash1 hash2
}
test_transitive_deps() {
set +e
bazel build test_expect_failure/transitive/scala_to_scala:d
if [ $? -eq 0 ]; then
echo "'bazel build test_expect_failure/transitive/scala_to_scala:d' should have failed."
exit 1
fi
bazel build test_expect_failure/transitive/java_to_scala:d
if [ $? -eq 0 ]; then
echo "'bazel build test_expect_failure/transitive/java_to_scala:d' should have failed."
exit 1
fi
bazel build test_expect_failure/transitive/scala_to_java:d
if [ $? -eq 0 ]; then
echo "'bazel build test_transitive_deps/scala_to_java:d' should have failed."
exit 1
fi
set -e
exit 0
}
test_scala_library_suite() {
action_should_fail build test_expect_failure/scala_library_suite:library_suite_dep_on_children
}
test_expect_failure_or_warning_on_missing_direct_deps_with_expected_message() {
set +e
expected_message=$1
test_target=$2
strict_deps_mode=$3
operator=${4:-"eq"}
if [ "${operator}" = "eq" ]; then
error_message="bazel build of scala_library with missing direct deps should have failed."
else
error_message="bazel build of scala_library with missing direct deps should not have failed."
fi
command="bazel build ${test_target} ${strict_deps_mode}"
output=$(${command} 2>&1)
status_code=$?
echo "$output"
if [ ${status_code} -${operator} 0 ]; then
echo ${error_message}
exit 1
fi
echo ${output} | grep "$expected_message"
if [ $? -ne 0 ]; then
echo "'bazel build ${test_target}' should have logged \"${expected_message}\"."
exit 1
fi
set -e
}
test_scala_library_expect_failure_on_missing_direct_deps_strict_is_disabled_by_default() {
expected_message="not found: value C"
test_target='test_expect_failure/missing_direct_deps/internal_deps:transitive_dependency_user'
test_expect_failure_or_warning_on_missing_direct_deps_with_expected_message "$expected_message" $test_target ""
}
test_scala_library_expect_failure_on_missing_direct_deps() {
dependenecy_target=$1
test_target=$2
local expected_message="buildozer 'add deps $dependenecy_target' //$test_target"
test_expect_failure_or_warning_on_missing_direct_deps_with_expected_message "${expected_message}" $test_target "--strict_java_deps=error"
}
test_scala_library_expect_failure_on_missing_direct_internal_deps() {
dependenecy_target='//test_expect_failure/missing_direct_deps/internal_deps:transitive_dependency'
test_target='test_expect_failure/missing_direct_deps/internal_deps:transitive_dependency_user'
test_scala_library_expect_failure_on_missing_direct_deps $dependenecy_target $test_target
}
test_scala_binary_expect_failure_on_missing_direct_deps() {
dependency_target='//test_expect_failure/missing_direct_deps/internal_deps:transitive_dependency'
test_target='test_expect_failure/missing_direct_deps/internal_deps:user_binary'
test_scala_library_expect_failure_on_missing_direct_deps ${dependency_target} ${test_target}
}
test_scala_library_expect_failure_on_missing_direct_external_deps_jar() {
dependenecy_target='@com_google_guava_guava_21_0//jar:jar'
test_target='test_expect_failure/missing_direct_deps/external_deps:transitive_external_dependency_user'
test_scala_library_expect_failure_on_missing_direct_deps $dependenecy_target $test_target
}
test_scala_library_expect_failure_on_missing_direct_external_deps_file_group() {
dependenecy_target='@com_google_guava_guava_21_0//jar:file'
test_target='test_expect_failure/missing_direct_deps/external_deps:transitive_external_dependency_user_file_group'
test_scala_library_expect_failure_on_missing_direct_deps $dependenecy_target $test_target
}
test_scala_library_expect_failure_on_missing_direct_deps_warn_mode() {
dependenecy_target='//test_expect_failure/missing_direct_deps/internal_deps:transitive_dependency'
test_target='test_expect_failure/missing_direct_deps/internal_deps:transitive_dependency_user'
expected_message="warning: Target '$dependenecy_target' is used but isn't explicitly declared, please add it to the deps"
test_expect_failure_or_warning_on_missing_direct_deps_with_expected_message "${expected_message}" ${test_target} "--strict_java_deps=warn" "ne"
}
test_scala_library_expect_failure_on_missing_direct_deps_off_mode() {
expected_message="test_expect_failure/missing_direct_deps/internal_deps/A.scala:[0-9+]: error: not found: value C"
test_target='test_expect_failure/missing_direct_deps/internal_deps:transitive_dependency_user'
test_expect_failure_or_warning_on_missing_direct_deps_with_expected_message "${expected_message}" ${test_target} "--strict_java_deps=off"
}
test_scala_junit_test_can_fail() {
action_should_fail test test_expect_failure/scala_junit_test:failing_test
}
test_repl() {
echo "import scala.test._; HelloLib.printMessage(\"foo\")" | bazel-bin/test/HelloLibRepl | grep "foo java" &&
echo "import scala.test._; TestUtil.foo" | bazel-bin/test/HelloLibTestRepl | grep "bar" &&
echo "import scala.test._; ScalaLibBinary.main(Array())" | bazel-bin/test/ScalaLibBinaryRepl | grep "A hui hou" &&
echo "import scala.test._; ResourcesStripScalaBinary.main(Array())" | bazel-bin/test/ResourcesStripScalaBinaryRepl | grep "More Hello"
echo "import scala.test._; A.main(Array())" | bazel-bin/test/ReplWithSources | grep "4 8 15"
}
test_benchmark_jmh() {
RES=$(bazel run -- test/jmh:test_benchmark -i1 -f1 -wi 1)
RESPONSE_CODE=$?
if [[ $RES != *Result*Benchmark* ]]; then
echo "Benchmark did not produce expected output:\n$RES"
exit 1
fi
exit $RESPONSE_CODE
}
NC='\033[0m'
GREEN='\033[0;32m'
RED='\033[0;31m'
TIMOUT=60
run_test_ci() {
# spawns the test to new process
local TEST_ARG=$@
local log_file=output_$$.log
echo "running test $TEST_ARG"
$TEST_ARG &>$log_file &
local test_pid=$!
SECONDS=0
test_pulse_printer $! $TIMOUT $TEST_ARG &
local pulse_printer_pid=$!
local result
{
wait $test_pid 2>/dev/null
result=$?
kill $pulse_printer_pid && wait $pulse_printer_pid 2>/dev/null || true
} || return 1
DURATION=$SECONDS
if [ $result -eq 0 ]; then
echo -e "\n${GREEN}Test \"$TEST_ARG\" successful ($DURATION sec) $NC"
else
echo -e "\nLog:\n"
cat $log_file
echo -e "\n${RED}Test \"$TEST_ARG\" failed $NC ($DURATION sec) $NC"
fi
return $result
}
test_pulse_printer() {
# makes sure something is printed to stdout while test is running
local test_pid=$1
shift
local timeout=$1 # in minutes
shift
local count=0
# clear the line
echo -e "\n"
while [ $count -lt $timeout ]; do
count=$(($count + 1))
echo -ne "Still running: \"$@\"\r"
sleep 60
done
echo -e "\n${RED}Timeout (${timeout} minutes) reached. Terminating \"$@\"${NC}\n"
kill -9 $test_pid
}
run_test_local() {
# runs the tests locally
set +e
SECONDS=0
TEST_ARG=$@
echo "running test $TEST_ARG"
RES=$($TEST_ARG 2>&1)
RESPONSE_CODE=$?
DURATION=$SECONDS
if [ $RESPONSE_CODE -eq 0 ]; then
echo -e "${GREEN} Test \"$TEST_ARG\" successful ($DURATION sec) $NC"
else
echo -e "\nLog:\n"
echo "$RES"
echo -e "${RED} Test \"$TEST_ARG\" failed $NC ($DURATION sec) $NC"
exit $RESPONSE_CODE
fi
}
action_should_fail() {
# runs the tests locally
set +e
TEST_ARG=$@
$(bazel $TEST_ARG)
RESPONSE_CODE=$?
if [ $RESPONSE_CODE -eq 0 ]; then
echo -e "${RED} \"bazel $TEST_ARG\" should have failed but passed. $NC"
exit -1
else
exit 0
fi
}
xmllint_test() {
find -L ./bazel-testlogs -iname "*.xml" | xargs -n1 xmllint > /dev/null
}
multiple_junit_suffixes() {
bazel test //test:JunitMultipleSuffixes
matches=$(grep -c -e 'Discovered classes' -e 'scala.test.junit.JunitSuffixIT' -e 'scala.test.junit.JunitSuffixE2E' ./bazel-testlogs/test/JunitMultipleSuffixes/test.log)
if [ $matches -eq 3 ]; then
return 0
else
return 1
fi
}
multiple_junit_prefixes() {
bazel test //test:JunitMultiplePrefixes
matches=$(grep -c -e 'Discovered classes' -e 'scala.test.junit.TestJunitCustomPrefix' -e 'scala.test.junit.OtherCustomPrefixJunit' ./bazel-testlogs/test/JunitMultiplePrefixes/test.log)
if [ $matches -eq 3 ]; then
return 0
else
return 1
fi
}
multiple_junit_patterns() {
bazel test //test:JunitPrefixesAndSuffixes
matches=$(grep -c -e 'Discovered classes' -e 'scala.test.junit.TestJunitCustomPrefix' -e 'scala.test.junit.JunitSuffixE2E' ./bazel-testlogs/test/JunitPrefixesAndSuffixes/test.log)
if [ $matches -eq 3 ]; then
return 0
else
return 1
fi
}
junit_generates_xml_logs() {
bazel test //test:JunitTestWithDeps
matches=$(grep -c -e "testcase name='hasCompileTimeDependencies'" -e "testcase name='hasRuntimeDependencies'" ./bazel-testlogs/test/JunitTestWithDeps/test.xml)
if [ $matches -eq 2 ]; then
return 0
else
return 1
fi
test -e
}
test_junit_test_must_have_prefix_or_suffix() {
action_should_fail test test_expect_failure/scala_junit_test:no_prefix_or_suffix
}
test_junit_test_errors_when_no_tests_found() {
action_should_fail test test_expect_failure/scala_junit_test:no_tests_found
}
test_resources() {
RESOURCE_NAME="resource.txt"
TARGET=$1
OUTPUT_JAR="bazel-bin/test/src/main/scala/scala/test/resources/$TARGET.jar"
FULL_TARGET="test/src/main/scala/scala/test/resources/$TARGET.jar"
bazel build $FULL_TARGET
jar tf $OUTPUT_JAR | grep $RESOURCE_NAME
}
scala_library_jar_without_srcs_must_include_direct_file_resources(){
test_resources "noSrcsWithDirectFileResources"
}
scala_library_jar_without_srcs_must_include_filegroup_resources(){
test_resources "noSrcsWithFilegroupResources"
}
scala_library_jar_without_srcs_must_fail_on_mismatching_resource_strip_prefix() {
action_should_fail build test_expect_failure/wrong_resource_strip_prefix:noSrcsJarWithWrongStripPrefix
}
scala_test_test_filters() {
# test package wildcard (both)
local output=$(bazel test \
--cache_test_results=no \
--test_output streamed \
--test_filter scala.test.* \
test:TestFilterTests)
if [[ $output != *"tests a"* || $output != *"tests b"* ]]; then
echo "Should have contained test output from both test filter test a and b"
exit 1
fi
# test just one
local output=$(bazel test \
--cache_test_results=no \
--test_output streamed \
--test_filter scala.test.TestFilterTestA \
test:TestFilterTests)
if [[ $output != *"tests a"* || $output == *"tests b"* ]]; then
echo "Should have only contained test output from test filter test a"
exit 1
fi
}
scala_junit_test_test_filter(){
local output=$(bazel test \
--nocache_test_results \
--test_output=streamed \
'--test_filter=scala.test.junit.FirstFilterTest#(method1|method2)$|scala.test.junit.SecondFilterTest#(method2|method3)$' \
test:JunitFilterTest)
local expected=(
"scala.test.junit.FirstFilterTest#method1"
"scala.test.junit.FirstFilterTest#method2"
"scala.test.junit.SecondFilterTest#method2"
"scala.test.junit.SecondFilterTest#method3")
local unexpected=(
"scala.test.junit.FirstFilterTest#method3"
"scala.test.junit.SecondFilterTest#method1"
"scala.test.junit.ThirdFilterTest#method1"
"scala.test.junit.ThirdFilterTest#method2"
"scala.test.junit.ThirdFilterTest#method3")
for method in "${expected[@]}"; do
if ! grep "$method" <<<$output; then
echo "output:"
echo "$output"
echo "Expected $method in output, but was not found."
exit 1
fi
done
for method in "${unexpected[@]}"; do
if grep "$method" <<<$output; then
echo "output:"
echo "$output"
echo "Not expecting $method in output, but was found."
exit 1
fi
done
}
scalac_jvm_flags_are_configured(){
action_should_fail build //test_expect_failure/compilers_jvm_flags:can_configure_jvm_flags_for_scalac
}
javac_jvm_flags_are_configured(){
action_should_fail build //test_expect_failure/compilers_jvm_flags:can_configure_jvm_flags_for_javac
}
revert_internal_change() {
sed -i.bak "s/println(\"altered\")/println(\"orig\")/" $no_recompilation_path/C.scala
rm $no_recompilation_path/C.scala.bak
}
test_scala_library_expect_no_recompilation_on_internal_change_of_transitive_dependency() {
set +e
no_recompilation_path="test/src/main/scala/scala/test/strict_deps/no_recompilation"
build_command="bazel build //$no_recompilation_path/... --subcommands --strict_java_deps=error"
echo "running initial build"
$build_command
echo "changing internal behaviour of C.scala"
sed -i.bak "s/println(\"orig\")/println(\"altered\")/" ./$no_recompilation_path/C.scala
echo "running second build"
output=$(${build_command} 2>&1)
not_expected_recompiled_target="//$no_recompilation_path:transitive_dependency_user"
echo ${output} | grep "$not_expected_recompiled_target"
if [ $? -eq 0 ]; then
echo "bazel build was executed after change of internal behaviour of 'transitive_dependency' target. compilation of 'transitive_dependency_user' should not have been triggered."
revert_internal_change
exit 1
fi
revert_internal_change
set -e
}
if [ "$1" != "ci" ]; then
runner="run_test_local"
else
runner="run_test_ci"
fi
$runner bazel build test/...
$runner bazel test test/...
$runner bazel test third_party/...
$runner bazel run test/src/main/scala/scala/test/twitter_scrooge:justscrooges
$runner bazel run test:JavaBinary
$runner bazel run test:JavaBinary2
$runner bazel run test:MixJavaScalaLibBinary
$runner bazel run test:MixJavaScalaSrcjarLibBinary
$runner bazel run test:ScalaBinary
$runner bazel run test:ScalaLibBinary
$runner test_disappearing_class
$runner find -L ./bazel-testlogs -iname "*.xml"
$runner xmllint_test
$runner test_build_is_identical
$runner test_transitive_deps
$runner test_scala_library_suite
$runner test_repl
$runner bazel run test:JavaOnlySources
$runner test_benchmark_jmh
$runner multiple_junit_suffixes
$runner multiple_junit_prefixes
$runner test_scala_junit_test_can_fail
$runner junit_generates_xml_logs
$runner scala_library_jar_without_srcs_must_fail_on_mismatching_resource_strip_prefix
$runner multiple_junit_patterns
$runner test_junit_test_must_have_prefix_or_suffix
$runner test_junit_test_errors_when_no_tests_found
$runner scala_library_jar_without_srcs_must_include_direct_file_resources
$runner scala_library_jar_without_srcs_must_include_filegroup_resources
$runner bazel run test/src/main/scala/scala/test/large_classpath:largeClasspath
$runner scala_test_test_filters
$runner scala_junit_test_test_filter
$runner scalac_jvm_flags_are_configured
$runner javac_jvm_flags_are_configured
$runner test_scala_library_expect_failure_on_missing_direct_internal_deps
$runner test_scala_library_expect_failure_on_missing_direct_external_deps_jar
$runner test_scala_library_expect_failure_on_missing_direct_external_deps_file_group
$runner test_scala_library_expect_failure_on_missing_direct_deps_strict_is_disabled_by_default
$runner test_scala_binary_expect_failure_on_missing_direct_deps
$runner test_scala_library_expect_failure_on_missing_direct_deps_warn_mode
$runner test_scala_library_expect_failure_on_missing_direct_deps_off_mode
$runner test_scala_library_expect_no_recompilation_on_internal_change_of_transitive_dependency