Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman committed Jun 21, 2024
1 parent 9780c77 commit 2936f0e
Show file tree
Hide file tree
Showing 15 changed files with 498 additions and 125 deletions.
133 changes: 89 additions & 44 deletions tests/follow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,97 @@ import (
func TestFollow(t *testing.T) {
t.Parallel()

base := filepath.Join("testdata", "follow")
t.Run("follow_verbose", func(t *testing.T) {
base := filepath.Join("testdata", "follow-verbose")

tt := []struct {
fileName string
err error
exitCode int
}{
// race detected
{"test_01", nil, 1},
{"test_02", nil, 0},
{"test_03", nil, 0},
{"test_04", nil, 0},
{"test_05", parse.ErrNotParsable, 1},
// build failure in one package
{"test_06", nil, 2},
}
for _, tc := range tt {
t.Run(tc.fileName, func(t *testing.T) {
inputFile := filepath.Join(base, tc.fileName+".jsonl")
options := app.Options{
FileName: inputFile,
FollowOutput: true,
FollowOutputVerbose: true,
DisableTableOutput: true,
}
var buf bytes.Buffer
gotExitCode, err := app.Run(&buf, options)
if err != nil && !errors.Is(err, tc.err) {
t.Fatal(err)
}
check.Number(t, gotExitCode, tc.exitCode)
goldenFile := filepath.Join(base, tc.fileName+".golden")
want, err := os.ReadFile(goldenFile)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(buf.Bytes(), want) {
t.Error("input does not match expected output; diff files in follow dir suffixed with .FAIL to debug")
t.Logf("diff %v %v",
"tests/"+goldenFile+".FAIL",
"tests/"+inputFile+".FAIL",
)
if err := os.WriteFile(goldenFile+".FAIL", buf.Bytes(), 0644); err != nil {
tt := []struct {
fileName string
err error
exitCode int
}{
// race detected
{"test_01", nil, 1},
{"test_02", nil, 0},
{"test_03", nil, 0},
{"test_04", nil, 0},
{"test_05", parse.ErrNotParsable, 1},
// build failure in one package
{"test_06", nil, 2},
}
for _, tc := range tt {
t.Run(tc.fileName, func(t *testing.T) {
inputFile := filepath.Join(base, tc.fileName+".jsonl")
options := app.Options{
FileName: inputFile,
FollowOutput: true,
FollowOutputVerbose: true,
DisableTableOutput: true,
}
var buf bytes.Buffer
gotExitCode, err := app.Run(&buf, options)
if err != nil && !errors.Is(err, tc.err) {
t.Fatal(err)
}
check.Number(t, gotExitCode, tc.exitCode)
goldenFile := filepath.Join(base, tc.fileName+".golden")
want, err := os.ReadFile(goldenFile)
if err != nil {
t.Fatal(err)
}
}
})
checkGolden(t, inputFile, goldenFile, buf.Bytes(), want)
})
}
})

t.Run("follow_no_verbose", func(t *testing.T) {
base := filepath.Join("testdata", "follow")

tt := []struct {
fileName string
err error
exitCode int
}{
{"test_01", nil, 0},
}
for _, tc := range tt {
t.Run(tc.fileName, func(t *testing.T) {
inputFile := filepath.Join(base, tc.fileName+".jsonl")
options := app.Options{
FileName: inputFile,
FollowOutput: true,
DisableTableOutput: true,
}
var buf bytes.Buffer
gotExitCode, err := app.Run(&buf, options)
if err != nil && !errors.Is(err, tc.err) {
t.Fatal(err)
}
check.Number(t, gotExitCode, tc.exitCode)
goldenFile := filepath.Join(base, tc.fileName+".golden")
want, err := os.ReadFile(goldenFile)
if err != nil {
t.Fatal(err)
}
checkGolden(t, inputFile, goldenFile, buf.Bytes(), want)
})
}
})
}

func checkGolden(
t *testing.T,
inputFile, goldenFile string,
got, want []byte,
) {
t.Helper()
if !bytes.Equal(got, want) {
t.Error("input does not match expected output; diff files in follow dir suffixed with .FAIL to debug")
t.Logf("diff %v %v",
"tests/"+goldenFile+".FAIL",
"tests/"+inputFile+".FAIL",
)
if err := os.WriteFile(goldenFile+".FAIL", got, 0644); err != nil {
t.Fatal(err)
}
}
}
39 changes: 39 additions & 0 deletions tests/testdata/follow-verbose/test_01.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
=== RUN TestRace
==================
WARNING: DATA RACE
Write at 0x00c000090090 by goroutine 7:
debug/tparse-24.TestRace.func1()
/Users/michael.fridman/go/src/debug/tparse-24/some_test.go:7 +0x38

Previous write at 0x00c000090090 by goroutine 6:
debug/tparse-24.TestRace()
/Users/michael.fridman/go/src/debug/tparse-24/some_test.go:8 +0x88
testing.tRunner()
/usr/local/go/src/testing/testing.go:827 +0x162

Goroutine 7 (running) created at:
debug/tparse-24.TestRace()
/Users/michael.fridman/go/src/debug/tparse-24/some_test.go:7 +0x7a
testing.tRunner()
/usr/local/go/src/testing/testing.go:827 +0x162

Goroutine 6 (running) created at:
testing.(*T).Run()
/usr/local/go/src/testing/testing.go:878 +0x650
testing.runTests.func1()
/usr/local/go/src/testing/testing.go:1119 +0xa8
testing.tRunner()
/usr/local/go/src/testing/testing.go:827 +0x162
testing.runTests()
/usr/local/go/src/testing/testing.go:1117 +0x4ee
testing.(*M).Run()
/usr/local/go/src/testing/testing.go:1034 +0x2ee
main.main()
_testmain.go:42 +0x221
==================
--- FAIL: TestRace (0.00s)
some_test.go:9: 64
testing.go:771: race detected during execution of test
FAIL
exit status 1
FAIL debug/tparse-24 0.020s
42 changes: 42 additions & 0 deletions tests/testdata/follow-verbose/test_01.jsonl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{"Time":"2018-12-04T22:42:29.634612-05:00","Action":"run","Package":"debug/tparse-24","Test":"TestRace"}
{"Time":"2018-12-04T22:42:29.634912-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":"=== RUN TestRace\n"}
{"Time":"2018-12-04T22:42:29.635129-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":"==================\n"}
{"Time":"2018-12-04T22:42:29.635163-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":"WARNING: DATA RACE\n"}
{"Time":"2018-12-04T22:42:29.635172-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":"Write at 0x00c000090090 by goroutine 7:\n"}
{"Time":"2018-12-04T22:42:29.635205-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":" debug/tparse-24.TestRace.func1()\n"}
{"Time":"2018-12-04T22:42:29.635213-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":" /Users/michael.fridman/go/src/debug/tparse-24/some_test.go:7 +0x38\n"}
{"Time":"2018-12-04T22:42:29.635226-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":"\n"}
{"Time":"2018-12-04T22:42:29.635236-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":"Previous write at 0x00c000090090 by goroutine 6:\n"}
{"Time":"2018-12-04T22:42:29.635254-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":" debug/tparse-24.TestRace()\n"}
{"Time":"2018-12-04T22:42:29.63527-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":" /Users/michael.fridman/go/src/debug/tparse-24/some_test.go:8 +0x88\n"}
{"Time":"2018-12-04T22:42:29.635283-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":" testing.tRunner()\n"}
{"Time":"2018-12-04T22:42:29.63529-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":" /usr/local/go/src/testing/testing.go:827 +0x162\n"}
{"Time":"2018-12-04T22:42:29.635302-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":"\n"}
{"Time":"2018-12-04T22:42:29.635322-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":"Goroutine 7 (running) created at:\n"}
{"Time":"2018-12-04T22:42:29.635334-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":" debug/tparse-24.TestRace()\n"}
{"Time":"2018-12-04T22:42:29.635341-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":" /Users/michael.fridman/go/src/debug/tparse-24/some_test.go:7 +0x7a\n"}
{"Time":"2018-12-04T22:42:29.635348-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":" testing.tRunner()\n"}
{"Time":"2018-12-04T22:42:29.635354-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":" /usr/local/go/src/testing/testing.go:827 +0x162\n"}
{"Time":"2018-12-04T22:42:29.635361-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":"\n"}
{"Time":"2018-12-04T22:42:29.635367-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":"Goroutine 6 (running) created at:\n"}
{"Time":"2018-12-04T22:42:29.635376-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":" testing.(*T).Run()\n"}
{"Time":"2018-12-04T22:42:29.635382-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":" /usr/local/go/src/testing/testing.go:878 +0x650\n"}
{"Time":"2018-12-04T22:42:29.635388-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":" testing.runTests.func1()\n"}
{"Time":"2018-12-04T22:42:29.635397-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":" /usr/local/go/src/testing/testing.go:1119 +0xa8\n"}
{"Time":"2018-12-04T22:42:29.635403-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":" testing.tRunner()\n"}
{"Time":"2018-12-04T22:42:29.635409-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":" /usr/local/go/src/testing/testing.go:827 +0x162\n"}
{"Time":"2018-12-04T22:42:29.635415-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":" testing.runTests()\n"}
{"Time":"2018-12-04T22:42:29.635422-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":" /usr/local/go/src/testing/testing.go:1117 +0x4ee\n"}
{"Time":"2018-12-04T22:42:29.635448-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":" testing.(*M).Run()\n"}
{"Time":"2018-12-04T22:42:29.635465-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":" /usr/local/go/src/testing/testing.go:1034 +0x2ee\n"}
{"Time":"2018-12-04T22:42:29.635472-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":" main.main()\n"}
{"Time":"2018-12-04T22:42:29.635478-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":" _testmain.go:42 +0x221\n"}
{"Time":"2018-12-04T22:42:29.635485-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":"==================\n"}
{"Time":"2018-12-04T22:42:29.635807-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":"--- FAIL: TestRace (0.00s)\n"}
{"Time":"2018-12-04T22:42:29.635821-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":" some_test.go:9: 64\n"}
{"Time":"2018-12-04T22:42:29.635829-05:00","Action":"output","Package":"debug/tparse-24","Test":"TestRace","Output":" testing.go:771: race detected during execution of test\n"}
{"Time":"2018-12-04T22:42:29.636065-05:00","Action":"fail","Package":"debug/tparse-24","Test":"TestRace","Elapsed":0}
{"Time":"2018-12-04T22:42:29.636085-05:00","Action":"output","Package":"debug/tparse-24","Output":"FAIL\n"}
{"Time":"2018-12-04T22:42:29.63706-05:00","Action":"output","Package":"debug/tparse-24","Output":"exit status 1\n"}
{"Time":"2018-12-04T22:42:29.637098-05:00","Action":"output","Package":"debug/tparse-24","Output":"FAIL\tdebug/tparse-24\t0.020s\n"}
{"Time":"2018-12-04T22:42:29.637108-05:00","Action":"fail","Package":"debug/tparse-24","Elapsed":0.02}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
42 changes: 3 additions & 39 deletions tests/testdata/follow/test_01.golden
Original file line number Diff line number Diff line change
@@ -1,39 +1,3 @@
=== RUN TestRace
==================
WARNING: DATA RACE
Write at 0x00c000090090 by goroutine 7:
debug/tparse-24.TestRace.func1()
/Users/michael.fridman/go/src/debug/tparse-24/some_test.go:7 +0x38

Previous write at 0x00c000090090 by goroutine 6:
debug/tparse-24.TestRace()
/Users/michael.fridman/go/src/debug/tparse-24/some_test.go:8 +0x88
testing.tRunner()
/usr/local/go/src/testing/testing.go:827 +0x162

Goroutine 7 (running) created at:
debug/tparse-24.TestRace()
/Users/michael.fridman/go/src/debug/tparse-24/some_test.go:7 +0x7a
testing.tRunner()
/usr/local/go/src/testing/testing.go:827 +0x162

Goroutine 6 (running) created at:
testing.(*T).Run()
/usr/local/go/src/testing/testing.go:878 +0x650
testing.runTests.func1()
/usr/local/go/src/testing/testing.go:1119 +0xa8
testing.tRunner()
/usr/local/go/src/testing/testing.go:827 +0x162
testing.runTests()
/usr/local/go/src/testing/testing.go:1117 +0x4ee
testing.(*M).Run()
/usr/local/go/src/testing/testing.go:1034 +0x2ee
main.main()
_testmain.go:42 +0x221
==================
--- FAIL: TestRace (0.00s)
some_test.go:9: 64
testing.go:771: race detected during execution of test
FAIL
exit status 1
FAIL debug/tparse-24 0.020s
fmt_test.go:1457: skipping; GOMAXPROCS>1
PASS
ok fmt 0.143s
Loading

0 comments on commit 2936f0e

Please sign in to comment.