Skip to content

Commit

Permalink
Fix coverage collection with modified COVERAGE_DIR env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Jan 9, 2024
1 parent 15ddf3b commit 1ff83cb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
9 changes: 8 additions & 1 deletion go/tools/bzltestutil/lcov.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import (
"testing/internal/testdeps"
)

// Lock in the COVERAGE_DIR during test setup in case the test uses e.g. os.Clearenv.
var coverageDir = os.Getenv("COVERAGE_DIR")

// ConvertCoverToLcov converts the go coverprofile file coverage.dat.cover to
// the expectedLcov format and stores it in coverage.dat, where it is picked up by
// Bazel.
Expand All @@ -42,8 +45,12 @@ func ConvertCoverToLcov() error {
}
defer in.Close()

if coverageDir == "" {
log.Printf("Not collecting coverage: COVERAGE_DIR is not set")
return nil
}
// All *.dat files in $COVERAGE_DIR will be merged by Bazel's lcov_merger tool.
out, err := os.CreateTemp(os.Getenv("COVERAGE_DIR"), "go_coverage.*.dat")
out, err := os.CreateTemp(coverageDir, "go_coverage.*.dat")
if err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions tests/core/coverage/lcov_coverage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public class Tool {
package lib_test
import (
"os"
"os/exec"
"path/filepath"
"strings"
Expand All @@ -125,6 +126,8 @@ import (
)
func TestLib(t *testing.T) {
// Test that coverage is collected even if this variable is corrupted by a test.
os.Setenv("COVERAGE_DIR", "invalid")
if !strings.Contains(lib.HelloFromLib(false), "\n") {
t.Error("Expected a newline in the output")
}
Expand Down

0 comments on commit 1ff83cb

Please sign in to comment.