...
Source file src/testing/run_example_js.go
1
2
3
4
5
6
7 package testing
8
9 import (
10 "fmt"
11 "io"
12 "os"
13 "strings"
14 "time"
15 )
16
17
18
19 func runExample(eg InternalExample) (ok bool) {
20 if *chatty {
21 fmt.Printf("=== RUN %s\n", eg.Name)
22 }
23
24
25
26 stdout := os.Stdout
27 f := createTempFile(eg.Name)
28 os.Stdout = f
29 start := time.Now()
30
31
32 defer func() {
33 timeSpent := time.Since(start)
34
35
36 os.Stdout = stdout
37 var buf strings.Builder
38 _, seekErr := f.Seek(0, os.SEEK_SET)
39 _, readErr := io.Copy(&buf, f)
40 out := buf.String()
41 f.Close()
42 os.Remove(f.Name())
43 if seekErr != nil {
44 fmt.Fprintf(os.Stderr, "testing: seek temp file: %v\n", seekErr)
45 os.Exit(1)
46 }
47 if readErr != nil {
48 fmt.Fprintf(os.Stderr, "testing: read temp file: %v\n", readErr)
49 os.Exit(1)
50 }
51
52 err := recover()
53 ok = eg.processRunResult(out, timeSpent, err)
54 }()
55
56
57 eg.F()
58 return
59 }
60
61 func createTempFile(exampleName string) *os.File {
62 for i := 0; ; i++ {
63 name := fmt.Sprintf("%s/go-example-stdout-%s-%d.txt", os.TempDir(), exampleName, i)
64 f, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
65 if err != nil {
66 if os.IsExist(err) {
67 continue
68 }
69 fmt.Fprintf(os.Stderr, "testing: open temp file: %v\n", err)
70 os.Exit(1)
71 }
72 return f
73 }
74 }
75
View as plain text