Skip to content

Commit

Permalink
Add branch coverage expectations to C++ coverage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
c-mita committed Jul 10, 2023
1 parent 08a11ad commit 33f23ec
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 24 deletions.
114 changes: 96 additions & 18 deletions src/test/shell/bazel/bazel_coverage_cc_test_gcc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,24 @@ was not found in actual coverage report:
<$( cat "$output_file" )>"
}

# Asserts if the given expected coverage result is included in the given output
# file, accounting for the fact that branch coverage is disabled for gcov v7
#
# - expected coverage The expected result that must be included in the output.
# - output_file The location of the coverage output file.
function assert_cc_coverage_result() {
local expected_coverage="${1}"; shift
local output_file="${1}"; shift

# we disable branch coverage when using gcov 7 so we should strip all branch
# information from the "expected" result before checking.
local gcov_version="$(gcov -v | grep "gcov" | cut -d " " -f 4 | cut -d "." -f 1)"
if [[ "$gcov_version" -le 7 ]]; then
expected_coverage=$(echo "$expected_coverage" | grep -v "^BR")
fi
assert_coverage_result "$expected_coverage" "$output_file"
}

# Returns the path of the code coverage report that was generated by Bazel by
# looking at the current $TEST_log. The method fails if TEST_log does not
# contain any coverage report for a passed test.
Expand Down Expand Up @@ -139,14 +157,18 @@ FN:3,_Z1ab
FNDA:1,_Z1ab
FNF:1
FNH:1
BRDA:4,0,0,1
BRDA:4,0,1,0
BRF:2
BRH:1
DA:3,1
DA:4,1
DA:5,1
DA:7,0
LH:3
LF:4
end_of_record"
assert_coverage_result "$expected_result_a_cc" "$coverage_file_path"
assert_cc_coverage_result "$expected_result_a_cc" "$coverage_file_path"
# t.cc is not included in the coverage report because test targets are not
# instrumented by default.
assert_not_contains "SF:t\.cc" "$coverage_file_path"
Expand Down Expand Up @@ -277,7 +299,7 @@ DA:10,1
LH:5
LF:5
end_of_record"
assert_coverage_result "$expected_result_num_lib" "$coverage_file_path"
assert_cc_coverage_result "$expected_result_num_lib" "$coverage_file_path"

local expected_result_a_header="SF:examples/cpp/foo/bar/baz/a_header.h
FN:3,_ZNK1A12num_whateverEv
Expand All @@ -289,7 +311,7 @@ DA:4,1
LH:2
LF:2
end_of_record"
assert_coverage_result "$expected_result_a_header" "$coverage_file_path"
assert_cc_coverage_result "$expected_result_a_header" "$coverage_file_path"

local coverage_result_num_lib_header="SF:examples/cpp/num-lib.h
FN:14,_ZN3num6NumLib18add_number_inlinedEi
Expand All @@ -301,7 +323,7 @@ DA:15,1
LH:2
LF:2
end_of_record"
assert_coverage_result "$coverage_result_num_lib_header" "$coverage_file_path"
assert_cc_coverage_result "$coverage_result_num_lib_header" "$coverage_file_path"
}

