From ea0fb5d8e2ba795f0fc985da27a9514492abd7bb Mon Sep 17 00:00:00 2001 From: Andrew Gerrand Date: Sat, 28 Jun 2014 07:15:22 +1000 Subject: [PATCH] cmd/go: build non-runnable examples in xtests Include these files in the build, even though they don't get executed. LGTM=r R=golang-codereviews, r CC=golang-codereviews https://golang.org/cl/108180043 --- src/cmd/go/test.bash | 8 ++++ src/cmd/go/test.go | 39 ++++++++++--------- .../go/testdata/norunexample/example_test.go | 11 ++++++ src/cmd/go/testdata/norunexample/test_test.go | 10 +++++ 4 files changed, 50 insertions(+), 18 deletions(-) create mode 100644 src/cmd/go/testdata/norunexample/example_test.go create mode 100644 src/cmd/go/testdata/norunexample/test_test.go diff --git a/src/cmd/go/test.bash b/src/cmd/go/test.bash index 0060ce2185..c62f629405 100755 --- a/src/cmd/go/test.bash +++ b/src/cmd/go/test.bash @@ -806,6 +806,14 @@ if ! ./testgo test xtestonly >/dev/null; then fi unset GOPATH +TEST 'go test builds an xtest containing only non-runnable examples' +if ! ./testgo test -v ./testdata/norunexample > testdata/std.out; then + echo "go test ./testdata/norunexample failed" + ok=false +elif ! grep 'File with non-runnable example was built.' testdata/std.out > /dev/null; then + echo "file with non-runnable example was not built" + ok=false +fi # clean up if $started; then stop; fi diff --git a/src/cmd/go/test.go b/src/cmd/go/test.go index 5935c98db9..a602469e44 100644 --- a/src/cmd/go/test.go +++ b/src/cmd/go/test.go @@ -723,10 +723,10 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action, if err != nil { return nil, nil, nil, err } - if t.NeedTest || ptest.coverMode != "" { + if t.ImportTest || ptest.coverMode != "" { pmain.imports = append(pmain.imports, ptest) } - if t.NeedXtest { + if t.ImportXtest { pmain.imports = append(pmain.imports, pxtest) } @@ -1082,12 +1082,12 @@ func loadTestFuncs(ptest *Package) (*testFuncs, error) { Package: ptest, } for _, file := range ptest.TestGoFiles { - if err := t.load(filepath.Join(ptest.Dir, file), "_test", &t.NeedTest); err != nil { + if err := t.load(filepath.Join(ptest.Dir, file), "_test", &t.ImportTest, &t.NeedTest); err != nil { return nil, err } } for _, file := range ptest.XTestGoFiles { - if err := t.load(filepath.Join(ptest.Dir, file), "_xtest", &t.NeedXtest); err != nil { + if err := t.load(filepath.Join(ptest.Dir, file), "_xtest", &t.ImportXtest, &t.NeedXtest); err != nil { return nil, err } } @@ -1110,13 +1110,15 @@ func writeTestmain(out string, t *testFuncs) error { } type testFuncs struct { - Tests []testFunc - Benchmarks []testFunc - Examples []testFunc - Package *Package - NeedTest bool - NeedXtest bool - Cover []coverInfo + Tests []testFunc + Benchmarks []testFunc + Examples []testFunc + Package *Package + ImportTest bool + NeedTest bool + ImportXtest bool + NeedXtest bool + Cover []coverInfo } func (t *testFuncs) CoverMode() string { @@ -1151,7 +1153,7 @@ type testFunc struct { var testFileSet = token.NewFileSet() -func (t *testFuncs) load(filename, pkg string, seen *bool) error { +func (t *testFuncs) load(filename, pkg string, doImport, seen *bool) error { f, err := parser.ParseFile(testFileSet, filename, nil, parser.ParseComments) if err != nil { return expandScanner(err) @@ -1168,15 +1170,16 @@ func (t *testFuncs) load(filename, pkg string, seen *bool) error { switch { case isTest(name, "Test"): t.Tests = append(t.Tests, testFunc{pkg, name, ""}) - *seen = true + *doImport, *seen = true, true case isTest(name, "Benchmark"): t.Benchmarks = append(t.Benchmarks, testFunc{pkg, name, ""}) - *seen = true + *doImport, *seen = true, true } } ex := doc.Examples(f) sort.Sort(byOrder(ex)) for _, e := range ex { + *doImport = true // import test file whether executed or not if e.Output == "" && !e.EmptyOutput { // Don't run examples with no output. continue @@ -1200,11 +1203,11 @@ import ( "regexp" "testing" -{{if .NeedTest}} - _test {{.Package.ImportPath | printf "%q"}} +{{if .ImportTest}} + {{if .NeedTest}}_test{{else}}_{{end}} {{.Package.ImportPath | printf "%q"}} {{end}} -{{if .NeedXtest}} - _xtest {{.Package.ImportPath | printf "%s_test" | printf "%q"}} +{{if .ImportXtest}} + {{if .NeedXtest}}_xtest{{else}}_{{end}} {{.Package.ImportPath | printf "%s_test" | printf "%q"}} {{end}} {{range $i, $p := .Cover}} _cover{{$i}} {{$p.Package.ImportPath | printf "%q"}} diff --git a/src/cmd/go/testdata/norunexample/example_test.go b/src/cmd/go/testdata/norunexample/example_test.go new file mode 100644 index 0000000000..e158305a6c --- /dev/null +++ b/src/cmd/go/testdata/norunexample/example_test.go @@ -0,0 +1,11 @@ +package pkg_test + +import "os" + +func init() { + os.Stdout.Write([]byte("File with non-runnable example was built.\n")) +} + +func Example_test() { + // This test will not be run, it has no "Output:" comment. +} diff --git a/src/cmd/go/testdata/norunexample/test_test.go b/src/cmd/go/testdata/norunexample/test_test.go new file mode 100644 index 0000000000..d2e919838f --- /dev/null +++ b/src/cmd/go/testdata/norunexample/test_test.go @@ -0,0 +1,10 @@ +package pkg + +import ( + "os" + "testing" +) + +func TestBuilt(t *testing.T) { + os.Stdout.Write([]byte("A normal test was executed.\n")) +} -- 2.48.1