Skip to content

Commit

Permalink
test: use diff module (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Aug 19, 2023
1 parent e20e642 commit ef568d0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
25 changes: 18 additions & 7 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"fmt"
"github.com/google/go-cmp/cmp"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -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()

Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/database/dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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))
}
}
3 changes: 2 additions & 1 deletion pkg/database/zip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
}
}

Expand Down

0 comments on commit ef568d0

Please sign in to comment.