Skip to content

Commit ca6e3c9

Browse files
Zhou-Zihengdnephin
authored andcommitted
fix swallow build error and added tests
1 parent 13c6821 commit ca6e3c9

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed

cmd/handler_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,51 @@ func TestScanTestOutput_TestTimeoutPanicRace(t *testing.T) {
174174
})
175175
}
176176
}
177+
178+
// Tests output when a package fails to build because of a compilation error
179+
// and no tests are run in the package.
180+
func TestEventHandler_TestBuildFail(t *testing.T) {
181+
t.Setenv("GITHUB_ACTIONS", "no")
182+
183+
buf := new(bufferCloser)
184+
errBuf := new(bytes.Buffer)
185+
format := testjson.NewEventFormatter(errBuf, "testname", testjson.FormatOptions{})
186+
187+
source := golden.Get(t, "input/go-test-build-failed.out")
188+
cfg := testjson.ScanConfig{
189+
Stdout: bytes.NewReader(source),
190+
Handler: &eventHandler{jsonFile: buf, formatter: format},
191+
}
192+
exec, err := testjson.ScanTestOutput(cfg)
193+
assert.NilError(t, err)
194+
195+
out := new(bytes.Buffer)
196+
testjson.PrintSummary(out, exec, testjson.SummarizeAll)
197+
198+
actual := text.ProcessLines(t, out, text.OpRemoveSummaryLineElapsedTime)
199+
golden.Assert(t, actual, "expected/build-fail-expected")
200+
}
201+
202+
// Tests output when a package fails to build because of a compilation error
203+
// due to syntax errors and the like.
204+
func TestEventHandler_SetupFail(t *testing.T) {
205+
t.Setenv("GITHUB_ACTIONS", "no")
206+
207+
buf := new(bufferCloser)
208+
errBuf := new(bytes.Buffer)
209+
format := testjson.NewEventFormatter(errBuf, "testname", testjson.FormatOptions{})
210+
211+
source := golden.Get(t, "input/go-test-setup-failed.out")
212+
cfg := testjson.ScanConfig{
213+
Stdout: bytes.NewReader(source),
214+
Handler: &eventHandler{jsonFile: buf, formatter: format},
215+
}
216+
exec, err := testjson.ScanTestOutput(cfg)
217+
assert.NilError(t, err)
218+
219+
out := new(bytes.Buffer)
220+
testjson.PrintSummary(out, exec, testjson.SummarizeAll)
221+
222+
actual := text.ProcessLines(t, out, text.OpRemoveSummaryLineElapsedTime)
223+
golden.Assert(t, actual, "expected/setup-fail-expected")
224+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
=== Failed
3+
=== FAIL: example.com/internal/cacher (0.00s)
4+
# example.com/internal/cacher [example.com/internal/cacher.test]
5+
./directory_test.go:321:10: undefined: assert.foo
6+
FAIL example.com/internal/cacher [build failed]
7+
8+
DONE 0 tests, 1 failure
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
=== Failed
3+
=== FAIL: example.com/internal/cacher (0.00s)
4+
# example.com/internal/cacher
5+
directory_test.go:321:13: expected ';', found o
6+
FAIL example.com/internal/cacher [setup failed]
7+
8+
=== FAIL: example.com/internal/cacher/subpkg (0.00s)
9+
# example.com/internal/cacher/subpkg
10+
subpkg/main_test.go:1:1: expected 'package', found aldfjadskfs
11+
FAIL example.com/internal/cacher/subpkg [setup failed]
12+
13+
DONE 0 tests, 2 failures
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{"ImportPath":"example.com/internal/cacher [example.com/internal/cacher.test]","Action":"build-output","Output":"# example.com/internal/cacher [example.com/internal/cacher.test]\n"}
2+
{"ImportPath":"example.com/internal/cacher [example.com/internal/cacher.test]","Action":"build-output","Output":"./directory_test.go:321:10: undefined: assert.foo\n"}
3+
{"ImportPath":"example.com/internal/cacher [example.com/internal/cacher.test]","Action":"build-fail"}
4+
{"Time":"2025-03-10T17:34:31.493978-07:00","Action":"start","Package":"example.com/internal/cacher"}
5+
{"Time":"2025-03-10T17:34:31.494027-07:00","Action":"output","Package":"example.com/internal/cacher","Output":"FAIL\texample.com/internal/cacher [build failed]\n"}
6+
{"Time":"2025-03-10T17:34:31.494039-07:00","Action":"fail","Package":"example.com/internal/cacher","Elapsed":0,"FailedBuild":"example.com/internal/cacher [example.com/internal/cacher.test]"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{"ImportPath":"example.com/internal/cacher.test","Action":"build-output","Output":"# example.com/internal/cacher\n"}
2+
{"ImportPath":"example.com/internal/cacher.test","Action":"build-output","Output":"directory_test.go:321:13: expected ';', found o\n"}
3+
{"ImportPath":"example.com/internal/cacher.test","Action":"build-fail"}
4+
{"Time":"2025-03-11T10:08:34.978868-07:00","Action":"start","Package":"example.com/internal/cacher"}
5+
{"Time":"2025-03-11T10:08:34.978905-07:00","Action":"output","Package":"example.com/internal/cacher","Output":"FAIL\texample.com/internal/cacher [setup failed]\n"}
6+
{"Time":"2025-03-11T10:08:34.978914-07:00","Action":"fail","Package":"example.com/internal/cacher","Elapsed":0,"FailedBuild":"example.com/internal/cacher.test"}
7+
{"ImportPath":"example.com/internal/cacher/subpkg","Action":"build-output","Output":"# example.com/internal/cacher/subpkg\n"}
8+
{"ImportPath":"example.com/internal/cacher/subpkg","Action":"build-output","Output":"subpkg/main_test.go:1:1: expected 'package', found aldfjadskfs\n"}
9+
{"ImportPath":"example.com/internal/cacher/subpkg","Action":"build-fail"}
10+
{"Time":"2025-03-11T10:08:34.978928-07:00","Action":"start","Package":"example.com/internal/cacher/subpkg"}
11+
{"Time":"2025-03-11T10:08:34.979019-07:00","Action":"output","Package":"example.com/internal/cacher/subpkg","Output":"FAIL\texample.com/internal/cacher/subpkg [setup failed]\n"}
12+
{"Time":"2025-03-11T10:08:34.979028-07:00","Action":"fail","Package":"example.com/internal/cacher/subpkg","Elapsed":0,"FailedBuild":"example.com/internal/cacher/subpkg"}

0 commit comments

Comments
 (0)