Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix coverage collection with modified COVERAGE_DIR env variable #3820

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading