Skip to content

Commit ef568d0

Browse files
authored
test: use diff module (#202)
1 parent e20e642 commit ef568d0

File tree

5 files changed

+25
-9
lines changed

5 files changed

+25
-9
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.17
55
require (
66
github.com/BurntSushi/toml v1.0.0
77
github.com/fatih/color v1.13.0
8+
github.com/google/go-cmp v0.5.9
89
golang.org/x/mod v0.5.1
910
gopkg.in/yaml.v2 v2.4.0
1011
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ github.com/BurntSushi/toml v1.0.0 h1:dtDWrepsVPfW9H/4y7dDgFc2MBUSeJhlaDtK13CxFlU
22
github.com/BurntSushi/toml v1.0.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
33
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
44
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
5+
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
6+
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
57
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
68
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
79
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=

main_test.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"bytes"
55
"fmt"
6+
"github.com/google/go-cmp/cmp"
67
"os"
78
"path/filepath"
89
"regexp"
@@ -80,6 +81,21 @@ func normalizeFilePaths(output string) string {
8081
return strings.ReplaceAll(strings.ReplaceAll(output, "\\\\", "/"), "\\", "/")
8182
}
8283

84+
func expectAreEqual(t *testing.T, subject, actual, expect string) {
85+
t.Helper()
86+
87+
actual = dedent(t, actual)
88+
expect = dedent(t, expect)
89+
90+
if !areEqual(t, actual, expect) {
91+
if os.Getenv("TEST_NO_DIFF") == "true" {
92+
t.Errorf("\nactual %s does not match expected:\n got:\n%s\n\n want:\n%s", subject, actual, expect)
93+
} else {
94+
t.Errorf("\nactual %s does not match expected:\n%s", subject, cmp.Diff(expect, actual))
95+
}
96+
}
97+
}
98+
8399
func testCli(t *testing.T, tc cliTestCase) {
84100
t.Helper()
85101

@@ -95,13 +111,8 @@ func testCli(t *testing.T, tc cliTestCase) {
95111
t.Errorf("cli exited with code %d, not %d", ec, tc.wantExitCode)
96112
}
97113

98-
if !areEqual(t, dedent(t, stdout), dedent(t, tc.wantStdout)) {
99-
t.Errorf("stdout\n got:\n%s\n\n want:\n%s", dedent(t, stdout), dedent(t, tc.wantStdout))
100-
}
101-
102-
if !areEqual(t, dedent(t, stderr), dedent(t, tc.wantStderr)) {
103-
t.Errorf("stderr\n got:\n%s\n\n want:\n%s", dedent(t, stderr), dedent(t, tc.wantStderr))
104-
}
114+
expectAreEqual(t, "stdout output", stdout, tc.wantStdout)
115+
expectAreEqual(t, "stderr output", stderr, tc.wantStderr)
105116
}
106117

107118
func TestRun(t *testing.T) {

pkg/database/dir_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package database_test
33
import (
44
"errors"
55
"github.com/g-rath/osv-detector/pkg/database"
6+
"github.com/google/go-cmp/cmp"
67
"reflect"
78
"testing"
89
)
@@ -79,6 +80,6 @@ func TestNewDirDB_WorkingDirectory(t *testing.T) {
7980
}
8081

8182
if !reflect.DeepEqual(db.Vulnerabilities(false), osvs) {
82-
t.Errorf("db is missing some vulnerabilities: %v vs %v", db.Vulnerabilities(false), osvs)
83+
t.Errorf("db is missing some vulnerabilities:\n%s", cmp.Diff(db.Vulnerabilities(false), osvs))
8384
}
8485
}

pkg/database/zip_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"errors"
99
"fmt"
1010
"github.com/g-rath/osv-detector/pkg/database"
11+
"github.com/google/go-cmp/cmp"
1112
"net/http"
1213
"net/http/httptest"
1314
"os"
@@ -36,7 +37,7 @@ func expectDBToHaveOSVs(
3637
})
3738

3839
if !reflect.DeepEqual(vulns, actual) {
39-
t.Errorf("db is missing some vulnerabilities: %v vs %v", vulns, actual)
40+
t.Errorf("db is missing some vulnerabilities:\n%s", cmp.Diff(vulns, actual))
4041
}
4142
}
4243

0 commit comments

Comments
 (0)