]> Cypherpunks repositories - gostls13.git/commitdiff
testing: add -test.example flag to control execution of examples
authorRob Pike <r@golang.org>
Mon, 27 Feb 2012 01:49:10 +0000 (12:49 +1100)
committerRob Pike <r@golang.org>
Mon, 27 Feb 2012 01:49:10 +0000 (12:49 +1100)
Also, don't run examples if -test.run is set.

R=golang-dev, dsymonds
CC=golang-dev
https://golang.org/cl/5697069

src/cmd/go/test.go
src/cmd/go/testflag.go
src/pkg/testing/example.go
src/pkg/testing/testing.go

index 22315e9822973f42a5f437811bc7c37289671920..a84013f209809c992fb760e86d6c0e6feea994f3 100644 (file)
@@ -99,6 +99,11 @@ directory containing the package sources, has its own flags:
            Run benchmarks matching the regular expression.
            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
            Write a CPU profile to the specified file before exiting.
 
index 7c9b7f16dd4e43d28d719f584c0a6f4a4b68f1f0..916e34649f8d1543a8214762e7ec42bb8e47eb10 100644 (file)
@@ -28,6 +28,7 @@ var usageMessage = `Usage of go test:
   -benchtime=1: passes -test.benchtime to test
   -cpu="": passes -test.cpu to test
   -cpuprofile="": passes -test.cpuprofile to test
+  -example="": passes -test.example to test
   -memprofile="": passes -test.memprofile to test
   -memprofilerate=0: passes -test.memprofilerate to test
   -parallel=0: passes -test.parallel to test
@@ -67,6 +68,7 @@ var testFlagDefn = []*testFlagSpec{
        {name: "benchtime", passToTest: true},
        {name: "cpu", passToTest: true},
        {name: "cpuprofile", passToTest: true},
+       {name: "example", passToTest: true},
        {name: "memprofile", passToTest: true},
        {name: "memprofilerate", passToTest: true},
        {name: "parallel", passToTest: true},
index 7f8ff2d0541552ba1863e17f2717ccc30adfef5e..c48d0d8159dc4aa434fe68cda034350dd623907b 100644 (file)
@@ -6,6 +6,7 @@ package testing
 
 import (
        "bytes"
+       "flag"
        "fmt"
        "io"
        "os"
@@ -13,13 +14,18 @@ import (
        "time"
 )
 
+var matchExamples = flag.String("test.example", "", "regular expression to select examples to run")
+
 type InternalExample struct {
        Name   string
        F      func()
        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
 
        var eg InternalExample
@@ -27,6 +33,14 @@ func RunExamples(examples []InternalExample) (ok bool) {
        stdout, stderr := os.Stdout, os.Stderr
 
        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 {
                        fmt.Printf("=== RUN: %s\n", eg.Name)
                }
index adc8c09f2173356269cbeca32f1aa0639e232e79..2bcf9d96a8703d3a0d05d1a48e475e981db455e5 100644 (file)
@@ -280,7 +280,7 @@ func Main(matchString func(pat, str string) (bool, error), tests []InternalTest,
        before()
        startAlarm()
        testOk := RunTests(matchString, tests)
-       exampleOk := RunExamples(examples)
+       exampleOk := RunExamples(matchString, examples)
        if !testOk || !exampleOk {
                fmt.Println("FAIL")
                os.Exit(1)