From ef568d001484bda71003b966d1e31b573999d025 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 19 Aug 2023 13:05:21 +1200 Subject: [PATCH] test: use diff module (#202) --- go.mod | 1 + go.sum | 2 ++ main_test.go | 25 ++++++++++++++++++------- pkg/database/dir_test.go | 3 ++- pkg/database/zip_test.go | 3 ++- 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index e9be68a1..cb879acd 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.17 require ( github.com/BurntSushi/toml v1.0.0 github.com/fatih/color v1.13.0 + github.com/google/go-cmp v0.5.9 golang.org/x/mod v0.5.1 gopkg.in/yaml.v2 v2.4.0 ) diff --git a/go.sum b/go.sum index a5dd2920..c6a9a35d 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/BurntSushi/toml v1.0.0 h1:dtDWrepsVPfW9H/4y7dDgFc2MBUSeJhlaDtK13CxFlU github.com/BurntSushi/toml v1.0.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U= github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= diff --git a/main_test.go b/main_test.go index 5d058f52..06861c36 100644 --- a/main_test.go +++ b/main_test.go @@ -3,6 +3,7 @@ package main import ( "bytes" "fmt" + "github.com/google/go-cmp/cmp" "os" "path/filepath" "regexp" @@ -80,6 +81,21 @@ func normalizeFilePaths(output string) string { return strings.ReplaceAll(strings.ReplaceAll(output, "\\\\", "/"), "\\", "/") } +func expectAreEqual(t *testing.T, subject, actual, expect string) { + t.Helper() + + actual = dedent(t, actual) + expect = dedent(t, expect) + + if !areEqual(t, actual, expect) { + if os.Getenv("TEST_NO_DIFF") == "true" { + t.Errorf("\nactual %s does not match expected:\n got:\n%s\n\n want:\n%s", subject, actual, expect) + } else { + t.Errorf("\nactual %s does not match expected:\n%s", subject, cmp.Diff(expect, actual)) + } + } +} + func testCli(t *testing.T, tc cliTestCase) { t.Helper() @@ -95,13 +111,8 @@ func testCli(t *testing.T, tc cliTestCase) { t.Errorf("cli exited with code %d, not %d", ec, tc.wantExitCode) } - if !areEqual(t, dedent(t, stdout), dedent(t, tc.wantStdout)) { - t.Errorf("stdout\n got:\n%s\n\n want:\n%s", dedent(t, stdout), dedent(t, tc.wantStdout)) - } - - if !areEqual(t, dedent(t, stderr), dedent(t, tc.wantStderr)) { - t.Errorf("stderr\n got:\n%s\n\n want:\n%s", dedent(t, stderr), dedent(t, tc.wantStderr)) - } + expectAreEqual(t, "stdout output", stdout, tc.wantStdout) + expectAreEqual(t, "stderr output", stderr, tc.wantStderr) } func TestRun(t *testing.T) { diff --git a/pkg/database/dir_test.go b/pkg/database/dir_test.go index 31251a89..e2b24125 100644 --- a/pkg/database/dir_test.go +++ b/pkg/database/dir_test.go @@ -3,6 +3,7 @@ package database_test import ( "errors" "github.com/g-rath/osv-detector/pkg/database" + "github.com/google/go-cmp/cmp" "reflect" "testing" ) @@ -79,6 +80,6 @@ func TestNewDirDB_WorkingDirectory(t *testing.T) { } if !reflect.DeepEqual(db.Vulnerabilities(false), osvs) { - t.Errorf("db is missing some vulnerabilities: %v vs %v", db.Vulnerabilities(false), osvs) + t.Errorf("db is missing some vulnerabilities:\n%s", cmp.Diff(db.Vulnerabilities(false), osvs)) } } diff --git a/pkg/database/zip_test.go b/pkg/database/zip_test.go index d5d714ed..718aa238 100644 --- a/pkg/database/zip_test.go +++ b/pkg/database/zip_test.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "github.com/g-rath/osv-detector/pkg/database" + "github.com/google/go-cmp/cmp" "net/http" "net/http/httptest" "os" @@ -36,7 +37,7 @@ func expectDBToHaveOSVs( }) if !reflect.DeepEqual(vulns, actual) { - t.Errorf("db is missing some vulnerabilities: %v vs %v", vulns, actual) + t.Errorf("db is missing some vulnerabilities:\n%s", cmp.Diff(vulns, actual)) } }