aboutsummaryrefslogtreecommitdiff
path: root/src/cmd
diff options
context:
space:
mode:
authorThan McIntosh <thanm@google.com>2023-02-07 11:11:45 -0500
committerThan McIntosh <thanm@google.com>2023-02-08 18:28:28 +0000
commitda9376604309b8c470985b8d517a2377cfa56efe (patch)
treec77c354fcd43ce9a4a957f8eac86e86611e6fc8a /src/cmd
parentcdc65b83fe07052d7b3fe740acd6a34450e93a8c (diff)
downloadgo-da9376604309b8c470985b8d517a2377cfa56efe.tar.xz
cmd/cover: add newline to fix -covermode=atomic build error
Fix a minor buglet in atomic mode fixup that would generate non-compilable code for a package containing only the "package X" clause with no trailing newline following the "X". Fixes #58370. Change-Id: I0d9bc4f2b687c6bd913595418f6db7dbe50cc5df Reviewed-on: https://go-review.googlesource.com/c/go/+/466115 Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Than McIntosh <thanm@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/cmd')
-rw-r--r--src/cmd/cover/cover.go2
-rw-r--r--src/cmd/cover/cover_test.go42
2 files changed, 43 insertions, 1 deletions
diff --git a/src/cmd/cover/cover.go b/src/cmd/cover/cover.go
index 74bb500cb9..49d3f580bc 100644
--- a/src/cmd/cover/cover.go
+++ b/src/cmd/cover/cover.go
@@ -656,7 +656,7 @@ func (p *Package) annotateFile(name string, fd io.Writer, last bool) {
// Emit a reference to the atomic package to avoid
// import and not used error when there's no code in a file.
if *mode == "atomic" {
- fmt.Fprintf(fd, "var _ = %sLoadUint32\n", atomicPackagePrefix())
+ fmt.Fprintf(fd, "\nvar _ = %sLoadUint32\n", atomicPackagePrefix())
}
// Last file? Emit meta-data and converage config.
diff --git a/src/cmd/cover/cover_test.go b/src/cmd/cover/cover_test.go
index af266b5e83..1292bbbf1f 100644
--- a/src/cmd/cover/cover_test.go
+++ b/src/cmd/cover/cover_test.go
@@ -126,6 +126,9 @@ func TestCoverWithToolExec(t *testing.T) {
t.Run("FuncWithDuplicateLines", func(t *testing.T) {
testFuncWithDuplicateLines(t, toolexecArg)
})
+ t.Run("MissingTrailingNewlineIssue58370", func(t *testing.T) {
+ testMissingTrailingNewlineIssue58370(t, toolexecArg)
+ })
}
// Execute this command sequence:
@@ -574,3 +577,42 @@ func runExpectingError(c *exec.Cmd, t *testing.T) string {
}
return string(out)
}
+
+// Test instrumentation of package that ends before an expected
+// trailing newline following package clause. Issue #58370.
+func testMissingTrailingNewlineIssue58370(t *testing.T, toolexecArg string) {
+ testenv.MustHaveGoBuild(t)
+ dir := tempDir(t)
+
+ t.Parallel()
+
+ noeolDir := filepath.Join(dir, "issue58370")
+ noeolGo := filepath.Join(noeolDir, "noeol.go")
+ noeolTestGo := filepath.Join(noeolDir, "noeol_test.go")
+
+ if err := os.Mkdir(noeolDir, 0777); err != nil {
+ t.Fatal(err)
+ }
+
+ if err := os.WriteFile(filepath.Join(noeolDir, "go.mod"), []byte("module noeol\n"), 0666); err != nil {
+ t.Fatal(err)
+ }
+ const noeolContents = `package noeol`
+ if err := os.WriteFile(noeolGo, []byte(noeolContents), 0444); err != nil {
+ t.Fatal(err)
+ }
+ const noeolTestContents = `
+package noeol
+import "testing"
+func TestCoverage(t *testing.T) { }
+`
+ if err := os.WriteFile(noeolTestGo, []byte(noeolTestContents), 0444); err != nil {
+ t.Fatal(err)
+ }
+
+ // go test -covermode atomic
+ cmd := testenv.Command(t, testenv.GoToolPath(t), "test", toolexecArg, "-covermode", "atomic")
+ cmd.Env = append(cmd.Environ(), "CMDCOVER_TOOLEXEC=true")
+ cmd.Dir = noeolDir
+ run(cmd, t)
+}