Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: enable errorf of perfsprint linter #21280

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linters:
- gomodguard
- gosimple
- govet
- importas
- ineffassign
- misspell
- perfsprint
Expand Down Expand Up @@ -47,13 +48,17 @@ linters-settings:
- github.com/pkg/errors:
recommendations:
- errors
importas:
alias:
- alias: stderrors
pkg: errors
perfsprint:
# Optimizes even if it requires an int or uint type cast.
int-conversion: true
# Optimizes into `err.Error()` even if it is only equivalent for non-nil errors.
err-error: true
# Optimizes `fmt.Errorf`.
errorf: false
errorf: true
# Optimizes `fmt.Sprintf` with only one argument.
sprintf1: true
# Optimizes into strings concatenation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controllers
import (
"context"
"encoding/json"
"errors"
"fmt"
"strconv"
"testing"
Expand Down Expand Up @@ -1864,7 +1865,7 @@ func TestRequeueGeneratorFails(t *testing.T) {
generatorMock.On("GetTemplate", &generator).
Return(&v1alpha1.ApplicationSetTemplate{})
generatorMock.On("GenerateParams", &generator, mock.AnythingOfType("*v1alpha1.ApplicationSet"), mock.Anything).
Return([]map[string]interface{}{}, fmt.Errorf("Simulated error generating params that could be related to an external service/API call"))
Return([]map[string]interface{}{}, errors.New("Simulated error generating params that could be related to an external service/API call"))

metrics := appsetmetrics.NewFakeAppsetMetrics(client)

Expand Down Expand Up @@ -1969,7 +1970,7 @@ func TestValidateGeneratedApplications(t *testing.T) {
},
},
},
validationErrors: map[int]error{0: fmt.Errorf("application destination spec is invalid: application destination can't have both name and server defined: my-cluster my-server")},
validationErrors: map[int]error{0: errors.New("application destination spec is invalid: application destination can't have both name and server defined: my-cluster my-server")},
},
{
name: "project mismatch should return error",
Expand All @@ -1991,7 +1992,7 @@ func TestValidateGeneratedApplications(t *testing.T) {
},
},
},
validationErrors: map[int]error{0: fmt.Errorf("application references project DOES-NOT-EXIST which does not exist")},
validationErrors: map[int]error{0: errors.New("application references project DOES-NOT-EXIST which does not exist")},
},
{
name: "valid app should return true",
Expand Down Expand Up @@ -2035,7 +2036,7 @@ func TestValidateGeneratedApplications(t *testing.T) {
},
},
},
validationErrors: map[int]error{0: fmt.Errorf("application destination spec is invalid: unable to find destination server: there are no clusters with this name: nonexistent-cluster")},
validationErrors: map[int]error{0: errors.New("application destination spec is invalid: unable to find destination server: there are no clusters with this name: nonexistent-cluster")},
},
} {
t.Run(cc.name, func(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions applicationset/controllers/template/template_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package template

import (
"fmt"
"errors"
"maps"
"testing"

Expand Down Expand Up @@ -53,7 +53,7 @@ func TestGenerateApplications(t *testing.T) {
},
{
name: "Handles error from the generator",
generateParamsError: fmt.Errorf("error"),
generateParamsError: errors.New("error"),
expectErr: true,
expectedReason: v1alpha1.ApplicationSetReasonApplicationParamsGenerationError,
},
Expand All @@ -68,7 +68,7 @@ func TestGenerateApplications(t *testing.T) {
},
Spec: v1alpha1.ApplicationSpec{},
},
rendererError: fmt.Errorf("error"),
rendererError: errors.New("error"),
expectErr: true,
expectedReason: v1alpha1.ApplicationSetReasonRenderTemplateParamsError,
},
Expand Down
8 changes: 4 additions & 4 deletions applicationset/generators/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package generators

import (
"context"
"fmt"
"errors"
"testing"

corev1 "k8s.io/api/core/v1"
Expand All @@ -27,7 +27,7 @@ type possiblyErroringFakeCtrlRuntimeClient struct {

func (p *possiblyErroringFakeCtrlRuntimeClient) List(ctx context.Context, secretList client.ObjectList, opts ...client.ListOption) error {
if p.shouldError {
return fmt.Errorf("could not list Secrets")
return errors.New("could not list Secrets")
}
return p.Client.List(ctx, secretList, opts...)
}
Expand Down Expand Up @@ -227,7 +227,7 @@ func TestGenerateParams(t *testing.T) {
values: nil,
expected: nil,
clientError: true,
expectedError: fmt.Errorf("error getting cluster secrets: could not list Secrets"),
expectedError: errors.New("error getting cluster secrets: could not list Secrets"),
},
{
name: "flat mode without selectors",
Expand Down Expand Up @@ -677,7 +677,7 @@ func TestGenerateParamsGoTemplate(t *testing.T) {
values: nil,
expected: nil,
clientError: true,
expectedError: fmt.Errorf("error getting cluster secrets: could not list Secrets"),
expectedError: errors.New("error getting cluster secrets: could not list Secrets"),
},
{
name: "Clusters with flat list mode and no selector",
Expand Down
7 changes: 4 additions & 3 deletions applicationset/generators/duck_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package generators

import (
"context"
"errors"
"fmt"
"strings"
"time"
Expand Down Expand Up @@ -96,13 +97,13 @@ func (g *DuckTypeGenerator) GenerateParams(appSetGenerator *argoprojiov1alpha1.A
// Validate the fields
if kind == "" || versionIdx < 1 {
log.Warningf("kind=%v, resourceName=%v, versionIdx=%v", kind, resourceName, versionIdx)
return nil, fmt.Errorf("There is a problem with the apiVersion, kind or resourceName provided")
return nil, errors.New("There is a problem with the apiVersion, kind or resourceName provided")
}

if (resourceName == "" && labelSelector.MatchLabels == nil && labelSelector.MatchExpressions == nil) ||
(resourceName != "" && (labelSelector.MatchExpressions != nil || labelSelector.MatchLabels != nil)) {
log.Warningf("You must choose either resourceName=%v, labelSelector.matchLabels=%v or labelSelect.matchExpressions=%v", resourceName, labelSelector.MatchLabels, labelSelector.MatchExpressions)
return nil, fmt.Errorf("There is a problem with the definition of the ClusterDecisionResource generator")
return nil, errors.New("There is a problem with the definition of the ClusterDecisionResource generator")
}

// Split up the apiVersion
Expand Down Expand Up @@ -130,7 +131,7 @@ func (g *DuckTypeGenerator) GenerateParams(appSetGenerator *argoprojiov1alpha1.A

if len(duckResources.Items) == 0 {
log.Warning("no resource found, make sure you clusterDecisionResource is defined correctly")
return nil, fmt.Errorf("no clusterDecisionResources found")
return nil, errors.New("no clusterDecisionResources found")
}

// Override the duck type in the status of the resource
Expand Down
10 changes: 5 additions & 5 deletions applicationset/generators/duck_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package generators

import (
"context"
"fmt"
"errors"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -160,7 +160,7 @@ func TestGenerateParamsForDuckType(t *testing.T) {
resource: duckType,
values: nil,
expected: []map[string]interface{}{},
expectedError: fmt.Errorf("There is a problem with the definition of the ClusterDecisionResource generator"),
expectedError: errors.New("There is a problem with the definition of the ClusterDecisionResource generator"),
},
/*** This does not work with the FAKE runtime client, fieldSelectors are broken.
{
Expand Down Expand Up @@ -271,7 +271,7 @@ func TestGenerateParamsForDuckType(t *testing.T) {
resource: duckType,
values: nil,
expected: nil,
expectedError: fmt.Errorf("There is a problem with the definition of the ClusterDecisionResource generator"),
expectedError: errors.New("There is a problem with the definition of the ClusterDecisionResource generator"),
},
}

Expand Down Expand Up @@ -456,7 +456,7 @@ func TestGenerateParamsForDuckTypeGoTemplate(t *testing.T) {
resource: duckType,
values: nil,
expected: []map[string]interface{}{},
expectedError: fmt.Errorf("There is a problem with the definition of the ClusterDecisionResource generator"),
expectedError: errors.New("There is a problem with the definition of the ClusterDecisionResource generator"),
},
/*** This does not work with the FAKE runtime client, fieldSelectors are broken.
{
Expand Down Expand Up @@ -567,7 +567,7 @@ func TestGenerateParamsForDuckTypeGoTemplate(t *testing.T) {
resource: duckType,
values: nil,
expected: nil,
expectedError: fmt.Errorf("There is a problem with the definition of the ClusterDecisionResource generator"),
expectedError: errors.New("There is a problem with the definition of the ClusterDecisionResource generator"),
},
}

Expand Down
23 changes: 12 additions & 11 deletions applicationset/generators/git_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generators

import (
"errors"
"fmt"
"testing"

Expand Down Expand Up @@ -307,9 +308,9 @@ func TestGitGenerateParamsFromDirectories(t *testing.T) {
name: "handles error from repo server",
directories: []argoprojiov1alpha1.GitDirectoryGeneratorItem{{Path: "*"}},
repoApps: []string{},
repoError: fmt.Errorf("error"),
repoError: errors.New("error"),
expected: []map[string]interface{}{},
expectedError: fmt.Errorf("error generating params from git: error getting directories from repo: error"),
expectedError: errors.New("error generating params from git: error getting directories from repo: error"),
},
}

Expand Down Expand Up @@ -608,9 +609,9 @@ func TestGitGenerateParamsFromDirectoriesGoTemplate(t *testing.T) {
name: "handles error from repo server",
directories: []argoprojiov1alpha1.GitDirectoryGeneratorItem{{Path: "*"}},
repoApps: []string{},
repoError: fmt.Errorf("error"),
repoError: errors.New("error"),
expected: []map[string]interface{}{},
expectedError: fmt.Errorf("error generating params from git: error getting directories from repo: error"),
expectedError: errors.New("error generating params from git: error getting directories from repo: error"),
},
}

Expand Down Expand Up @@ -808,9 +809,9 @@ func TestGitGenerateParamsFromFiles(t *testing.T) {
name: "handles error during getting repo paths",
files: []argoprojiov1alpha1.GitFileGeneratorItem{{Path: "**/config.json"}},
repoFileContents: map[string][]byte{},
repoPathsError: fmt.Errorf("paths error"),
repoPathsError: errors.New("paths error"),
expected: []map[string]interface{}{},
expectedError: fmt.Errorf("error generating params from git: paths error"),
expectedError: errors.New("error generating params from git: paths error"),
},
{
name: "test invalid JSON file returns error",
Expand All @@ -820,7 +821,7 @@ func TestGitGenerateParamsFromFiles(t *testing.T) {
},
repoPathsError: nil,
expected: []map[string]interface{}{},
expectedError: fmt.Errorf("error generating params from git: unable to process file 'cluster-config/production/config.json': unable to parse file: error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go value of type map[string]interface {}"),
expectedError: errors.New("error generating params from git: unable to process file 'cluster-config/production/config.json': unable to parse file: error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go value of type map[string]interface {}"),
},
{
name: "test JSON array",
Expand Down Expand Up @@ -1120,9 +1121,9 @@ func TestGitGenerateParamsFromFilesGoTemplate(t *testing.T) {
name: "handles error during getting repo paths",
files: []argoprojiov1alpha1.GitFileGeneratorItem{{Path: "**/config.json"}},
repoFileContents: map[string][]byte{},
repoPathsError: fmt.Errorf("paths error"),
repoPathsError: errors.New("paths error"),
expected: []map[string]interface{}{},
expectedError: fmt.Errorf("error generating params from git: paths error"),
expectedError: errors.New("error generating params from git: paths error"),
},
{
name: "test invalid JSON file returns error",
Expand All @@ -1132,7 +1133,7 @@ func TestGitGenerateParamsFromFilesGoTemplate(t *testing.T) {
},
repoPathsError: nil,
expected: []map[string]interface{}{},
expectedError: fmt.Errorf("error generating params from git: unable to process file 'cluster-config/production/config.json': unable to parse file: error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go value of type map[string]interface {}"),
expectedError: errors.New("error generating params from git: unable to process file 'cluster-config/production/config.json': unable to parse file: error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go value of type map[string]interface {}"),
},
{
name: "test JSON array",
Expand Down Expand Up @@ -1464,7 +1465,7 @@ func TestGitGenerator_GenerateParams(t *testing.T) {
},
callGetDirectories: false,
expected: []map[string]interface{}{{"path": "app1", "path.basename": "app1", "path.basenameNormalized": "app1", "path[0]": "app1", "values.foo": "bar"}},
expectedError: fmt.Errorf("error getting project project: appprojects.argoproj.io \"project\" not found"),
expectedError: errors.New("error getting project project: appprojects.argoproj.io \"project\" not found"),
},
}
for _, testCase := range cases {
Expand Down
4 changes: 2 additions & 2 deletions applicationset/generators/interface.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package generators

import (
"fmt"
"errors"
"time"

"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -27,7 +27,7 @@ type Generator interface {
}

var (
EmptyAppSetGeneratorError = fmt.Errorf("ApplicationSet is empty")
EmptyAppSetGeneratorError = errors.New("ApplicationSet is empty")
NoRequeueAfter time.Duration
)

Expand Down
3 changes: 2 additions & 1 deletion applicationset/generators/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package generators

import (
"encoding/json"
"errors"
"fmt"
"time"

Expand Down Expand Up @@ -54,7 +55,7 @@ func (g *ListGenerator) GenerateParams(appSetGenerator *argoprojiov1alpha1.Appli
if key == "values" {
values, ok := (value).(map[string]interface{})
if !ok {
return nil, fmt.Errorf("error parsing values map")
return nil, errors.New("error parsing values map")
}
for k, v := range values {
value, ok := v.(string)
Expand Down
9 changes: 5 additions & 4 deletions applicationset/generators/matrix.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generators

import (
"errors"
"fmt"
"time"

Expand All @@ -16,9 +17,9 @@ import (
var _ Generator = (*MatrixGenerator)(nil)

var (
ErrMoreThanTwoGenerators = fmt.Errorf("found more than two generators, Matrix support only two")
ErrLessThanTwoGenerators = fmt.Errorf("found less than two generators, Matrix support only two")
ErrMoreThenOneInnerGenerators = fmt.Errorf("found more than one generator in matrix.Generators")
ErrMoreThanTwoGenerators = errors.New("found more than two generators, Matrix support only two")
ErrLessThanTwoGenerators = errors.New("found less than two generators, Matrix support only two")
ErrMoreThenOneInnerGenerators = errors.New("found more than one generator in matrix.Generators")
)

type MatrixGenerator struct {
Expand Down Expand Up @@ -125,7 +126,7 @@ func (m *MatrixGenerator) getParams(appSetBaseGenerator argoprojiov1alpha1.Appli
}

if len(t) == 0 {
return nil, fmt.Errorf("child generator generated no parameters")
return nil, errors.New("child generator generated no parameters")
}

if len(t) > 1 {
Expand Down
9 changes: 5 additions & 4 deletions applicationset/generators/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package generators

import (
"encoding/json"
"errors"
"fmt"
"time"

Expand All @@ -17,9 +18,9 @@ import (
var _ Generator = (*MergeGenerator)(nil)

var (
ErrLessThanTwoGeneratorsInMerge = fmt.Errorf("found less than two generators, Merge requires two or more")
ErrNoMergeKeys = fmt.Errorf("no merge keys were specified, Merge requires at least one")
ErrNonUniqueParamSets = fmt.Errorf("the parameters from a generator were not unique by the given mergeKeys, Merge requires all param sets to be unique")
ErrLessThanTwoGeneratorsInMerge = errors.New("found less than two generators, Merge requires two or more")
ErrNoMergeKeys = errors.New("no merge keys were specified, Merge requires at least one")
ErrNonUniqueParamSets = errors.New("the parameters from a generator were not unique by the given mergeKeys, Merge requires all param sets to be unique")
)

type MergeGenerator struct {
Expand Down Expand Up @@ -182,7 +183,7 @@ func (m *MergeGenerator) getParams(appSetBaseGenerator argoprojiov1alpha1.Applic
}

if len(t) == 0 {
return nil, fmt.Errorf("child generator generated no parameters")
return nil, errors.New("child generator generated no parameters")
}

if len(t) > 1 {
Expand Down
Loading
Loading