function test_cc_test_gcov_multiple_headers() {
Expand Down Expand Up @@ -371,6 +393,10 @@ FN:4,_Z1ab
FNDA:1,_Z1ab
FNF:1
FNH:1
BRDA:5,0,0,1
BRDA:5,0,1,0
BRF:2
BRH:1
DA:4,1
DA:5,1
DA:6,1
Expand All @@ -383,6 +409,10 @@ FN:1,_Z1bi
FNDA:1,_Z1bi
FNF:1
FNH:1
BRDA:2,0,0,1
BRDA:2,0,1,0
BRF:2
BRH:1
DA:1,1
DA:2,1
DA:3,1
Expand All @@ -393,8 +423,8 @@ end_of_record"
local expected_result_t_cc="SF:coverage_srcs/t.cc"

############## Asserting the coverage results ##############
assert_coverage_result "$expected_result_a_cc" "$coverage_file_path"
assert_coverage_result "$expected_result_b_h" "$coverage_file_path"
assert_cc_coverage_result "$expected_result_a_cc" "$coverage_file_path"
assert_cc_coverage_result "$expected_result_b_h" "$coverage_file_path"
# coverage_srcs/t.cc is not included in the coverage report because the test
# targets are not instrumented by default.
assert_not_contains "SF:coverage_srcs/t\.cc" "$coverage_file_path"
Expand Down Expand Up @@ -469,6 +499,10 @@ FN:4,_Z1ab
FNDA:1,_Z1ab
FNF:1
FNH:1
BRDA:5,0,0,1
BRDA:5,0,1,0
BRF:2
BRH:1
DA:4,1
DA:5,1
DA:6,1
Expand All @@ -481,6 +515,10 @@ FN:1,_Z1bi
FNDA:1,_Z1bi
FNF:1
FNH:1
BRDA:2,0,0,1
BRDA:2,0,1,0
BRF:2
BRH:1
DA:1,1
DA:2,1
DA:3,1
Expand All @@ -491,14 +529,14 @@ end_of_record"
local expected_result_t_cc="SF:coverage_srcs/t.cc"

############## Asserting the coverage results ##############
assert_coverage_result "$expected_result_a_cc" "$coverage_file_path"
assert_coverage_result "$expected_result_b_h" "$coverage_file_path"
assert_cc_coverage_result "$expected_result_a_cc" "$coverage_file_path"
assert_cc_coverage_result "$expected_result_b_h" "$coverage_file_path"
# iostream should not be in the final coverage report because it is a syslib.
assert_not_contains "iostream" "$coverage_file_path"
# coverage_srcs/t.cc should be included in the coverage report. We don't check
# for the full contents of the t.cc report because it might vary from system
# to system depending on the system headers.
assert_coverage_result "$expected_result_t_cc" "$coverage_file_path"
assert_cc_coverage_result "$expected_result_t_cc" "$coverage_file_path"
}

function test_cc_test_gcov_same_header_different_libs() {
Expand Down Expand Up @@ -602,6 +640,10 @@ FN:4,_Z1ab
FNDA:1,_Z1ab
FNF:1
FNH:1
BRDA:5,0,0,1
BRDA:5,0,1,0
BRF:2
BRH:1
DA:4,1
DA:5,1
DA:6,1
Expand All @@ -616,6 +658,12 @@ FNDA:1,_Z7b_for_ai
FNDA:1,_Z7b_for_ci
FNF:2
FNH:2
BRDA:3,0,0,1
BRDA:3,0,1,0
BRDA:12,0,0,0
BRDA:12,0,1,1
BRF:4
BRH:2
DA:2,1
DA:3,1
DA:4,1
Expand All @@ -632,6 +680,10 @@ FN:4,_Z1cb
FNDA:1,_Z1cb
FNF:1
FNH:1
BRDA:5,0,0,0
BRDA:5,0,1,1
BRF:2
BRH:1
DA:4,1
DA:5,1
DA:6,0
Expand All @@ -641,9 +693,9 @@ LF:4
end_of_record"

############## Asserting the coverage results ##############
assert_coverage_result "$expected_result_a_cc" "$coverage_file_path"
assert_coverage_result "$expected_result_b_h" "$coverage_file_path"
assert_coverage_result "$expected_result_c_cc" "$coverage_file_path"
assert_cc_coverage_result "$expected_result_a_cc" "$coverage_file_path"
assert_cc_coverage_result "$expected_result_b_h" "$coverage_file_path"
assert_cc_coverage_result "$expected_result_c_cc" "$coverage_file_path"
# coverage_srcs/t.cc is not included in the coverage report because the test
# targets are not instrumented by default.
assert_not_contains "SF:coverage_srcs/t\.cc" "$coverage_file_path"
Expand Down Expand Up @@ -769,6 +821,10 @@ FNDA:2,_Z10a_redirectv
FNDA:1,_Z1ab
FNF:2
FNH:2
BRDA:5,0,0,1
BRDA:5,0,1,0
BRF:2
BRH:1
DA:4,1
DA:5,1
DA:6,1
Expand All @@ -787,6 +843,12 @@ FNDA:1,_Z7b_for_ci
FNDA:3,_Z9b_for_allv
FNF:3
FNH:3
BRDA:3,0,0,1
BRDA:3,0,1,0
BRDA:12,0,0,0
BRDA:12,0,1,1
BRF:4
BRH:2
DA:2,1
DA:3,1
DA:4,1
Expand All @@ -807,6 +869,10 @@ FNDA:1,_Z10c_redirectv
FNDA:1,_Z1cb
FNF:2
FNH:2
BRDA:5,0,0,0
BRDA:5,0,1,1
BRF:2
BRH:1
DA:4,1
DA:5,1
DA:6,0
Expand All @@ -818,9 +884,9 @@ LF:6
end_of_record"

############## Asserting the coverage results ##############
assert_coverage_result "$expected_result_a_cc" "$coverage_file_path"
assert_coverage_result "$expected_result_b_h" "$coverage_file_path"
assert_coverage_result "$expected_result_c_cc" "$coverage_file_path"
assert_cc_coverage_result "$expected_result_a_cc" "$coverage_file_path"
assert_cc_coverage_result "$expected_result_b_h" "$coverage_file_path"
assert_cc_coverage_result "$expected_result_c_cc" "$coverage_file_path"
# coverage_srcs/t.cc is not included in the coverage report because the test
# targets are not instrumented by default.
assert_not_contains "SF:coverage_srcs/t\.cc" "$coverage_file_path"
Expand Down Expand Up @@ -1017,6 +1083,10 @@ FN:4,_Z1ab
FNDA:1,_Z1ab
FNF:1
FNH:1
BRDA:5,0,0,1
BRDA:5,0,1,0
BRF:2
BRH:1
DA:4,1
DA:5,1
DA:6,1
Expand All @@ -1029,6 +1099,10 @@ FN:1,_Z1bb
FNDA:1,_Z1bb
FNF:1
FNH:1
BRDA:2,0,0,1
BRDA:2,0,1,0
BRF:2
BRH:1
DA:1,1
DA:2,1
DA:3,1
Expand All @@ -1037,8 +1111,8 @@ LH:3
LF:4
end_of_record'

assert_coverage_result "$expected_result_a_cc" "$coverage_file_path"
assert_coverage_result "$expected_result_b_cc" "$coverage_file_path"
assert_cc_coverage_result "$expected_result_a_cc" "$coverage_file_path"
assert_cc_coverage_result "$expected_result_b_cc" "$coverage_file_path"
}

