1
0
mirror of https://github.com/golang/go synced 2024-11-24 06:00:11 -07:00

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
This commit is contained in:
Andrew Gerrand 2014-06-28 07:15:22 +10:00
parent 128eed2749
commit ea0fb5d8e2
4 changed files with 50 additions and 18 deletions

View File

@ -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

View 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"}}

View File

@ -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.
}

View File

@ -0,0 +1,10 @@
package pkg
import (
"os"
"testing"
)
func TestBuilt(t *testing.T) {
os.Stdout.Write([]byte("A normal test was executed.\n"))
}