Commit 62ed6ee6 authored by Rob Pike's avatar Rob Pike

ngotest: correctly handle packages with tests outside the package.

R=rsc
CC=golang-dev
https://golang.org/cl/4330043
parent 9225bbfc
...@@ -23,6 +23,7 @@ import ( ...@@ -23,6 +23,7 @@ import (
// Environment for commands. // Environment for commands.
var ( var (
XGC []string // 6g -I _test -o _xtest_.6
GC []string // 6g -I _test _testmain.go GC []string // 6g -I _test _testmain.go
GL []string // 6l -L _test _testmain.6 GL []string // 6l -L _test _testmain.6
GOARCH string GOARCH string
...@@ -32,8 +33,14 @@ var ( ...@@ -32,8 +33,14 @@ var (
env = os.Environ() env = os.Environ()
) )
// These strings are created by getTestNames.
var ( var (
files = make([]*File, 0, 10) insideFileNames []string // list of *.go files inside the package.
outsideFileNames []string // list of *.go files outside the package (in package foo_test).
)
var (
files []*File
importPath string importPath string
) )
...@@ -72,7 +79,10 @@ func main() { ...@@ -72,7 +79,10 @@ func main() {
parseFiles() parseFiles()
getTestNames() getTestNames()
run("gomake", "testpackage-clean") run("gomake", "testpackage-clean")
run("gomake", "testpackage", fmt.Sprintf("GOTESTFILES=%s", insideFileNames())) run("gomake", "testpackage", fmt.Sprintf("GOTESTFILES=%s", strings.Join(insideFileNames, " ")))
if len(outsideFileNames) > 0 {
run(append(XGC, outsideFileNames...)...)
}
importPath = runWithStdout("gomake", "-s", "importpath") importPath = runWithStdout("gomake", "-s", "importpath")
writeTestmainGo() writeTestmainGo()
run(GC...) run(GC...)
...@@ -149,6 +159,7 @@ func setEnvironment() { ...@@ -149,6 +159,7 @@ func setEnvironment() {
if gc == "" { if gc == "" {
gc = O + "g" gc = O + "g"
} }
XGC = []string{gc, "-I", "_test", "-o", "_xtest_." + O}
GC = []string{gc, "-I", "_test", "_testmain.go"} GC = []string{gc, "-I", "_test", "_testmain.go"}
gl := os.Getenv("GL") gl := os.Getenv("GL")
if gl == "" { if gl == "" {
...@@ -185,9 +196,10 @@ func getTestFileNames() { ...@@ -185,9 +196,10 @@ func getTestFileNames() {
func parseFiles() { func parseFiles() {
fileSet := token.NewFileSet() fileSet := token.NewFileSet()
for _, f := range files { for _, f := range files {
file, err := parser.ParseFile(fileSet, f.name, nil, 0) // Report declaration errors so we can abort if the files are incorrect Go.
file, err := parser.ParseFile(fileSet, f.name, nil, parser.DeclarationErrors)
if err != nil { if err != nil {
Fatalf("could not parse %s: %s", f.name, err) Fatalf("parse error: %s", err)
} }
f.astFile = file f.astFile = file
f.pkg = file.Name.String() f.pkg = file.Name.String()
...@@ -217,6 +229,11 @@ func getTestNames() { ...@@ -217,6 +229,11 @@ func getTestNames() {
} }
// TODO: worth checking the signature? Probably not. // TODO: worth checking the signature? Probably not.
} }
if strings.HasSuffix(f.pkg, "_test") {
outsideFileNames = append(outsideFileNames, f.name)
} else {
insideFileNames = append(insideFileNames, f.name)
}
} }
} }
...@@ -231,19 +248,6 @@ func isTest(name, prefix string) bool { ...@@ -231,19 +248,6 @@ func isTest(name, prefix string) bool {
return !unicode.IsLower(rune) return !unicode.IsLower(rune)
} }
// insideFileNames returns the list of files in package foo, not a package foo_test, as a space-separated string.
func insideFileNames() (result string) {
for _, f := range files {
if !strings.HasSuffix(f.pkg, "_test") {
if len(result) > 0 {
result += " "
}
result += f.name
}
}
return
}
func run(args ...string) { func run(args ...string) {
doRun(args, false) doRun(args, false)
} }
...@@ -349,6 +353,8 @@ func writeTestmainGo() { ...@@ -349,6 +353,8 @@ func writeTestmainGo() {
// Package and imports. // Package and imports.
fmt.Fprint(b, "package main\n\n") fmt.Fprint(b, "package main\n\n")
// Are there tests from a package other than the one we're testing? // Are there tests from a package other than the one we're testing?
// We can't just use file names because some of the things we compiled
// contain no tests.
outsideTests := false outsideTests := false
insideTests := false insideTests := false
for _, f := range files { for _, f := range files {
......
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