Source file
src/testing/run_example.go
1
2
3
4
5
6
7
8
9
10
11 package testing
12
13 import (
14 "fmt"
15 "io"
16 "os"
17 "strings"
18 "time"
19 )
20
21 func runExample(eg InternalExample) (ok bool) {
22 if *chatty {
23 fmt.Printf("=== RUN %s\n", eg.Name)
24 }
25
26
27 stdout := os.Stdout
28 r, w, err := os.Pipe()
29 if err != nil {
30 fmt.Fprintln(os.Stderr, err)
31 os.Exit(1)
32 }
33 os.Stdout = w
34 outC := make(chan string)
35 go func() {
36 var buf strings.Builder
37 _, err := io.Copy(&buf, r)
38 r.Close()
39 if err != nil {
40 fmt.Fprintf(os.Stderr, "testing: copying pipe: %v\n", err)
41 os.Exit(1)
42 }
43 outC <- buf.String()
44 }()
45
46 finished := false
47 start := time.Now()
48
49
50 defer func() {
51 timeSpent := time.Since(start)
52
53
54 w.Close()
55 os.Stdout = stdout
56 out := <-outC
57
58 err := recover()
59 ok = eg.processRunResult(out, timeSpent, finished, err)
60 }()
61
62
63 eg.F()
64 finished = true
65 return
66 }
67
View as plain text