-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathutils_test.go
More file actions
341 lines (279 loc) · 8.61 KB
/
utils_test.go
File metadata and controls
341 lines (279 loc) · 8.61 KB
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
package testcase_test
import (
"bytes"
"context"
"fmt"
"io"
"math/rand"
"os"
"testing"
"time"
"go.llib.dev/testcase"
"go.llib.dev/testcase/assert"
"go.llib.dev/testcase/internal/doubles"
"go.llib.dev/testcase/internal/env"
"go.llib.dev/testcase/let"
"go.llib.dev/testcase/random"
"go.llib.dev/testcase/sandbox"
"go.llib.dev/testcase/tcsync"
)
func TestSkipUntil(t *testing.T) {
const timeLayout = "2006-01-02"
const skipUntilFormat = "Skip time %s"
const skipExpiredFormat = "[SkipUntil] expired on %s"
rnd := random.New(rand.NewSource(time.Now().UnixNano()))
t.Run("before SkipUntil deadline, test is skipped", func(t *testing.T) {
stubTB := &doubles.TB{}
future := time.Now().AddDate(0, 0, 1)
ro := sandbox.Run(func() {
testcase.SkipUntil(stubTB, future.Year(), future.Month(), future.Day(), future.Hour())
})
assert.Must(t).False(ro.OK)
assert.Must(t).True(ro.Goexit)
assert.Must(t).False(stubTB.IsFailed)
assert.Must(t).True(stubTB.IsSkipped)
assert.Must(t).Contains(stubTB.Logs.String(), fmt.Sprintf(skipUntilFormat, future.Format(timeLayout)))
})
t.Run("SkipUntil won't skip when the deadline reached", func(t *testing.T) {
stubTB := &doubles.TB{}
now := time.Now()
ro := sandbox.Run(func() { testcase.SkipUntil(stubTB, now.Year(), now.Month(), now.Day(), now.Hour()) })
assert.Must(t).True(ro.OK)
assert.Must(t).False(ro.Goexit)
assert.Must(t).False(stubTB.IsFailed)
assert.Must(t).False(stubTB.IsSkipped)
assert.Must(t).Contains(stubTB.Logs.String(), fmt.Sprintf(skipExpiredFormat, now.Format(timeLayout)))
})
t.Run("at or after SkipUntil deadline, test is failed", func(t *testing.T) {
stubTB := &doubles.TB{}
today := time.Now().AddDate(0, 0, -1*rnd.IntN(3))
ro := sandbox.Run(func() { testcase.SkipUntil(stubTB, today.Year(), today.Month(), today.Day(), today.Hour()) })
assert.Must(t).True(ro.OK)
assert.Must(t).False(ro.Goexit)
assert.Must(t).False(stubTB.IsFailed)
assert.Must(t).Contains(stubTB.Logs.String(), fmt.Sprintf(skipExpiredFormat, today.Format(timeLayout)))
})
}
func TestSetEnv(t *testing.T) {
rnd := random.New(random.CryptoSeed{})
key := rnd.StringNC(5, random.CharsetAlpha())
ovalue := rnd.StringNC(5, random.CharsetAlpha())
env.SetEnv(t, key, ovalue)
t.Run("on use", func(t *testing.T) {
var dtb doubles.TB
defer dtb.Finish()
nvalue := rnd.StringNC(5, random.CharsetAlpha())
testcase.SetEnv(&dtb, key, nvalue)
got, ok := os.LookupEnv(key)
assert.True(t, ok)
assert.Equal(t, got, nvalue)
dtb.Finish()
got, ok = os.LookupEnv(key)
assert.True(t, ok)
assert.Equal(t, got, ovalue)
assert.Empty(t, dtb.Logs.String())
})
t.Run("on not using it", func(t *testing.T) {
assert.Equal(t, ovalue, os.Getenv(key))
})
t.Run("on use when failure occurs", func(t *testing.T) {
var dtb doubles.TB
defer dtb.Finish()
nvalue := rnd.StringNC(5, random.CharsetAlpha())
testcase.SetEnv(&dtb, key, nvalue)
dtb.Fail()
dtb.Finish()
assert.Contains(t, dtb.Logs.String(), fmt.Sprintf("env %s=%q", key, nvalue))
})
}
func TestUnsetEnv(t *testing.T) {
rnd := random.New(random.CryptoSeed{})
key := rnd.StringNC(5, random.CharsetAlpha())
value := rnd.StringNC(5, random.CharsetAlpha())
env.SetEnv(t, key, value)
t.Run("on use", func(t *testing.T) {
var dtb doubles.TB
defer dtb.Finish()
testcase.UnsetEnv(&dtb, key)
_, ok := os.LookupEnv(key)
assert.False(t, ok)
dtb.Finish()
_, ok = os.LookupEnv(key)
assert.True(t, ok)
assert.Empty(t, dtb.Logs.String())
})
t.Run("on not using it", func(t *testing.T) {
env, ok := os.LookupEnv(key)
assert.True(t, ok)
assert.Equal(t, value, env)
})
t.Run("on use when failure occurs", func(t *testing.T) {
var dtb doubles.TB
defer dtb.Finish()
testcase.UnsetEnv(&dtb, key)
dtb.Fail()
dtb.Finish()
assert.Contains(t, dtb.Logs.String(), fmt.Sprintf("env unset %s", key))
})
}
func TestOnFail(t *testing.T) {
t.Run("happy", func(t *testing.T) {
var dtb doubles.TB
var ran bool
testcase.OnFail(&dtb, func() { ran = true })
dtb.Finish()
assert.False(t, ran)
})
t.Run("rainy", func(t *testing.T) {
var dtb doubles.TB
var ran bool
testcase.OnFail(&dtb, func() { ran = true })
dtb.Fail()
dtb.Finish()
assert.True(t, ran)
})
}
func ExampleGetEnv() {
var tb testing.TB = &testing.T{}
const EnvKey = "THE_ENV_KEY"
// get an environment variable, or skip the test
testcase.GetEnv(tb, EnvKey, tb.SkipNow)
// get an environment variable, or fail now the test
testcase.GetEnv(tb, EnvKey, tb.Fail)
testcase.GetEnv(tb, EnvKey, tb.FailNow)
}
func TestGetEnv(t *testing.T) {
s := testcase.NewSpec(t)
var (
dtb = let.Var(s, func(t *testcase.T) *doubles.TB {
return &doubles.TB{}
})
key = let.Var(s, func(t *testcase.T) string {
return t.Random.StringNWithCharset(
t.Random.IntBetween(3, 10),
random.CharsetAlpha())
})
)
actWithSkip := let.Act(func(t *testcase.T) string {
return random.Pick(t.Random,
func() string { return testcase.GetEnv(dtb.Get(t), key.Get(t), dtb.Get(t).SkipNow) },
func() string { return testcase.GetEnv(dtb.Get(t), key.Get(t)) },
)()
})
actWithFatal := let.Act(func(t *testcase.T) string {
return random.Pick(t.Random,
func() string { return testcase.GetEnv(dtb.Get(t), key.Get(t), dtb.Get(t).Fail) },
func() string { return testcase.GetEnv(dtb.Get(t), key.Get(t), dtb.Get(t).FailNow) },
)()
})
s.When("env variable present in the environment", func(s *testcase.Spec) {
value := let.String(s)
s.Before(func(t *testcase.T) {
testcase.SetEnv(t, key.Get(t), value.Get(t))
})
s.Then("value is returned", func(t *testcase.T) {
assert.Equal(t, value.Get(t), actWithSkip(t))
assert.Equal(t, value.Get(t), actWithFatal(t))
})
})
s.When("env variable is absent in the environment", func(s *testcase.Spec) {
s.Before(func(t *testcase.T) {
testcase.UnsetEnv(t, key.Get(t))
})
s.Then("on fail use, fail call is expected", func(t *testcase.T) {
sandbox.Run(func() { actWithFatal(t) })
assert.True(t, dtb.Get(t).IsFailed)
})
s.Then("on skip use, skip call is expected", func(t *testcase.T) {
sandbox.Run(func() { actWithSkip(t) })
assert.True(t, dtb.Get(t).IsSkipped)
})
s.Then("on every case, logging of the missing env variable is expected", func(t *testcase.T) {
sandbox.Run(func() {
if t.Random.Bool() {
actWithSkip(t)
} else {
actWithFatal(t)
}
})
assert.Contains(t, dtb.Get(t).Logs.String(), key.Get(t))
assert.Contains(t, dtb.Get(t).Logs.String(), "not found")
})
})
}
var Stdout io.Writer = os.Stdout
func FuncWithGlobalDependency(a ...any) {
fmt.Fprintln(Stdout, a...)
}
func ExampleSetGlobal() {
var t *testing.T
t.Run("foo", func(t *testing.T) {
var buf bytes.Buffer
testcase.SetGlobal[io.Writer](t, &Stdout, &buf)
FuncWithGlobalDependency("hello")
assert.Contains(t, buf.String(), "hello")
})
t.Run("bar", func(t *testing.T) {
var buf bytes.Buffer
testcase.SetGlobal[io.Writer](t, &Stdout, &buf)
FuncWithGlobalDependency("world")
assert.Contains(t, buf.String(), "world")
})
}
func TestSetGlobal_smoke(t *testing.T) {
s := testcase.NewSpec(t)
s.Test("single value single use", func(t *testcase.T) {
var ftb testcase.FakeTB
var (
oldVal = t.Random.Int()
newVal = t.Random.Int()
)
var globVar int = oldVal
testcase.SetGlobal(&ftb, &globVar, newVal)
assert.Equal(t, globVar, newVal)
ftb.Finish()
assert.Equal(t, globVar, oldVal)
})
s.Test("consequential calls on same variable", func(t *testcase.T) {
var ftb testcase.FakeTB
var (
oldVal = t.Random.Int()
newVal = t.Random.Int()
newVal2 = t.Random.Int()
)
var globVar int = oldVal
testcase.SetGlobal(&ftb, &globVar, newVal)
assert.Equal(t, globVar, newVal)
testcase.SetGlobal(&ftb, &globVar, newVal2)
assert.Equal(t, globVar, newVal2)
ftb.Finish()
assert.Equal(t, globVar, oldVal)
})
s.Test("the use during parallel execution is not allowed", func(t *testcase.T) {
var ftb testcase.FakeTB
var (
oldVal = t.Random.Int()
newVal = t.Random.Int()
)
var globVar int = oldVal
testcase.SetGlobal(&ftb, &globVar, newVal)
assert.Equal(t, globVar, newVal)
var done tcsync.Phaser
go func() {
defer done.Finish()
var ftb testcase.FakeTB
defer ftb.Finish()
var newVal = t.Random.Int()
o := sandbox.Run(func() {
testcase.SetGlobal(&ftb, &globVar, newVal)
})
assert.Should(t).False(o.OK, "expected that the execution was interrupted")
assert.Should(t).True(ftb.IsFailed, "expected that the test is marked as failed due to incorrect test arrangment code with SetGlobal")
}()
assert.Within(t, time.Second, func(ctx context.Context) {
done.Wait()
})
ftb.Finish()
assert.Equal(t, globVar, oldVal)
})
}