cmd/go/internal/clean: fix clean -testcache does not clean test cache

Truncate changes the size of the file. It does not change the I/O offset.

Fixes #29757

Change-Id: I1aa9223a86d6a8ce3c0efc3ac1d7d7647b77f589
Reviewed-on: https://go-review.googlesource.com/c/158117
Reviewed-by: Bryan C. Mills <bcmills@google.com>
This commit is contained in:
Baokun Lee 2019-01-16 18:53:35 +08:00 committed by Bryan C. Mills
parent 5fae09b738
commit 34817dd365
2 changed files with 19 additions and 1 deletions

View file

@ -152,7 +152,9 @@ func runClean(cmd *base.Command, args []string) {
prev, _ := strconv.ParseInt(strings.TrimSpace(string(buf)), 10, 64)
if now > prev {
if err = f.Truncate(0); err == nil {
_, err = fmt.Fprintf(f, "%d\n", now)
if _, err = f.Seek(0, 0); err == nil {
_, err = fmt.Fprintf(f, "%d\n", now)
}
}
}
if closeErr := f.Close(); err == nil {

View file

@ -0,0 +1,16 @@
# go clean -testcache
# should work (see golang.org/issue/29757).
cd x
go test x_test.go
go clean -testcache
go test x_test.go
! stdout 'cached'
-- x/x_test.go --
package x_test
import (
"testing"
)
func TestMain(t *testing.T) {
}