Skip to content

Commit 4c5fb3c

Browse files
committed
Simplify saveCoverProfile using bcmills' comment
1 parent 8dcfa55 commit 4c5fb3c

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

src/cmd/go/internal/test/test.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,25 +1854,21 @@ func (c *runCache) saveOutput(a *work.Action, coverprofileFile string) {
18541854
if err != nil {
18551855
return
18561856
}
1857-
var coverprof *os.File
1857+
saveCoverProfile := func(testID cache.ActionID) {}
18581858
if coverprofileFile != "" {
1859-
coverprof, err = os.Open(coverprofileFile)
1860-
if err != nil && cache.DebugTest {
1861-
fmt.Fprintf(os.Stderr, "testcache: %s: failed to open temporary coverprofile: %s", a.Package.ImportPath, err)
1862-
}
1863-
}
1864-
if coverprof != nil {
1865-
defer func() {
1866-
if err := coverprof.Close(); err != nil && cache.DebugTest {
1867-
fmt.Fprintf(os.Stderr, "testcache: %s: closing temporary coverprofile: %v", a.Package.ImportPath, err)
1859+
coverprof, err := os.Open(coverprofileFile)
1860+
if err == nil {
1861+
saveCoverProfile = func(testID cache.ActionID) {
1862+
cache.Default().Put(testCoverProfileKey(testID, testInputsID), coverprof)
18681863
}
1869-
}()
1870-
}
1871-
saveCoverProfile := func(testID cache.ActionID) {
1872-
if coverprof == nil {
1873-
return
1864+
defer func() {
1865+
if err := coverprof.Close(); err != nil && cache.DebugTest {
1866+
fmt.Fprintf(os.Stderr, "testcache: %s: closing temporary coverprofile: %v", a.Package.ImportPath, err)
1867+
}
1868+
}()
1869+
} else if cache.DebugTest {
1870+
fmt.Fprintf(os.Stderr, "testcache: %s: failed to open temporary coverprofile: %s", a.Package.ImportPath, err)
18741871
}
1875-
cache.Default().Put(testCoverProfileKey(testID, testInputsID), coverprof)
18761872
}
18771873
if c.id1 != (cache.ActionID{}) {
18781874
if cache.DebugTest {

0 commit comments

Comments
 (0)