Commit ff5d47eb authored by Andrew Gerrand's avatar Andrew Gerrand

testing: only capture stdout when running examples

Fixes #4550.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/6973048
parent 91934ff5
...@@ -76,7 +76,13 @@ The build flags are shared by the build, install, run, and test commands: ...@@ -76,7 +76,13 @@ The build flags are shared by the build, install, run, and test commands:
do not delete it when exiting. do not delete it when exiting.
-x -x
print the commands. print the commands.
-race
enable data race detection.
Currently supported only on linux/amd64,
darwin/amd64 and windows/amd64.
-ccflags 'arg list'
arguments to pass on each 5c, 6c, or 8c compiler invocation
-compiler name -compiler name
name of compiler to use, as in runtime.Compiler (gccgo or gc) name of compiler to use, as in runtime.Compiler (gccgo or gc)
-gccgoflags 'arg list' -gccgoflags 'arg list'
...@@ -121,6 +127,7 @@ source directories corresponding to the import paths: ...@@ -121,6 +127,7 @@ source directories corresponding to the import paths:
DIR(.exe) from go build DIR(.exe) from go build
DIR.test(.exe) from go test -c DIR.test(.exe) from go test -c
MAINFILE(.exe) from go build MAINFILE.go MAINFILE(.exe) from go build MAINFILE.go
*.so from SWIG
In the list, DIR represents the final path element of the In the list, DIR represents the final path element of the
directory, and MAINFILE is the base name of any Go source directory, and MAINFILE is the base name of any Go source
...@@ -276,10 +283,10 @@ The default output shows the package import path: ...@@ -276,10 +283,10 @@ The default output shows the package import path:
code.google.com/p/goauth2/oauth code.google.com/p/goauth2/oauth
code.google.com/p/sqlite code.google.com/p/sqlite
The -f flag specifies an alternate format for the list, The -f flag specifies an alternate format for the list, using the
using the syntax of package template. The default output syntax of package template. The default output is equivalent to -f
is equivalent to -f '{{.ImportPath}}'. The struct '{{.ImportPath}}'. One extra template function is available, "join",
being passed to the template is: which calls strings.Join. The struct being passed to the template is:
type Package struct { type Package struct {
Dir string // directory containing package sources Dir string // directory containing package sources
...@@ -679,6 +686,9 @@ directory containing the package sources, has its own flags: ...@@ -679,6 +686,9 @@ directory containing the package sources, has its own flags:
Run benchmarks matching the regular expression. Run benchmarks matching the regular expression.
By default, no benchmarks run. By default, no benchmarks run.
-test.benchmem
Print memory allocation statistics for benchmarks.
-test.cpuprofile cpu.out -test.cpuprofile cpu.out
Write a CPU profile to the specified file before exiting. Write a CPU profile to the specified file before exiting.
...@@ -694,6 +704,18 @@ directory containing the package sources, has its own flags: ...@@ -694,6 +704,18 @@ directory containing the package sources, has its own flags:
garbage collector, provided the test can run in the available garbage collector, provided the test can run in the available
memory without garbage collection. memory without garbage collection.
-test.blockprofile block.out
Write a goroutine blocking profile to the specified file
when all tests are complete.
-test.blockprofilerate n
Control the detail provided in goroutine blocking profiles by setting
runtime.BlockProfileRate to n. See 'godoc runtime BlockProfileRate'.
The profiler aims to sample, on average, one blocking event every
n nanoseconds the program spends blocked. By default,
if -test.blockprofile is set without this flag, all blocking events
are recorded, equivalent to -test.blockprofilerate=1.
-test.parallel n -test.parallel n
Allow parallel execution of test functions that call t.Parallel. Allow parallel execution of test functions that call t.Parallel.
The value of this flag is the maximum number of tests to run The value of this flag is the maximum number of tests to run
......
...@@ -175,8 +175,8 @@ A benchmark function is one named BenchmarkXXX and should have the signature, ...@@ -175,8 +175,8 @@ A benchmark function is one named BenchmarkXXX and should have the signature,
func BenchmarkXXX(b *testing.B) { ... } func BenchmarkXXX(b *testing.B) { ... }
An example function is similar to a test function but, instead of using *testing.T An example function is similar to a test function but, instead of using
to report success or failure, prints output to os.Stdout and os.Stderr. *testing.T to report success or failure, prints output to os.Stdout.
That output is compared against the function's "Output:" comment, which That output is compared against the function's "Output:" comment, which
must be the last comment in the function body (see example below). An must be the last comment in the function body (see example below). An
example with no such comment, or with no text after "Output:" is compiled example with no such comment, or with no text after "Output:" is compiled
......
...@@ -24,7 +24,7 @@ func RunExamples(matchString func(pat, str string) (bool, error), examples []Int ...@@ -24,7 +24,7 @@ func RunExamples(matchString func(pat, str string) (bool, error), examples []Int
var eg InternalExample var eg InternalExample
stdout, stderr := os.Stdout, os.Stderr stdout := os.Stdout
for _, eg = range examples { for _, eg = range examples {
matched, err := matchString(*match, eg.Name) matched, err := matchString(*match, eg.Name)
...@@ -39,19 +39,19 @@ func RunExamples(matchString func(pat, str string) (bool, error), examples []Int ...@@ -39,19 +39,19 @@ func RunExamples(matchString func(pat, str string) (bool, error), examples []Int
fmt.Printf("=== RUN: %s\n", eg.Name) fmt.Printf("=== RUN: %s\n", eg.Name)
} }
// capture stdout and stderr // capture stdout
r, w, err := os.Pipe() r, w, err := os.Pipe()
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
os.Exit(1) os.Exit(1)
} }
os.Stdout, os.Stderr = w, w os.Stdout = w
outC := make(chan string) outC := make(chan string)
go func() { go func() {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
_, err := io.Copy(buf, r) _, err := io.Copy(buf, r)
if err != nil { if err != nil {
fmt.Fprintf(stderr, "testing: copying pipe: %v\n", err) fmt.Fprintf(os.Stderr, "testing: copying pipe: %v\n", err)
os.Exit(1) os.Exit(1)
} }
outC <- buf.String() outC <- buf.String()
...@@ -62,9 +62,9 @@ func RunExamples(matchString func(pat, str string) (bool, error), examples []Int ...@@ -62,9 +62,9 @@ func RunExamples(matchString func(pat, str string) (bool, error), examples []Int
eg.F() eg.F()
dt := time.Now().Sub(t0) dt := time.Now().Sub(t0)
// close pipe, restore stdout/stderr, get output // close pipe, restore stdout, get output
w.Close() w.Close()
os.Stdout, os.Stderr = stdout, stderr os.Stdout = stdout
out := <-outC out := <-outC
// report any errors // report any errors
......
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