]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: fix go test again
authorRuss Cox <rsc@golang.org>
Tue, 13 May 2014 05:38:10 +0000 (01:38 -0400)
committerRuss Cox <rsc@golang.org>
Tue, 13 May 2014 05:38:10 +0000 (01:38 -0400)
Fixes subrepo builds.

LGTM=iant, mikioh.mikioh
R=golang-codereviews, iant, mikioh.mikioh
CC=golang-codereviews
https://golang.org/cl/96310043

src/cmd/go/test.bash
src/cmd/go/test.go
src/cmd/go/testdata/src/xtestonly/f.go [new file with mode: 0644]
src/cmd/go/testdata/src/xtestonly/f_test.go [new file with mode: 0644]

index 92b6cf5962ac2ec07d4ab0ad9f1d43f46b1b23bc..9a4fc1fa66420a20a6628b57a4be147807eede5e 100755 (executable)
@@ -789,6 +789,16 @@ if ! ./testgo test testdata/standalone_test.go; then
        ok=false
 fi
 
+TEST 'go test xtestonly works'
+export GOPATH=$(pwd)/testdata
+./testgo clean -i xtestonly
+if ! ./testgo test xtestonly >/dev/null; then
+       echo "go test xtestonly failed"
+       ok=false
+fi
+unset GOPATH
+
+
 # clean up
 if $started; then stop; fi
 rm -rf testdata/bin testdata/bin1
index e309501f827766e6b57bac273cd11306003cf7b6..b30182791ecc6a747b9ba0d4fd4dc668af65f803 100644 (file)
@@ -559,8 +559,10 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action,
        }
        stk.pop()
        stk.push(p.ImportPath + "_test")
+       pxtestNeedsPtest := false
        for _, path := range p.XTestImports {
                if path == p.ImportPath {
+                       pxtestNeedsPtest = true
                        continue
                }
                p1 := loadImport(path, p.Dir, &stk, p.build.XTestImportPos[path])
@@ -666,7 +668,7 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action,
                        fake:    true,
                        Stale:   true,
                }
-               if ptest != p {
+               if pxtestNeedsPtest {
                        pxtest.imports = append(pxtest.imports, ptest)
                }
        }
@@ -684,17 +686,13 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action,
                Stale:      true,
                omitDWARF:  !testC && !testNeedBinary,
        }
-       if ptest != p {
-               pmain.imports = append(pmain.imports, ptest)
-       }
-       if pxtest != nil {
-               pmain.imports = append(pmain.imports, pxtest)
-       }
 
        // The generated main also imports testing and regexp.
        stk.push("testmain")
        for dep := range testMainDeps {
-               if ptest.ImportPath != dep {
+               if dep == ptest.ImportPath {
+                       pmain.imports = append(pmain.imports, ptest)
+               } else {
                        p1 := loadImport(dep, "", &stk, nil)
                        if p1.Error != nil {
                                return nil, nil, nil, p1.Error
@@ -717,6 +715,14 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action,
                }
        }
 
+       // writeTestmain writes _testmain.go but also updates
+       // pmain.imports to reflect the import statements written
+       // to _testmain.go. This metadata is needed for recompileForTest
+       // and the builds below.
+       if err := writeTestmain(filepath.Join(testDir, "_testmain.go"), pmain, ptest, pxtest); err != nil {
+               return nil, nil, nil, err
+       }
+
        if ptest != p && localCover {
                // We have made modifications to the package p being tested
                // and are rebuilding p (as ptest), writing it to the testDir tree.
@@ -733,10 +739,6 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action,
                recompileForTest(pmain, p, ptest, testDir)
        }
 
-       if err := writeTestmain(filepath.Join(testDir, "_testmain.go"), pmain, ptest); err != nil {
-               return nil, nil, nil, err
-       }
-
        computeStale(pmain)
 
        if ptest != p {
@@ -1056,30 +1058,36 @@ type coverInfo struct {
 }
 
 // writeTestmain writes the _testmain.go file for package p to
-// the file named out.
-func writeTestmain(out string, pmain, p *Package) error {
-       var cover []coverInfo
-       for _, cp := range pmain.imports {
-               if len(cp.coverVars) > 0 {
-                       cover = append(cover, coverInfo{cp, cp.coverVars})
-               }
-       }
-
+// the file named out. It also updates pmain.imports to include
+// ptest and/or pxtest, depending on what it writes to _testmain.go.
+func writeTestmain(out string, pmain, ptest, pxtest *Package) error {
        t := &testFuncs{
-               Package: p,
-               Cover:   cover,
+               Package: ptest,
        }
-       for _, file := range p.TestGoFiles {
-               if err := t.load(filepath.Join(p.Dir, file), "_test", &t.NeedTest); err != nil {
+       for _, file := range ptest.TestGoFiles {
+               if err := t.load(filepath.Join(ptest.Dir, file), "_test", &t.NeedTest); err != nil {
                        return err
                }
        }
-       for _, file := range p.XTestGoFiles {
-               if err := t.load(filepath.Join(p.Dir, file), "_xtest", &t.NeedXtest); err != nil {
+       for _, file := range ptest.XTestGoFiles {
+               if err := t.load(filepath.Join(ptest.Dir, file), "_xtest", &t.NeedXtest); err != nil {
                        return err
                }
        }
 
+       if t.NeedTest {
+               pmain.imports = append(pmain.imports, ptest)
+       }
+       if t.NeedXtest {
+               pmain.imports = append(pmain.imports, pxtest)
+       }
+
+       for _, cp := range pmain.imports {
+               if len(cp.coverVars) > 0 {
+                       t.Cover = append(t.Cover, coverInfo{cp, cp.coverVars})
+               }
+       }
+
        f, err := os.Create(out)
        if err != nil {
                return err
diff --git a/src/cmd/go/testdata/src/xtestonly/f.go b/src/cmd/go/testdata/src/xtestonly/f.go
new file mode 100644 (file)
index 0000000..dac039e
--- /dev/null
@@ -0,0 +1,3 @@
+package xtestonly
+
+func F() int { return 42 }
diff --git a/src/cmd/go/testdata/src/xtestonly/f_test.go b/src/cmd/go/testdata/src/xtestonly/f_test.go
new file mode 100644 (file)
index 0000000..01f6e83
--- /dev/null
@@ -0,0 +1,12 @@
+package xtestonly_test
+
+import (
+       "testing"
+       "xtestonly"
+)
+
+func TestF(t *testing.T) {
+       if x := xtestonly.F(); x != 42 {
+               t.Errorf("f.F() = %d, want 42", x)
+       }
+}