Skip to content

Commit

Permalink
Fixup coverage script
Browse files Browse the repository at this point in the history
Summary:
rules_go reports coverage in LCOV format by default as of version 0.32.0
See bazelbuild/rules_go#3146 and bazelbuild/rules_go#3117
So the coverage script was no longer doing the correct thing. This fixes the script.

Test Plan: Ran the coverage script locally, generated an HTML report. Ensured that we see cpp, go and js coverage.

Reviewers: zasgar, michelle

Reviewed By: zasgar

Signed-off-by: Vihang Mehta <[email protected]>

Differential Revision: https://phab.corp.pixielabs.ai/D12345

GitOrigin-RevId: 6f4ff98f66fedd68e44f04954688c3efa95616b4
  • Loading branch information
vihangm authored and copybaranaut committed Oct 5, 2022
1 parent 3b58f99 commit 294f001
Showing 1 changed file with 20 additions and 35 deletions.
55 changes: 20 additions & 35 deletions ci/collect_coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ GENERATE_HTML=false
UPLOAD_TO_CODECOV=false
HTML_OUTPUT_DIR=""

CC_COVERAGE_FILE="cc_coverage.info"
GO_COVERAGE_FILE="coverage.txt"
UI_OUTPUT=bazel-testlogs/src/ui/ui-tests/coverage.dat
COVERAGE_FILE="coverage.info"

# Print out the usage information and exit.
usage() {
Expand Down Expand Up @@ -121,16 +119,11 @@ parse_args() {
}

generate_html() {
genhtml -o ${HTML_OUTPUT_DIR} -s ${CC_COVERAGE_FILE}

echo "****************************************************"
echo "* For Go HTML do the following: "
echo "* go tool cover -html=${GO_COVERAGE_FILE} "
echo "****************************************************"
genhtml -o "${HTML_OUTPUT_DIR}" -s ${COVERAGE_FILE}
}

upload_to_codecov() {
codecov -t "${CODECOV_TOKEN}" -B "${GIT_BRANCH}" -C "${GIT_COMMIT}" -r "${GIT_REPO}" -f "${CC_COVERAGE_FILE}" -f "${GO_COVERAGE_FILE}" -f "${UI_OUTPUT}"
codecov -t "${CODECOV_TOKEN}" -B "${GIT_BRANCH}" -C "${GIT_COMMIT}" -r "${GIT_REPO}" -f "${COVERAGE_FILE}"
}

# We use globs, make sure they are supported.
Expand All @@ -151,43 +144,35 @@ cd $(bazel info workspace)
bazel coverage --remote_download_outputs=all //src/...

# Fixup paths for the UI coverage output
sed -i "s|SF:src|SF:src/ui/src|g" ${UI_OUTPUT}
sed -i "s|SF:src|SF:src/ui/src|g" bazel-testlogs/src/ui/ui-tests/coverage.dat

# This finds all the valid coverage files and then creates a list of them
# prefixed by -a, which allows up to add them to the lcov output.
# This part only works for C++ coverage.
file_merge_args=""
for file in bazel-out/**/coverage.dat
do
# Only consider valid files. Some files only contain Go coverage and that
# does not work with LCOV.
lcov --summary "${file}" >/dev/null 2>&1 && file_merge_args+=" -a ${file}"
file_merge_args=()
for file in bazel-out/**/coverage.dat; do
# Only consider valid files.
if [ -s "${file}" ]; then
file_merge_args+=("-a" "${file}")
fi
done

# Merge all the files.
lcov $file_merge_args -o cc_coverage.info
lcov "${file_merge_args[@]}" -o ${COVERAGE_FILE}

# Print out the summary.
lcov --summary ${CC_COVERAGE_FILE}
lcov --summary ${COVERAGE_FILE}

# Remove test files from the coverage files.
lcov -r ${CC_COVERAGE_FILE} '**/*_test.cc' -o ${CC_COVERAGE_FILE}
lcov -r ${CC_COVERAGE_FILE} '**/*_mock.cc' -o ${CC_COVERAGE_FILE}
lcov -r ${CC_COVERAGE_FILE} '**/*_mock.h' -o ${CC_COVERAGE_FILE}
lcov -r ${COVERAGE_FILE} '**/*_test.cc' -o ${COVERAGE_FILE}
lcov -r ${COVERAGE_FILE} '**/*_mock.cc' -o ${COVERAGE_FILE}
lcov -r ${COVERAGE_FILE} '**/*_mock.h' -o ${COVERAGE_FILE}
lcov -r ${COVERAGE_FILE} '**/*_test.go' -o ${COVERAGE_FILE}
lcov -r ${COVERAGE_FILE} '**/*.gen.go' -o ${COVERAGE_FILE}
lcov -r ${COVERAGE_FILE} '**/*-mock.tsx' -o ${COVERAGE_FILE}
lcov -r ${COVERAGE_FILE} '**/*-mock.ts' -o ${COVERAGE_FILE}

# Print out the final summary.
lcov --summary ${CC_COVERAGE_FILE}

# Create go coverage file, by grabbing all the .go entries.
echo "mode: set" > coverage.tmp
for file in bazel-out/**/coverage.dat
do
grep ".go" ${file} >> coverage.tmp || true
done

# Remove test files from the go coverage.
grep -v "_test.go" coverage.tmp > ${GO_COVERAGE_FILE}
rm -f coverage.tmp
lcov --summary ${COVERAGE_FILE}

# Upload to codecov.io.
if [ "${UPLOAD_TO_CODECOV}" = true ]; then
Expand Down

0 comments on commit 294f001

Please sign in to comment.