From 458bfb4136416dfeb418f20774ba9623545f6f26 Mon Sep 17 00:00:00 2001 From: Michael Fridman Date: Thu, 5 Sep 2024 20:05:18 -0400 Subject: [PATCH] tests: use stretchr/testify instead of custom check package (#130) --- go.mod | 4 ++ go.sum | 10 +++++ internal/check/check.go | 86 ------------------------------------ parse/event_test.go | 16 +++---- tests/cached_test.go | 10 +++-- tests/cover_test.go | 10 +++-- tests/follow_test.go | 7 +-- tests/outcome_test.go | 10 +++-- tests/package_start_test.go | 12 ++--- tests/panic_test.go | 10 +++-- tests/prescan_test.go | 11 +++-- tests/race_test.go | 12 ++--- tests/sort_test.go | 22 ++++----- tests/summary_counts_test.go | 18 ++++---- 14 files changed, 94 insertions(+), 144 deletions(-) delete mode 100644 internal/check/check.go diff --git a/go.mod b/go.mod index 637ff96..965de24 100644 --- a/go.mod +++ b/go.mod @@ -7,14 +7,18 @@ require ( github.com/mfridman/buildversion v0.3.0 github.com/muesli/termenv v0.15.2 github.com/olekukonko/tablewriter v0.0.5 + github.com/stretchr/testify v1.9.0 ) require ( github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect github.com/charmbracelet/x/ansi v0.2.3 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-runewidth v0.0.16 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rivo/uniseg v0.4.7 // indirect golang.org/x/sys v0.25.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 20d354c..71c003c 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/charmbracelet/lipgloss v0.13.0 h1:4X3PPeoWEDCMvzDvGmTajSyYPcZM4+y8sCA github.com/charmbracelet/lipgloss v0.13.0/go.mod h1:nw4zy0SBX/F/eAO1cWdcvy6qnkDUxr8Lw7dvFrAIbbY= github.com/charmbracelet/x/ansi v0.2.3 h1:VfFN0NUpcjBRd4DnKfRaIRo53KRgey/nhOoEqosGDEY= github.com/charmbracelet/x/ansi v0.2.3/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= @@ -17,9 +19,17 @@ github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8= github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/check/check.go b/internal/check/check.go deleted file mode 100644 index c0db38a..0000000 --- a/internal/check/check.go +++ /dev/null @@ -1,86 +0,0 @@ -package check - -import ( - "errors" - "fmt" - "reflect" - "strings" - "testing" -) - -func NoError(t *testing.T, err error) { - t.Helper() - if err != nil { - t.Fatalf("unexpected error: %v", err) - } -} - -func HasError(t *testing.T, err error) { - t.Helper() - if err == nil { - t.Fatal("expecting an error: got nil") - } -} - -func IsError(t *testing.T, err, target error) { - t.Helper() - if !errors.Is(err, target) { - t.Fatalf("expecting specific error:\ngot: %v\nwant: %v", err, target) - - } -} - -func Number(t *testing.T, got, want any) { - t.Helper() - gotNumber, err := reflectToInt64(got) - if err != nil { - t.Fatal(err) - } - wantNumber, err := reflectToInt64(want) - if err != nil { - t.Fatal(err) - } - if gotNumber != wantNumber { - t.Fatalf("unexpected number value: got:%d want:%d ", gotNumber, wantNumber) - } -} - -func NumberNotZero(t *testing.T, got any) { - t.Helper() - gotNumber, err := reflectToInt64(got) - if err != nil { - t.Fatal(err) - } - if gotNumber == 0 { - t.Fatalf("unexpected number value: got:%d want non-zero ", gotNumber) - } -} - -func Bool(t *testing.T, got, want bool) { - t.Helper() - if got != want { - t.Fatalf("unexpected boolean value: got:%t want:%t", got, want) - } -} - -func Contains(t *testing.T, got, want string) { - t.Helper() - if !strings.Contains(got, want) { - t.Fatalf("failed to find substring %q in string value %q", got, want) - } -} - -func Equal(t *testing.T, got, want any) { - t.Helper() - if !reflect.DeepEqual(got, want) { - t.Fatalf("failed deep equal:\ngot: %v\nwant: %v\v", got, want) - } -} - -func reflectToInt64(v any) (int64, error) { - switch typ := v.(type) { - case int, int8, int16, int32, int64: - return reflect.ValueOf(typ).Int(), nil - } - return 0, fmt.Errorf("invalid number: must be int64 type: got:%T", v) -} diff --git a/parse/event_test.go b/parse/event_test.go index c98b199..bbe00a3 100644 --- a/parse/event_test.go +++ b/parse/event_test.go @@ -5,7 +5,7 @@ import ( "strings" "testing" - "github.com/mfridman/tparse/internal/check" + "github.com/stretchr/testify/require" ) func TestNewEvent(t *testing.T) { @@ -99,7 +99,7 @@ func TestNewEvent(t *testing.T) { for i, tc := range tt { t.Run(fmt.Sprintf("event_%d", i), func(t *testing.T) { e, err := NewEvent([]byte(tc.raw)) - check.NoError(t, err) + require.NoError(t, err) if e.Action != tc.action { t.Errorf("wrong action: got %q, want %q", e.Action, tc.action) @@ -166,7 +166,7 @@ func TestCachedEvent(t *testing.T) { for i, tc := range tt { t.Run(fmt.Sprintf("event_%d", i), func(t *testing.T) { e, err := NewEvent([]byte(tc.raw)) - check.NoError(t, err) + require.NoError(t, err) got := e.IsCached() want := tc.cached @@ -220,7 +220,7 @@ func TestCoverEvent(t *testing.T) { for i, tc := range tt { t.Run(fmt.Sprintf("event_%d", i), func(t *testing.T) { e, err := NewEvent([]byte(tc.raw)) - check.NoError(t, err) + require.NoError(t, err) f, ok := e.Cover() if ok != tc.cover { @@ -264,7 +264,7 @@ func TestNoTestFiles(t *testing.T) { for i, tc := range tt { t.Run(fmt.Sprintf("event_%d", i), func(t *testing.T) { e, err := NewEvent([]byte(tc.raw)) - check.NoError(t, err) + require.NoError(t, err) got := e.NoTestFiles() want := tc.noTestFiles @@ -303,7 +303,7 @@ func TestNoTestsToRun(t *testing.T) { for i, tc := range tt { t.Run(fmt.Sprintf("event_%d", i), func(t *testing.T) { e, err := NewEvent([]byte(tc.raw)) - check.NoError(t, err) + require.NoError(t, err) got := e.NoTestsToRun() want := tc.noTests @@ -344,7 +344,7 @@ func TestNoTestsWarn(t *testing.T) { for i, tc := range tt { t.Run(fmt.Sprintf("event_%d", i), func(t *testing.T) { e, err := NewEvent([]byte(tc.raw)) - check.NoError(t, err) + require.NoError(t, err) got := e.NoTestsWarn() want := tc.wanNoTests @@ -394,7 +394,7 @@ func TestDiscardOutput(t *testing.T) { } for _, tc := range tt { e, err := NewEvent([]byte(tc)) - check.NoError(t, err) + require.NoError(t, err) if e.DiscardOutput() != true { t.Errorf("%s - %s failed discard check: got:%v, want:%v", e.Package, e.Test, e.DiscardOutput(), true) } diff --git a/tests/cached_test.go b/tests/cached_test.go index e74dd17..43cd101 100644 --- a/tests/cached_test.go +++ b/tests/cached_test.go @@ -5,7 +5,9 @@ import ( "path/filepath" "testing" - "github.com/mfridman/tparse/internal/check" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/mfridman/tparse/parse" ) @@ -56,11 +58,11 @@ func TestPackageCache(t *testing.T) { t.Run(tc.fileName, func(t *testing.T) { inputFile := filepath.Join(base, tc.fileName+".jsonl") f, err := os.Open(inputFile) - check.NoError(t, err) + require.NoError(t, err) summary, err := parse.Process(f) - check.NoError(t, err) - check.Number(t, len(summary.Packages), len(tc.expected)) + require.NoError(t, err) + assert.Equal(t, len(summary.Packages), len(tc.expected)) for name, pkg := range summary.Packages { t.Run(name, func(t *testing.T) { diff --git a/tests/cover_test.go b/tests/cover_test.go index 7274d1e..4c5873c 100644 --- a/tests/cover_test.go +++ b/tests/cover_test.go @@ -5,7 +5,9 @@ import ( "path/filepath" "testing" - "github.com/mfridman/tparse/internal/check" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/mfridman/tparse/parse" ) @@ -59,11 +61,11 @@ func Test(t *testing.T) { t.Run(tc.fileName, func(t *testing.T) { inputFile := filepath.Join(base, tc.fileName+".jsonl") f, err := os.Open(inputFile) - check.NoError(t, err) + require.NoError(t, err) summary, err := parse.Process(f) - check.NoError(t, err) - check.Number(t, len(summary.Packages), len(tc.expected)) + require.NoError(t, err) + assert.Equal(t, len(summary.Packages), len(tc.expected)) for name, pkg := range summary.Packages { t.Run(name, func(t *testing.T) { diff --git a/tests/follow_test.go b/tests/follow_test.go index ae8e372..66ba053 100644 --- a/tests/follow_test.go +++ b/tests/follow_test.go @@ -7,8 +7,9 @@ import ( "path/filepath" "testing" + "github.com/stretchr/testify/assert" + "github.com/mfridman/tparse/internal/app" - "github.com/mfridman/tparse/internal/check" "github.com/mfridman/tparse/parse" ) @@ -46,7 +47,7 @@ func TestFollow(t *testing.T) { if err != nil && !errors.Is(err, tc.err) { t.Fatal(err) } - check.Number(t, gotExitCode, tc.exitCode) + assert.Equal(t, gotExitCode, tc.exitCode) goldenFile := filepath.Join(base, tc.fileName+".golden") want, err := os.ReadFile(goldenFile) if err != nil { @@ -80,7 +81,7 @@ func TestFollow(t *testing.T) { if err != nil && !errors.Is(err, tc.err) { t.Fatal(err) } - check.Number(t, gotExitCode, tc.exitCode) + assert.Equal(t, gotExitCode, tc.exitCode) goldenFile := filepath.Join(base, tc.fileName+".golden") want, err := os.ReadFile(goldenFile) if err != nil { diff --git a/tests/outcome_test.go b/tests/outcome_test.go index f81c158..154c357 100644 --- a/tests/outcome_test.go +++ b/tests/outcome_test.go @@ -5,7 +5,9 @@ import ( "path/filepath" "testing" - "github.com/mfridman/tparse/internal/check" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/mfridman/tparse/parse" ) @@ -59,9 +61,9 @@ func TestFinalOutcome(t *testing.T) { t.Fatal(err) } summary, err := parse.Process(f) - check.NoError(t, err) - check.Number(t, len(summary.Packages), len(tc.registry)) - check.Number(t, summary.ExitCode(), tc.exitCode) + require.NoError(t, err) + assert.Equal(t, len(summary.Packages), len(tc.registry)) + assert.Equal(t, summary.ExitCode(), tc.exitCode) for name, pkg := range summary.Packages { want, ok := tc.registry[name] diff --git a/tests/package_start_test.go b/tests/package_start_test.go index 9be48a2..7fbca4e 100644 --- a/tests/package_start_test.go +++ b/tests/package_start_test.go @@ -5,7 +5,9 @@ import ( "testing" "time" - "github.com/mfridman/tparse/internal/check" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/mfridman/tparse/parse" ) @@ -30,12 +32,12 @@ func TestPackageStartTime(t *testing.T) { fileName := "./testdata/go120_start_action.jsonl" f, err := os.Open(fileName) - check.NoError(t, err) + require.NoError(t, err) defer f.Close() summary, err := parse.Process(f) - check.NoError(t, err) - check.Number(t, len(summary.Packages), len(expected)) + require.NoError(t, err) + assert.Equal(t, len(summary.Packages), len(expected)) for _, p := range summary.Packages { if p.StartTime.IsZero() { @@ -46,7 +48,7 @@ func TestPackageStartTime(t *testing.T) { t.Fatalf("package %q not found in expected map", p.Summary.Package) } want, err := time.Parse(time.RFC3339, unparsed) - check.NoError(t, err) + require.NoError(t, err) if !p.StartTime.Equal(want) { t.Fatalf("package %q start time got %q want %q", p.Summary.Package, p.StartTime, want) } diff --git a/tests/panic_test.go b/tests/panic_test.go index 4acc543..e063952 100644 --- a/tests/panic_test.go +++ b/tests/panic_test.go @@ -5,7 +5,9 @@ import ( "path/filepath" "testing" - "github.com/mfridman/tparse/internal/check" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/mfridman/tparse/parse" ) @@ -65,11 +67,11 @@ func TestPanic(t *testing.T) { for _, tc := range tt { t.Run(tc.fileName, func(t *testing.T) { f, err := os.Open(filepath.Join(base, tc.fileName)) - check.NoError(t, err) + require.NoError(t, err) summary, err := parse.Process(f) - check.NoError(t, err) - check.Number(t, summary.ExitCode(), 1) + require.NoError(t, err) + assert.Equal(t, 1, summary.ExitCode()) for name, pkg := range summary.Packages { want, ok := tc.expected[name] diff --git a/tests/prescan_test.go b/tests/prescan_test.go index 55d0ef8..a161f9c 100644 --- a/tests/prescan_test.go +++ b/tests/prescan_test.go @@ -5,7 +5,8 @@ import ( "path/filepath" "testing" - "github.com/mfridman/tparse/internal/check" + "github.com/stretchr/testify/require" + "github.com/mfridman/tparse/parse" ) @@ -31,10 +32,14 @@ func TestPrescan(t *testing.T) { t.Run(tc.fileName, func(t *testing.T) { inputFile := filepath.Join(base, tc.fileName) f, err := os.Open(inputFile) - check.NoError(t, err) + require.NoError(t, err) _, err = parse.Process(f) - check.IsError(t, err, tc.err) + if tc.err != nil { + require.ErrorIs(t, err, tc.err) + return + } + require.NoError(t, err) }) } diff --git a/tests/race_test.go b/tests/race_test.go index baab254..def0d0e 100644 --- a/tests/race_test.go +++ b/tests/race_test.go @@ -5,7 +5,9 @@ import ( "path/filepath" "testing" - "github.com/mfridman/tparse/internal/check" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/mfridman/tparse/parse" ) @@ -57,11 +59,11 @@ func TestRaceDetected(t *testing.T) { t.Run(tc.fileName, func(t *testing.T) { inputFile := filepath.Join(base, tc.fileName+".jsonl") f, err := os.Open(inputFile) - check.NoError(t, err) + require.NoError(t, err) defer f.Close() summary, err := parse.Process(f) - check.NoError(t, err) + require.NoError(t, err) if summary.ExitCode() == 0 { t.Fatalf("expecting non-zero exit code") @@ -71,10 +73,10 @@ func TestRaceDetected(t *testing.T) { if !ok { t.Fatalf("failed to find package: %q", name) } - check.Number(t, len(pkg.DataRaceTests), len(wantTestName)) + assert.Equal(t, len(pkg.DataRaceTests), len(wantTestName)) if len(pkg.DataRaceTests) > 0 { for i := range pkg.DataRaceTests { - check.Equal(t, pkg.DataRaceTests[i], wantTestName[i]) + assert.Equal(t, pkg.DataRaceTests[i], wantTestName[i]) } } } diff --git a/tests/sort_test.go b/tests/sort_test.go index e77249c..85469c4 100644 --- a/tests/sort_test.go +++ b/tests/sort_test.go @@ -5,7 +5,9 @@ import ( "path/filepath" "testing" - "github.com/mfridman/tparse/internal/check" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/mfridman/tparse/parse" ) @@ -52,11 +54,11 @@ func TestSortName(t *testing.T) { t.Run(tc.fileName, func(t *testing.T) { inputFile := filepath.Join(base, tc.fileName+".jsonl") f, err := os.Open(inputFile) - check.NoError(t, err) + require.NoError(t, err) summary, err := parse.Process(f) - check.NoError(t, err) - check.Number(t, len(summary.Packages), len(tc.expected)) + require.NoError(t, err) + assert.Equal(t, len(summary.Packages), len(tc.expected)) packages := summary.GetSortedPackages(parse.SortByPackageName) for i, pkg := range packages { @@ -114,11 +116,11 @@ func TestSortCoverage(t *testing.T) { t.Run(tc.fileName, func(t *testing.T) { inputFile := filepath.Join(base, tc.fileName+".jsonl") f, err := os.Open(inputFile) - check.NoError(t, err) + require.NoError(t, err) summary, err := parse.Process(f) - check.NoError(t, err) - check.Number(t, len(summary.Packages), len(tc.expected)) + require.NoError(t, err) + assert.Equal(t, len(summary.Packages), len(tc.expected)) packages := summary.GetSortedPackages(parse.SortByCoverage) for i, pkg := range packages { @@ -176,11 +178,11 @@ func TestSortElapsed(t *testing.T) { t.Run(tc.fileName, func(t *testing.T) { inputFile := filepath.Join(base, tc.fileName+".jsonl") f, err := os.Open(inputFile) - check.NoError(t, err) + require.NoError(t, err) summary, err := parse.Process(f) - check.NoError(t, err) - check.Number(t, len(summary.Packages), len(tc.expected)) + require.NoError(t, err) + assert.Equal(t, len(summary.Packages), len(tc.expected)) packages := summary.GetSortedPackages(parse.SortByElapsed) for i, pkg := range packages { diff --git a/tests/summary_counts_test.go b/tests/summary_counts_test.go index 6c1fda9..bd9edef 100644 --- a/tests/summary_counts_test.go +++ b/tests/summary_counts_test.go @@ -4,7 +4,9 @@ import ( "os" "testing" - "github.com/mfridman/tparse/internal/check" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/mfridman/tparse/parse" ) @@ -16,11 +18,11 @@ func TestSummaryCounts(t *testing.T) { fileName := "./testdata/metrics_test.jsonl" f, err := os.Open(fileName) - check.NoError(t, err) + require.NoError(t, err) summary, err := parse.Process(f) - check.NoError(t, err) - check.Number(t, len(summary.Packages), 9) + require.NoError(t, err) + assert.Len(t, summary.Packages, 9) for name, pkg := range summary.Packages { if pkg.Summary == nil { @@ -91,18 +93,18 @@ func TestElapsed(t *testing.T) { fileName := "./testdata/elapsed_test.jsonl" f, err := os.Open(fileName) - check.NoError(t, err) + require.NoError(t, err) defer f.Close() summary, err := parse.Process(f) - check.NoError(t, err) - check.Number(t, len(summary.Packages), 1) + require.NoError(t, err) + assert.Len(t, summary.Packages, 1) pkg, ok := summary.Packages["strings"] if !ok { t.Fatalf(`got unexpected pkg: %v\nwant "strings"`, pkg) } - check.Number(t, len(pkg.Tests), 2) + assert.Len(t, pkg.Tests, 2) for _, test := range pkg.Tests { wantElapsed, ok := expected[test.Name]