Commit ea0fb5d8 authored by Andrew Gerrand's avatar Andrew Gerrand

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
parent 128eed27
...@@ -806,6 +806,14 @@ if ! ./testgo test xtestonly >/dev/null; then ...@@ -806,6 +806,14 @@ if ! ./testgo test xtestonly >/dev/null; then
fi fi
unset GOPATH 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 # clean up
if $started; then stop; fi if $started; then stop; fi
......
...@@ -723,10 +723,10 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action, ...@@ -723,10 +723,10 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action,
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, err
} }
if t.NeedTest || ptest.coverMode != "" { if t.ImportTest || ptest.coverMode != "" {
pmain.imports = append(pmain.imports, ptest) pmain.imports = append(pmain.imports, ptest)
} }
if t.NeedXtest { if t.ImportXtest {
pmain.imports = append(pmain.imports, pxtest) pmain.imports = append(pmain.imports, pxtest)
} }
...@@ -1082,12 +1082,12 @@ func loadTestFuncs(ptest *Package) (*testFuncs, error) { ...@@ -1082,12 +1082,12 @@ func loadTestFuncs(ptest *Package) (*testFuncs, error) {
Package: ptest, Package: ptest,
} }
for _, file := range ptest.TestGoFiles { 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 return nil, err
} }
} }
for _, file := range ptest.XTestGoFiles { 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 return nil, err
} }
} }
...@@ -1114,7 +1114,9 @@ type testFuncs struct { ...@@ -1114,7 +1114,9 @@ type testFuncs struct {
Benchmarks []testFunc Benchmarks []testFunc
Examples []testFunc Examples []testFunc
Package *Package Package *Package
ImportTest bool
NeedTest bool NeedTest bool
ImportXtest bool
NeedXtest bool NeedXtest bool
Cover []coverInfo Cover []coverInfo
} }
...@@ -1151,7 +1153,7 @@ type testFunc struct { ...@@ -1151,7 +1153,7 @@ type testFunc struct {
var testFileSet = token.NewFileSet() 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) f, err := parser.ParseFile(testFileSet, filename, nil, parser.ParseComments)
if err != nil { if err != nil {
return expandScanner(err) return expandScanner(err)
...@@ -1168,15 +1170,16 @@ func (t *testFuncs) load(filename, pkg string, seen *bool) error { ...@@ -1168,15 +1170,16 @@ func (t *testFuncs) load(filename, pkg string, seen *bool) error {
switch { switch {
case isTest(name, "Test"): case isTest(name, "Test"):
t.Tests = append(t.Tests, testFunc{pkg, name, ""}) t.Tests = append(t.Tests, testFunc{pkg, name, ""})
*seen = true *doImport, *seen = true, true
case isTest(name, "Benchmark"): case isTest(name, "Benchmark"):
t.Benchmarks = append(t.Benchmarks, testFunc{pkg, name, ""}) t.Benchmarks = append(t.Benchmarks, testFunc{pkg, name, ""})
*seen = true *doImport, *seen = true, true
} }
} }
ex := doc.Examples(f) ex := doc.Examples(f)
sort.Sort(byOrder(ex)) sort.Sort(byOrder(ex))
for _, e := range ex { for _, e := range ex {
*doImport = true // import test file whether executed or not
if e.Output == "" && !e.EmptyOutput { if e.Output == "" && !e.EmptyOutput {
// Don't run examples with no output. // Don't run examples with no output.
continue continue
...@@ -1200,11 +1203,11 @@ import ( ...@@ -1200,11 +1203,11 @@ import (
"regexp" "regexp"
"testing" "testing"
{{if .NeedTest}} {{if .ImportTest}}
_test {{.Package.ImportPath | printf "%q"}} {{if .NeedTest}}_test{{else}}_{{end}} {{.Package.ImportPath | printf "%q"}}
{{end}} {{end}}
{{if .NeedXtest}} {{if .ImportXtest}}
_xtest {{.Package.ImportPath | printf "%s_test" | printf "%q"}} {{if .NeedXtest}}_xtest{{else}}_{{end}} {{.Package.ImportPath | printf "%s_test" | printf "%q"}}
{{end}} {{end}}
{{range $i, $p := .Cover}} {{range $i, $p := .Cover}}
_cover{{$i}} {{$p.Package.ImportPath | printf "%q"}} _cover{{$i}} {{$p.Package.ImportPath | printf "%q"}}
......
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.
}
package pkg
import (
"os"
"testing"
)
func TestBuilt(t *testing.T) {
os.Stdout.Write([]byte("A normal test was executed.\n"))
}
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