Skip to content

Commit 355aeb9

Browse files
authored
chore: Replace reflect.DeepEqual with cmp.Equal in tests (#3691)
1 parent 3646f33 commit 355aeb9

7 files changed

+35
-29
lines changed

.golangci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ linters:
88
- copyloopvar
99
- dogsled
1010
- dupl
11+
- forbidigo
1112
- gocritic
1213
- godot
1314
- goheader
@@ -40,6 +41,10 @@ linters:
4041
4142
Use of this source code is governed by a BSD-style
4243
license that can be found in the LICENSE file.
44+
forbidigo:
45+
forbid:
46+
- pattern: ^reflect\.DeepEqual$
47+
msg: "Use cmp.Equal instead of reflect.DeepEqual"
4348
gosec:
4449
excludes:
4550
# duplicates errcheck

github/github_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3186,7 +3186,7 @@ func TestPtr(t *testing.T) {
31863186
t.Parallel()
31873187
equal := func(t *testing.T, want, got any) {
31883188
t.Helper()
3189-
if !reflect.DeepEqual(want, got) {
3189+
if !cmp.Equal(want, got) {
31903190
t.Errorf("want %#v, got %#v", want, got)
31913191
}
31923192
}

github/orgs_codesecurity_configurations_test.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import (
1010
"encoding/json"
1111
"fmt"
1212
"net/http"
13-
"reflect"
1413
"testing"
14+
15+
"github.com/google/go-cmp/cmp"
1516
)
1617

1718
func TestOrganizationsService_GetCodeSecurityConfigurations(t *testing.T) {
@@ -43,7 +44,7 @@ func TestOrganizationsService_GetCodeSecurityConfigurations(t *testing.T) {
4344
{ID: Ptr(int64(1)), Name: Ptr("config1"), CodeScanningDefaultSetup: Ptr("enabled")},
4445
{ID: Ptr(int64(2)), Name: Ptr("config2"), PrivateVulnerabilityReporting: Ptr("enabled")},
4546
}
46-
if !reflect.DeepEqual(configurations, want) {
47+
if !cmp.Equal(configurations, want) {
4748
t.Errorf("Organizations.GetCodeSecurityConfigurations returned %+v, want %+v", configurations, want)
4849
}
4950
const methodName = "GetCodeSecurityConfigurations"
@@ -80,7 +81,7 @@ func TestOrganizationsService_GetCodeSecurityConfiguration(t *testing.T) {
8081
}
8182

8283
want := &CodeSecurityConfiguration{ID: Ptr(int64(1)), Name: Ptr("config1"), CodeScanningDefaultSetup: Ptr("enabled")}
83-
if !reflect.DeepEqual(configuration, want) {
84+
if !cmp.Equal(configuration, want) {
8485
t.Errorf("Organizations.GetCodeSecurityConfiguration returned %+v, want %+v", configuration, want)
8586
}
8687

@@ -113,7 +114,7 @@ func TestOrganizationsService_CreateCodeSecurityConfiguration(t *testing.T) {
113114
v := new(CodeSecurityConfiguration)
114115
assertNilError(t, json.NewDecoder(r.Body).Decode(v))
115116

116-
if !reflect.DeepEqual(v, input) {
117+
if !cmp.Equal(v, input) {
117118
t.Errorf("Organizations.CreateCodeSecurityConfiguration request body = %+v, want %+v", v, input)
118119
}
119120

@@ -130,7 +131,7 @@ func TestOrganizationsService_CreateCodeSecurityConfiguration(t *testing.T) {
130131
}
131132

132133
want := &CodeSecurityConfiguration{ID: Ptr(int64(1)), Name: Ptr("config1"), CodeScanningDefaultSetup: Ptr("enabled")}
133-
if !reflect.DeepEqual(configuration, want) {
134+
if !cmp.Equal(configuration, want) {
134135
t.Errorf("Organizations.CreateCodeSecurityConfiguration returned %+v, want %+v", configuration, want)
135136
}
136137

@@ -178,7 +179,7 @@ func TestOrganizationsService_GetDefaultCodeSecurityConfigurations(t *testing.T)
178179
{ID: Ptr(int64(1)), Name: Ptr("config1"), CodeScanningDefaultSetup: Ptr("enabled")},
179180
{ID: Ptr(int64(2)), Name: Ptr("config2"), PrivateVulnerabilityReporting: Ptr("enabled")},
180181
}
181-
if !reflect.DeepEqual(configurations, want) {
182+
if !cmp.Equal(configurations, want) {
182183
t.Errorf("Organizations.GetDefaultCodeSecurityConfigurations returned %+v, want %+v", configurations, want)
183184
}
184185

@@ -243,7 +244,7 @@ func TestOrganizationsService_UpdateCodeSecurityConfiguration(t *testing.T) {
243244
v := new(CodeSecurityConfiguration)
244245
assertNilError(t, json.NewDecoder(r.Body).Decode(v))
245246

246-
if !reflect.DeepEqual(v, input) {
247+
if !cmp.Equal(v, input) {
247248
t.Errorf("Organizations.UpdateCodeSecurityConfiguration request body = %+v, want %+v", v, input)
248249
}
249250

@@ -260,7 +261,7 @@ func TestOrganizationsService_UpdateCodeSecurityConfiguration(t *testing.T) {
260261
}
261262

262263
want := &CodeSecurityConfiguration{ID: Ptr(int64(1)), Name: Ptr("config1"), CodeScanningDefaultSetup: Ptr("enabled")}
263-
if !reflect.DeepEqual(configuration, want) {
264+
if !cmp.Equal(configuration, want) {
264265
t.Errorf("Organizations.UpdateCodeSecurityConfiguration returned %+v, want %+v", configuration, want)
265266
}
266267

@@ -327,7 +328,7 @@ func TestOrganizationsService_AttachCodeSecurityConfigurationsToRepositories(t *
327328
if v.Scope != "selected" {
328329
t.Errorf("Organizations.AttachCodeSecurityConfigurationsToRepositories request body scope = %s, want selected", v.Scope)
329330
}
330-
if !reflect.DeepEqual(v.SelectedRepositoryIDs, []int64{5, 20}) {
331+
if !cmp.Equal(v.SelectedRepositoryIDs, []int64{5, 20}) {
331332
t.Errorf("Organizations.AttachCodeSecurityConfigurationsToRepositories request body selected_repository_ids = %+v, want %+v", v.SelectedRepositoryIDs, []int64{5, 20})
332333
}
333334
w.WriteHeader(http.StatusAccepted)
@@ -387,7 +388,7 @@ func TestOrganizationsService_SetDefaultCodeSecurityConfiguration(t *testing.T)
387388
ID: Ptr(int64(1)), Name: Ptr("config1"), CodeScanningDefaultSetup: Ptr("enabled"),
388389
},
389390
}
390-
if !reflect.DeepEqual(got, want) {
391+
if !cmp.Equal(got, want) {
391392
t.Errorf("Organizations.SetDefaultCodeSecurityConfiguration returned %+v, want %+v", got, want)
392393
}
393394

@@ -433,7 +434,7 @@ func TestOrganizationsService_GetRepositoriesForCodeSecurityConfiguration(t *tes
433434
{ID: Ptr(int64(8)), Name: Ptr("repo8")},
434435
{ID: Ptr(int64(42)), Name: Ptr("repo42")},
435436
}
436-
if !reflect.DeepEqual(repositories, want) {
437+
if !cmp.Equal(repositories, want) {
437438
t.Errorf("Organizations.GetRepositoriesForCodeSecurityConfiguration returned %+v, want %+v", repositories, want)
438439
}
439440

@@ -478,7 +479,7 @@ func TestOrganizationsService_GetCodeSecurityConfigurationForRepository(t *testi
478479
State: Ptr("attached"),
479480
Configuration: c,
480481
}
481-
if !reflect.DeepEqual(rc, want) {
482+
if !cmp.Equal(rc, want) {
482483
t.Errorf("Organizations.GetCodeSecurityConfigurationForRepository returned %+v, want %+v", rc, want)
483484
}
484485

github/packages_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ package github
77

88
import (
99
"encoding/json"
10-
"reflect"
1110
"testing"
11+
12+
"github.com/google/go-cmp/cmp"
1213
)
1314

1415
func TestPackageRegistry_Marshal(t *testing.T) {
@@ -556,7 +557,7 @@ func TestPackageVersion_GetBodyAsPackageVersionBody(t *testing.T) {
556557

557558
resValue, resOk := test.pv.GetBodyAsPackageVersionBody()
558559

559-
if !reflect.DeepEqual(resValue, test.wantValue) || resOk != test.wantOk {
560+
if !cmp.Equal(resValue, test.wantValue) || resOk != test.wantOk {
560561
t.Errorf("PackageVersion.GetBodyAsPackageVersionBody() - got: %v, %v; want: %v, %v", resValue, resOk, test.wantValue, test.wantOk)
561562
}
562563
})
@@ -615,7 +616,7 @@ func TestPackageVersion_GetMetadata(t *testing.T) {
615616

616617
resValue, resOk := test.pv.GetMetadata()
617618

618-
if !reflect.DeepEqual(resValue, test.wantValue) || resOk != test.wantOk {
619+
if !cmp.Equal(resValue, test.wantValue) || resOk != test.wantOk {
619620
t.Errorf("PackageVersion.GetMetadata() - got: %v, %v; want: %v, %v", resValue, resOk, test.wantValue, test.wantOk)
620621
}
621622
})

github/repos_deployment_branch_policies_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import (
99
"context"
1010
"fmt"
1111
"net/http"
12-
"reflect"
1312
"testing"
13+
14+
"github.com/google/go-cmp/cmp"
1415
)
1516

1617
func TestRepositoriesService_ListDeploymentBranchPolicies(t *testing.T) {
@@ -34,7 +35,7 @@ func TestRepositoriesService_ListDeploymentBranchPolicies(t *testing.T) {
3435
},
3536
TotalCount: Ptr(2),
3637
}
37-
if !reflect.DeepEqual(got, want) {
38+
if !cmp.Equal(got, want) {
3839
t.Errorf("Repositories.ListDeploymentBranchPolicies = %+v, want %+v", got, want)
3940
}
4041

@@ -63,7 +64,7 @@ func TestRepositoriesService_GetDeploymentBranchPolicy(t *testing.T) {
6364
}
6465

6566
want := &DeploymentBranchPolicy{ID: Ptr(int64(1))}
66-
if !reflect.DeepEqual(got, want) {
67+
if !cmp.Equal(got, want) {
6768
t.Errorf("Repositories.GetDeploymentBranchPolicy = %+v, want %+v", got, want)
6869
}
6970

@@ -93,7 +94,7 @@ func TestRepositoriesService_CreateDeploymentBranchPolicy(t *testing.T) {
9394
}
9495

9596
want := &DeploymentBranchPolicy{ID: Ptr(int64(1)), Type: Ptr("branch")}
96-
if !reflect.DeepEqual(got, want) {
97+
if !cmp.Equal(got, want) {
9798
t.Errorf("Repositories.CreateDeploymentBranchPolicy = %+v, want %+v", got, want)
9899
}
99100

@@ -123,7 +124,7 @@ func TestRepositoriesService_UpdateDeploymentBranchPolicy(t *testing.T) {
123124
}
124125

125126
want := &DeploymentBranchPolicy{ID: Ptr(int64(1))}
126-
if !reflect.DeepEqual(got, want) {
127+
if !cmp.Equal(got, want) {
127128
t.Errorf("Repositories.UpdateDeploymentBranchPolicy = %+v, want %+v", got, want)
128129
}
129130

github/repos_deployment_protection_rules_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"encoding/json"
1111
"fmt"
1212
"net/http"
13-
"reflect"
1413
"testing"
1514

1615
"github.com/google/go-cmp/cmp"
@@ -38,7 +37,7 @@ func TestRepositoriesService_GetAllDeploymentProtectionRules(t *testing.T) {
3837
},
3938
TotalCount: Ptr(2),
4039
}
41-
if !reflect.DeepEqual(got, want) {
40+
if !cmp.Equal(got, want) {
4241
t.Errorf("Repositories.GetAllDeploymentProtectionRules = %+v, want %+v", got, want)
4342
}
4443

@@ -66,7 +65,7 @@ func TestRepositoriesService_CreateCustomDeploymentProtectionRule(t *testing.T)
6665

6766
testMethod(t, r, "POST")
6867
want := input
69-
if !reflect.DeepEqual(v, want) {
68+
if !cmp.Equal(v, want) {
7069
t.Errorf("Request body = %+v, want %+v", v, want)
7170
}
7271

@@ -90,7 +89,7 @@ func TestRepositoriesService_CreateCustomDeploymentProtectionRule(t *testing.T)
9089
IntegrationURL: Ptr("https://api.github.com/apps/a-custom-app"),
9190
},
9291
}
93-
if !reflect.DeepEqual(got, want) {
92+
if !cmp.Equal(got, want) {
9493
t.Errorf("Repositories.CreateCustomDeploymentProtectionRule = %+v, want %+v", got, want)
9594
}
9695

@@ -131,7 +130,7 @@ func TestRepositoriesService_ListCustomDeploymentRuleIntegrations(t *testing.T)
131130
{ID: Ptr(int64(2)), NodeID: Ptr("UHVE67RlcGxveW1lbnRTdTY!jfeuy"), Slug: Ptr("another-custom-app"), IntegrationURL: Ptr("https://api.github.com/apps/another-custom-app")},
132131
},
133132
}
134-
if !reflect.DeepEqual(got, want) {
133+
if !cmp.Equal(got, want) {
135134
t.Errorf("Repositories.ListCustomDeploymentRuleIntegrations = %+v, want %+v", got, want)
136135
}
137136

@@ -172,7 +171,7 @@ func TestRepositoriesService_GetCustomDeploymentProtectionRule(t *testing.T) {
172171
},
173172
}
174173

175-
if !reflect.DeepEqual(got, want) {
174+
if !cmp.Equal(got, want) {
176175
t.Errorf("Repositories.GetCustomDeploymentProtectionRule = %+v, want %+v", got, want)
177176
}
178177

github/repos_hooks_deliveries_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"encoding/json"
1111
"fmt"
1212
"net/http"
13-
"reflect"
1413
"testing"
1514

1615
"github.com/google/go-cmp/cmp"
@@ -227,7 +226,7 @@ func TestHookDelivery_ParsePayload(t *testing.T) {
227226
t.Error(err)
228227
}
229228

230-
if !reflect.DeepEqual(obj, got) {
229+
if !cmp.Equal(obj, got) {
231230
t.Errorf("want %T %v, got %T %v", obj, obj, got, got)
232231
}
233232
})

0 commit comments

Comments
 (0)