function test_external_cc_target_coverage_not_collected_by_default() {
Expand All @@ -1057,6 +1131,10 @@ FN:1,_Z1bb
FNDA:1,_Z1bb
FNF:1
FNH:1
BRDA:2,0,0,1
BRDA:2,0,1,0
BRF:2
BRH:1
DA:1,1
DA:2,1
DA:3,1
Expand All @@ -1065,7 +1143,7 @@ LH:3
LF:4
end_of_record'

assert_coverage_result "$expected_result_b_cc" "$coverage_file_path"
assert_cc_coverage_result "$expected_result_b_cc" "$coverage_file_path"
assert_not_contains "SF:external/other_repo/a.cc" "$coverage_file_path"
}

Expand Down
28 changes: 27 additions & 1 deletion src/test/shell/bazel/bazel_coverage_java_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,24 @@ was not found in actual coverage report:
<$( cat "$output_file" )>"
}

# Asserts if the given expected coverage result is included in the given output
# file, accounting for the fact that branch coverage is disabled for gcov v7
#
# - expected coverage The expected result that must be included in the output.
# - output_file The location of the coverage output file.
function assert_cc_coverage_result() {
local expected_coverage="${1}"; shift
local output_file="${1}"; shift

# we disable branch coverage when using gcov 7 so we should strip all branch
# information from the "expected" result before checking.
local gcov_version="$(gcov -v | grep "gcov" | cut -d " " -f 4 | cut -d "." -f 1)"
if [[ "$gcov_version" -le 7 ]]; then
expected_coverage=$(echo "$expected_coverage" | grep -v "^BR")
fi
assert_coverage_result "$expected_coverage" "$output_file"
}

# Returns the path of the code coverage report that was generated by Bazel by
# looking at the current $TEST_log. The method fails if TEST_log does not
# contain any coverage report for a passed test.
Expand Down Expand Up @@ -1133,6 +1151,14 @@ FN:5,main
FNDA:1,main
FNF:1
FNH:1
BRDA:6,0,0,1
BRDA:6,0,1,0
BRDA:8,0,0,0
BRDA:8,0,1,1
BRDA:11,0,0,1
BRDA:11,0,1,0
BRF:6
BRH:3
DA:5,1
DA:6,1
DA:7,1
Expand All @@ -1143,7 +1169,7 @@ DA:12,1
LH:6
LF:7
end_of_record"
assert_coverage_result "$coverage_result_num_lib_header" "$coverage_file_path"
assert_cc_coverage_result "$coverage_result_num_lib_header" "$coverage_file_path"
}

function setup_external_java_target() {
Expand Down
Loading

0 comments on commit 33f23ec

Please sign in to comment.