-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestctx_test.go
180 lines (150 loc) · 4.61 KB
/
testctx_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
package testctx_test
import (
"context"
"testing"
"github.com/dagger/testctx"
"github.com/stretchr/testify/assert"
)
func TestMiddlewareInvocation(t *testing.T) {
var invocations []string
tt := testctx.New(t).Using(func(next testctx.TestFunc) testctx.TestFunc {
return func(ctx context.Context, t *testctx.W[*testing.T]) {
invocations = append(invocations, "before:outer")
next(ctx, t)
invocations = append(invocations, "after:outer")
}
}, func(next testctx.TestFunc) testctx.TestFunc {
return func(ctx context.Context, t *testctx.W[*testing.T]) {
invocations = append(invocations, "before:inner")
next(ctx, t)
invocations = append(invocations, "after:inner")
}
})
tt.Run("subtest", func(ctx context.Context, t *testctx.T) {
invocations = append(invocations, "test")
})
// Middleware should be applied in order, with inner middleware wrapped by outer
assert.Equal(t, []string{
"before:outer",
"before:inner",
"test",
"after:inner",
"after:outer",
}, invocations)
}
func TestMiddlewareReuse(t *testing.T) {
var count int
tt := testctx.New(t).Using(func(next testctx.TestFunc) testctx.TestFunc {
return func(ctx context.Context, t *testctx.T) {
count++
next(ctx, t)
}
})
tt.Run("first", func(ctx context.Context, t *testctx.T) {})
tt.Run("second", func(ctx context.Context, t *testctx.T) {})
// Now that each subtest calls the middleware factory, expect count == 2
assert.Equal(t, 2, count)
}
func TestCleanup(t *testing.T) {
var cleanupOrder []string
// Register our assertion first, so it runs last
t.Cleanup(func() {
assert.Equal(t, []string{
"child2",
"child1",
"another",
"parent",
}, cleanupOrder)
})
tt := testctx.New(t)
tt.Cleanup(func() {
cleanupOrder = append(cleanupOrder, "parent")
})
tt.Run("nested", func(ctx context.Context, t *testctx.T) {
t.Cleanup(func() {
cleanupOrder = append(cleanupOrder, "child1")
})
t.Cleanup(func() {
cleanupOrder = append(cleanupOrder, "child2")
})
})
tt.Run("another", func(ctx context.Context, t *testctx.T) {
t.Cleanup(func() {
cleanupOrder = append(cleanupOrder, "another")
})
})
}
func TestContextPropagation(t *testing.T) {
type ctxKey struct{}
tt := testctx.New(t).Using(func(next testctx.TestFunc) testctx.TestFunc {
return func(ctx context.Context, t *testctx.W[*testing.T]) {
count := 0
if v := ctx.Value(ctxKey{}); v != nil {
count = v.(int)
}
ctx = context.WithValue(ctx, ctxKey{}, count+1)
next(ctx, t)
}
})
tt.Run("parent", func(ctx context.Context, t *testctx.T) {
assert.Equal(t, 1, ctx.Value(ctxKey{}))
t.Run("child", func(ctx context.Context, t *testctx.T) {
assert.Equal(t, 2, ctx.Value(ctxKey{}))
t.Run("grandchild", func(ctx context.Context, t *testctx.T) {
assert.Equal(t, 3, ctx.Value(ctxKey{}))
})
})
})
}
func TestMiddlewareNesting(t *testing.T) {
var callCount int
tt := testctx.New(t).Using(func(next testctx.TestFunc) testctx.TestFunc {
return func(ctx context.Context, t *testctx.W[*testing.T]) {
callCount++
next(ctx, t)
}
})
tt.Run("parent", func(ctx context.Context, t *testctx.T) {
assert.Equal(t, 1, callCount, "middleware should be called once for parent")
t.Run("child", func(ctx context.Context, t *testctx.T) {
assert.Equal(t, 2, callCount, "middleware should be called again for child")
t.Run("grandchild", func(ctx context.Context, t *testctx.T) {
assert.Equal(t, 3, callCount, "middleware should be called again for grandchild")
})
})
})
}
func TestMiddlewareDynamicAddition(t *testing.T) {
var order []string
tt := testctx.New(t).Using(func(next testctx.TestFunc) testctx.TestFunc {
return func(ctx context.Context, t *testctx.W[*testing.T]) {
order = append(order, "first")
next(ctx, t)
}
})
tt.Run("parent", func(ctx context.Context, t *testctx.T) {
// Add middleware during test execution
t = t.Using(func(next testctx.TestFunc) testctx.TestFunc {
return func(ctx context.Context, t *testctx.W[*testing.T]) {
order = append(order, "dynamic")
next(ctx, t)
}
})
t.Run("child", func(ctx context.Context, t *testctx.T) {
order = append(order, "test")
})
})
tt.Run("parent 2", func(ctx context.Context, t *testctx.T) {
order = append(order, "parent 2")
})
// Expect first middleware to run for both parent and child,
// dynamic middleware to run for child only
assert.Equal(t, []string{
"first", // outer middleware for parent
"first", // outer middleware for child
"dynamic", // inner middleware for child
"test", // child test execution
"first", // outer middleware for parent 2
"parent 2", // parent 2 test execution
}, order)
}