-
Notifications
You must be signed in to change notification settings - Fork 10
/
utils_test.go
92 lines (83 loc) · 2.87 KB
/
utils_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
package testcase_test
import (
"fmt"
"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/random"
"go.llib.dev/testcase/sandbox"
)
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).Contain(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).Contain(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).Contain(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())
value := rnd.StringNC(5, random.CharsetAlpha())
env.SetEnv(t, key, value)
t.Run("on use", func(t *testing.T) {
nvalue := rnd.StringNC(5, random.CharsetAlpha())
testcase.SetEnv(t, key, nvalue)
env, ok := os.LookupEnv(key)
assert.True(t, ok)
assert.Equal(t, nvalue, env)
})
t.Run("on not using it", func(t *testing.T) {
assert.Equal(t, value, os.Getenv(key))
})
}
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) {
testcase.UnsetEnv(t, key)
_, ok := os.LookupEnv(key)
assert.False(t, ok)
})
t.Run("on not using it", func(t *testing.T) {
env, ok := os.LookupEnv(key)
assert.True(t, ok)
assert.Equal(t, value, env)
})
}