@@ -126,6 +126,9 @@ func TestCoverWithToolExec(t *testing.T) {
126
126
t .Run ("FuncWithDuplicateLines" , func (t * testing.T ) {
127
127
testFuncWithDuplicateLines (t , toolexecArg )
128
128
})
129
+ t .Run ("MissingTrailingNewlineIssue58370" , func (t * testing.T ) {
130
+ testMissingTrailingNewlineIssue58370 (t , toolexecArg )
131
+ })
129
132
}
130
133
131
134
// Execute this command sequence:
@@ -574,3 +577,42 @@ func runExpectingError(c *exec.Cmd, t *testing.T) string {
574
577
}
575
578
return string (out )
576
579
}
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