@@ -27,6 +27,41 @@ if [[ "${COVERAGE_GENERATOR_DIR}" != "released" ]]; then
27
27
add_to_bazelrc " build --override_repository=remote_coverage_tools=${COVERAGE_GENERATOR_DIR} "
28
28
fi
29
29
30
+ # Configures Bazel to emit coverage using LLVM tools, returning a non-zero exit
31
+ # code if the tools are not available.
32
+ function setup_llvm_coverage_tools_for_lcov() {
33
+ local -r clang=$( which clang || true)
34
+ if [[ ! -x " ${clang} " ]]; then
35
+ echo " clang not installed. Skipping test."
36
+ return 1
37
+ fi
38
+ local -r clang_version=$( clang --version | grep -o " clang version [0-9]*" | cut -d " " -f 3)
39
+ if [ " $clang_version " -lt 9 ]; then
40
+ # No lcov produced with <9.0.
41
+ echo " clang versions <9.0 are not supported, got $clang_version . Skipping test."
42
+ return 1
43
+ fi
44
+
45
+ local -r llvm_profdata=$( which llvm-profdata || true)
46
+ if [[ ! -x " ${llvm_profdata} " ]]; then
47
+ echo " llvm-profdata not installed. Skipping test."
48
+ return 1
49
+ fi
50
+
51
+ local -r llvm_cov=$( which llvm-cov || true)
52
+ if [[ ! -x " ${llvm_cov} " ]]; then
53
+ echo " llvm-cov not installed. Skipping test."
54
+ return 1
55
+ fi
56
+
57
+ add_to_bazelrc " common --repo_env=BAZEL_LLVM_COV=${llvm_cov} "
58
+ add_to_bazelrc " common --repo_env=BAZEL_LLVM_PROFDATA=${llvm_profdata} "
59
+ add_to_bazelrc " common --repo_env=BAZEL_USE_LLVM_NATIVE_COVERAGE=1"
60
+ add_to_bazelrc " common --repo_env=CC=${clang} "
61
+ add_to_bazelrc " common --repo_env=GCOV=${llvm_profdata} "
62
+ add_to_bazelrc " common --experimental_generate_llvm_lcov"
63
+ }
64
+
30
65
# Writes the C++ source files and a corresponding BUILD file for which to
31
66
# collect code coverage. The sources are a.cc, a.h and t.cc.
32
67
function setup_a_cc_lib_and_t_cc_test() {
@@ -109,31 +144,37 @@ function test_cc_test_llvm_coverage_doesnt_fail() {
109
144
}
110
145
111
146
function test_cc_test_llvm_coverage_produces_lcov_report() {
112
- local -r clang=" /usr/bin/clang"
113
- if [[ ! -x ${clang} ]]; then
114
- return
115
- fi
116
- local -r clang_version=$( clang --version | grep -o " clang version [0-9]*" | cut -d " " -f 3)
117
- if [ " $clang_version " -lt 9 ] || [ " $clang_version " -eq 10 ] || [ " $clang_version " -eq 11 ]; then
118
- # No lcov produced with <9.0, no branch coverage with 10.0 and 11.0.
119
- echo " clang versions <9.0 as well as 10.0 and 11.0 are not supported." && return
120
- fi
147
+ setup_llvm_coverage_tools_for_lcov || return 0
148
+ setup_a_cc_lib_and_t_cc_test
121
149
122
- local -r llvm_profdata=" /usr/bin/llvm-profdata"
123
- if [[ ! -x ${llvm_profdata} ]]; then
124
- return
125
- fi
150
+ bazel coverage --test_output=all //:t & > $TEST_log || fail " Coverage for //:t failed"
126
151
127
- local -r llvm_cov=" /usr/bin/llvm-cov"
128
- if [[ ! -x ${llvm_cov} ]]; then
129
- return
130
- fi
152
+ local expected_result=" SF:a.cc
153
+ FN:3,_Z1ab
154
+ FNDA:1,_Z1ab
155
+ FNF:1
156
+ FNH:1
157
+ DA:3,1
158
+ DA:4,1
159
+ DA:5,1
160
+ DA:6,1
161
+ DA:7,0
162
+ DA:8,0
163
+ DA:9,1
164
+ LH:5
165
+ LF:7
166
+ end_of_record"
131
167
168
+ assert_equals " $expected_result " " $( cat $( get_coverage_file_path_from_test_log) | grep -v ' ^BR' ) "
169
+ }
170
+
171
+ function test_cc_test_llvm_coverage_produces_lcov_report_with_split_postprocessing() {
172
+ setup_llvm_coverage_tools_for_lcov || return 0
132
173
setup_a_cc_lib_and_t_cc_test
133
174
134
- BAZEL_USE_LLVM_NATIVE_COVERAGE=1 GCOV= $llvm_profdata CC= $clang \
135
- BAZEL_LLVM_COV= $llvm_cov bazel coverage --experimental_generate_llvm_lcov \
136
- --test_output=all //:t & > $TEST_log || fail " Coverage for //:t failed"
175
+ bazel coverage \
176
+ --experimental_split_coverage_postprocessing --experimental_fetch_all_coverage_outputs \
177
+ --test_env=VERBOSE_COVERAGE=1 -- test_output=all //:t & > $TEST_log || fail " Coverage for //:t failed"
137
178
138
179
local expected_result=" SF:a.cc
139
180
FN:3,_Z1ab
@@ -151,29 +192,11 @@ LH:5
151
192
LF:7
152
193
end_of_record"
153
194
154
- assert_equals " $expected_result " " $( cat $( get_coverage_file_path_from_test_log) ) "
195
+ assert_equals " $expected_result " " $( cat $( get_coverage_file_path_from_test_log) | grep -v ' ^BR ' ) "
155
196
}
156
197
157
198
function test_cc_test_with_runtime_objects_not_in_runfiles() {
158
- local -r clang=" /usr/bin/clang"
159
- if [[ ! -x ${clang} ]]; then
160
- return
161
- fi
162
- local -r clang_version=$( clang --version | grep -o " clang version [0-9]*" | cut -d " " -f 3)
163
- if [ " $clang_version " -lt 9 ] || [ " $clang_version " -eq 10 ] || [ " $clang_version " -eq 11 ]; then
164
- # No lcov produced with <9.0, no branch coverage with 10.0 and 11.0.
165
- echo " clang versions <9.0 as well as 10.0 and 11.0 are not supported." && return
166
- fi
167
-
168
- local -r llvm_profdata=" /usr/bin/llvm-profdata"
169
- if [[ ! -x ${llvm_profdata} ]]; then
170
- return
171
- fi
172
-
173
- local -r llvm_cov=" /usr/bin/llvm-cov"
174
- if [[ ! -x ${llvm_cov} ]]; then
175
- return
176
- fi
199
+ setup_llvm_coverage_tools_for_lcov || return 0
177
200
178
201
cat << EOF > BUILD
179
202
cc_test(
@@ -206,10 +229,8 @@ int main(int argc, char const *argv[])
206
229
EOF
207
230
208
231
209
- BAZEL_USE_LLVM_NATIVE_COVERAGE=1 GCOV=$llvm_profdata CC=$clang \
210
- BAZEL_LLVM_COV=$llvm_cov bazel coverage --experimental_generate_llvm_lcov \
211
- --test_output=all --instrument_test_targets \
212
- //:main & > $TEST_log || fail " Coverage for //:main failed"
232
+ bazel coverage --test_output=all --instrument_test_targets \
233
+ //:main & > $TEST_log || fail " Coverage for //:main failed"
213
234
214
235
local expected_result=" SF:main.cpp
215
236
FN:4,main
225
246
LF:5
226
247
end_of_record"
227
248
228
- assert_equals " $expected_result " " $( cat $( get_coverage_file_path_from_test_log) ) "
249
+ assert_equals " $expected_result " " $( cat $( get_coverage_file_path_from_test_log) | grep -v ' ^BR ' ) "
229
250
}
230
251
231
252
function setup_external_cc_target() {
@@ -306,42 +327,17 @@ EOF
306
327
}
307
328
308
329
function test_external_cc_target_can_collect_coverage() {
309
- local -r clang=" /usr/bin/clang"
310
- if [[ ! -x ${clang} ]]; then
311
- return
312
- fi
313
- local -r clang_version=$( clang --version | grep -o " clang version [0-9]*" | cut -d " " -f 3)
314
- if [ " $clang_version " -lt 9 ] || [ " $clang_version " -eq 10 ] || [ " $clang_version " -eq 11 ]; then
315
- # No lcov produced with <9.0, no branch coverage with 10.0 and 11.0.
316
- echo " clang versions <9.0 as well as 10.0 and 11.0 are not supported." && return
317
- fi
318
-
319
- local -r llvm_profdata=" /usr/bin/llvm-profdata"
320
- if [[ ! -x ${llvm_profdata} ]]; then
321
- return
322
- fi
323
-
324
- local -r llvm_cov=" /usr/bin/llvm-cov"
325
- if [[ ! -x ${llvm_cov} ]]; then
326
- return
327
- fi
328
-
330
+ setup_llvm_coverage_tools_for_lcov || return 0
329
331
setup_external_cc_target
330
332
331
- BAZEL_USE_LLVM_NATIVE_COVERAGE=1 GCOV=$llvm_profdata CC=$clang \
332
- BAZEL_LLVM_COV=$llvm_cov bazel coverage --experimental_generate_llvm_lcov \
333
- --combined_report=lcov --test_output=all \
334
- @other_repo//:t --instrumentation_filter=// & > $TEST_log || fail " Coverage for @other_repo//:t failed"
333
+ bazel coverage --combined_report=lcov --test_output=all \
334
+ @other_repo//:t --instrumentation_filter=// & > $TEST_log || fail " Coverage for @other_repo//:t failed"
335
335
336
336
local expected_result=' SF:b.cc
337
337
FN:1,_Z1bb
338
338
FNDA:1,_Z1bb
339
339
FNF:1
340
340
FNH:1
341
- BRDA:2,0,0,1
342
- BRDA:2,0,1,0
343
- BRF:2
344
- BRH:1
345
341
DA:1,1
346
342
DA:2,1
347
343
DA:3,1
@@ -357,10 +353,6 @@ FN:4,_Z1ab
357
353
FNDA:1,_Z1ab
358
354
FNF:1
359
355
FNH:1
360
- BRDA:5,0,0,1
361
- BRDA:5,0,1,0
362
- BRF:2
363
- BRH:1
364
356
DA:4,1
365
357
DA:5,1
366
358
DA:6,1
@@ -372,47 +364,22 @@ LH:5
372
364
LF:7
373
365
end_of_record'
374
366
375
- assert_equals " $expected_result " " $( cat $( get_coverage_file_path_from_test_log) ) "
376
- assert_equals " $expected_result " " $( cat bazel-out/_coverage/_coverage_report.dat) "
367
+ assert_equals " $expected_result " " $( cat $( get_coverage_file_path_from_test_log) | grep -v ' ^BR ' ) "
368
+ assert_equals " $expected_result " " $( cat bazel-out/_coverage/_coverage_report.dat | grep -v ' ^BR ' ) "
377
369
}
378
370
379
371
function test_external_cc_target_coverage_not_collected_by_default() {
380
- local -r clang=" /usr/bin/clang"
381
- if [[ ! -x ${clang} ]]; then
382
- return
383
- fi
384
- local -r clang_version=$( clang --version | grep -o " clang version [0-9]*" | cut -d " " -f 3)
385
- if [ " $clang_version " -lt 9 ] || [ " $clang_version " -eq 10 ] || [ " $clang_version " -eq 11 ]; then
386
- # No lcov produced with <9.0, no branch coverage with 10.0 and 11.0.
387
- echo " clang versions <9.0 as well as 10.0 and 11.0 are not supported." && return
388
- fi
389
-
390
- local -r llvm_profdata=" /usr/bin/llvm-profdata"
391
- if [[ ! -x ${llvm_profdata} ]]; then
392
- return
393
- fi
394
-
395
- local -r llvm_cov=" /usr/bin/llvm-cov"
396
- if [[ ! -x ${llvm_cov} ]]; then
397
- return
398
- fi
399
-
372
+ setup_llvm_coverage_tools_for_lcov || return 0
400
373
setup_external_cc_target
401
374
402
- BAZEL_USE_LLVM_NATIVE_COVERAGE=1 GCOV=$llvm_profdata CC=$clang \
403
- BAZEL_LLVM_COV=$llvm_cov bazel coverage --experimental_generate_llvm_lcov \
404
- --combined_report=lcov --test_output=all \
405
- @other_repo//:t & > $TEST_log || fail " Coverage for @other_repo//:t failed"
375
+ bazel coverage --combined_report=lcov --test_output=all \
376
+ @other_repo//:t & > $TEST_log || fail " Coverage for @other_repo//:t failed"
406
377
407
378
local expected_result=' SF:b.cc
408
379
FN:1,_Z1bb
409
380
FNDA:1,_Z1bb
410
381
FNF:1
411
382
FNH:1
412
- BRDA:2,0,0,1
413
- BRDA:2,0,1,0
414
- BRF:2
415
- BRH:1
416
383
DA:1,1
417
384
DA:2,1
418
385
DA:3,1
@@ -424,30 +391,12 @@ LH:5
424
391
LF:7
425
392
end_of_record'
426
393
427
- assert_equals " $expected_result " " $( cat $( get_coverage_file_path_from_test_log) ) "
428
- assert_equals " $expected_result " " $( cat bazel-out/_coverage/_coverage_report.dat) "
394
+ assert_equals " $expected_result " " $( cat $( get_coverage_file_path_from_test_log) | grep -v ' ^BR ' ) "
395
+ assert_equals " $expected_result " " $( cat bazel-out/_coverage/_coverage_report.dat | grep -v ' ^BR ' ) "
429
396
}
430
397
431
398
function test_coverage_with_tmp_in_path() {
432
- local -r clang=" /usr/bin/clang"
433
- if [[ ! -x ${clang} ]]; then
434
- return
435
- fi
436
- local -r clang_version=$( clang --version | grep -o " clang version [0-9]*" | cut -d " " -f 3)
437
- if [ " $clang_version " -lt 9 ] || [ " $clang_version " -eq 10 ] || [ " $clang_version " -eq 11 ]; then
438
- # No lcov produced with <9.0, no branch coverage with 10.0 and 11.0.
439
- echo " clang versions <9.0 as well as 10.0 and 11.0 are not supported." && return
440
- fi
441
-
442
- local -r llvm_profdata=" /usr/bin/llvm-profdata"
443
- if [[ ! -x ${llvm_profdata} ]]; then
444
- return
445
- fi
446
-
447
- local -r llvm_cov=" /usr/bin/llvm-cov"
448
- if [[ ! -x ${llvm_cov} ]]; then
449
- return
450
- fi
399
+ setup_llvm_coverage_tools_for_lcov || return 0
451
400
452
401
mkdir -p foo/tmp
453
402
cat > foo/tmp/BUILD << 'EOF '
@@ -490,20 +439,14 @@ int main(void) {
490
439
}
491
440
EOF
492
441
493
- BAZEL_USE_LLVM_NATIVE_COVERAGE=1 GCOV=$llvm_profdata CC=$clang \
494
- BAZEL_LLVM_COV=$llvm_cov bazel coverage --experimental_generate_llvm_lcov \
495
- --combined_report=lcov --test_output=all \
496
- //foo/tmp:t --instrumentation_filter=// & > $TEST_log || fail " Coverage failed"
442
+ bazel coverage --combined_report=lcov --test_output=all \
443
+ //foo/tmp:t --instrumentation_filter=// & > $TEST_log || fail " Coverage failed"
497
444
498
445
local expected_result=' SF:foo/tmp/a.cc
499
446
FN:3,_Z1ab
500
447
FNDA:1,_Z1ab
501
448
FNF:1
502
449
FNH:1
503
- BRDA:4,0,0,1
504
- BRDA:4,0,1,0
505
- BRF:2
506
- BRH:1
507
450
DA:3,1
508
451
DA:4,1
509
452
DA:5,1
515
458
LF:7
516
459
end_of_record'
517
460
518
- assert_equals " $expected_result " " $( cat $( get_coverage_file_path_from_test_log) ) "
519
- assert_equals " $expected_result " " $( cat bazel-out/_coverage/_coverage_report.dat) "
461
+ assert_equals " $expected_result " " $( cat $( get_coverage_file_path_from_test_log) | grep -v ' ^BR ' ) "
462
+ assert_equals " $expected_result " " $( cat bazel-out/_coverage/_coverage_report.dat | grep -v ' ^BR ' ) "
520
463
}
521
464
522
465
run_suite " test tests"
0 commit comments