From 62ed6ee6c4fcacce444bcc48be0cd490105351c2 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Tue, 29 Mar 2011 12:50:41 -0700 Subject: [PATCH] ngotest: correctly handle packages with tests outside the package. R=rsc CC=golang-dev https://golang.org/cl/4330043 --- src/cmd/gotest/ngotest.go | 40 ++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/src/cmd/gotest/ngotest.go b/src/cmd/gotest/ngotest.go index 6eb2dbebff..ae33eb7181 100644 --- a/src/cmd/gotest/ngotest.go +++ b/src/cmd/gotest/ngotest.go @@ -23,6 +23,7 @@ import ( // Environment for commands. var ( + XGC []string // 6g -I _test -o _xtest_.6 GC []string // 6g -I _test _testmain.go GL []string // 6l -L _test _testmain.6 GOARCH string @@ -32,8 +33,14 @@ var ( env = os.Environ() ) +// These strings are created by getTestNames. 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 ) @@ -72,7 +79,10 @@ func main() { parseFiles() getTestNames() 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") writeTestmainGo() run(GC...) @@ -149,6 +159,7 @@ func setEnvironment() { if gc == "" { gc = O + "g" } + XGC = []string{gc, "-I", "_test", "-o", "_xtest_." + O} GC = []string{gc, "-I", "_test", "_testmain.go"} gl := os.Getenv("GL") if gl == "" { @@ -185,9 +196,10 @@ func getTestFileNames() { func parseFiles() { fileSet := token.NewFileSet() 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 { - Fatalf("could not parse %s: %s", f.name, err) + Fatalf("parse error: %s", err) } f.astFile = file f.pkg = file.Name.String() @@ -217,6 +229,11 @@ func getTestNames() { } // 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 { 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) { doRun(args, false) } @@ -349,6 +353,8 @@ func writeTestmainGo() { // Package and imports. fmt.Fprint(b, "package main\n\n") // 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 insideTests := false for _, f := range files { -- 2.50.0