-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep.go
93 lines (78 loc) · 1.67 KB
/
step.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package elicit
import (
"bytes"
"fmt"
"io"
"os"
"reflect"
"testing"
)
type step struct {
context *Context
spec *spec
scenario *scenario
text string
params []string
tables []stringTable
textBlocks []TextBlock
impl func(*testing.T)
result result
log bytes.Buffer
}
func (s *step) setImpl(impl func(*testing.T)) {
s.impl = impl
// We set this to skipped in case it never gets a chance to run
s.result = skipped
}
func (s *step) run(scenarioT *testing.T) {
defer s.restoreStdout(s.redirectStdout())
if s.impl == nil {
s.result = pending
scenarioT.SkipNow()
} else {
s.impl(scenarioT)
}
}
func (s *step) createCall(fn reflect.Value, params []reflect.Value) func(*testing.T) {
return func(t *testing.T) {
defer func() {
if rcvr := recover(); rcvr != nil {
s.result = panicked
fmt.Fprintf(os.Stderr, "panic during step %s/%s/%s/%s: %s\n", s.spec.path, s.spec.name, s.scenario.name, s.text, rcvr)
t.Fail()
} else if t.Failed() {
s.result = failed
} else if t.Skipped() {
s.result = skipped
} else {
s.result = passed
}
}()
params[0] = reflect.ValueOf(t)
fn.Call(params)
}
}
func (s *step) redirectStdout() (*os.File, chan bool) {
stdout := os.Stdout
r, w, err := os.Pipe()
if err != nil {
return stdout, nil
}
waitChan := make(chan bool)
go func() {
// This will continue until w is closed
io.Copy(&s.log, r)
// Signal that copying has been completed
waitChan <- true
}()
os.Stdout = w
return stdout, waitChan
}
func (s *step) restoreStdout(stdout *os.File, waitChan chan bool) {
w := os.Stdout
os.Stdout = stdout
if w != stdout {
w.Close()
<-waitChan
}
}