Skip to content

Commit

Permalink
test: ensure cmp.Diff usage is consistent (#1251)
Browse files Browse the repository at this point in the history
The examples in the docs suggest passing `want, got` rather the other
way around which we're already mostly doing but have a few cases where
we're not - likewise with the messaging we've been mostly consistent
between three slightly different phrasing, which I've updated to always
be `(-want +got)` which is what's in the example for `cmp.Diff`
  • Loading branch information
G-Rath authored Sep 16, 2024
1 parent 60609a4 commit bc35854
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions internal/resolution/lockfile/npm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ r 1.0.0
}

if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("npm lockfile mismatch (-want/+got):\n%s", diff)
t.Errorf("npm lockfile mismatch (-want +got):\n%s", diff)
}
}

Expand Down Expand Up @@ -109,7 +109,7 @@ r 1.0.0
}

if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("npm lockfile mismatch (-want/+got):\n%s", diff)
t.Errorf("npm lockfile mismatch (-want +got):\n%s", diff)
}
}

Expand Down
8 changes: 4 additions & 4 deletions internal/resolution/manifest/maven_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,8 @@ func TestMavenReadWrite(t *testing.T) {
},
},
}
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Maven manifest mismatch: %s", diff)
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("Maven manifest mismatch (-want +got):\n%s", diff)
}

// Re-open the file for writing.
Expand Down Expand Up @@ -953,8 +953,8 @@ func TestBuildPatches(t *testing.T) {
if err != nil {
t.Fatalf("failed to build patches: %v", err)
}
if diff := cmp.Diff(allPatches, want); diff != "" {
t.Errorf("result patches mismatch: %s", diff)
if diff := cmp.Diff(want, allPatches); diff != "" {
t.Errorf("result patches mismatch (-want +got):\n%s", diff)
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/sbom/cyclonedx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func runCycloneGetPackages(t *testing.T, bomFile string, want []sbom.Identifier)
}

if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("GetPackages() returned an unexpected result (-want, +got):\n%s", diff)
t.Errorf("GetPackages() returned an unexpected result (-want +got):\n%s", diff)
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/sbom/spdx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func runSPDXGetPackages(t *testing.T, bomFile string, want []sbom.Identifier) {
}

if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("GetPackages() returned an unexpected result (-want, +got):\n%s", diff)
t.Errorf("GetPackages() returned an unexpected result (-want +got):\n%s", diff)
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/grouper/grouper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func TestGroup(t *testing.T) {
} {
grouped := grouper.Group(tc.vulns)
if diff := cmp.Diff(tc.want, grouped); diff != "" {
t.Errorf("GroupedVulns() returned an unexpected result (-want, +got):\n%s", diff)
t.Errorf("GroupedVulns() returned an unexpected result (-want +got):\n%s", diff)
}
}
}
16 changes: 8 additions & 8 deletions pkg/models/results_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ func TestFlatten(t *testing.T) {
vulns := models.VulnerabilityResults{Results: []models.PackageSource{}}
expectedFlattened := []models.VulnerabilityFlattened{}
flattened := vulns.Flatten()
if diff := cmp.Diff(flattened, expectedFlattened); diff != "" {
t.Errorf("Flatten() returned unexpected result (-got +want):\n%s", diff)
if diff := cmp.Diff(expectedFlattened, flattened); diff != "" {
t.Errorf("Flatten() returned unexpected result (-want +got):\n%s", diff)
}

// Test case 2: When there are vulnerabilities
Expand Down Expand Up @@ -48,8 +48,8 @@ func TestFlatten(t *testing.T) {
},
}
flattened = vulns.Flatten()
if diff := cmp.Diff(flattened, expectedFlattened); diff != "" {
t.Errorf("Flatten() returned unexpected result (-got +want):\n%s", diff)
if diff := cmp.Diff(expectedFlattened, flattened); diff != "" {
t.Errorf("Flatten() returned unexpected result (-want +got):\n%s", diff)
}

// Test case 3: When there are no vulnerabilities and license violations
Expand All @@ -73,8 +73,8 @@ func TestFlatten(t *testing.T) {
},
}
flattened = vulns.Flatten()
if diff := cmp.Diff(flattened, expectedFlattened); diff != "" {
t.Errorf("Flatten() returned unexpected result (-got +want):\n%s", diff)
if diff := cmp.Diff(expectedFlattened, flattened); diff != "" {
t.Errorf("Flatten() returned unexpected result (-want +got):\n%s", diff)
}

// Test case 4: When there are vulnerabilities and license violations
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestFlatten(t *testing.T) {
},
}
flattened = vulns.Flatten()
if diff := cmp.Diff(flattened, expectedFlattened); diff != "" {
t.Errorf("Flatten() returned unexpected result (-got +want):\n%s", diff)
if diff := cmp.Diff(expectedFlattened, flattened); diff != "" {
t.Errorf("Flatten() returned unexpected result (-want +got):\n%s", diff)
}
}
2 changes: 1 addition & 1 deletion pkg/osvscanner/osvscanner_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func Test_scanGit(t *testing.T) {
if (err != nil) != tt.wantErr {
t.Errorf("scanGit() error = %v, wantErr %v", err, tt.wantErr)
}
if diff := cmp.Diff(tt.wantPkg, pkg); diff != "" {
if !cmp.Equal(tt.wantPkg, pkg) {
t.Errorf("scanGit() package = %v, wantPackage %v", pkg, tt.wantPkg)
}
}
Expand Down

0 comments on commit bc35854

Please sign in to comment.