From: Esko Luontola Date: Mon, 17 Oct 2011 03:19:02 +0000 (+1100) Subject: gotest: avoid conflicts with the name of the tested package X-Git-Tag: weekly.2011-10-18~50 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=edd1c9111d9e7d140f186124a89d9c47239e27b1;p=gostls13.git gotest: avoid conflicts with the name of the tested package Uses a generic solution of renaming the tested package, instead of using cryptic names for all other imports, variables and methods in the generated _testmain.go file. Fixes #2346. R=golang-dev, r, adg CC=golang-dev, rsc https://golang.org/cl/5254061 --- diff --git a/src/cmd/gotest/gotest.go b/src/cmd/gotest/gotest.go index 69b0d580b0..b03e79f00a 100644 --- a/src/cmd/gotest/gotest.go +++ b/src/cmd/gotest/gotest.go @@ -234,7 +234,7 @@ func getTestNames() { } // TODO: worth checking the signature? Probably not. } - if strings.HasSuffix(f.pkg, "_test") { + if isOutsideTest(f.pkg) { outsideFileNames = append(outsideFileNames, f.name) } else { insideFileNames = append(insideFileNames, f.name) @@ -371,38 +371,41 @@ func writeTestmainGo() { if len(f.tests) == 0 && len(f.benchmarks) == 0 { continue } - if strings.HasSuffix(f.pkg, "_test") { + if isOutsideTest(f.pkg) { outsideTests = true } else { insideTests = true } } + // Rename the imports for the system under test, + // in case the tested package has the same name + // as any of the other imports, variables or methods. if insideTests { switch importPath { case "testing": case "main": // Import path main is reserved, so import with // explicit reference to ./_test/main instead. - // Also, the file we are writing defines a function named main, - // so rename this import to __main__ to avoid name conflict. - fmt.Fprintf(b, "import __main__ %q\n", "./_test/main") + fmt.Fprintf(b, "import target %q\n", "./_test/main") default: - fmt.Fprintf(b, "import %q\n", importPath) + fmt.Fprintf(b, "import target %q\n", importPath) } } if outsideTests { - fmt.Fprintf(b, "import %q\n", "./_xtest_") + // It is possible to have both inside and outside tests + // at the same time, so a different import name is needed. + fmt.Fprintf(b, "import target_test %q\n", "./_xtest_") } fmt.Fprintf(b, "import %q\n", "testing") - fmt.Fprintf(b, "import __os__ %q\n", "os") // rename in case tested package is called os - fmt.Fprintf(b, "import __regexp__ %q\n", "regexp") // rename in case tested package is called regexp - fmt.Fprintln(b) // for gofmt + fmt.Fprintf(b, "import %q\n", "os") + fmt.Fprintf(b, "import %q\n", "regexp") + fmt.Fprintln(b) // for gofmt // Tests. fmt.Fprintln(b, "var tests = []testing.InternalTest{") for _, f := range files { for _, t := range f.tests { - fmt.Fprintf(b, "\t{\"%s.%s\", %s.%s},\n", f.pkg, t, notMain(f.pkg), t) + fmt.Fprintf(b, "\t{\"%s.%s\", %s.%s},\n", f.pkg, t, renamedPackage(f.pkg), t) } } fmt.Fprintln(b, "}") @@ -412,7 +415,7 @@ func writeTestmainGo() { fmt.Fprintf(b, "var benchmarks = []testing.InternalBenchmark{") for _, f := range files { for _, bm := range f.benchmarks { - fmt.Fprintf(b, "\t{\"%s.%s\", %s.%s},\n", f.pkg, bm, notMain(f.pkg), bm) + fmt.Fprintf(b, "\t{\"%s.%s\", %s.%s},\n", f.pkg, bm, renamedPackage(f.pkg), bm) } } fmt.Fprintln(b, "}") @@ -421,7 +424,7 @@ func writeTestmainGo() { fmt.Fprintf(b, "var examples = []testing.InternalExample{") for _, f := range files { for _, eg := range f.examples { - fmt.Fprintf(b, "\t{%q, %s.%s, %q},\n", eg.name, f.pkg, eg.name, eg.output) + fmt.Fprintf(b, "\t{%q, %s.%s, %q},\n", eg.name, renamedPackage(f.pkg), eg.name, eg.output) } } fmt.Fprintln(b, "}") @@ -430,23 +433,27 @@ func writeTestmainGo() { fmt.Fprintln(b, testBody) } -// notMain returns the package, renaming as appropriate if it's "main". -func notMain(pkg string) string { - if pkg == "main" { - return "__main__" +// renamedPackage returns the name under which the test package was imported. +func renamedPackage(pkg string) string { + if isOutsideTest(pkg) { + return "target_test" } - return pkg + return "target" +} + +func isOutsideTest(pkg string) bool { + return strings.HasSuffix(pkg, "_test") } // testBody is just copied to the output. It's the code that runs the tests. var testBody = ` var matchPat string -var matchRe *__regexp__.Regexp +var matchRe *regexp.Regexp -func matchString(pat, str string) (result bool, err __os__.Error) { +func matchString(pat, str string) (result bool, err os.Error) { if matchRe == nil || matchPat != pat { matchPat = pat - matchRe, err = __regexp__.Compile(matchPat) + matchRe, err = regexp.Compile(matchPat) if err != nil { return }