forked from bazelbuild/rules_scala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_rules_scala.sh
executable file
·1146 lines (984 loc) · 43 KB
/
test_rules_scala.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
set -e
if ! bazel_loc="$(type -p 'bazel')" || [[ -z "$bazel_loc" ]]; then
export PATH="$(cd "$(dirname "$0")"; pwd)"/tools:$PATH
echo 'Using ./tools/bazel directly for bazel calls'
fi
test_disappearing_class() {
git checkout test_expect_failure/disappearing_class/ClassProvider.scala
bazel build test_expect_failure/disappearing_class:uses_class
echo -e "package scalarules.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
}
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_override_javabin() {
# set the JAVABIN to nonsense
JAVABIN=/etc/basdf action_should_fail run test:ScalaBinary
}
test_scala_library_suite() {
action_should_fail build test_expect_failure/scala_library_suite:library_suite_dep_on_children
}
test_expect_failure_with_message() {
set +e
expected_message=$1
test_filter=$2
test_command=$3
command="bazel test --nocache_test_results --test_output=streamed ${test_filter} ${test_command}"
output=$(${command} 2>&1)
echo ${output} | grep "$expected_message"
if [ $? -ne 0 ]; then
echo "'bazel test ${test_command}' should have logged \"${expected_message}\"."
exit 1
fi
if [ "${additional_expected_message}" != "" ]; then
echo ${output} | grep "$additional_expected_message"
if [ $? -ne 0 ]; then
echo "'bazel test ${test_command}' should have logged \"${additional_expected_message}\"."
exit 1
fi
fi
set -e
}
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"}
additional_expected_message=${5:-""}
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
if [ "${additional_expected_message}" != "" ]; then
echo ${output} | grep "$additional_expected_message"
if [ $? -ne 0 ]; then
echo "'bazel build ${test_target}' should have logged \"${additional_expected_message}\"."
exit 1
fi
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_binary_expect_failure_on_missing_direct_deps_located_in_dependency_which_is_scala_binary() {
dependency_target='//test_expect_failure/missing_direct_deps/internal_deps:transitive_dependency'
test_target='test_expect_failure/missing_direct_deps/internal_deps:binary_user_of_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//:com_google_guava_guava_21_0'
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_with_file//jar:jar'
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_java() {
dependency_target='//test_expect_failure/missing_direct_deps/internal_deps:transitive_dependency'
test_target='//test_expect_failure/missing_direct_deps/internal_deps:transitive_dependency_java_user'
expected_message="$dependency_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_java_in_src_jar_when_disabled() {
test_target='//test_expect_failure/java_in_src_jar_when_disabled:java_source_jar'
expected_message=".*Found java files in source jars but expect Java output is set to false"
test_expect_failure_with_message "${expected_message}" $test_target
}
test_scala_library_expect_better_failure_message_on_missing_transitive_dependency_labels_from_other_jvm_rules() {
transitive_target='.*transitive_dependency-ijar.jar'
direct_target='//test_expect_failure/missing_direct_deps/internal_deps:direct_java_provider_dependency'
test_target='//test_expect_failure/missing_direct_deps/internal_deps:dependent_on_some_java_provider'
expected_message="Unknown label of file $transitive_target which came from $direct_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_deps_warn_mode_java() {
dependency_target='//test_expect_failure/missing_direct_deps/internal_deps:transitive_dependency'
test_target='//test_expect_failure/missing_direct_deps/internal_deps:transitive_dependency_java_user'
local expected_message="$dependency_target.*$test_target"
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 scalarules.test._; HelloLib.printMessage(\"foo\")" | bazel-bin/test/HelloLibRepl | grep "foo java" &&
echo "import scalarules.test._; TestUtil.foo" | bazel-bin/test/HelloLibTestRepl | grep "bar" &&
echo "import scalarules.test._; ScalaLibBinary.main(Array())" | bazel-bin/test/ScalaLibBinaryRepl | grep "A hui hou" &&
echo "import scalarules.test._; ResourcesStripScalaBinary.main(Array())" | bazel-bin/test/ResourcesStripScalaBinaryRepl | grep "More Hello"
echo "import scalarules.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
}
test_multi_service_manifest() {
deploy_jar='ScalaBinary_with_service_manifest_srcs_deploy.jar'
meta_file='META-INF/services/org.apache.beam.sdk.io.FileSystemRegistrar'
bazel build test:$deploy_jar
unzip -p bazel-bin/test/$deploy_jar $meta_file > service_manifest.txt
diff service_manifest.txt test/example_jars/expected_service_manifest.txt
RESPONSE_CODE=$?
rm service_manifest.txt
exit $RESPONSE_CODE
}
action_should_fail() {
# runs the tests locally
set +e
TEST_ARG=$@
DUMMY=$(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
}
action_should_fail_with_message() {
set +e
MSG=$1
TEST_ARG=${@:2}
RES=$(bazel $TEST_ARG 2>&1)
RESPONSE_CODE=$?
echo $RES | grep -- "$MSG"
GREP_RES=$?
if [ $RESPONSE_CODE -eq 0 ]; then
echo -e "${RED} \"bazel $TEST_ARG\" should have failed but passed. $NC"
exit 1
elif [ $GREP_RES -ne 0 ]; then
echo -e "${RED} \"bazel $TEST_ARG\" should have failed with message \"$MSG\" but did not. $NC"
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 'scalarules.test.junit.JunitSuffixIT' -e 'scalarules.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 'scalarules.test.junit.TestJunitCustomPrefix' -e 'scalarules.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 'scalarules.test.junit.TestJunitCustomPrefix' -e 'scalarules.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/scalarules/test/resources/$TARGET.jar"
FULL_TARGET="test/src/main/scala/scalarules/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 scalarules.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 scalarules.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=scalarules.test.junit.FirstFilterTest#(method1|method2)$|scalarules.test.junit.SecondFilterTest#(method2|method3)$' \
test:JunitFilterTest)
local expected=(
"scalarules.test.junit.FirstFilterTest#method1"
"scalarules.test.junit.FirstFilterTest#method2"
"scalarules.test.junit.SecondFilterTest#method2"
"scalarules.test.junit.SecondFilterTest#method3")
local unexpected=(
"scalarules.test.junit.FirstFilterTest#method3"
"scalarules.test.junit.SecondFilterTest#method1"
"scalarules.test.junit.ThirdFilterTest#method1"
"scalarules.test.junit.ThirdFilterTest#method2"
"scalarules.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
}
scala_junit_test_test_filter_custom_runner(){
bazel test \
--nocache_test_results \
--test_output=streamed \
'--test_filter=scalarules.test.junit.JunitCustomRunnerTest#' \
test:JunitCustomRunner
}
scala_specs2_junit_test_test_filter_everything(){
local output=$(bazel test \
--nocache_test_results \
--test_output=streamed \
'--test_filter=.*' \
test:Specs2Tests)
local expected=(
"[info] JunitSpec2RegexTest"
"[info] JunitSpecs2AnotherTest"
"[info] JunitSpecs2Test")
local unexpected=(
"[info] UnrelatedTest")
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
}
scala_specs2_junit_test_test_filter_whole_spec(){
local output=$(bazel test \
--nocache_test_results \
--test_output=streamed \
'--test_filter=scalarules.test.junit.specs2.JunitSpecs2Test#' \
test:Specs2Tests)
local expected=(
"+ run smoothly in bazel"
"+ not run smoothly in bazel")
local unexpected=(
"+ run from another test")
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
}
scala_specs2_junit_test_test_filter_one_test(){
local output=$(bazel test \
--nocache_test_results \
--test_output=streamed \
'--test_filter=scalarules.test.junit.specs2.JunitSpecs2Test#specs2 tests should::run smoothly in bazel$' \
test:Specs2Tests)
local expected="+ run smoothly in bazel"
local unexpected="+ not run smoothly in bazel"
if ! grep "$expected" <<<$output; then
echo "output:"
echo "$output"
echo "Expected $expected in output, but was not found."
exit 1
fi
if grep "$unexpected" <<<$output; then
echo "output:"
echo "$output"
echo "Not expecting $unexpected in output, but was found."
exit 1
fi
}
scala_specs2_only_filtered_test_shows_in_the_xml(){
bazel test \
--nocache_test_results \
--test_output=streamed \
'--test_filter=scalarules.test.junit.specs2.JunitSpecs2Test#specs2 tests should::run smoothly in bazel$' \
test:Specs2Tests
matches=$(grep -c -e "testcase name='specs2 tests should::run smoothly in bazel'" -e "testcase name='specs2 tests should::not run smoothly in bazel'" ./bazel-testlogs/test/Specs2Tests/test.xml)
if [ $matches -eq 1 ]; then
return 0
else
echo "Expecting only one result, found more than one. Please check 'bazel-testlogs/test/Specs2Tests/test.xml'"
return 1
fi
test -e
}
scala_specs2_all_tests_show_in_the_xml(){
bazel test \
--nocache_test_results \
--test_output=streamed \
'--test_filter=scalarules.test.junit.specs2.JunitSpecs2Test#' \
test:Specs2Tests
matches=$(grep -c -e "testcase name='specs2 tests should::run smoothly in bazel'" -e "testcase name='specs2 tests should::not run smoothly in bazel'" ./bazel-testlogs/test/Specs2Tests/test.xml)
if [ $matches -eq 2 ]; then
return 0
else
echo "Expecting two results, found a different number ($matches). Please check 'bazel-testlogs/test/Specs2Tests/test.xml'"
return 1
fi
test -e
}
scala_specs2_only_failed_test_shows_in_the_xml(){
set +e
bazel test \
--nocache_test_results \
--test_output=streamed \
'--test_filter=scalarules.test.junit.specs2.SuiteWithOneFailingTest#specs2 tests should::fail$' \
test_expect_failure/scala_junit_test:specs2_failing_test
echo "got results"
matches=$(grep -c -e "testcase name='specs2 tests should::fail'" -e "testcase name='specs2 tests should::succeed'" ./bazel-testlogs/test_expect_failure/scala_junit_test/specs2_failing_test/test.xml)
if [ $matches -eq 1 ]; then
return 0
else
echo "Expecting only one result, found more than one. Please check './bazel-testlogs/test_expect_failure/scala_junit_test/specs2_failing_test/test.xml'"
return 1
fi
}
scala_specs2_junit_test_test_filter_exact_match(){
local output=$(bazel test \
--nocache_test_results \
--test_output=streamed \
'--test_filter=scalarules.test.junit.specs2.JunitSpecs2AnotherTest#other specs2 tests should::run from another test$' \
test:Specs2Tests)
local expected="+ run from another test"
local unexpected="+ run from another test 2"
if ! grep "$expected" <<<$output; then
echo "output:"
echo "$output"
echo "Expected $expected in output, but was not found."
exit 1
fi
if grep "$unexpected" <<<$output; then
echo "output:"
echo "$output"
echo "Not expecting $unexpected in output, but was found."
exit 1
fi
}
scala_specs2_junit_test_test_filter_exact_match_unsafe_characters(){
local output=$(bazel test \
--nocache_test_results \
--test_output=streamed \
'--test_filter=scalarules.test.junit.specs2.JunitSpec2RegexTest#\Qtests with unsafe characters should::2 + 2 != 5\E$' \
test:Specs2Tests)
local expected="+ 2 + 2 != 5"
local unexpected="+ work escaped (with regex)"
if ! grep "$expected" <<<$output; then
echo "output:"
echo "$output"
echo "Expected $expected in output, but was not found."
exit 1
fi
if grep "$unexpected" <<<$output; then
echo "output:"
echo "$output"
echo "Not expecting $unexpected in output, but was found."
exit 1
fi
}
scala_specs2_junit_test_test_filter_exact_match_escaped_and_sanitized(){
local output=$(bazel test \
--nocache_test_results \
--test_output=streamed \
'--test_filter=scalarules.test.junit.specs2.JunitSpec2RegexTest#\Qtests with unsafe characters should::work escaped [with regex]\E$' \
test:Specs2Tests)
local expected="+ work escaped (with regex)"
local unexpected="+ 2 + 2 != 5"
if ! grep "$expected" <<<$output; then
echo "output:"
echo "$output"
echo "Expected $expected in output, but was not found."
exit 1
fi
if grep "$unexpected" <<<$output; then
echo "output:"
echo "$output"
echo "Not expecting $unexpected in output, but was found."
exit 1
fi
}
scala_specs2_junit_test_test_filter_match_multiple_methods(){
local output=$(bazel test \
--nocache_test_results \
--test_output=streamed \
'--test_filter=scalarules.test.junit.specs2.JunitSpecs2AnotherTest#other specs2 tests should::(\Qrun from another test\E|\Qrun from another test 2\E)$' \
test:Specs2Tests)
local expected=(
"+ run from another test"
"+ run from another test 2")
local unexpected=(
"+ not run")
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
}
scala_specs2_exception_in_initializer_without_filter(){
expected_message="org.specs2.control.UserException: cannot create an instance for class scalarules.test.junit.specs2.FailingTest"
test_command="test_expect_failure/scala_junit_test:specs2_failing_test"
test_expect_failure_with_message "$expected_message" $test_filter $test_command
}
scala_specs2_exception_in_initializer_terminates_without_timeout(){
local output=$(bazel test \
--test_output=streamed \
--test_timeout=10 \
'--test_filter=scalarules.test.junit.specs2.FailingTest#' \
test_expect_failure/scala_junit_test:specs2_failing_test)
local expected=(
"org.specs2.control.UserException: cannot create an instance for class scalarules.test.junit.specs2.FailingTest")
local unexpected=(
"TIMEOUT")
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
}
javac_jvm_flags_via_javacopts_are_configured(){
action_should_fail build //test_expect_failure/compilers_jvm_flags:can_configure_jvm_flags_for_javac_via_javacopts
}
scalac_jvm_flags_are_expanded(){
action_should_fail_with_message \
"--jvm_flag=test_expect_failure/compilers_jvm_flags/args.txt" \
build --verbose_failures //test_expect_failure/compilers_jvm_flags:can_expand_jvm_flags_for_scalac
}
javac_jvm_flags_are_expanded(){
action_should_fail_with_message \
"invalid flag: test_expect_failure/compilers_jvm_flags/args.txt" \
build --verbose_failures //test_expect_failure/compilers_jvm_flags:can_expand_jvm_flags_for_javac
}
javac_jvm_flags_via_javacopts_are_expanded(){
action_should_fail_with_message \
"invalid flag: test_expect_failure/compilers_jvm_flags/args.txt" \
build --verbose_failures //test_expect_failure/compilers_jvm_flags:can_expand_jvm_flags_for_javac_via_javacopts
}
java_toolchain_javacopts_are_used(){
action_should_fail_with_message \
"invalid flag: -InvalidFlag" \
build --java_toolchain=//test_expect_failure/compilers_javac_opts:a_java_toolchain \
--verbose_failures //test_expect_failure/compilers_javac_opts:can_configure_jvm_flags_for_javac_via_javacopts
}
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/scalarules/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
}
test_scala_library_expect_no_recompilation_on_internal_change_of_java_dependency() {
test_scala_library_expect_no_recompilation_of_target_on_internal_change_of_dependency "C.java" "s/System.out.println(\"orig\")/System.out.println(\"altered\")/"
}
test_scala_library_expect_no_recompilation_on_internal_change_of_scala_dependency() {
test_scala_library_expect_no_recompilation_of_target_on_internal_change_of_dependency "B.scala" "s/println(\"orig\")/println(\"altered\")/"
}
test_scala_library_expect_no_recompilation_of_target_on_internal_change_of_dependency() {
test_scala_library_expect_no_recompilation_on_internal_change $1 $2 ":user" "'user'"
}
test_scala_library_expect_no_java_recompilation_on_internal_change_of_scala_sibling() {
test_scala_library_expect_no_recompilation_on_internal_change "B.scala" "s/println(\"orig_sibling\")/println(\"altered_sibling\")/" "/dependency_java" "java sibling"
}
test_scala_library_expect_no_recompilation_on_internal_change() {
changed_file=$1
changed_content=$2
dependency=$3
dependency_description=$4
set +e
no_recompilation_path="test/src/main/scala/scalarules/test/ijar"
build_command="bazel build //$no_recompilation_path/... --subcommands"
echo "running initial build"
$build_command
echo "changing internal behaviour of $changed_file"
sed -i.bak $changed_content ./$no_recompilation_path/$changed_file
echo "running second build"
output=$(${build_command} 2>&1)
not_expected_recompiled_action="$no_recompilation_path$dependency"
echo ${output} | grep "$not_expected_recompiled_action"
if [ $? -eq 0 ]; then
echo "bazel build was executed after change of internal behaviour of 'dependency' target. compilation of $dependency_description should not have been triggered."
revert_change $no_recompilation_path $changed_file
exit 1
fi
revert_change $no_recompilation_path $changed_file
set -e
}
revert_change() {
mv $1/$2.bak $1/$2
}
test_scala_import_expect_failure_on_missing_direct_deps_warn_mode() {
dependency_target1='//test_expect_failure/scala_import:cats'
dependency_target2='//test_expect_failure/scala_import:guava'
test_target='test_expect_failure/scala_import:scala_import_propagates_compile_deps'
local expected_message1="buildozer 'add deps $dependency_target1' //$test_target"
local expected_message2="buildozer 'add deps $dependency_target2' //$test_target"
test_expect_failure_or_warning_on_missing_direct_deps_with_expected_message "${expected_message1}" ${test_target} "--strict_java_deps=warn" "ne" "${expected_message2}"
}
test_scalaopts_from_scala_toolchain() {
action_should_fail build --extra_toolchains="//test_expect_failure/scalacopts_from_toolchain:failing_scala_toolchain" //test_expect_failure/scalacopts_from_toolchain:failing_build
}
test_scalac_jvm_flags_from_scala_toolchain_fails() {
action_should_fail build --extra_toolchains="//test_expect_failure/scalac_jvm_opts:failing_scala_toolchain" //test_expect_failure/scalac_jvm_opts:empty_build
}
test_scalac_jvm_flags_from_scala_toolchain_passes() {
bazel build --extra_toolchains="//manual_test/scalac_jvm_opts:passing_scala_toolchain" //manual_test/scalac_jvm_opts:empty_build
}
test_scalac_jvm_flags_on_target_overrides_toolchain_passes() {
bazel build --extra_toolchains="//manual_test/scalac_jvm_opts:failing_scala_toolchain" //manual_test/scalac_jvm_opts:empty_overriding_build
}
test_scalac_jvm_flags_work_with_scalapb() {
bazel build --extra_toolchains="//manual_test/scalac_jvm_opts:passing_scala_toolchain" //manual_test/scalac_jvm_opts:proto
}
test_scala_test_jvm_flags_from_scala_toolchain_fails() {
action_should_fail test --extra_toolchains="//test_expect_failure/scala_test_jvm_flags:failing_scala_toolchain" //test_expect_failure/scala_test_jvm_flags:empty_test
}
test_scala_test_jvm_flags_from_scala_toolchain_passes() {
bazel test --extra_toolchains="//manual_test/scala_test_jvm_flags:passing_scala_toolchain" //manual_test/scala_test_jvm_flags:empty_test
}
test_scala_test_jvm_flags_on_target_overrides_toolchain_passes() {
bazel test --extra_toolchains="//manual_test/scala_test_jvm_flags:failing_scala_toolchain" //manual_test/scala_test_jvm_flags:empty_overriding_test
}
test_unused_dependency_checker_mode_set_in_rule() {
action_should_fail build //test_expect_failure/unused_dependency_checker:failing_build
}
test_unused_dependency_checker_mode_from_scala_toolchain() {
action_should_fail build --extra_toolchains="//test_expect_failure/unused_dependency_checker:failing_scala_toolchain" //test_expect_failure/unused_dependency_checker:toolchain_failing_build
}
test_unused_dependency_checker_mode_override_toolchain() {
bazel build --extra_toolchains="//test_expect_failure/unused_dependency_checker:failing_scala_toolchain" //test_expect_failure/unused_dependency_checker:toolchain_override
}
test_unused_dependency_checker_mode_warn() {
# this is a hack to invalidate the cache, so that the target actually gets built and outputs warnings.
bazel build \
--strict_java_deps=warn \
//test:UnusedDependencyCheckerWarn
local output
output=$(bazel build \
--strict_java_deps=off \
//test:UnusedDependencyCheckerWarn 2>&1
)
if [ $? -ne 0 ]; then
echo "Target with unused dependency failed to build with status $?"
echo "$output"
exit 1
fi
local expected="warning: Target '//test:UnusedLib' is specified as a dependency to //test:UnusedDependencyCheckerWarn but isn't used, please remove it from the deps."
echo "$output" | grep "$expected"
if [ $? -ne 0 ]; then
echo "Expected output:[$output] to contain [$expected]"
exit 1
fi
}
test_scala_import_library_passes_labels_of_direct_deps() {
dependency_target='//test_expect_failure/scala_import:root_for_scala_import_passes_labels_of_direct_deps'
test_target='test_expect_failure/scala_import:leaf_for_scala_import_passes_labels_of_direct_deps'
test_scala_library_expect_failure_on_missing_direct_deps $dependency_target $test_target
}
test_scala_classpath_resources_expect_warning_on_namespace_conflict() {
local output=$(bazel build \
--verbose_failures \
//test/src/main/scala/scalarules/test/classpath_resources:classpath_resource_duplicates
)
local expected="Classpath resource file classpath-resourcehas a namespace conflict with another file: classpath-resource"
if ! grep "$method" <<<$output; then
echo "output:"
echo "$output"
echo "Expected $method in output, but was not found."
exit 1
fi
}
scala_pb_library_targets_do_not_have_host_deps() {
set -e
bazel build test/proto:test_binary_to_ensure_no_host_deps
set +e
find bazel-bin/test/proto/test_binary_to_ensure_no_host_deps.runfiles -name '*.jar' -exec readlink {} \; | grep 'bazel-out/host'
RET=$?
set -e
if [ "$RET" == "0" ]; then
echo "Host deps exist in output of target:"
echo "Possibly toolchains limitation?"
find bazel-bin/test/proto/test_binary_to_ensure_no_host_deps.runfiles -name '*.jar' -exec readlink {} \; | grep 'bazel-out/host'
exit 1
fi
}
scala_binary_common_jar_is_exposed_in_build_event_protocol() {
local target=$1
set +e
bazel build test:$target --build_event_text_file=$target_bes.txt
cat $target_bes.txt | grep "test/$target.jar"
if [ $? -ne 0 ]; then
echo "test/$target.jar was not found in build event protocol:"
cat $target_bes.txt
rm $target_bes.txt
exit 1
fi
rm $target_bes.txt
set -e
}
scala_binary_jar_is_exposed_in_build_event_protocol() {
scala_binary_common_jar_is_exposed_in_build_event_protocol ScalaLibBinary
}
scala_test_jar_is_exposed_in_build_event_protocol() {
scala_binary_common_jar_is_exposed_in_build_event_protocol HelloLibTest
}
scala_junit_test_jar_is_exposed_in_build_event_protocol() {
scala_binary_common_jar_is_exposed_in_build_event_protocol JunitTestWithDeps
}
test_scala_import_source_jar_should_be_fetched_when_fetch_sources_is_set_to_true() {
test_scala_import_fetch_sources
}
test_scala_import_source_jar_should_be_fetched_when_env_bazel_jvm_fetch_sources_is_set_to_true() {
test_scala_import_fetch_sources_with_env_bazel_jvm_fetch_sources_set_to "TruE" # as implied, the value is case insensitive
}
test_scala_import_source_jar_should_not_be_fetched_when_env_bazel_jvm_fetch_sources_is_set_to_non_true() {
test_scala_import_fetch_sources_with_env_bazel_jvm_fetch_sources_set_to "false" "and expect no source jars"
}
test_scala_import_fetch_sources_with_env_bazel_jvm_fetch_sources_set_to() {
# the existence of the env var should cause the import repository rule to re-fetch the dependency
# and therefore the order of tests is not expected to matter
export BAZEL_JVM_FETCH_SOURCES=$1
local expect_failure=$2
if [[ ${expect_failure} ]]; then
action_should_fail test_scala_import_fetch_sources
else
test_scala_import_fetch_sources
fi
unset BAZEL_JVM_FETCH_SOURCES
}
test_scala_import_fetch_sources() {
local srcjar_name="guava-21.0-src.jar"
local bazel_out_external_guava_21=$(bazel info output_base)/external/com_google_guava_guava_21_0
set -e
bazel build //test/src/main/scala/scalarules/test/fetch_sources/...
set +e