-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht.go
177 lines (135 loc) · 2.91 KB
/
t.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package handoff
import (
"context"
"fmt"
"strings"
"time"
"github.com/raphi011/handoff/internal/model"
)
// make sure we adhere to the TB interface
var _ model.TB = &T{}
type T struct {
suiteName string
testName string
attempt int
logs strings.Builder
result model.Result
runtimeContext model.TestContext
cleanupFunc func()
softFailure bool
ctx context.Context
spans []*model.Span
}
func (t *T) Cleanup(c func()) {
t.cleanupFunc = c
}
func (t *T) Error(args ...any) {
t.Fail()
t.Log(args...)
}
func (t *T) Errorf(format string, args ...any) {
t.Logf(format, args...)
t.Fail()
}
func (t *T) Fail() {
t.result = model.ResultFailed
}
func (t *T) FailNow() {
t.Fail()
panic(failTestErr{})
}
func (t *T) Failed() bool {
return t.result == model.ResultFailed
}
func (t *T) Fatal(args ...any) {
t.Error(args...)
panic(failTestErr{})
}
func (t *T) StartSpan(name string, kv ...any) *model.Span {
s, err := model.StartSpan(name, kv...)
if err != nil {
t.Fatalf("Error on StartSpan(): %v", err)
}
t.spans = append(t.spans, s)
return s
}
func (t *T) Fatalf(format string, args ...any) {
t.Errorf(format, args...)
panic(failTestErr{})
}
func (t *T) Helper() {}
func (t *T) Log(args ...any) {
t.logs.WriteString(fmt.Sprint(args...) + "\n")
}
func (t *T) Logf(format string, args ...any) {
t.logs.WriteString(fmt.Sprintf(format, args...) + "\n")
}
func (t *T) Name() string {
return t.testName
}
func (t *T) Setenv(key, value string) {
}
func (t *T) Skip(args ...any) {
t.Log(args...)
t.SkipNow()
}
func (t *T) SkipNow() {
t.result = model.ResultSkipped
panic(skipTestErr{})
}
func (t *T) Skipf(format string, args ...any) {
t.Logf(format, args...)
t.SkipNow()
}
func (t *T) Skipped() bool {
return t.result == model.ResultSkipped
}
func (t *T) TempDir() string {
// TODO
return ""
}
/* Handoff specific functions that are not part of the testing.TB interface */
/* ------------------------------------------------------------------------ */
func (t *T) Context() context.Context {
return t.ctx
}
func (t *T) SoftFailure() {
t.softFailure = true
}
func (t *T) Attempt() int {
return t.attempt
}
func (t *T) Value(key string) any {
return t.runtimeContext[key]
}
func (t *T) SetValue(key string, value any) {
t.runtimeContext[key] = value
}
func (t *T) Result() model.Result {
if t.result == "" {
return model.ResultPassed
}
return t.result
}
func (t *T) SetTimeout(timeout time.Duration) {
// TODO
}
func (t *T) runTestCleanup() error {
if t.cleanupFunc == nil {
return nil
}
var err error
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("cleanup func panic'd: %v", err)
}
}()
t.cleanupFunc()
return err
}
// skipTestErr is passed to panic() to signal
// that a test was skipped.
type skipTestErr struct{}
// failTestErr is passed to panic() to signal
// that a test has failed.
type failTestErr struct{}