Skip to content

Commit

Permalink
scripts: add code coverage script
Browse files Browse the repository at this point in the history
Add a script that collects overall code coverage profiles from all
tests (including the metamorphic test).

It generates three profiles, in LCOV format: `profile-tests.lcov`,
`profile-meta.lcov`, and `profile-combined.lcov`.

The intention is to make this script run nightly and add a front end
to visualize the coverage.
  • Loading branch information
RaduBerinde committed Sep 3, 2023
1 parent a207649 commit a4fe050
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions scripts/code-coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

set -euxo pipefail

mkdir -p artifacts

tmpdir=$(mktemp -d)
trap 'rm -rf "$tmpdir"' EXIT

test_failed=0
# The coverpkg argument ensures that coverage is not restricted to the tested
# package; so this will get us overall coverage for all tests.
go test ./... -coverprofile=artifacts/profile-tests.gocov -coverpkg=./... || test_failed=1

# The metamorphic test executes itself for each run; we don't get coverage for
# the inner run. To fix this, we use metarunner as the "inner" binary and we
# instrument it with coverage (see https://go.dev/testing/coverage/#building).
go build -o "${tmpdir}/metarunner" -cover ./internal/metamorphic/metarunner
mkdir -p "${tmpdir}/metacover"

GOCOVERDIR="${tmpdir}/metacover" go test ./internal/metamorphic \
-count 50 --inner-binary="${tmpdir}/metarunner" || test_failed=1

go tool covdata textfmt -i "${tmpdir}/metacover" -o artifacts/profile-meta.gocov

# TODO(radu): make the crossversion metamorphic test work.

go run github.com/cockroachdb/code-cov-utils/[email protected] -out artifacts/profile-tests.lcov \
-trim-prefix github.com/cockroachdb/pebble/ \
artifacts/profile-tests.gocov

go run github.com/cockroachdb/code-cov-utils/[email protected] -out artifacts/profile-meta.lcov \
-trim-prefix github.com/cockroachdb/pebble/ \
artifacts/profile-meta.gocov

go run github.com/cockroachdb/code-cov-utils/[email protected] -out artifacts/profile-tests-and-meta.lcov \
-trim-prefix github.com/cockroachdb/pebble/ \
artifacts/profile-tests.gocov artifacts/profile-meta.gocov

if [ $test_failed -eq 1 ]; then
echo "WARNING: some tests have failed; coverage might be incomplete."
fi

0 comments on commit a4fe050

Please sign in to comment.