Commit a3fb1aec authored by Russ Cox's avatar Russ Cox

testing: print test results to standard output

Errors in the code under test go to standard output.
Errors in testing or its usage go to standard error.

R=r
CC=golang-dev
https://golang.org/cl/5374090
parent 3db59611
...@@ -205,7 +205,7 @@ func RunBenchmarks(matchString func(pat, str string) (bool, error), benchmarks [ ...@@ -205,7 +205,7 @@ func RunBenchmarks(matchString func(pat, str string) (bool, error), benchmarks [
for _, Benchmark := range benchmarks { for _, Benchmark := range benchmarks {
matched, err := matchString(*matchBenchmarks, Benchmark.Name) matched, err := matchString(*matchBenchmarks, Benchmark.Name)
if err != nil { if err != nil {
println("invalid regexp for -test.bench:", err.Error()) fmt.Fprintf(os.Stderr, "testing: invalid regexp for -test.bench: %s\n", err)
os.Exit(1) os.Exit(1)
} }
if !matched { if !matched {
...@@ -218,11 +218,11 @@ func RunBenchmarks(matchString func(pat, str string) (bool, error), benchmarks [ ...@@ -218,11 +218,11 @@ func RunBenchmarks(matchString func(pat, str string) (bool, error), benchmarks [
if procs != 1 { if procs != 1 {
benchName = fmt.Sprintf("%s-%d", Benchmark.Name, procs) benchName = fmt.Sprintf("%s-%d", Benchmark.Name, procs)
} }
print(fmt.Sprintf("%s\t", benchName)) fmt.Printf("%s\t", benchName)
r := b.run() r := b.run()
print(fmt.Sprintf("%v\n", r)) fmt.Printf("%v\n", r)
if p := runtime.GOMAXPROCS(-1); p != procs { if p := runtime.GOMAXPROCS(-1); p != procs {
print(fmt.Sprintf("%s left GOMAXPROCS set to %d\n", benchName, p)) fmt.Fprintf(os.Stderr, "testing: %s left GOMAXPROCS set to %d\n", benchName, p)
} }
} }
} }
......
...@@ -21,24 +21,23 @@ type InternalExample struct { ...@@ -21,24 +21,23 @@ type InternalExample struct {
func RunExamples(examples []InternalExample) (ok bool) { func RunExamples(examples []InternalExample) (ok bool) {
ok = true ok = true
var eg InternalExample
stdout, stderr := os.Stdout, os.Stderr stdout, stderr := os.Stdout, os.Stderr
defer func() { defer func() {
os.Stdout, os.Stderr = stdout, stderr os.Stdout, os.Stderr = stdout, stderr
if e := recover(); e != nil { if e := recover(); e != nil {
if err, ok := e.(error); ok { fmt.Printf("--- FAIL: %s\npanic: %v\n", eg.Name, e)
fmt.Fprintln(os.Stderr, err) os.Exit(1)
os.Exit(1)
}
panic(e)
} }
}() }()
for _, eg := range examples { for _, eg = range examples {
if *chatty { if *chatty {
fmt.Fprintln(os.Stderr, "=== RUN:", eg.Name) fmt.Printf("=== RUN: %s\n", eg.Name)
} }
// capture stdout and stderr for testing purposes // capture stdout and stderr
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)
...@@ -50,7 +49,7 @@ func RunExamples(examples []InternalExample) (ok bool) { ...@@ -50,7 +49,7 @@ func RunExamples(examples []InternalExample) (ok bool) {
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.Fprintln(os.Stderr, err) fmt.Fprintf(stderr, "testing: copying pipe: %v\n", err)
os.Exit(1) os.Exit(1)
} }
outC <- buf.String() outC <- buf.String()
...@@ -67,16 +66,15 @@ func RunExamples(examples []InternalExample) (ok bool) { ...@@ -67,16 +66,15 @@ func RunExamples(examples []InternalExample) (ok bool) {
out := <-outC out := <-outC
// report any errors // report any errors
tstr := fmt.Sprintf("(%.2f seconds)", float64(ns)/1e9)
if out != eg.Output { if out != eg.Output {
fmt.Fprintf( fmt.Printf(
os.Stderr, "--- FAIL: %s %s\ngot:\n%s\nwant:\n%s\n",
"--- FAIL: %s\ngot:\n%s\nwant:\n%s\n", eg.Name, tstr, out, eg.Output,
eg.Name, out, eg.Output,
) )
ok = false ok = false
} else if *chatty { } else if *chatty {
tstr := fmt.Sprintf("(%.2f seconds)", float64(ns)/1e9) fmt.Printf("--- PASS: %s %s\n", eg.Name, tstr)
fmt.Fprintln(os.Stderr, "--- PASS:", eg.Name, tstr)
} }
} }
......
...@@ -201,10 +201,10 @@ func Main(matchString func(pat, str string) (bool, error), tests []InternalTest, ...@@ -201,10 +201,10 @@ func Main(matchString func(pat, str string) (bool, error), tests []InternalTest,
testOk := RunTests(matchString, tests) testOk := RunTests(matchString, tests)
exampleOk := RunExamples(examples) exampleOk := RunExamples(examples)
if !testOk || !exampleOk { if !testOk || !exampleOk {
fmt.Fprintln(os.Stderr, "FAIL") fmt.Println("FAIL")
os.Exit(1) os.Exit(1)
} }
fmt.Fprintln(os.Stderr, "PASS") fmt.Println("PASS")
stopAlarm() stopAlarm()
RunBenchmarks(matchString, benchmarks) RunBenchmarks(matchString, benchmarks)
after() after()
...@@ -214,9 +214,9 @@ func report(t *T) { ...@@ -214,9 +214,9 @@ func report(t *T) {
tstr := fmt.Sprintf("(%.2f seconds)", float64(t.ns)/1e9) tstr := fmt.Sprintf("(%.2f seconds)", float64(t.ns)/1e9)
format := "--- %s: %s %s\n%s" format := "--- %s: %s %s\n%s"
if t.failed { if t.failed {
fmt.Fprintf(os.Stderr, format, "FAIL", t.name, tstr, t.errors) fmt.Printf(format, "FAIL", t.name, tstr, t.errors)
} else if *chatty { } else if *chatty {
fmt.Fprintf(os.Stderr, format, "PASS", t.name, tstr, t.errors) fmt.Printf(format, "PASS", t.name, tstr, t.errors)
} }
} }
...@@ -236,7 +236,7 @@ func RunTests(matchString func(pat, str string) (bool, error), tests []InternalT ...@@ -236,7 +236,7 @@ func RunTests(matchString func(pat, str string) (bool, error), tests []InternalT
for i := 0; i < len(tests); i++ { for i := 0; i < len(tests); i++ {
matched, err := matchString(*match, tests[i].Name) matched, err := matchString(*match, tests[i].Name)
if err != nil { if err != nil {
println("invalid regexp for -test.run:", err.Error()) fmt.Fprintf(os.Stderr, "testing: invalid regexp for -test.run: %s\n", err)
os.Exit(1) os.Exit(1)
} }
if !matched { if !matched {
...@@ -248,7 +248,7 @@ func RunTests(matchString func(pat, str string) (bool, error), tests []InternalT ...@@ -248,7 +248,7 @@ func RunTests(matchString func(pat, str string) (bool, error), tests []InternalT
} }
t := &T{ch: ch, name: testName, startParallel: startParallel} t := &T{ch: ch, name: testName, startParallel: startParallel}
if *chatty { if *chatty {
println("=== RUN", t.name) fmt.Printf("=== RUN %s\n", t.name)
} }
go tRunner(t, &tests[i]) go tRunner(t, &tests[i])
out := <-t.ch out := <-t.ch
...@@ -344,7 +344,7 @@ func parseCpuList() { ...@@ -344,7 +344,7 @@ func parseCpuList() {
for _, val := range strings.Split(*cpuListStr, ",") { for _, val := range strings.Split(*cpuListStr, ",") {
cpu, err := strconv.Atoi(val) cpu, err := strconv.Atoi(val)
if err != nil || cpu <= 0 { if err != nil || cpu <= 0 {
println("invalid value for -test.cpu") fmt.Fprintf(os.Stderr, "testing: invalid value %q for -test.cpu", val)
os.Exit(1) os.Exit(1)
} }
cpuList = append(cpuList, cpu) cpuList = append(cpuList, cpu)
......
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