From b2e0621d76ad741276c976322be746633b5068bb Mon Sep 17 00:00:00 2001 From: Charles Mita Date: Tue, 11 Jul 2023 16:22:37 +0200 Subject: [PATCH] Extract coverage test helper functions into a separate shell script. Removes a lot of the duplication across different coverage shell tests, which all want similar functions. --- src/test/shell/bazel/BUILD | 1 + .../bazel/bazel_cc_code_coverage_test.sh | 38 ++----- .../shell/bazel/bazel_coverage_cc_test_gcc.sh | 72 +----------- .../bazel/bazel_coverage_cc_test_llvm.sh | 15 +-- .../shell/bazel/bazel_coverage_java_test.sh | 73 +------------ .../shell/bazel/bazel_coverage_py_test.sh | 54 +-------- .../shell/bazel/bazel_coverage_sh_test.sh | 72 +----------- src/test/shell/bazel/coverage_helpers.sh | 103 ++++++++++++++++++ 8 files changed, 125 insertions(+), 303 deletions(-) create mode 100644 src/test/shell/bazel/coverage_helpers.sh diff --git a/src/test/shell/bazel/BUILD b/src/test/shell/bazel/BUILD index b5bbc81f9c9592..670a46d64af614 100644 --- a/src/test/shell/bazel/BUILD +++ b/src/test/shell/bazel/BUILD @@ -50,6 +50,7 @@ filegroup( testonly = 1, srcs = [ "cc_api_rules.bzl", + "coverage_helpers.sh", "remote_helpers.sh", "testing_server.py", ":hamcrest_jar", diff --git a/src/test/shell/bazel/bazel_cc_code_coverage_test.sh b/src/test/shell/bazel/bazel_cc_code_coverage_test.sh index 750df318db1928..31798e6db88069 100755 --- a/src/test/shell/bazel/bazel_cc_code_coverage_test.sh +++ b/src/test/shell/bazel/bazel_cc_code_coverage_test.sh @@ -20,6 +20,8 @@ CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "${CURRENT_DIR}/../integration_test_setup.sh" \ || { echo "integration_test_setup.sh not found!" >&2; exit 1; } +source "${CURRENT_DIR}/coverage_helpers.sh" \ + || { echo "coverage_helpers.sh not found!" >&2; exit 1; } # Check if all the tools required by CC coverage are installed. [[ -z $( which gcov ) ]] && fail "gcov not installed. Skipping test" && exit 0 @@ -188,26 +190,6 @@ function tear_down() { rm -rf coverage_srcs/ } -# Asserts if the given expected coverage result is included in the given output -# file. -# -# - expected_coverage The expected result that must be included in the output. -# - output_file The location of the coverage output file. -function assert_coverage_entry_in_file() { - local expected_coverage="${1}"; shift - local output_file="${1}"; shift - - # Replace newlines with commas to facilitate the assertion. - local expected_coverage_no_newlines="$( echo -n "$expected_coverage" | tr '\n' ',' | tr -d "\"")" - local output_file_no_newlines="$( cat "$output_file" | tr '\n' ',' | tr -d "\"")" - - (echo "$output_file_no_newlines" | fgrep "$expected_coverage_no_newlines")\ - || fail "Expected coverage result -<$expected_coverage> -was not found in actual coverage report: -<$( cat $output_file )>" -} - # Asserts if coverage result in gcov format for coverage_srcs/a.cc is included # in the given output file. # @@ -222,7 +204,7 @@ lcount:4,1 lcount:5,1 lcount:6,1 lcount:8,0" - assert_coverage_entry_in_file "$expected_gcov_result_a_cc" "$output_file" + assert_coverage_result "$expected_gcov_result_a_cc" "$output_file" } @@ -240,7 +222,7 @@ lcount:5,1 lcount:6,1 lcount:7,1 lcount:8,1" - assert_coverage_entry_in_file "$expected_gcov_result_t_cc" "$output_file" + assert_coverage_result "$expected_gcov_result_t_cc" "$output_file" } function assert_gcov_coverage_srcs_b_h() { @@ -253,7 +235,7 @@ lcount:1,1 lcount:2,1 lcount:3,1 lcount:5,0" - assert_coverage_entry_in_file "$expected_gcov_result" "$output_file" + assert_coverage_result "$expected_gcov_result" "$output_file" } function assert_gcov_coverage_srcs_d_a_cc() { @@ -266,7 +248,7 @@ lcount:1,1 lcount:2,1 lcount:3,0 lcount:5,1" - assert_coverage_entry_in_file "$expected_gcov_result_d_a_cc" "$output_file" + assert_coverage_result "$expected_gcov_result_d_a_cc" "$output_file" } function assert_gcov_coverage_srcs_a_cc_json() { @@ -277,7 +259,7 @@ function assert_gcov_coverage_srcs_a_cc_json() { {"lines": [{"branches": [], "count": 1, "line_number": 4, "unexecuted_block": false, "function_name": "_Z1ab"}, {"branches": [], "count": 1, "line_number": 5, "unexecuted_block": false, "function_name": "_Z1ab"}, {"branches": [], "count": 1, "line_number": 6, "unexecuted_block": false, "function_name": "_Z1ab"}, {"branches": [], "count": 0, "line_number": 8, "unexecuted_block": true, "function_name": "_Z1ab"}], "functions": [{"blocks": 4, "end_column": 1, "start_line": 4, "name": "_Z1ab", "blocks_executed": 3, "execution_count": 1, "demangled_name": "a(bool)", "start_column": 5, "end_line": 10}], "file": "coverage_srcs/a.cc"} EOF local expected_gcov_result_a_cc=$(cat expected_gcov_result_a_cc | tr -d '\n') - assert_coverage_entry_in_file "$expected_gcov_result_a_cc" "$output_file" + assert_coverage_result "$expected_gcov_result_a_cc" "$output_file" } @@ -293,7 +275,7 @@ function assert_gcov_coverage_srcs_t_cc_json() { {"lines": [{"branches": [], "count": 1, "line_number": 5, "unexecuted_block": false, "function_name": "main"}, {"branches": [], "count": 1, "line_number": 6, "unexecuted_block": false, "function_name": "main"}, {"branches": [], "count": 1, "line_number": 7, "unexecuted_block": false, "function_name": "main"}, {"branches": [], "count": 1, "line_number": 8, "unexecuted_block": false, "function_name": "main"}], "functions": [{"blocks": 4, "end_column": 1, "start_line": 5, "name": "main", "blocks_executed": 4, "execution_count": 1, "demangled_name": "main", "start_column": 5, "end_line": 8}], "file": "coverage_srcs/t.cc"} EOF local expected_gcov_result_t_cc=$(cat expected_gcov_result_t_cc | tr -d '\n') - assert_coverage_entry_in_file "$expected_gcov_result_t_cc" "$output_file" + assert_coverage_result "$expected_gcov_result_t_cc" "$output_file" } function assert_gcov_coverage_srcs_b_h_json() { @@ -304,7 +286,7 @@ function assert_gcov_coverage_srcs_b_h_json() { {"lines": [{"branches": [], "count": 1, "line_number": 1, "unexecuted_block": false, "function_name": "_Z1bi"}, {"branches": [], "count": 1, "line_number": 2, "unexecuted_block": false, "function_name": "_Z1bi"}, {"branches": [], "count": 1, "line_number": 3, "unexecuted_block": false, "function_name": "_Z1bi"}, {"branches": [], "count": 0, "line_number": 5, "unexecuted_block": true, "function_name": "_Z1bi"}], "functions": [{"blocks": 4, "end_column": 1, "start_line": 1, "name": "_Z1bi", "blocks_executed": 3, "execution_count": 1, "demangled_name": "b(int)", "start_column": 5, "end_line": 7}], "file": "coverage_srcs/b.h"} EOF local expected_gcov_result_b_h=$(cat expected_gcov_result_b_h | tr -d '\n') - assert_coverage_entry_in_file "$expected_gcov_result_b_h" "$output_file" + assert_coverage_result "$expected_gcov_result_b_h" "$output_file" } function assert_gcov_coverage_srcs_d_a_cc_json() { @@ -315,7 +297,7 @@ function assert_gcov_coverage_srcs_d_a_cc_json() { {"lines": [{"branches": [], "count": 1, "line_number": 1, "unexecuted_block": false, "function_name": "_Z11different_ab"}, {"branches": [], "count": 1, "line_number": 2, "unexecuted_block": false, "function_name": "_Z11different_ab"}, {"branches": [], "count": 0, "line_number": 3, "unexecuted_block": true, "function_name": "_Z11different_ab"}, {"branches": [], "count": 1, "line_number": 5, "unexecuted_block": false, "function_name": "_Z11different_ab"}], "functions": [{"blocks": 4, "end_column": 1, "start_line": 1, "name": "_Z11different_ab", "blocks_executed": 3, "execution_count": 1, "demangled_name": "different_a(bool)", "start_column": 5, "end_line": 7}], "file": "coverage_srcs/different/a.cc"} EOF local expected_gcov_result_d_a_cc=$(cat expected_gcov_result_d_a_cc | tr -d '\n') - assert_coverage_entry_in_file "$expected_gcov_result_d_a_cc" "$output_file" + assert_coverage_result "$expected_gcov_result_d_a_cc" "$output_file" } function test_cc_test_coverage_gcov() { diff --git a/src/test/shell/bazel/bazel_coverage_cc_test_gcc.sh b/src/test/shell/bazel/bazel_coverage_cc_test_gcc.sh index 3f508dcac79d71..709cb9947c9607 100755 --- a/src/test/shell/bazel/bazel_coverage_cc_test_gcc.sh +++ b/src/test/shell/bazel/bazel_coverage_cc_test_gcc.sh @@ -20,6 +20,8 @@ set -eu CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "${CURRENT_DIR}/../integration_test_setup.sh" \ || { echo "integration_test_setup.sh not found!" >&2; exit 1; } +source "${CURRENT_DIR}/coverage_helpers.sh" \ + || { echo "coverage_helpers.sh not found!" >&2; exit 1; } COVERAGE_GENERATOR_DIR="$1"; shift if [[ "${COVERAGE_GENERATOR_DIR}" != "released" ]]; then @@ -70,76 +72,6 @@ int main(void) { EOF } -# Returns 0 if gcov is not installed or if a version before 7.0 was found. -# Returns 1 otherwise. -function is_gcov_missing_or_wrong_version() { - local -r gcov_location=$(which gcov) - if [[ ! -x ${gcov_location:-/usr/bin/gcov} ]]; then - echo "gcov not installed." - return 0 - fi - - "$gcov_location" -version | grep "LLVM" && \ - echo "gcov LLVM version not supported." && return 0 - # gcov -v | grep "gcov" outputs a line that looks like this: - # gcov (Debian 7.3.0-5) 7.3.0 - local gcov_version="$(gcov -v | grep "gcov" | cut -d " " -f 4 | cut -d "." -f 1)" - [ "$gcov_version" -lt 7 ] \ - && echo "gcov versions before 7.0 is not supported." && return 0 - return 1 -} - -# Asserts if the given expected coverage result is included in the given output -# file. -# -# - expected_coverage The expected result that must be included in the output. -# - output_file The location of the coverage output file. -function assert_coverage_result() { - local expected_coverage="${1}"; shift - local output_file="${1}"; shift - - # Replace newlines with commas to facilitate the assertion. - local expected_coverage_no_newlines="$( echo "$expected_coverage" | tr '\n' ',' )" - local output_file_no_newlines="$( cat "$output_file" | tr '\n' ',' )" - - (echo "$output_file_no_newlines" \ - | grep -F "$expected_coverage_no_newlines") \ - || fail "Expected coverage result -<$expected_coverage> -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. - # gcov -v | grep "gcov" outputs a line that looks like this: - # gcov (Debian 7.3.0-5) 7.3.0 - 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. -function get_coverage_file_path_from_test_log() { - local ending_part="$(sed -n -e '/PASSED/,$p' "$TEST_log")" - - local coverage_file_path=$(grep -Eo "/[/a-zA-Z0-9\.\_\-]+\.dat$" <<< "$ending_part") - [[ -e "$coverage_file_path" ]] || fail "Coverage output file does not exist!" - echo "$coverage_file_path" -} function test_cc_test_coverage_gcov() { if is_gcov_missing_or_wrong_version; then diff --git a/src/test/shell/bazel/bazel_coverage_cc_test_llvm.sh b/src/test/shell/bazel/bazel_coverage_cc_test_llvm.sh index 6f12bbc6be2f8a..7664558bb60cb4 100755 --- a/src/test/shell/bazel/bazel_coverage_cc_test_llvm.sh +++ b/src/test/shell/bazel/bazel_coverage_cc_test_llvm.sh @@ -20,6 +20,8 @@ set -eu CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "${CURRENT_DIR}/../integration_test_setup.sh" \ || { echo "integration_test_setup.sh not found!" >&2; exit 1; } +source "${CURRENT_DIR}/coverage_helpers.sh" \ + || { echo "coverage_helpers.sh not found!" >&2; exit 1; } COVERAGE_GENERATOR_DIR="$1"; shift if [[ "${COVERAGE_GENERATOR_DIR}" != "released" ]]; then @@ -105,17 +107,6 @@ int main(void) { EOF } -# 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. -function get_coverage_file_path_from_test_log() { - local ending_part="$(sed -n -e '/PASSED/,$p' "$TEST_log")" - - local coverage_file_path=$(grep -Eo "/[/a-zA-Z0-9\.\_\-]+\.dat$" <<< "$ending_part") - [[ -e "$coverage_file_path" ]] || fail "Coverage output file does not exist!" - echo "$coverage_file_path" -} - function test_cc_test_llvm_coverage_doesnt_fail() { local -r llvmprofdata=$(which llvm-profdata) if [[ ! -x ${llvmprofdata:-/usr/bin/llvm-profdata} ]]; then @@ -462,4 +453,4 @@ end_of_record' assert_equals "$expected_result" "$(cat bazel-out/_coverage/_coverage_report.dat | grep -v '^BR')" } -run_suite "test tests" \ No newline at end of file +run_suite "test tests" diff --git a/src/test/shell/bazel/bazel_coverage_java_test.sh b/src/test/shell/bazel/bazel_coverage_java_test.sh index fb9659351e9a16..c80528ec470744 100755 --- a/src/test/shell/bazel/bazel_coverage_java_test.sh +++ b/src/test/shell/bazel/bazel_coverage_java_test.sh @@ -20,6 +20,8 @@ set -eu CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "${CURRENT_DIR}/../integration_test_setup.sh" \ || { echo "integration_test_setup.sh not found!" >&2; exit 1; } +source "${CURRENT_DIR}/coverage_helpers.sh" \ + || { echo "coverage_helpers.sh not found!" >&2; exit 1; } JAVA_TOOLS_ZIP="$1"; shift @@ -92,77 +94,6 @@ EOF fi } -# Returns 0 if gcov is not installed or if a version before 7.0 was found. -# Returns 1 otherwise. -function is_gcov_missing_or_wrong_version() { - local -r gcov_location=$(which gcov) - if [[ ! -x ${gcov_location:-/usr/bin/gcov} ]]; then - echo "gcov not installed." - return 0 - fi - - "$gcov_location" -version | grep "LLVM" && \ - echo "gcov LLVM version not supported." && return 0 - # gcov -v | grep "gcov" outputs a line that looks like this: - # gcov (Debian 7.3.0-5) 7.3.0 - local gcov_version="$(gcov -v | grep "gcov" | cut -d " " -f 4 | cut -d "." -f 1)" - [ "$gcov_version" -lt 7 ] \ - && echo "gcov versions before 7.0 is not supported." && return 0 - return 1 -} - -# Asserts if the given expected coverage result is included in the given output -# file. -# -# - expected_coverage The expected result that must be included in the output. -# - output_file The location of the coverage output file. -function assert_coverage_result() { - local expected_coverage="${1}"; shift - local output_file="${1}"; shift - - # Replace newlines with commas to facilitate the assertion. - local expected_coverage_no_newlines="$( echo "$expected_coverage" | tr '\n' ',' )" - local output_file_no_newlines="$( cat "$output_file" | tr '\n' ',' )" - - (echo "$output_file_no_newlines" \ - | grep -F "$expected_coverage_no_newlines") \ - || fail "Expected coverage result -<$expected_coverage> -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. - # gcov -v | grep "gcov" outputs a line that looks like this: - # gcov (Debian 7.3.0-5) 7.3.0 - 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. -function get_coverage_file_path_from_test_log() { - local ending_part="$(sed -n -e '/PASSED/,$p' "$TEST_log")" - - local coverage_file_path=$(grep -Eo "/[/a-zA-Z0-9\.\_\-]+\.dat$" <<< "$ending_part") - [[ -e "$coverage_file_path" ]] || fail "Coverage output file does not exist!" - echo "$coverage_file_path" -} - function test_java_test_coverage() { cat < BUILD load("@bazel_tools//tools/jdk:default_java_toolchain.bzl", "default_java_toolchain") diff --git a/src/test/shell/bazel/bazel_coverage_py_test.sh b/src/test/shell/bazel/bazel_coverage_py_test.sh index e7a56d34cdc41c..36c4bfdecb864f 100755 --- a/src/test/shell/bazel/bazel_coverage_py_test.sh +++ b/src/test/shell/bazel/bazel_coverage_py_test.sh @@ -20,6 +20,8 @@ set -eu CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "${CURRENT_DIR}/../integration_test_setup.sh" \ || { echo "integration_test_setup.sh not found!" >&2; exit 1; } +source "${CURRENT_DIR}/coverage_helpers.sh" \ + || { echo "coverage_helpers.sh not found!" >&2; exit 1; } # Returns 0 if gcov is not installed or if a version before 7.0 was found. # Returns 1 otherwise. @@ -40,58 +42,6 @@ function is_gcov_missing_or_wrong_version() { return 1 } -# Asserts if the given expected coverage result is included in the given output -# file. -# -# - expected_coverage The expected result that must be included in the output. -# - output_file The location of the coverage output file. -function assert_coverage_result() { - local expected_coverage="${1}"; shift - local output_file="${1}"; shift - - # Replace newlines with commas to facilitate the assertion. - local expected_coverage_no_newlines="$( echo "$expected_coverage" | tr '\n' ',' )" - local output_file_no_newlines="$( cat "$output_file" | tr '\n' ',' )" - - (echo "$output_file_no_newlines" \ - | grep -F "$expected_coverage_no_newlines") \ - || fail "Expected coverage result -<$expected_coverage> -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. - # gcov -v | grep "gcov" outputs a line that looks like this: - # gcov (Debian 7.3.0-5) 7.3.0 - 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. -function get_coverage_file_path_from_test_log() { - local ending_part="$(sed -n -e '/PASSED/,$p' "$TEST_log")" - - local coverage_file_path=$(grep -Eo "/[/a-zA-Z0-9\.\_\-]+\.dat$" <<< "$ending_part") - [[ -e "$coverage_file_path" ]] || fail "Coverage output file does not exist!" - echo "$coverage_file_path" -} - function set_up_py_test_coverage() { cat < BUILD py_test( diff --git a/src/test/shell/bazel/bazel_coverage_sh_test.sh b/src/test/shell/bazel/bazel_coverage_sh_test.sh index 9d2adb80e55388..487296978e8088 100755 --- a/src/test/shell/bazel/bazel_coverage_sh_test.sh +++ b/src/test/shell/bazel/bazel_coverage_sh_test.sh @@ -20,77 +20,9 @@ set -eu CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "${CURRENT_DIR}/../integration_test_setup.sh" \ || { echo "integration_test_setup.sh not found!" >&2; exit 1; } +source "${CURRENT_DIR}/coverage_helpers.sh" \ + || { echo "coverage_helpers.sh not found!" >&2; exit 1; } -# Returns 0 if gcov is not installed or if a version before 7.0 was found. -# Returns 1 otherwise. -function is_gcov_missing_or_wrong_version() { - local -r gcov_location=$(which gcov) - if [[ ! -x ${gcov_location:-/usr/bin/gcov} ]]; then - echo "gcov not installed." - return 0 - fi - - "$gcov_location" -version | grep "LLVM" && \ - echo "gcov LLVM version not supported." && return 0 - # gcov -v | grep "gcov" outputs a line that looks like this: - # gcov (Debian 7.3.0-5) 7.3.0 - local gcov_version="$(gcov -v | grep "gcov" | cut -d " " -f 4 | cut -d "." -f 1)" - [ "$gcov_version" -lt 7 ] \ - && echo "gcov versions before 7.0 is not supported." && return 0 - return 1 -} - -# Asserts if the given expected coverage result is included in the given output -# file. -# -# - expected_coverage The expected result that must be included in the output. -# - output_file The location of the coverage output file. -function assert_coverage_result() { - local expected_coverage="${1}"; shift - local output_file="${1}"; shift - - # Replace newlines with commas to facilitate the assertion. - local expected_coverage_no_newlines="$( echo "$expected_coverage" | tr '\n' ',' )" - local output_file_no_newlines="$( cat "$output_file" | tr '\n' ',' )" - - (echo "$output_file_no_newlines" \ - | grep -F "$expected_coverage_no_newlines") \ - || fail "Expected coverage result -<$expected_coverage> -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. - # gcov -v | grep "gcov" outputs a line that looks like this: - # gcov (Debian 7.3.0-5) 7.3.0 - 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. -function get_coverage_file_path_from_test_log() { - local ending_part="$(sed -n -e '/PASSED/,$p' "$TEST_log")" - - local coverage_file_path=$(grep -Eo "/[/a-zA-Z0-9\.\_\-]+\.dat$" <<< "$ending_part") - [[ -e "$coverage_file_path" ]] || fail "Coverage output file does not exist!" - echo "$coverage_file_path" -} function set_up_sh_test_coverage() { cat < BUILD diff --git a/src/test/shell/bazel/coverage_helpers.sh b/src/test/shell/bazel/coverage_helpers.sh new file mode 100644 index 00000000000000..6d0a1d135c989d --- /dev/null +++ b/src/test/shell/bazel/coverage_helpers.sh @@ -0,0 +1,103 @@ +#!/bin/bash + +# Copyright 2023 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -eu + +# Outpus the major version of gcov. +# Returns 1 if gcov is not installed or a version before 7.0 was found. +function get_gcov_version() { + local -r gcov_location=$(which gcov) + if [[ ! -x ${gcov_location:-/usr/bin/gcov} ]]; then + echo "gcov not installed." + return 1 + fi + # Extract gcov's version: the output of `gcov --version` contains the + # version as a set of major-minor-patch numbers, of which we extract + # the major version. + # gcov --version outputs a line like: + # gcov (Debian 7.3.0-5) 7.3.0 + # llvm-cov gcov --version outputs a line like: + # LLVM version 9.0.1 + local version=$("${gcov_location}" --version | sed -n -E -e 's/^.*\s([0-9]+)\.[0-9]+\.[0-9]+\s?.*$/\1/p') + [ "$version" -lt 7 ] \ + && echo "gcov versions before 7.0 is not supported." && return 1 + echo "$version" +} + +# Returns 0 if gcov is not installed or if a version before 7.0 was found. +# Returns 1 otherwise. +# Inverts the return value of get_gcov_version +function is_gcov_missing_or_wrong_version() { + if get_gcov_version; then + return 1 + else + return 0 + fi +} + +# Asserts if the given expected coverage result is included in the given output +# file. +# +# - expected_coverage The expected result that must be included in the output. +# - output_file The location of the coverage output file. +function assert_coverage_result() { + local expected_coverage="${1}"; shift + local output_file="${1}"; shift + + # Replace newlines with commas to facilitate the assertion. + local expected_coverage_no_newlines="$( echo "$expected_coverage" | tr '\n' ',' )" + local output_file_no_newlines="$( cat "$output_file" | tr '\n' ',' )" + # strip any trailing commas + expected_coverage_no_newlines="$( echo "$expected_coverage_no_newlines" | sed 's/,*$//')" + output_file_no_newlines="$( echo "$output_file_no_newlines" | sed 's/,*$//' )" + + (echo "$output_file_no_newlines" \ + | grep -F "$expected_coverage_no_newlines") \ + || fail "Expected coverage result +<$expected_coverage> +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="$(get_gcov_version)" + 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. +function get_coverage_file_path_from_test_log() { + local ending_part="$(sed -n -e '/PASSED/,$p' "$TEST_log")" + + local coverage_file_path=$(grep -Eo "/[/a-zA-Z0-9\.\_\-]+\.dat$" <<< "$ending_part") + [[ -e "$coverage_file_path" ]] || fail "Coverage output file does not exist!" + echo "$coverage_file_path" +} +