Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract coverage test helper functions into a separate shell script. #18898

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/test/shell/bazel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ filegroup(
testonly = 1,
srcs = [
"cc_api_rules.bzl",
"coverage_helpers.sh",
"remote_helpers.sh",
"testing_server.py",
":hamcrest_jar",
Expand Down
38 changes: 10 additions & 28 deletions src/test/shell/bazel/bazel_cc_code_coverage_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
#
Expand All @@ -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"
}


Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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"
}


Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down
72 changes: 2 additions & 70 deletions src/test/shell/bazel/bazel_coverage_cc_test_gcc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 3 additions & 12 deletions src/test/shell/bazel/bazel_coverage_cc_test_llvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
run_suite "test tests"
73 changes: 2 additions & 71 deletions src/test/shell/bazel/bazel_coverage_java_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <<EOF > BUILD
load("@bazel_tools//tools/jdk:default_java_toolchain.bzl", "default_java_toolchain")
Expand Down
54 changes: 2 additions & 52 deletions src/test/shell/bazel/bazel_coverage_py_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 <<EOF > BUILD
py_test(
Expand Down
Loading
Loading