-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspy.go
88 lines (74 loc) · 1.35 KB
/
spy.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
package assert
import (
"bytes"
"io"
"log"
"os"
)
type params struct {
Args []any
Format string
}
type spy struct {
failed,
skipped bool
calls map[string]bool
params map[string]params
setOutput func(io.Writer)
t T
}
func newSpy(t T) *spy {
return &spy{
calls: make(map[string]bool),
params: make(map[string]params),
t: t,
setOutput: log.SetOutput,
}
}
func (s *spy) Cleanup(fn func()) {
s.calls["Cleanup"] = true
fn()
}
func (s *spy) Fail() {
s.calls["Fail"] = true
s.failed = true
}
func (s *spy) Failed() bool {
s.calls["Failed"] = true
return s.failed
}
func (s *spy) Fatal(args ...any) {
name := "Fatal"
s.calls[name] = true
s.params[name] = params{Args: args}
}
func (s *spy) Helper() {
s.calls["Helper"] = true
}
func (s *spy) Setenv(key, value string) {
name := "Setenv"
s.calls[name] = true
s.params[name] = params{Args: []any{key, value}}
}
func (s *spy) Skip(args ...any) {
name := "Skip"
s.calls[name] = true
s.params[name] = params{Args: args}
s.skipped = true
}
func (s *spy) Skipped() bool {
s.calls["Skipped"] = true
return s.skipped
}
func (s *spy) TempDir() string {
s.calls["TempDir"] = true
return "./spy"
}
func (s *spy) LogOutput() (buf *bytes.Buffer, cancel func()) {
buf = new(bytes.Buffer)
s.setOutput(buf)
cancel = func() {
s.setOutput(os.Stdout)
}
return
}