-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_test.go
More file actions
325 lines (256 loc) · 11.8 KB
/
Copy pathintegration_test.go
File metadata and controls
325 lines (256 loc) · 11.8 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
package main
import (
"bytes"
"strings"
"testing"
)
// TestIntegration_CompleteWorkflow tests the complete workflow from JSON input to final output
func TestIntegration_CompleteWorkflow(t *testing.T) {
t.Parallel()
// Sample JSON input that represents a real go test -json output
jsonInput := `{"Time":"2023-01-01T00:00:00Z","Action":"run","Package":"example","Test":"TestMath"}
{"Time":"2023-01-01T00:00:01Z","Action":"output","Package":"example","Test":"TestMath","Output":"=== RUN TestMath\n"}
{"Time":"2023-01-01T00:00:02Z","Action":"output","Package":"example","Test":"TestMath","Output":" math_test.go:10: 2 + 2 should equal 4\n"}
{"Time":"2023-01-01T00:00:03Z","Action":"pass","Package":"example","Test":"TestMath","Elapsed":0.01}
{"Time":"2023-01-01T00:00:04Z","Action":"run","Package":"example","Test":"TestFail"}
{"Time":"2023-01-01T00:00:05Z","Action":"output","Package":"example","Test":"TestFail","Output":"=== RUN TestFail\n"}
{"Time":"2023-01-01T00:00:06Z","Action":"output","Package":"example","Test":"TestFail","Output":" math_test.go:20: assertion failed\n"}
{"Time":"2023-01-01T00:00:07Z","Action":"output","Package":"example","Test":"TestFail","Output":" expected: 5\n"}
{"Time":"2023-01-01T00:00:08Z","Action":"output","Package":"example","Test":"TestFail","Output":" actual: 4\n"}
{"Time":"2023-01-01T00:00:09Z","Action":"fail","Package":"example","Test":"TestFail","Elapsed":0.02}
{"Time":"2023-01-01T00:00:10Z","Action":"output","Package":"example","Output":"FAIL\n"}
{"Time":"2023-01-01T00:00:11Z","Action":"fail","Package":"example","Elapsed":0.03}
`
input := strings.NewReader(jsonInput)
var output bytes.Buffer
processor := NewEventProcessor()
display := NewTerminalDisplay(&output, false) // Disable colors for easier testing
runner := NewRunner(processor, display, input, &output)
exitCode := runner.Run()
// Should exit with code 1 due to test failure
if exitCode != 1 {
t.Errorf("Expected exit code 1 for failed tests, got %d", exitCode)
}
outputStr := output.String()
// Check that failed test is displayed
if !strings.Contains(outputStr, "FAIL") {
t.Error("Output should contain FAIL for failed test")
}
if !strings.Contains(outputStr, "TestFail") {
t.Error("Output should contain the failed test name")
}
if !strings.Contains(outputStr, "math_test.go:20") {
t.Error("Output should contain the test file location")
}
if !strings.Contains(outputStr, "assertion failed") {
t.Error("Output should contain the failure message")
}
// Check final summary
if !strings.Contains(outputStr, "Tests failed") {
t.Error("Output should contain final failure message")
}
if !strings.Contains(outputStr, "Total: 2 tests") {
t.Error("Output should contain total test count")
}
if !strings.Contains(outputStr, "Passed: 1") {
t.Error("Output should contain passed count")
}
if !strings.Contains(outputStr, "Failed: 1") {
t.Error("Output should contain failed count")
}
}
// TestIntegration_SuccessfulTests tests the workflow with all passing tests
func TestIntegration_SuccessfulTests(t *testing.T) {
t.Parallel()
jsonInput := `{"Time":"2023-01-01T00:00:00Z","Action":"run","Package":"example","Test":"TestA"}
{"Time":"2023-01-01T00:00:01Z","Action":"pass","Package":"example","Test":"TestA","Elapsed":0.01}
{"Time":"2023-01-01T00:00:02Z","Action":"run","Package":"example","Test":"TestB"}
{"Time":"2023-01-01T00:00:03Z","Action":"pass","Package":"example","Test":"TestB","Elapsed":0.02}
{"Time":"2023-01-01T00:00:04Z","Action":"output","Package":"example","Output":"PASS\n"}
{"Time":"2023-01-01T00:00:05Z","Action":"pass","Package":"example","Elapsed":0.05}
`
input := strings.NewReader(jsonInput)
var output bytes.Buffer
processor := NewEventProcessor()
display := NewTerminalDisplay(&output, false)
runner := NewRunner(processor, display, input, &output)
exitCode := runner.Run()
// Should exit with code 0 for successful tests
if exitCode != 0 {
t.Errorf("Expected exit code 0 for successful tests, got %d", exitCode)
}
outputStr := output.String()
// Check final success message
if !strings.Contains(outputStr, "All tests passed") {
t.Error("Output should contain success message")
}
if !strings.Contains(outputStr, "Total: 2 tests") {
t.Error("Output should contain total test count")
}
if !strings.Contains(outputStr, "Passed: 2") {
t.Error("Output should contain passed count")
}
if !strings.Contains(outputStr, "Failed: 0") {
t.Error("Output should contain failed count of 0")
}
}
// TestIntegration_SkippedTests tests the workflow with skipped tests
func TestIntegration_SkippedTests(t *testing.T) {
t.Parallel()
jsonInput := `{"Time":"2023-01-01T00:00:00Z","Action":"run","Package":"example","Test":"TestSkipped"}
{"Time":"2023-01-01T00:00:01Z","Action":"output","Package":"example","Test":"TestSkipped","Output":"=== RUN TestSkipped\n"}
{"Time":"2023-01-01T00:00:02Z","Action":"output","Package":"example","Test":"TestSkipped","Output":"--- SKIP: TestSkipped (0.00s)\n"}
{"Time":"2023-01-01T00:00:03Z","Action":"skip","Package":"example","Test":"TestSkipped","Elapsed":0.00}
{"Time":"2023-01-01T00:00:04Z","Action":"output","Package":"example","Output":"PASS\n"}
{"Time":"2023-01-01T00:00:05Z","Action":"pass","Package":"example","Elapsed":0.01}
`
input := strings.NewReader(jsonInput)
var output bytes.Buffer
processor := NewEventProcessor()
display := NewTerminalDisplay(&output, false)
runner := NewRunner(processor, display, input, &output)
exitCode := runner.Run()
// Should exit with code 0 for skipped tests (not failures)
if exitCode != 0 {
t.Errorf("Expected exit code 0 for skipped tests, got %d", exitCode)
}
outputStr := output.String()
if !strings.Contains(outputStr, "All tests passed") {
t.Error("Output should contain success message for skipped tests")
}
if !strings.Contains(outputStr, "Skipped: 1") {
t.Error("Output should contain skipped count")
}
}
// TestIntegration_PackageBuildFailure tests the workflow with package build failures
func TestIntegration_PackageBuildFailure(t *testing.T) {
t.Parallel()
jsonInput := `{"Time":"2023-01-01T00:00:00Z","Action":"output","Package":"broken","Output":"# broken [build failed]\n"}
{"Time":"2023-01-01T00:00:01Z","Action":"output","Package":"broken","Output":"syntax error: unexpected '}' at end of statement\n"}
{"Time":"2023-01-01T00:00:02Z","Action":"fail","Package":"broken","Elapsed":0.1}
`
input := strings.NewReader(jsonInput)
var output bytes.Buffer
processor := NewEventProcessor()
display := NewTerminalDisplay(&output, false)
runner := NewRunner(processor, display, input, &output)
exitCode := runner.Run()
// Should exit with code 1 for build failures
if exitCode != 1 {
t.Errorf("Expected exit code 1 for build failures, got %d", exitCode)
}
outputStr := output.String()
// Check that package failure is displayed
if !strings.Contains(outputStr, "PACKAGE FAIL") {
t.Error("Output should contain PACKAGE FAIL for build failures")
}
if !strings.Contains(outputStr, "broken") {
t.Error("Output should contain the failed package name")
}
if !strings.Contains(outputStr, "syntax error") {
t.Error("Output should contain the build error message")
}
}
// TestIntegration_Subtests tests the workflow with subtests
func TestIntegration_Subtests(t *testing.T) {
t.Parallel()
jsonInput := `{"Time":"2023-01-01T00:00:00Z","Action":"run","Package":"example","Test":"TestParent"}
{"Time":"2023-01-01T00:00:01Z","Action":"run","Package":"example","Test":"TestParent/SubA"}
{"Time":"2023-01-01T00:00:02Z","Action":"pass","Package":"example","Test":"TestParent/SubA","Elapsed":0.01}
{"Time":"2023-01-01T00:00:03Z","Action":"run","Package":"example","Test":"TestParent/SubB"}
{"Time":"2023-01-01T00:00:04Z","Action":"output","Package":"example","Test":"TestParent/SubB","Output":" test_test.go:25: subtest failed\n"}
{"Time":"2023-01-01T00:00:05Z","Action":"fail","Package":"example","Test":"TestParent/SubB","Elapsed":0.02}
{"Time":"2023-01-01T00:00:06Z","Action":"fail","Package":"example","Test":"TestParent","Elapsed":0.03}
{"Time":"2023-01-01T00:00:07Z","Action":"output","Package":"example","Output":"FAIL\n"}
{"Time":"2023-01-01T00:00:08Z","Action":"fail","Package":"example","Elapsed":0.05}
`
input := strings.NewReader(jsonInput)
var output bytes.Buffer
processor := NewEventProcessor()
display := NewTerminalDisplay(&output, false)
runner := NewRunner(processor, display, input, &output)
exitCode := runner.Run()
// Should exit with code 1 due to subtest failure
if exitCode != 1 {
t.Errorf("Expected exit code 1 for subtest failures, got %d", exitCode)
}
outputStr := output.String()
// Check that failed subtest is displayed but not parent test
if !strings.Contains(outputStr, "TestParent/SubB") {
t.Error("Output should contain the failed subtest name")
}
// Parent test should not be shown in detail (only subtest failures)
failLines := strings.Split(outputStr, "\n")
parentFailCount := 0
for _, line := range failLines {
if strings.Contains(line, "FAIL") && strings.Contains(line, "TestParent") && !strings.Contains(line, "/") {
parentFailCount++
}
}
// Parent test failure should not be displayed as individual failure
if parentFailCount > 1 {
t.Error("Parent test with subtests should not show detailed failure output")
}
}
// TestIntegration_MultiplePackages tests the workflow with multiple packages
func TestIntegration_MultiplePackages(t *testing.T) {
t.Parallel()
jsonInput := `{"Time":"2023-01-01T00:00:00Z","Action":"run","Package":"pkg1","Test":"TestPkg1"}
{"Time":"2023-01-01T00:00:01Z","Action":"pass","Package":"pkg1","Test":"TestPkg1","Elapsed":0.01}
{"Time":"2023-01-01T00:00:02Z","Action":"pass","Package":"pkg1","Elapsed":0.02}
{"Time":"2023-01-01T00:00:03Z","Action":"run","Package":"pkg2","Test":"TestPkg2"}
{"Time":"2023-01-01T00:00:04Z","Action":"output","Package":"pkg2","Test":"TestPkg2","Output":" pkg2_test.go:15: test failed\n"}
{"Time":"2023-01-01T00:00:05Z","Action":"fail","Package":"pkg2","Test":"TestPkg2","Elapsed":0.02}
{"Time":"2023-01-01T00:00:06Z","Action":"fail","Package":"pkg2","Elapsed":0.03}
`
input := strings.NewReader(jsonInput)
var output bytes.Buffer
processor := NewEventProcessor()
display := NewTerminalDisplay(&output, false)
runner := NewRunner(processor, display, input, &output)
exitCode := runner.Run()
// Should exit with code 1 due to pkg2 failure
if exitCode != 1 {
t.Errorf("Expected exit code 1 for package failures, got %d", exitCode)
}
outputStr := output.String()
// Check that failed test from pkg2 is displayed
if !strings.Contains(outputStr, "TestPkg2") {
t.Error("Output should contain the failed test from pkg2")
}
if !strings.Contains(outputStr, "pkg2_test.go:15") {
t.Error("Output should contain the failure location")
}
// Check final summary includes both packages
if !strings.Contains(outputStr, "Total: 2 tests") {
t.Error("Output should contain total test count from both packages")
}
if !strings.Contains(outputStr, "Passed: 1") {
t.Error("Output should contain passed count")
}
if !strings.Contains(outputStr, "Failed: 1") {
t.Error("Output should contain failed count")
}
}
// TestIntegration_EmptyOutput tests the workflow with no test events
func TestIntegration_EmptyOutput(t *testing.T) {
t.Parallel()
input := strings.NewReader("")
var output bytes.Buffer
processor := NewEventProcessor()
display := NewTerminalDisplay(&output, false)
runner := NewRunner(processor, display, input, &output)
exitCode := runner.Run()
// Should exit with code 0 for no tests
if exitCode != 0 {
t.Errorf("Expected exit code 0 for no tests, got %d", exitCode)
}
outputStr := output.String()
// Should still show final summary even with no tests
if !strings.Contains(outputStr, "Total: 0 tests") {
t.Error("Output should contain total test count of 0")
}
if !strings.Contains(outputStr, "All tests passed") {
t.Error("Output should contain success message when no tests to fail")
}
}