-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstep_test.go
111 lines (99 loc) · 2.66 KB
/
step_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
package modmake
import (
"context"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestContextAware(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel() // Intentionally cancelling early to test execution
fn := ContextAware(Error("should not have executed"))
for i := 0; i < 100; i++ {
assert.ErrorIs(t, fn.Run(ctx), context.Canceled)
}
}
func TestError(t *testing.T) {
r := IfExists("step.go", Error("File '%s' should not exist", "step.go"))
err := r.Run(context.Background())
assert.Equal(t, "File 'step.go' should not exist", err.Error())
}
func TestNoColonInStepName(t *testing.T) {
assert.Panics(t, func() {
NewStep("test:step", "This should not be allowed")
}, "Colon characters should not be allowed in a base step name")
}
func TestStepLifecycle(t *testing.T) {
step := NewStep("example", "a basis Step to show the lifecycle")
step.DependsOnRunner("dependency", "", Print("Ran a dependency"))
step.BeforeRun(Print("Running before hook"))
step.Does(Print("Running the 'example' step"))
step.AfterRun(Print("Running after hook"))
assert.NoError(t, step.Run(context.Background()))
}
func TestStep_ResetState(t *testing.T) {
var (
aExecuted, bExecuted int
ctx = context.Background()
)
stepA := NewStep("step-a", "").Does(Plain(func() {
aExecuted++
}))
stepB := NewStep("step-b", "").Does(Plain(func() {
bExecuted++
}))
stepA.DependsOn(stepB)
assert.NoError(t, stepA.Run(ctx))
assert.Equal(t, 1, aExecuted)
assert.Equal(t, 1, bExecuted)
assert.NoError(t, stepA.Run(ctx))
assert.Equal(t, 1, aExecuted)
assert.Equal(t, 1, bExecuted)
assert.NoError(t, stepB.Run(ctx))
assert.Equal(t, 1, bExecuted)
stepA.ResetState()
assert.NoError(t, stepA.Run(ctx))
assert.Equal(t, 2, aExecuted)
assert.Equal(t, 2, bExecuted)
assert.NoError(t, stepB.Run(ctx))
assert.Equal(t, 2, bExecuted)
}
func TestStep_Debounce(t *testing.T) {
var (
executed int
stopped int
ctx = context.Background()
)
stepA := NewStep("step-a", "").Does(WithoutErr(func(ctx context.Context) {
select {
case <-ctx.Done():
t.Log("stopped")
stopped++
case <-time.After(200 * time.Millisecond):
t.Log("executed")
executed++
}
}))
debounced := stepA.Debounce(100 * time.Millisecond)
sync := func() { _ = debounced.Run(ctx) }
async := func() {
go func() {
_ = debounced.Run(ctx)
}()
}
assert.Equal(t, 0, executed)
assert.Equal(t, 0, stopped)
async()
async()
async()
time.Sleep(250 * time.Millisecond)
assert.Equal(t, 1, executed)
assert.Equal(t, 0, stopped)
async()
async()
async()
time.Sleep(150 * time.Millisecond)
sync()
assert.Equal(t, 2, executed)
assert.Equal(t, 1, stopped)
}