Commit 95e4181b authored by Russ Cox's avatar Russ Cox

cmd/go: fix go test again

Fixes subrepo builds.

LGTM=iant, mikioh.mikioh
R=golang-codereviews, iant, mikioh.mikioh
CC=golang-codereviews
https://golang.org/cl/96310043
parent 30b8af98
...@@ -789,6 +789,16 @@ if ! ./testgo test testdata/standalone_test.go; then ...@@ -789,6 +789,16 @@ if ! ./testgo test testdata/standalone_test.go; then
ok=false ok=false
fi 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 # clean up
if $started; then stop; fi if $started; then stop; fi
rm -rf testdata/bin testdata/bin1 rm -rf testdata/bin testdata/bin1
......
...@@ -559,8 +559,10 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action, ...@@ -559,8 +559,10 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action,
} }
stk.pop() stk.pop()
stk.push(p.ImportPath + "_test") stk.push(p.ImportPath + "_test")
pxtestNeedsPtest := false
for _, path := range p.XTestImports { for _, path := range p.XTestImports {
if path == p.ImportPath { if path == p.ImportPath {
pxtestNeedsPtest = true
continue continue
} }
p1 := loadImport(path, p.Dir, &stk, p.build.XTestImportPos[path]) p1 := loadImport(path, p.Dir, &stk, p.build.XTestImportPos[path])
...@@ -666,7 +668,7 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action, ...@@ -666,7 +668,7 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action,
fake: true, fake: true,
Stale: true, Stale: true,
} }
if ptest != p { if pxtestNeedsPtest {
pxtest.imports = append(pxtest.imports, ptest) pxtest.imports = append(pxtest.imports, ptest)
} }
} }
...@@ -684,17 +686,13 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action, ...@@ -684,17 +686,13 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action,
Stale: true, Stale: true,
omitDWARF: !testC && !testNeedBinary, 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. // The generated main also imports testing and regexp.
stk.push("testmain") stk.push("testmain")
for dep := range testMainDeps { for dep := range testMainDeps {
if ptest.ImportPath != dep { if dep == ptest.ImportPath {
pmain.imports = append(pmain.imports, ptest)
} else {
p1 := loadImport(dep, "", &stk, nil) p1 := loadImport(dep, "", &stk, nil)
if p1.Error != nil { if p1.Error != nil {
return nil, nil, nil, p1.Error return nil, nil, nil, p1.Error
...@@ -717,6 +715,14 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action, ...@@ -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 { if ptest != p && localCover {
// We have made modifications to the package p being tested // We have made modifications to the package p being tested
// and are rebuilding p (as ptest), writing it to the testDir tree. // 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, ...@@ -733,10 +739,6 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action,
recompileForTest(pmain, p, ptest, testDir) recompileForTest(pmain, p, ptest, testDir)
} }
if err := writeTestmain(filepath.Join(testDir, "_testmain.go"), pmain, ptest); err != nil {
return nil, nil, nil, err
}
computeStale(pmain) computeStale(pmain)
if ptest != p { if ptest != p {
...@@ -1056,30 +1058,36 @@ type coverInfo struct { ...@@ -1056,30 +1058,36 @@ type coverInfo struct {
} }
// writeTestmain writes the _testmain.go file for package p to // writeTestmain writes the _testmain.go file for package p to
// the file named out. // the file named out. It also updates pmain.imports to include
func writeTestmain(out string, pmain, p *Package) error { // ptest and/or pxtest, depending on what it writes to _testmain.go.
var cover []coverInfo func writeTestmain(out string, pmain, ptest, pxtest *Package) error {
for _, cp := range pmain.imports {
if len(cp.coverVars) > 0 {
cover = append(cover, coverInfo{cp, cp.coverVars})
}
}
t := &testFuncs{ t := &testFuncs{
Package: p, Package: ptest,
Cover: cover,
} }
for _, file := range p.TestGoFiles { for _, file := range ptest.TestGoFiles {
if err := t.load(filepath.Join(p.Dir, file), "_test", &t.NeedTest); err != nil { if err := t.load(filepath.Join(ptest.Dir, file), "_test", &t.NeedTest); err != nil {
return err return err
} }
} }
for _, file := range p.XTestGoFiles { for _, file := range ptest.XTestGoFiles {
if err := t.load(filepath.Join(p.Dir, file), "_xtest", &t.NeedXtest); err != nil { if err := t.load(filepath.Join(ptest.Dir, file), "_xtest", &t.NeedXtest); err != nil {
return err 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) f, err := os.Create(out)
if err != nil { if err != nil {
return err return err
......
package xtestonly
func F() int { return 42 }
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)
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment