From 7190404b14597fa0afffff1313b192c2b83e04cb Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Fri, 20 Nov 2026 22:44:17 +0100 Subject: test.bash: collect coverage from unit and integration tests Most of the test suite runs the gocryptfs binary, so "go test -cover" alone misses most of the coverage. test.bash now builds a separate coverage-instrumented binary into ./coverage (./gocryptfs stays a normal build, so "make install" does not pick it up) and points the tests at it via $GOCRYPTFS_TEST_BINARY. "go test -cover" overwrites $GOCOVERDIR with a temporary directory and ignores the data of other executables in it, so the directory is passed as $GOCRYPTFS_COVERAGE_DATA and test_helpers sets $GOCOVERDIR from it. Only our packages that go into the gocryptfs binary are instrumented, so the reported percentage is relative to that code. The merged result is written to coverage/coverage.out and coverage/coverage.html. Co-Authored-By: Claude Opus 5.5 --- .gitignore | 4 +++- test.bash | 35 ++++++++++++++++++++++++++++------- tests/canonical-benchmarks.bash | 2 +- tests/test_helpers/helpers.go | 18 ++++++++++++++++-- 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 73b7357..dac064a 100644 --- a/.gitignore +++ b/.gitignore @@ -22,4 +22,6 @@ gocryptfs.1 /VERSION # Ignore macos specific files -.DS_Store \ No newline at end of file +.DS_Store +# Coverage data written by test.bash +/coverage diff --git a/test.bash b/test.bash index 12e6b7f..df7f9ac 100755 --- a/test.bash +++ b/test.bash @@ -89,13 +89,34 @@ if [[ $VERBOSE -eq 1 ]]; then EXTRA_ARGS="-p 1" fi -# We don't want all the subprocesses -# holding the lock file open -# vvvvv -# shellcheck disable=SC2086 -go test -count 1 $EXTRA_ARGS ./... "$@" 200>&- -# ^^^^^^^^ -# Disable result caching +# Coverage data from both the test binaries (unit tests) and the gocryptfs +# processes they start (integration tests) ends up in $COVERAGE/data. +COVERAGE=$PWD/coverage +rm -rf "$COVERAGE" +mkdir -p "$COVERAGE/data" +# Separate instrumented binary so that ./gocryptfs, which "make install" and +# build.bash install, stays a normal build. +# Only instrument our packages that go into the gocryptfs binary, so the +# reported percentage is relative to that code. +COVERPKG=$(go list -deps -f '{{if .Module}}{{if .Module.Main}}{{.ImportPath}}{{end}}{{end}}' . | paste -sd,) +go build -cover -coverpkg="$COVERPKG" -o "$COVERAGE/gocryptfs" . +export GOCRYPTFS_TEST_BINARY=$COVERAGE/gocryptfs +export GOCRYPTFS_COVERAGE_DATA=$COVERAGE/data + +( + # We don't want all the subprocesses holding the lock file open + exec 200>&- + # shellcheck disable=SC2086 + go test -count 1 -cover -coverpkg="$COVERPKG" $EXTRA_ARGS ./... "$@" -args -test.gocoverdir="$GOCRYPTFS_COVERAGE_DATA" + # ^^^^^^^^ + # Disable result caching +) +unset GOCRYPTFS_COVERAGE_DATA + +go tool covdata textfmt -i="$COVERAGE/data" -o "$COVERAGE/coverage.out" +go tool cover -html="$COVERAGE/coverage.out" -o "$COVERAGE/coverage.html" +echo "Total coverage: $(go tool cover -func="$COVERAGE/coverage.out" | tail -1 | awk '{print $NF}')" \ + "(details: $COVERAGE/coverage.html)" # Clean up dangling filesystems but do exit with an error if we found one unmount_leftovers || { echo "Error: the tests left mounted filesystems behind" ; exit 1 ; } diff --git a/tests/canonical-benchmarks.bash b/tests/canonical-benchmarks.bash index 963fd6a..5803896 100755 --- a/tests/canonical-benchmarks.bash +++ b/tests/canonical-benchmarks.bash @@ -49,7 +49,7 @@ dd if=zero of=/dev/null bs=131072 count=2000 2>&1 | tail -n 1 rm zero sleep 0.1 echo -n "UNTAR: " -etime tar xzf /tmp/linux-3.0.tar.gz +etime tar xf /tmp/linux-3.0.tar sleep 0.1 echo -n "MD5: " etime md5sum --quiet -c "$MD5" diff --git a/tests/test_helpers/helpers.go b/tests/test_helpers/helpers.go index fd2ea9c..53187ac 100644 --- a/tests/test_helpers/helpers.go +++ b/tests/test_helpers/helpers.go @@ -22,8 +22,16 @@ import ( // $TMPDIR/gocryptfs-test-parent . var testParentDir = "" -// GocryptfsBinary is the assumed path to the gocryptfs build. -const GocryptfsBinary = "../../gocryptfs" +// GocryptfsBinary is the path to the gocryptfs build under test. +// test.bash sets $GOCRYPTFS_TEST_BINARY to a coverage-instrumented build. +var GocryptfsBinary = gocryptfsBinary() + +func gocryptfsBinary() string { + if b := os.Getenv("GOCRYPTFS_TEST_BINARY"); b != "" { + return b + } + return "../../gocryptfs" +} // UnmountScript is the fusermount/umount compatibility wrapper script const UnmountScript = "../../tests/fuse-unmount.bash" @@ -42,6 +50,12 @@ var DefaultPlainDir string var DefaultCipherDir string func init() { + // "go test -cover" overwrites $GOCOVERDIR with a temporary directory, so + // let's overwrite it back. Then the gocryptfs processes, that we start, + // write coverage info to ./coverage/data like the unit tests do. + if d := os.Getenv("GOCRYPTFS_COVERAGE_DATA"); d != "" { + os.Setenv("GOCOVERDIR", d) + } doInit() } -- cgit v1.2.3