]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: build non-runnable examples in xtests
authorAndrew Gerrand <adg@golang.org>
Fri, 27 Jun 2014 21:15:22 +0000 (07:15 +1000)
committerAndrew Gerrand <adg@golang.org>
Fri, 27 Jun 2014 21:15:22 +0000 (07:15 +1000)
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
src/cmd/go/test.go
src/cmd/go/testdata/norunexample/example_test.go [new file with mode: 0644]
src/cmd/go/testdata/norunexample/test_test.go [new file with mode: 0644]

index 0060ce2185f3f8e2f3d3545344db5b2814ee9306..c62f629405fd27f50e1d99420975e4962af2aebc 100755 (executable)
@@ -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
index 5935c98db9e82b1a6499a76cccded50e49c6ff1c..a602469e443f9e648b9d8ebccd4de7f093a32ebf 100644 (file)
@@ -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 (file)
index 0000000..e158305
--- /dev/null
@@ -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 (file)
index 0000000..d2e9198
--- /dev/null
@@ -0,0 +1,10 @@
+package pkg
+
+import (
+       "os"
+       "testing"
+)
+
+func TestBuilt(t *testing.T) {
+       os.Stdout.Write([]byte("A normal test was executed.\n"))
+}