From 790f4e24dc974a6c2bc8c17fb148510d5867f236 Mon Sep 17 00:00:00 2001 From: jorge-ferrero-ag Date: Mon, 18 Aug 2025 23:59:48 +0200 Subject: [PATCH 1/2] chore: Replace reflect.DeepEqual with cmp.Equal in tests Updated multiple test files to use github.com/google/go-cmp/cmp.Equal instead of reflect.DeepEqual for value comparisons. This improves test output readability and consistency across the codebase. --- github/github_test.go | 2 +- .../orgs_codesecurity_configurations_test.go | 25 ++++++++++--------- github/packages_test.go | 7 +++--- .../repos_deployment_branch_policies_test.go | 11 ++++---- .../repos_deployment_protection_rules_test.go | 11 ++++---- github/repos_hooks_deliveries_test.go | 3 +-- 6 files changed, 30 insertions(+), 29 deletions(-) diff --git a/github/github_test.go b/github/github_test.go index ff0fa16a4a3..21a913e068c 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -3181,7 +3181,7 @@ func TestPtr(t *testing.T) { t.Parallel() equal := func(t *testing.T, want, got any) { t.Helper() - if !reflect.DeepEqual(want, got) { + if !cmp.Equal(want, got) { t.Errorf("want %#v, got %#v", want, got) } } diff --git a/github/orgs_codesecurity_configurations_test.go b/github/orgs_codesecurity_configurations_test.go index 188e31fbcfc..d62a3534462 100644 --- a/github/orgs_codesecurity_configurations_test.go +++ b/github/orgs_codesecurity_configurations_test.go @@ -10,8 +10,9 @@ import ( "encoding/json" "fmt" "net/http" - "reflect" "testing" + + "github.com/google/go-cmp/cmp" ) func TestOrganizationsService_GetCodeSecurityConfigurations(t *testing.T) { @@ -43,7 +44,7 @@ func TestOrganizationsService_GetCodeSecurityConfigurations(t *testing.T) { {ID: Ptr(int64(1)), Name: Ptr("config1"), CodeScanningDefaultSetup: Ptr("enabled")}, {ID: Ptr(int64(2)), Name: Ptr("config2"), PrivateVulnerabilityReporting: Ptr("enabled")}, } - if !reflect.DeepEqual(configurations, want) { + if !cmp.Equal(configurations, want) { t.Errorf("Organizations.GetCodeSecurityConfigurations returned %+v, want %+v", configurations, want) } const methodName = "GetCodeSecurityConfigurations" @@ -80,7 +81,7 @@ func TestOrganizationsService_GetCodeSecurityConfiguration(t *testing.T) { } want := &CodeSecurityConfiguration{ID: Ptr(int64(1)), Name: Ptr("config1"), CodeScanningDefaultSetup: Ptr("enabled")} - if !reflect.DeepEqual(configuration, want) { + if !cmp.Equal(configuration, want) { t.Errorf("Organizations.GetCodeSecurityConfiguration returned %+v, want %+v", configuration, want) } @@ -113,7 +114,7 @@ func TestOrganizationsService_CreateCodeSecurityConfiguration(t *testing.T) { v := new(CodeSecurityConfiguration) assertNilError(t, json.NewDecoder(r.Body).Decode(v)) - if !reflect.DeepEqual(v, input) { + if !cmp.Equal(v, input) { t.Errorf("Organizations.CreateCodeSecurityConfiguration request body = %+v, want %+v", v, input) } @@ -130,7 +131,7 @@ func TestOrganizationsService_CreateCodeSecurityConfiguration(t *testing.T) { } want := &CodeSecurityConfiguration{ID: Ptr(int64(1)), Name: Ptr("config1"), CodeScanningDefaultSetup: Ptr("enabled")} - if !reflect.DeepEqual(configuration, want) { + if !cmp.Equal(configuration, want) { t.Errorf("Organizations.CreateCodeSecurityConfiguration returned %+v, want %+v", configuration, want) } @@ -178,7 +179,7 @@ func TestOrganizationsService_GetDefaultCodeSecurityConfigurations(t *testing.T) {ID: Ptr(int64(1)), Name: Ptr("config1"), CodeScanningDefaultSetup: Ptr("enabled")}, {ID: Ptr(int64(2)), Name: Ptr("config2"), PrivateVulnerabilityReporting: Ptr("enabled")}, } - if !reflect.DeepEqual(configurations, want) { + if !cmp.Equal(configurations, want) { t.Errorf("Organizations.GetDefaultCodeSecurityConfigurations returned %+v, want %+v", configurations, want) } @@ -243,7 +244,7 @@ func TestOrganizationsService_UpdateCodeSecurityConfiguration(t *testing.T) { v := new(CodeSecurityConfiguration) assertNilError(t, json.NewDecoder(r.Body).Decode(v)) - if !reflect.DeepEqual(v, input) { + if !cmp.Equal(v, input) { t.Errorf("Organizations.UpdateCodeSecurityConfiguration request body = %+v, want %+v", v, input) } @@ -260,7 +261,7 @@ func TestOrganizationsService_UpdateCodeSecurityConfiguration(t *testing.T) { } want := &CodeSecurityConfiguration{ID: Ptr(int64(1)), Name: Ptr("config1"), CodeScanningDefaultSetup: Ptr("enabled")} - if !reflect.DeepEqual(configuration, want) { + if !cmp.Equal(configuration, want) { t.Errorf("Organizations.UpdateCodeSecurityConfiguration returned %+v, want %+v", configuration, want) } @@ -327,7 +328,7 @@ func TestOrganizationsService_AttachCodeSecurityConfigurationsToRepositories(t * if v.Scope != "selected" { t.Errorf("Organizations.AttachCodeSecurityConfigurationsToRepositories request body scope = %s, want selected", v.Scope) } - if !reflect.DeepEqual(v.SelectedRepositoryIDs, []int64{5, 20}) { + if !cmp.Equal(v.SelectedRepositoryIDs, []int64{5, 20}) { t.Errorf("Organizations.AttachCodeSecurityConfigurationsToRepositories request body selected_repository_ids = %+v, want %+v", v.SelectedRepositoryIDs, []int64{5, 20}) } w.WriteHeader(http.StatusAccepted) @@ -387,7 +388,7 @@ func TestOrganizationsService_SetDefaultCodeSecurityConfiguration(t *testing.T) ID: Ptr(int64(1)), Name: Ptr("config1"), CodeScanningDefaultSetup: Ptr("enabled"), }, } - if !reflect.DeepEqual(got, want) { + if !cmp.Equal(got, want) { t.Errorf("Organizations.SetDefaultCodeSecurityConfiguration returned %+v, want %+v", got, want) } @@ -433,7 +434,7 @@ func TestOrganizationsService_GetRepositoriesForCodeSecurityConfiguration(t *tes {ID: Ptr(int64(8)), Name: Ptr("repo8")}, {ID: Ptr(int64(42)), Name: Ptr("repo42")}, } - if !reflect.DeepEqual(repositories, want) { + if !cmp.Equal(repositories, want) { t.Errorf("Organizations.GetRepositoriesForCodeSecurityConfiguration returned %+v, want %+v", repositories, want) } @@ -478,7 +479,7 @@ func TestOrganizationsService_GetCodeSecurityConfigurationForRepository(t *testi State: Ptr("attached"), Configuration: c, } - if !reflect.DeepEqual(rc, want) { + if !cmp.Equal(rc, want) { t.Errorf("Organizations.GetCodeSecurityConfigurationForRepository returned %+v, want %+v", rc, want) } diff --git a/github/packages_test.go b/github/packages_test.go index fa72bfcceea..ba453b55750 100644 --- a/github/packages_test.go +++ b/github/packages_test.go @@ -7,8 +7,9 @@ package github import ( "encoding/json" - "reflect" "testing" + + "github.com/google/go-cmp/cmp" ) func TestPackageRegistry_Marshal(t *testing.T) { @@ -556,7 +557,7 @@ func TestPackageVersion_GetBodyAsPackageVersionBody(t *testing.T) { resValue, resOk := test.pv.GetBodyAsPackageVersionBody() - if !reflect.DeepEqual(resValue, test.wantValue) || resOk != test.wantOk { + if !cmp.Equal(resValue, test.wantValue) || resOk != test.wantOk { t.Errorf("PackageVersion.GetBodyAsPackageVersionBody() - got: %v, %v; want: %v, %v", resValue, resOk, test.wantValue, test.wantOk) } }) @@ -615,7 +616,7 @@ func TestPackageVersion_GetMetadata(t *testing.T) { resValue, resOk := test.pv.GetMetadata() - if !reflect.DeepEqual(resValue, test.wantValue) || resOk != test.wantOk { + if !cmp.Equal(resValue, test.wantValue) || resOk != test.wantOk { t.Errorf("PackageVersion.GetMetadata() - got: %v, %v; want: %v, %v", resValue, resOk, test.wantValue, test.wantOk) } }) diff --git a/github/repos_deployment_branch_policies_test.go b/github/repos_deployment_branch_policies_test.go index 4eb9eab7820..f64dbf0c85e 100644 --- a/github/repos_deployment_branch_policies_test.go +++ b/github/repos_deployment_branch_policies_test.go @@ -9,8 +9,9 @@ import ( "context" "fmt" "net/http" - "reflect" "testing" + + "github.com/google/go-cmp/cmp" ) func TestRepositoriesService_ListDeploymentBranchPolicies(t *testing.T) { @@ -34,7 +35,7 @@ func TestRepositoriesService_ListDeploymentBranchPolicies(t *testing.T) { }, TotalCount: Ptr(2), } - if !reflect.DeepEqual(got, want) { + if !cmp.Equal(got, want) { t.Errorf("Repositories.ListDeploymentBranchPolicies = %+v, want %+v", got, want) } @@ -63,7 +64,7 @@ func TestRepositoriesService_GetDeploymentBranchPolicy(t *testing.T) { } want := &DeploymentBranchPolicy{ID: Ptr(int64(1))} - if !reflect.DeepEqual(got, want) { + if !cmp.Equal(got, want) { t.Errorf("Repositories.GetDeploymentBranchPolicy = %+v, want %+v", got, want) } @@ -93,7 +94,7 @@ func TestRepositoriesService_CreateDeploymentBranchPolicy(t *testing.T) { } want := &DeploymentBranchPolicy{ID: Ptr(int64(1)), Type: Ptr("branch")} - if !reflect.DeepEqual(got, want) { + if !cmp.Equal(got, want) { t.Errorf("Repositories.CreateDeploymentBranchPolicy = %+v, want %+v", got, want) } @@ -123,7 +124,7 @@ func TestRepositoriesService_UpdateDeploymentBranchPolicy(t *testing.T) { } want := &DeploymentBranchPolicy{ID: Ptr(int64(1))} - if !reflect.DeepEqual(got, want) { + if !cmp.Equal(got, want) { t.Errorf("Repositories.UpdateDeploymentBranchPolicy = %+v, want %+v", got, want) } diff --git a/github/repos_deployment_protection_rules_test.go b/github/repos_deployment_protection_rules_test.go index d17f0695a0c..dffb8beedf9 100644 --- a/github/repos_deployment_protection_rules_test.go +++ b/github/repos_deployment_protection_rules_test.go @@ -10,7 +10,6 @@ import ( "encoding/json" "fmt" "net/http" - "reflect" "testing" "github.com/google/go-cmp/cmp" @@ -38,7 +37,7 @@ func TestRepositoriesService_GetAllDeploymentProtectionRules(t *testing.T) { }, TotalCount: Ptr(2), } - if !reflect.DeepEqual(got, want) { + if !cmp.Equal(got, want) { t.Errorf("Repositories.GetAllDeploymentProtectionRules = %+v, want %+v", got, want) } @@ -66,7 +65,7 @@ func TestRepositoriesService_CreateCustomDeploymentProtectionRule(t *testing.T) testMethod(t, r, "POST") want := input - if !reflect.DeepEqual(v, want) { + if !cmp.Equal(v, want) { t.Errorf("Request body = %+v, want %+v", v, want) } @@ -90,7 +89,7 @@ func TestRepositoriesService_CreateCustomDeploymentProtectionRule(t *testing.T) IntegrationURL: Ptr("https://api.github.com/apps/a-custom-app"), }, } - if !reflect.DeepEqual(got, want) { + if !cmp.Equal(got, want) { t.Errorf("Repositories.CreateCustomDeploymentProtectionRule = %+v, want %+v", got, want) } @@ -131,7 +130,7 @@ func TestRepositoriesService_ListCustomDeploymentRuleIntegrations(t *testing.T) {ID: Ptr(int64(2)), NodeID: Ptr("UHVE67RlcGxveW1lbnRTdTY!jfeuy"), Slug: Ptr("another-custom-app"), IntegrationURL: Ptr("https://api.github.com/apps/another-custom-app")}, }, } - if !reflect.DeepEqual(got, want) { + if !cmp.Equal(got, want) { t.Errorf("Repositories.ListCustomDeploymentRuleIntegrations = %+v, want %+v", got, want) } @@ -172,7 +171,7 @@ func TestRepositoriesService_GetCustomDeploymentProtectionRule(t *testing.T) { }, } - if !reflect.DeepEqual(got, want) { + if !cmp.Equal(got, want) { t.Errorf("Repositories.GetCustomDeploymentProtectionRule = %+v, want %+v", got, want) } diff --git a/github/repos_hooks_deliveries_test.go b/github/repos_hooks_deliveries_test.go index b3f8d7d570a..e0a8955f018 100644 --- a/github/repos_hooks_deliveries_test.go +++ b/github/repos_hooks_deliveries_test.go @@ -10,7 +10,6 @@ import ( "encoding/json" "fmt" "net/http" - "reflect" "testing" "github.com/google/go-cmp/cmp" @@ -227,7 +226,7 @@ func TestHookDelivery_ParsePayload(t *testing.T) { t.Error(err) } - if !reflect.DeepEqual(obj, got) { + if !cmp.Equal(obj, got) { t.Errorf("want %T %v, got %T %v", obj, obj, got, got) } }) From e7ab55371b05a5d7ad15c08b064efd13bff165a1 Mon Sep 17 00:00:00 2001 From: jorge-ferrero-ag Date: Tue, 19 Aug 2025 09:26:55 +0200 Subject: [PATCH 2/2] Add forbidigo linter to enforce cmp.Equal usage Configured the forbidigo linter in .golangci.yml to forbid the use of reflect.DeepEqual, recommending cmp.Equal instead. This helps ensure more reliable equality checks in the codebase. --- .golangci.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.golangci.yml b/.golangci.yml index 80c21f9d99b..2b2f0368202 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -8,6 +8,7 @@ linters: - copyloopvar - dogsled - dupl + - forbidigo - gocritic - godot - goheader @@ -40,6 +41,10 @@ linters: Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. + forbidigo: + forbid: + - pattern: ^reflect\.DeepEqual$ + msg: "Use cmp.Equal instead of reflect.DeepEqual" gosec: excludes: # duplicates errcheck