-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_test.go
354 lines (303 loc) · 6.97 KB
/
integration_test.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package faket_test
import (
"math"
"os"
"path"
"testing"
"github.com/prashantv/faket/internal/cmptest"
"github.com/prashantv/faket/internal/sliceutil"
)
// These integration-style tests are used to compare the real output of running
// a test to the result of `RunTest`.
var goLatest = os.Getenv("GO_NOT_LATEST") != "true"
func TestCmp_Success(t *testing.T) {
cmptest.Compare(t, func(t testing.TB) {
t.Log("log1")
t.Log("log2")
})
}
func TestCmp_LogFormatting(t *testing.T) {
cmptest.Compare(t, func(t testing.TB) {
t.Log("a", 1, "b", 2, "c", "d")
t.Logf("a: %v b: %v", 1, 2)
})
}
func TestCmp_Skip(t *testing.T) {
cmptest.Compare(t, func(t testing.TB) {
t.Log("pre-skip")
t.Skip("skip")
t.Log("post-skip")
})
}
func TestCmp_Failure(t *testing.T) {
cmptest.Compare(t, func(t testing.TB) {
t.Log("pre-fail log")
t.Error("error log")
t.Log("post-fail log")
})
}
func TestCmp_FailThenSkipCmp(t *testing.T) {
cmptest.Compare(t, func(t testing.TB) {
t.Error("error")
t.Skip("skipped")
})
}
func TestCmp_SkipThenFail(t *testing.T) {
cmptest.Compare(t, func(t testing.TB) {
t.Skip("skip")
t.Error("skipped error")
})
}
func TestCmp_Fatal(t *testing.T) {
cmptest.Compare(t, func(t testing.TB) {
t.Log("pre-fatal")
t.Fatal("fatal") //nolint:revive // skip unreachable skip
t.Log("post-fatal")
})
}
func TestCmp_Cleanup(t *testing.T) {
cmptest.Compare(t, func(t testing.TB) {
t.Log("log 1")
t.Cleanup(func() {
t.Log("log in cleanup")
})
t.Log("log 2")
})
}
func TestCmp_CleanupError(t *testing.T) {
cmptest.Compare(t, func(t testing.TB) {
t.Log("log 1")
t.Cleanup(func() {
t.Error("error in cleanup")
})
t.Log("log 2")
})
}
func TestCmp_CleanupSkip(t *testing.T) {
cmptest.Compare(t, func(t testing.TB) {
t.Log("log 1")
t.Cleanup(func() {
t.Log("cleanup 1")
})
t.Cleanup(func() {
t.Log("cleanup 2")
t.Skip("skip in cleanup")
t.Log("log after skip in cleanup")
})
t.Cleanup(func() {
t.Log("cleanup 3")
})
t.Log("log 2")
})
}
func TestCmp_Helper(t *testing.T) {
cmptest.Compare(t, func(t testing.TB) {
t.Log("call log directly")
log(t)
t.Log("log in helper")
logHelper(t, 1, func() {})
logHelper(t, 3, func() {})
t.Log("log helper then log")
logHelper(t, 3, func() { log(t) })
})
}
func logHelper(t testing.TB, n int, last func()) {
t.Helper()
t.Log("logHelper", n)
if n == 0 {
last()
return
}
logHelper(t, n-1, last)
}
func log(t testing.TB) {
t.Log("log")
}
func TestCmp_NestedCleanup(t *testing.T) {
// bit mask helpers
const never = 0
const all = math.MaxUint32
iter := func(i int) uint32 {
return (1 << i)
}
isSet := func(mask uint32, i int) bool {
return mask&iter(i) > 0
}
tests := []struct {
name string
onlyLatest bool
iterations int
// bit masks of which iterations to do the action on.
returnOuter uint32
skipInner uint32
}{
{
name: "always nest without skip",
iterations: 2,
returnOuter: never,
skipInner: never,
},
{
name: "always nest with skips",
iterations: 2,
returnOuter: never,
skipInner: all,
onlyLatest: true, // log caller of panic.go:<line> changes line across versions.
},
{
name: "mix nesting and skips",
iterations: 3,
returnOuter: iter(2),
skipInner: iter(1) | iter(2),
onlyLatest: true, // log caller of panic.go:<line> changes line across versions.
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.onlyLatest && !goLatest {
t.Skip("can only run with latest go")
}
cmptest.Compare(t, func(t testing.TB) {
t.Log("log 1")
defer t.Log("defer 1")
for i := 1; i <= tt.iterations; i++ {
t.Cleanup(func() {
defer t.Log("defer cleanup", i)
t.Log("cleanup", i)
if isSet(tt.returnOuter, i) {
return
}
t.Cleanup(func() {
defer t.Log("defer nested cleanup", i)
t.Log("nested cleanup", i)
if isSet(tt.skipInner, i) {
t.Skip("skip in nested cleanup", i)
}
})
})
}
})
})
}
}
func TestCmp_CleanupHelper(t *testing.T) {
cmptest.Compare(t, func(t testing.TB) {
helperWithCleanup(t)
})
}
func helperWithCleanup(t testing.TB) {
t.Helper()
t.Cleanup(func() {
t.Helper()
t.Log("cleanup func log")
})
}
func TestCmp_Setenv(t *testing.T) {
const k = "FAKET_CMP_SETENV_TEST_KEY"
// k shouldn't be set initially, or tests will fail.
_, ok := os.LookupEnv(k)
if ok {
t.Fatalf("environment key %v cannot be set for test to run", k)
}
tests := []struct {
name string
setup func(t testing.TB)
}{
{
name: "unset initially",
setup: func(t testing.TB) {},
},
{
name: "set initially",
setup: func(t testing.TB) {
if err := os.Setenv(k, "initial"); err != nil {
t.Fatal("Setenv err", err)
}
t.Cleanup(func() {
if err := os.Unsetenv(k); err != nil {
t.Fatal("Unsetenv err", err)
}
})
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmptest.Compare(t, func(t testing.TB) {
t.Cleanup(func() {
v, ok := os.LookupEnv(k)
t.Logf("LookupEnv in cleanup got %v %v", v, ok)
})
t.Setenv(k, "s1")
t.Log("Getenv s1:", os.Getenv(k))
t.Setenv(k, "s2")
t.Log("Getenv s2:", os.Getenv(k))
})
})
}
}
func TestCmp_TmpDir(t *testing.T) {
cmptest.Compare(t, func(t testing.TB) {
listFiles := func(msg, dir string) {
// Note: dir is not included in messages for deterministic test output.
entries, err := os.ReadDir(dir)
if os.IsNotExist(err) {
t.Logf("missing dir %s", msg)
return
} else if err != nil {
t.Fatalf("ReadDir %s failed: %v", msg, err)
}
t.Logf("ReadDir %s: %v", msg, sliceutil.Map(entries, os.DirEntry.Name))
}
var d1, d2 string
t.Cleanup(func() {
listFiles("d1 post-cleanup", d1)
listFiles("d2 post-cleanup", d2)
})
d1 = t.TempDir()
d2 = t.TempDir()
listFiles("d1 initial", d1)
listFiles("d2 initial", d2)
createFile := func(dir, f string) {
if err := os.WriteFile(path.Join(dir, f), []byte("dummy"), 0o666); err != nil {
t.Fatalf("WriteFile %s in %s failed: %v", f, dir, err)
}
}
createFile(d1, "f1")
createFile(d1, "f2")
createFile(d2, "f3")
listFiles("d1 after create", d1)
listFiles("d2 after create", d2)
})
}
func TestCmp_Fatalf(t *testing.T) {
cmptest.Compare(t, func(t testing.TB) {
t.Error("pre-fail log")
t.Fatalf("log: %v", "fatal") //nolint:revive // skip unreachable skip
t.Error("post-fail log")
})
}
func TestCmp_FailNow(t *testing.T) {
cmptest.Compare(t, func(t testing.TB) {
t.Error("pre-fail log")
t.FailNow() //nolint:revive // skip unreachable skip
t.Error("post-fail log")
})
}
func TestCmp_SkipNow(t *testing.T) {
cmptest.Compare(t, func(t testing.TB) {
t.Error("pre-skip log")
t.SkipNow()
t.Error("post-skip log")
})
}
func TestCmp_Fail(t *testing.T) {
cmptest.Compare(t, func(t testing.TB) {
t.Fail()
})
}
func TestCmp_Skipf(t *testing.T) {
cmptest.Compare(t, func(t testing.TB) {
t.Skipf("skip %s", "test")
})
}