-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2e_test.go
More file actions
284 lines (230 loc) · 8.63 KB
/
Copy pathe2e_test.go
File metadata and controls
284 lines (230 loc) · 8.63 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
package main
import (
"os/exec"
"strings"
"testing"
"time"
)
// TestE2E_HelpFlag tests the help flag functionality
func TestE2E_HelpFlag(t *testing.T) {
t.Parallel()
cmd := exec.Command("go", "run", ".", "-help")
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("Failed to run gotestshow with -help: %v", err)
}
outputStr := string(output)
if !strings.Contains(outputStr, "gotestshow - A real-time formatter") {
t.Error("Help output should contain application description")
}
if !strings.Contains(outputStr, "go test -json ./... | gotestshow") {
t.Error("Help output should contain usage example")
}
}
// TestE2E_RealTestOutput tests with real go test output from example directory
func TestE2E_RealTestOutput(t *testing.T) {
t.Parallel()
// First, build the gotestshow binary
buildCmd := exec.Command("go", "build", "-o", "gotestshow", ".")
if err := buildCmd.Run(); err != nil {
t.Fatalf("Failed to build gotestshow: %v", err)
}
// Run go test -json on example directory and pipe to gotestshow
cmd := exec.Command("bash", "-c", "cd example && go test -json ./... | ../gotestshow")
output, err := cmd.CombinedOutput()
// The exit code should be non-zero because example has failing tests
if err == nil {
t.Error("Expected non-zero exit code due to failing tests in example")
}
outputStr := string(output)
// Check that it shows failed tests
if !strings.Contains(outputStr, "FAIL") {
t.Error("Output should contain FAIL for failed tests in example")
}
// Check that it shows test statistics
if !strings.Contains(outputStr, "Total:") {
t.Error("Output should contain total test count")
}
if !strings.Contains(outputStr, "Passed:") {
t.Error("Output should contain passed count")
}
if !strings.Contains(outputStr, "Failed:") {
t.Error("Output should contain failed count")
}
// Check final failure message
if !strings.Contains(outputStr, "Tests failed") {
t.Error("Output should contain final failure message")
}
}
// TestE2E_SuccessfulTests tests with only successful tests from specific package
func TestE2E_SuccessfulTests(t *testing.T) {
t.Parallel()
// Build the gotestshow binary
buildCmd := exec.Command("go", "build", "-o", "gotestshow", ".")
if err := buildCmd.Run(); err != nil {
t.Fatalf("Failed to build gotestshow: %v", err)
}
// Run go test -json on a successful test (math.go functions that should pass)
cmd := exec.Command("bash", "-c", "cd example && go test -json -run TestAdd | ../gotestshow")
output, err := cmd.CombinedOutput()
// The exit code should be zero for passing tests
if err != nil {
t.Errorf("Expected zero exit code for passing tests, got error: %v", err)
}
outputStr := string(output)
// Check that it shows success message
if !strings.Contains(outputStr, "All tests passed") {
t.Error("Output should contain success message for passing tests")
}
// Should not contain FAIL
if strings.Contains(outputStr, "FAIL") {
t.Error("Output should not contain FAIL for passing tests")
}
}
// TestE2E_BuildFailure tests with package that has build errors
func TestE2E_BuildFailure(t *testing.T) {
t.Parallel()
// Build the gotestshow binary
buildCmd := exec.Command("go", "build", "-o", "gotestshow", ".")
if err := buildCmd.Run(); err != nil {
t.Fatalf("Failed to build gotestshow: %v", err)
}
// Run go test -json on broken package
cmd := exec.Command("bash", "-c", "cd example && go test -json ./broken | ../gotestshow")
output, err := cmd.CombinedOutput()
// The exit code should be non-zero due to build failure
if err == nil {
t.Error("Expected non-zero exit code due to build failure")
}
outputStr := string(output)
// Check that it shows package failure
if !strings.Contains(outputStr, "PACKAGE FAIL") {
t.Error("Output should contain PACKAGE FAIL for build errors")
}
// Check that it shows the package name
if !strings.Contains(outputStr, "broken") {
t.Error("Output should contain the failed package name")
}
// Check final failure message
if !strings.Contains(outputStr, "Tests failed") {
t.Error("Output should contain final failure message")
}
}
// TestE2E_NoStdin tests behavior when no stdin is provided
func TestE2E_NoStdin(t *testing.T) {
t.Parallel()
// Build the gotestshow binary
buildCmd := exec.Command("go", "build", "-o", "gotestshow", ".")
if err := buildCmd.Run(); err != nil {
t.Fatalf("Failed to build gotestshow: %v", err)
}
// Run gotestshow without any input
cmd := exec.Command("./gotestshow")
output, err := cmd.CombinedOutput()
// Should show help when no stdin
if err != nil {
t.Errorf("Expected zero exit code when showing help, got error: %v", err)
}
outputStr := string(output)
// Should show help message
if !strings.Contains(outputStr, "gotestshow - A real-time formatter") {
t.Error("Output should contain help message when no stdin provided")
}
}
// TestE2E_Performance tests that gotestshow can handle a reasonable load
func TestE2E_Performance(t *testing.T) {
t.Parallel()
// Skip this test in short mode
if testing.Short() {
t.Skip("Skipping performance test in short mode")
}
// Build the gotestshow binary
buildCmd := exec.Command("go", "build", "-o", "gotestshow", ".")
if err := buildCmd.Run(); err != nil {
t.Fatalf("Failed to build gotestshow: %v", err)
}
// Run tests with timeout to ensure it doesn't hang
cmd := exec.Command("bash", "-c", "cd example && timeout 30s go test -json ./... | ../gotestshow")
start := time.Now()
cmd.CombinedOutput()
elapsed := time.Since(start)
// Should complete within reasonable time (30 seconds is generous)
if elapsed > 30*time.Second {
t.Error("gotestshow took too long to process test output")
}
// Note: We expect an error here because the tests in example fail,
// but we're mainly testing that it doesn't hang or crash
}
// TestE2E_LongRunningTest tests with slow tests
func TestE2E_LongRunningTest(t *testing.T) {
t.Parallel()
// Build the gotestshow binary
buildCmd := exec.Command("go", "build", "-o", "gotestshow", ".")
if err := buildCmd.Run(); err != nil {
t.Fatalf("Failed to build gotestshow: %v", err)
}
// Run the slow test - note: some slow tests in example may fail
cmd := exec.Command("bash", "-c", "cd example && go test -json -run TestSlowOperation1 | ../gotestshow")
output, err := cmd.CombinedOutput()
// Should handle slow tests without issues (this specific test should pass)
if err != nil {
t.Errorf("Expected zero exit code for slow test, got error: %v", err)
}
outputStr := string(output)
// Should show progress and completion
if !strings.Contains(outputStr, "All tests passed") {
t.Error("Output should show completion of slow test")
}
}
// TestE2E_InvalidJSON tests behavior with malformed JSON input
func TestE2E_InvalidJSON(t *testing.T) {
t.Parallel()
// Build the gotestshow binary
buildCmd := exec.Command("go", "build", "-o", "gotestshow", ".")
if err := buildCmd.Run(); err != nil {
t.Fatalf("Failed to build gotestshow: %v", err)
}
// Create some invalid JSON mixed with valid JSON
invalidJSON := `{"invalid": json}
{"Time":"2023-01-01T00:00:00Z","Action":"run","Package":"example","Test":"TestExample"}
not json at all
{"Time":"2023-01-01T00:00:01Z","Action":"pass","Package":"example","Test":"TestExample","Elapsed":0.01}
`
cmd := exec.Command("./gotestshow")
cmd.Stdin = strings.NewReader(invalidJSON)
output, err := cmd.CombinedOutput()
// Should handle invalid JSON gracefully
if err != nil {
t.Errorf("gotestshow should handle invalid JSON gracefully, got error: %v", err)
}
outputStr := string(output)
// Should still process valid JSON events
if !strings.Contains(outputStr, "All tests passed") {
t.Error("Should still process valid JSON events despite invalid ones")
}
}
// TestE2E_ColorOutput tests that color output can be controlled
func TestE2E_ColorOutput(t *testing.T) {
t.Parallel()
// Build the gotestshow binary
buildCmd := exec.Command("go", "build", "-o", "gotestshow", ".")
if err := buildCmd.Run(); err != nil {
t.Fatalf("Failed to build gotestshow: %v", err)
}
// Create simple test JSON
testJSON := `{"Time":"2023-01-01T00:00:00Z","Action":"run","Package":"example","Test":"TestExample"}
{"Time":"2023-01-01T00:00:01Z","Action":"pass","Package":"example","Test":"TestExample","Elapsed":0.01}
`
cmd := exec.Command("./gotestshow")
cmd.Stdin = strings.NewReader(testJSON)
output, err := cmd.CombinedOutput()
if err != nil {
t.Errorf("Expected zero exit code, got error: %v", err)
}
outputStr := string(output)
// Output should contain ANSI color codes by default (when output is to terminal)
// Note: This might not work in all CI environments, so we check for content instead
if !strings.Contains(outputStr, "tests") {
t.Error("Output should contain test information")
}
}