forked from bazelbuild/rules_scala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_run.sh
executable file
·372 lines (329 loc) · 10.5 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
#!/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() {
set +e
bazel build test_expect_failure/scala_library_suite:library_suite_dep_on_children
if [ $? -eq 0 ]; then
echo "'bazel build test_expect_failure/scala_library_suite:library_suite_dep_on_children' should have failed."
exit 1
fi
set -e
exit 0
}
test_scala_junit_test_can_fail() {
set +e
bazel test test_expect_failure/scala_junit_test:failing_test
if [ $? -eq 0 ]; then
echo "'bazel build test_expect_failure/scala_junit_test:failing_test' should have failed."
exit 1
fi
set -e
exit 0
}
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._; MoreScalaLibBinary.main(Array())" | bazel-bin/test/MoreScalaLibBinaryRepl | 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
}
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() {
set +e
bazel test test_expect_failure/scala_junit_test:no_prefix_or_suffix
if [ $? -eq 0 ]; then
echo "'bazel build test_expect_failure/scala_junit_test:no_prefix_or_suffix' should have failed."
exit 1
fi
set -e
exit 0
}
test_junit_test_errors_when_no_tests_found() {
set +e
bazel test test_expect_failure/scala_junit_test:no_tests_found
if [ $? -eq 0 ]; then
echo "'bazel build test_expect_failure/scala_junit_test:no_tests_found' should have failed."
exit 1
fi
set -e
exit 0
}
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() {
set +e
bazel build test_expect_failure/wrong_resource_strip_prefix:noSrcsJarWithWrongStripPrefix
if [ $? -eq 0 ]; then
echo "'bazel build of scala_library with invalid resource_strip_prefix should have failed."
exit 1
fi
set -e
exit 0
}
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
}
if [ "$1" != "ci" ]; then
runner="run_test_local"
else
runner="run_test_ci"
fi
$runner bazel build test/...
$runner bazel test test/...
$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