Skip to content

Commit 48a8d01

Browse files
fmeumcopybara-github
authored andcommitted
Allow C/C++ coverage collection for external targets
Removes hardcoded filters that result in no coverage being reported for `cc_*` targets in external repositories even when matched by the `--instrumentation_filter`. Work towards bazelbuild#16228 Work towards bazelbuild#16208 Closes bazelbuild#16261. PiperOrigin-RevId: 483911568 Change-Id: Ibca6fb39a8e946e06beeb03414ad8ccbc42f53d6
1 parent f02bcf8 commit 48a8d01

File tree

4 files changed

+364
-12
lines changed

4 files changed

+364
-12
lines changed

src/test/shell/bazel/bazel_coverage_cc_test_gcc.sh

+145
Original file line numberDiff line numberDiff line change
@@ -924,4 +924,149 @@ EOF
924924
expect_log "WARNING: There was no coverage found."
925925
}
926926

927+
function setup_external_cc_target() {
928+
cat > WORKSPACE <<'EOF'
929+
local_repository(
930+
name = "other_repo",
931+
path = "other_repo",
932+
)
933+
EOF
934+
935+
cat > BUILD <<'EOF'
936+
cc_library(
937+
name = "b",
938+
srcs = ["b.cc"],
939+
hdrs = ["b.h"],
940+
visibility = ["//visibility:public"],
941+
)
942+
EOF
943+
944+
cat > b.h <<'EOF'
945+
int b(bool what);
946+
EOF
947+
948+
cat > b.cc <<'EOF'
949+
int b(bool what) {
950+
if (what) {
951+
return 1;
952+
} else {
953+
return 2;
954+
}
955+
}
956+
EOF
957+
958+
mkdir -p other_repo
959+
touch other_repo/WORKSPACE
960+
961+
cat > other_repo/BUILD <<'EOF'
962+
cc_library(
963+
name = "a",
964+
srcs = ["a.cc"],
965+
hdrs = ["a.h"],
966+
deps = ["@//:b"],
967+
)
968+
969+
cc_test(
970+
name = "t",
971+
srcs = ["t.cc"],
972+
linkstatic = True,
973+
deps = [":a"],
974+
)
975+
EOF
976+
977+
cat > other_repo/a.h <<'EOF'
978+
int a(bool what);
979+
EOF
980+
981+
cat > other_repo/a.cc <<'EOF'
982+
#include "a.h"
983+
#include "b.h"
984+
985+
int a(bool what) {
986+
if (what) {
987+
return b(what);
988+
} else {
989+
return 1 + b(what);
990+
}
991+
}
992+
EOF
993+
994+
cat > other_repo/t.cc <<'EOF'
995+
#include <stdio.h>
996+
#include "a.h"
997+
998+
int main(void) {
999+
a(true);
1000+
}
1001+
EOF
1002+
}
1003+
1004+
function test_external_cc_target_can_collect_coverage() {
1005+
if is_gcov_missing_or_wrong_version; then
1006+
echo "Skipping test." && return
1007+
fi
1008+
1009+
setup_external_cc_target
1010+
1011+
bazel coverage --test_output=all --instrumentation_filter=// @other_repo//:t \
1012+
&>"$TEST_log" || fail "Coverage for @other_repo//:t failed"
1013+
1014+
local coverage_file_path="$(get_coverage_file_path_from_test_log)"
1015+
local expected_result_a_cc='SF:external/other_repo/a.cc
1016+
FN:4,_Z1ab
1017+
FNDA:1,_Z1ab
1018+
FNF:1
1019+
FNH:1
1020+
DA:4,1
1021+
DA:5,1
1022+
DA:6,1
1023+
DA:8,0
1024+
LH:3
1025+
LF:4
1026+
end_of_record'
1027+
local expected_result_b_cc='SF:b.cc
1028+
FN:1,_Z1bb
1029+
FNDA:1,_Z1bb
1030+
FNF:1
1031+
FNH:1
1032+
DA:1,1
1033+
DA:2,1
1034+
DA:3,1
1035+
DA:5,0
1036+
LH:3
1037+
LF:4
1038+
end_of_record'
1039+
1040+
assert_coverage_result "$expected_result_a_cc" "$coverage_file_path"
1041+
assert_coverage_result "$expected_result_b_cc" "$coverage_file_path"
1042+
}
1043+
1044+
function test_external_cc_target_coverage_not_collected_by_default() {
1045+
if is_gcov_missing_or_wrong_version; then
1046+
echo "Skipping test." && return
1047+
fi
1048+
1049+
setup_external_cc_target
1050+
1051+
bazel coverage --test_output=all @other_repo//:t \
1052+
&>"$TEST_log" || fail "Coverage for @other_repo//:t failed"
1053+
1054+
local coverage_file_path="$(get_coverage_file_path_from_test_log)"
1055+
local expected_result_b_cc='SF:b.cc
1056+
FN:1,_Z1bb
1057+
FNDA:1,_Z1bb
1058+
FNF:1
1059+
FNH:1
1060+
DA:1,1
1061+
DA:2,1
1062+
DA:3,1
1063+
DA:5,0
1064+
LH:3
1065+
LF:4
1066+
end_of_record'
1067+
1068+
assert_coverage_result "$expected_result_b_cc" "$coverage_file_path"
1069+
assert_not_contains "SF:external/other_repo/a.cc" "$coverage_file_path"
1070+
}
1071+
9271072
run_suite "test tests"

0 commit comments

Comments
 (0)