diff --git a/scripts/code-coverage.sh b/scripts/code-coverage.sh new file mode 100755 index 0000000000..c046dadafd --- /dev/null +++ b/scripts/code-coverage.sh @@ -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/convert@v1.1.0 -out artifacts/profile-tests.lcov \ + -trim-prefix github.com/cockroachdb/pebble/ \ + artifacts/profile-tests.gocov + +go run github.com/cockroachdb/code-cov-utils/convert@v1.1.0 -out artifacts/profile-meta.lcov \ + -trim-prefix github.com/cockroachdb/pebble/ \ + artifacts/profile-meta.gocov + +go run github.com/cockroachdb/code-cov-utils/convert@v1.1.0 -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