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