Skip to content

Commit 8ddc110

Browse files
thanmjohanbrandhorst
authored andcommitted
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 golang#58370. Change-Id: I0d9bc4f2b687c6bd913595418f6db7dbe50cc5df Reviewed-on: https://go-review.googlesource.com/c/go/+/466115 Reviewed-by: David Chase <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Than McIntosh <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent fd3b1d0 commit 8ddc110

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/cmd/cover/cover.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ func (p *Package) annotateFile(name string, fd io.Writer, last bool) {
656656
// Emit a reference to the atomic package to avoid
657657
// import and not used error when there's no code in a file.
658658
if *mode == "atomic" {
659-
fmt.Fprintf(fd, "var _ = %sLoadUint32\n", atomicPackagePrefix())
659+
fmt.Fprintf(fd, "\nvar _ = %sLoadUint32\n", atomicPackagePrefix())
660660
}
661661

662662
// Last file? Emit meta-data and converage config.

src/cmd/cover/cover_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ func TestCoverWithToolExec(t *testing.T) {
126126
t.Run("FuncWithDuplicateLines", func(t *testing.T) {
127127
testFuncWithDuplicateLines(t, toolexecArg)
128128
})
129+
t.Run("MissingTrailingNewlineIssue58370", func(t *testing.T) {
130+
testMissingTrailingNewlineIssue58370(t, toolexecArg)
131+
})
129132
}
130133

131134
// Execute this command sequence:
@@ -574,3 +577,42 @@ func runExpectingError(c *exec.Cmd, t *testing.T) string {
574577
}
575578
return string(out)
576579
}
580+
581+
// Test instrumentation of package that ends before an expected
582+
// trailing newline following package clause. Issue #58370.
583+
func testMissingTrailingNewlineIssue58370(t *testing.T, toolexecArg string) {
584+
testenv.MustHaveGoBuild(t)
585+
dir := tempDir(t)
586+
587+
t.Parallel()
588+
589+
noeolDir := filepath.Join(dir, "issue58370")
590+
noeolGo := filepath.Join(noeolDir, "noeol.go")
591+
noeolTestGo := filepath.Join(noeolDir, "noeol_test.go")
592+
593+
if err := os.Mkdir(noeolDir, 0777); err != nil {
594+
t.Fatal(err)
595+
}
596+
597+
if err := os.WriteFile(filepath.Join(noeolDir, "go.mod"), []byte("module noeol\n"), 0666); err != nil {
598+
t.Fatal(err)
599+
}
600+
const noeolContents = `package noeol`
601+
if err := os.WriteFile(noeolGo, []byte(noeolContents), 0444); err != nil {
602+
t.Fatal(err)
603+
}
604+
const noeolTestContents = `
605+
package noeol
606+
import "testing"
607+
func TestCoverage(t *testing.T) { }
608+
`
609+
if err := os.WriteFile(noeolTestGo, []byte(noeolTestContents), 0444); err != nil {
610+
t.Fatal(err)
611+
}
612+
613+
// go test -covermode atomic
614+
cmd := testenv.Command(t, testenv.GoToolPath(t), "test", toolexecArg, "-covermode", "atomic")
615+
cmd.Env = append(cmd.Environ(), "CMDCOVER_TOOLEXEC=true")
616+
cmd.Dir = noeolDir
617+
run(cmd, t)
618+
}

0 commit comments

Comments
 (0)