mirror of
https://github.com/golang/go
synced 2024-11-22 03:14:41 -07:00
testing: add -test.example flag to control execution of examples
Also, don't run examples if -test.run is set. R=golang-dev, dsymonds CC=golang-dev https://golang.org/cl/5697069
This commit is contained in:
parent
c7482b9196
commit
5876b4eb28
@ -99,6 +99,11 @@ 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.example pattern
|
||||||
|
Run examples matching the regular expression.
|
||||||
|
By default, all examples run, but if -test.run is set,
|
||||||
|
no examples are run.
|
||||||
|
|
||||||
-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.
|
||||||
|
|
||||||
|
@ -28,6 +28,7 @@ var usageMessage = `Usage of go test:
|
|||||||
-benchtime=1: passes -test.benchtime to test
|
-benchtime=1: passes -test.benchtime to test
|
||||||
-cpu="": passes -test.cpu to test
|
-cpu="": passes -test.cpu to test
|
||||||
-cpuprofile="": passes -test.cpuprofile to test
|
-cpuprofile="": passes -test.cpuprofile to test
|
||||||
|
-example="": passes -test.example to test
|
||||||
-memprofile="": passes -test.memprofile to test
|
-memprofile="": passes -test.memprofile to test
|
||||||
-memprofilerate=0: passes -test.memprofilerate to test
|
-memprofilerate=0: passes -test.memprofilerate to test
|
||||||
-parallel=0: passes -test.parallel to test
|
-parallel=0: passes -test.parallel to test
|
||||||
@ -67,6 +68,7 @@ var testFlagDefn = []*testFlagSpec{
|
|||||||
{name: "benchtime", passToTest: true},
|
{name: "benchtime", passToTest: true},
|
||||||
{name: "cpu", passToTest: true},
|
{name: "cpu", passToTest: true},
|
||||||
{name: "cpuprofile", passToTest: true},
|
{name: "cpuprofile", passToTest: true},
|
||||||
|
{name: "example", passToTest: true},
|
||||||
{name: "memprofile", passToTest: true},
|
{name: "memprofile", passToTest: true},
|
||||||
{name: "memprofilerate", passToTest: true},
|
{name: "memprofilerate", passToTest: true},
|
||||||
{name: "parallel", passToTest: true},
|
{name: "parallel", passToTest: true},
|
||||||
|
@ -6,6 +6,7 @@ package testing
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
@ -13,13 +14,18 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var matchExamples = flag.String("test.example", "", "regular expression to select examples to run")
|
||||||
|
|
||||||
type InternalExample struct {
|
type InternalExample struct {
|
||||||
Name string
|
Name string
|
||||||
F func()
|
F func()
|
||||||
Output string
|
Output string
|
||||||
}
|
}
|
||||||
|
|
||||||
func RunExamples(examples []InternalExample) (ok bool) {
|
func RunExamples(matchString func(pat, str string) (bool, error), examples []InternalExample) (ok bool) {
|
||||||
|
if *match != "" {
|
||||||
|
return // Don't run examples if testing is restricted: we're debugging.
|
||||||
|
}
|
||||||
ok = true
|
ok = true
|
||||||
|
|
||||||
var eg InternalExample
|
var eg InternalExample
|
||||||
@ -27,6 +33,14 @@ func RunExamples(examples []InternalExample) (ok bool) {
|
|||||||
stdout, stderr := os.Stdout, os.Stderr
|
stdout, stderr := os.Stdout, os.Stderr
|
||||||
|
|
||||||
for _, eg = range examples {
|
for _, eg = range examples {
|
||||||
|
matched, err := matchString(*matchExamples, eg.Name)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "testing: invalid regexp for -test.example: %s\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
if !matched {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if *chatty {
|
if *chatty {
|
||||||
fmt.Printf("=== RUN: %s\n", eg.Name)
|
fmt.Printf("=== RUN: %s\n", eg.Name)
|
||||||
}
|
}
|
||||||
|
@ -280,7 +280,7 @@ func Main(matchString func(pat, str string) (bool, error), tests []InternalTest,
|
|||||||
before()
|
before()
|
||||||
startAlarm()
|
startAlarm()
|
||||||
testOk := RunTests(matchString, tests)
|
testOk := RunTests(matchString, tests)
|
||||||
exampleOk := RunExamples(examples)
|
exampleOk := RunExamples(matchString, examples)
|
||||||
if !testOk || !exampleOk {
|
if !testOk || !exampleOk {
|
||||||
fmt.Println("FAIL")
|
fmt.Println("FAIL")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
Loading…
Reference in New Issue
Block a user