]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: don't let "go test -c -o /dev/null" overwrite /dev/null
authorIan Lance Taylor <iant@golang.org>
Fri, 2 Nov 2018 14:34:52 +0000 (07:34 -0700)
committerIan Lance Taylor <iant@golang.org>
Fri, 2 Nov 2018 17:56:07 +0000 (17:56 +0000)
Fixes #28549

Change-Id: Iba71bb2edd0759004e0c7df92b2b8f1197bd62d3
Reviewed-on: https://go-review.googlesource.com/c/146901
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/cmd/go/go_test.go
src/cmd/go/internal/test/test.go

index aa6ce27ffc2555a2e125d8b4783d1df3aa13761f..f956ecb9163fc9c741ba2efbd69665588469ef03 100644 (file)
@@ -1462,8 +1462,38 @@ func TestInstallIntoGOPATH(t *testing.T) {
 func TestBuildOutputToDevNull(t *testing.T) {
        tg := testgo(t)
        defer tg.cleanup()
+       fi1, err1 := os.Lstat(os.DevNull)
        tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
        tg.run("build", "-o", os.DevNull, "go-cmd-test")
+       fi2, err2 := os.Lstat(os.DevNull)
+       if err1 == nil {
+               if err2 != nil {
+                       t.Errorf("second stat of /dev/null failed: %v", err2)
+               } else if !os.SameFile(fi1, fi2) {
+                       t.Errorf("/dev/null changed: now %v was %v", fi1, fi2)
+               }
+       }
+}
+
+// Issue 28549.
+func TestTestOutputToDevNull(t *testing.T) {
+       tg := testgo(t)
+       defer tg.cleanup()
+       fi1, err1 := os.Lstat(os.DevNull)
+       tg.makeTempdir()
+       tg.setenv("GOPATH", tg.path("."))
+       tg.tempFile("src/p/p.go", "package p\n")
+       tg.tempFile("src/p/p_test.go", "package p\nimport \"testing\"\nfunc TestX(t *testing.T) {}\n")
+       tg.run("test", "-o", os.DevNull, "-c", "p")
+       tg.mustNotExist("p.test")
+       fi2, err2 := os.Lstat(os.DevNull)
+       if err1 == nil {
+               if err2 != nil {
+                       t.Errorf("second stat of /dev/null failed: %v", err2)
+               } else if !os.SameFile(fi1, fi2) {
+                       t.Errorf("/dev/null changed: now %v was %v", fi1, fi2)
+               }
+       }
 }
 
 func TestPackageMainTestImportsArchiveNotBinary(t *testing.T) {
index 70deea3643b7588ccc7c485f842d67601556a9d3..750b515e414873361e7c165965274db46853401e 100644 (file)
@@ -887,15 +887,19 @@ func builderTest(b *work.Builder, p *load.Package) (buildAction, runAction, prin
                                target = filepath.Join(base.Cwd, target)
                        }
                }
-               pmain.Target = target
-               installAction = &work.Action{
-                       Mode:    "test build",
-                       Func:    work.BuildInstallFunc,
-                       Deps:    []*work.Action{buildAction},
-                       Package: pmain,
-                       Target:  target,
+               if target == os.DevNull {
+                       runAction = buildAction
+               } else {
+                       pmain.Target = target
+                       installAction = &work.Action{
+                               Mode:    "test build",
+                               Func:    work.BuildInstallFunc,
+                               Deps:    []*work.Action{buildAction},
+                               Package: pmain,
+                               Target:  target,
+                       }
+                       runAction = installAction // make sure runAction != nil even if not running test
                }
-               runAction = installAction // make sure runAction != nil even if not running test
        }
        var vetRunAction *work.Action
        if testC {