Skip to content

Commit

Permalink
Merge pull request #8322 from mmorel-35/golangci-lint/contains
Browse files Browse the repository at this point in the history
fix: use Contains or ErrorContains with testify
  • Loading branch information
reasonerjt authored Oct 21, 2024
2 parents 732b87b + d06601e commit c53ab20
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pkg/client/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func TestFactory(t *testing.T) {
LabelSelector: "none",
},
)
assert.Contains(t, e.Error(), fmt.Sprintf("Get \"%s/apis/%s/%s/namespaces/%s", test.expectedHost, resource.Group, resource.Version, namespace))
assert.ErrorContains(t, e, fmt.Sprintf("Get \"%s/apis/%s/%s/namespaces/%s", test.expectedHost, resource.Group, resource.Version, namespace))
assert.Nil(t, list)
assert.NotNil(t, dynamicClient)

Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/cli/backup/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ func TestCreateCommand(t *testing.T) {

//Validate
e = o.Validate(cmd, args, f)
require.Contains(t, e.Error(), "include-resources, exclude-resources and include-cluster-resources are old filter parameters")
require.Contains(t, e.Error(), "include-cluster-scoped-resources, exclude-cluster-scoped-resources, include-namespace-scoped-resources and exclude-namespace-scoped-resources are new filter parameters.\nThey cannot be used together")
require.ErrorContains(t, e, "include-resources, exclude-resources and include-cluster-resources are old filter parameters")
require.ErrorContains(t, e, "include-cluster-scoped-resources, exclude-cluster-scoped-resources, include-namespace-scoped-resources and exclude-namespace-scoped-resources are new filter parameters.\nThey cannot be used together")

//cmd
e = o.Run(cmd, f)
Expand Down Expand Up @@ -292,7 +292,7 @@ func TestCreateCommand(t *testing.T) {

// Validate
e = o.Validate(cmd, args, f)
assert.Contains(t, e.Error(), fmt.Sprintf("backupstoragelocations.velero.io \"%s\" not found", bsl))
assert.ErrorContains(t, e, fmt.Sprintf("backupstoragelocations.velero.io \"%s\" not found", bsl))
})

t.Run("create a backup create command with specific volume-snapshot-locations setting", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/cli/backuplocation/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func TestCreateCommand_Run(t *testing.T) {
assert.NoError(t, e)

e = o.Run(c, f)
assert.Contains(t, e.Error(), fmt.Sprintf("%s: no such file or directory", caCertFile))
assert.ErrorContains(t, e, fmt.Sprintf("%s: no such file or directory", caCertFile))

// verify all options are set as expected
assert.Equal(t, name, o.Name)
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/cli/backuplocation/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ func TestDeleteFunctions(t *testing.T) {
bkList.Items = append(bkList.Items, bk)
errList := deleteBackups(kbclient, bkList)
assert.Len(t, errList, 1)
assert.Contains(t, errList[0].Error(), fmt.Sprintf("delete backup \"%s\" associated with deleted BSL: backups.velero.io \"%s\" not found", bk.Name, bk.Name))
assert.ErrorContains(t, errList[0], fmt.Sprintf("delete backup \"%s\" associated with deleted BSL: backups.velero.io \"%s\" not found", bk.Name, bk.Name))
})
t.Run("deleteBackupRepos", func(t *testing.T) {
bkrepo := velerov1api.BackupRepository{}
bkrepo.Name = "bk-repo-name-last"
bkrepoList.Items = append(bkrepoList.Items, bkrepo)
errList := deleteBackupRepos(kbclient, bkrepoList)
assert.Len(t, errList, 1)
assert.Contains(t, errList[0].Error(), fmt.Sprintf("delete backup repository \"%s\" associated with deleted BSL: backuprepositories.velero.io \"%s\" not found", bkrepo.Name, bkrepo.Name))
assert.ErrorContains(t, errList[0], fmt.Sprintf("delete backup repository \"%s\" associated with deleted BSL: backuprepositories.velero.io \"%s\" not found", bkrepo.Name, bkrepo.Name))
})
}
4 changes: 2 additions & 2 deletions pkg/cmd/cli/backuplocation/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ func TestNewSetCommand(t *testing.T) {
assert.NoError(t, e)

e = o.Run(c, f)
assert.Contains(t, e.Error(), fmt.Sprintf("%s: no such file or directory", cacert))
assert.ErrorContains(t, e, fmt.Sprintf("%s: no such file or directory", cacert))

// verify all options are set as expected
assert.Equal(t, backupName, o.Name)
assert.Equal(t, cacert, o.CACertFile)
assert.Equal(t, defaultBackupStorageLocation, boolptr.IsSetToTrue(o.DefaultBackupStorageLocation.Value))
assert.True(t, reflect.DeepEqual(credential, o.Credential))

assert.Contains(t, e.Error(), fmt.Sprintf("%s: no such file or directory", cacert))
assert.ErrorContains(t, e, fmt.Sprintf("%s: no such file or directory", cacert))
}

func TestSetCommand_Execute(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/cli/restore/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func TestCreateCommand(t *testing.T) {

//Validate
e = o.Validate(cmd, args, f)
require.Contains(t, e.Error(), "either a backup or schedule must be specified, but not both")
require.ErrorContains(t, e, "either a backup or schedule must be specified, but not both")

//cmd
e = o.Run(cmd, f)
Expand Down
3 changes: 1 addition & 2 deletions pkg/datapath/micro_service_watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"io"
"os"
"path"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -617,7 +616,7 @@ func TestRedirectDataMoverLogs(t *testing.T) {
} else {
assert.NoError(t, err)

assert.True(t, strings.Contains(buffer, test.logMessage))
assert.Contains(t, buffer, test.logMessage)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/repository/maintenance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ func TestGetMaintenanceJobConfig(t *testing.T) {
)

if tc.expectedError != nil {
require.Contains(t, err.Error(), tc.expectedError.Error())
require.ErrorContains(t, err, tc.expectedError.Error())
} else {
require.NoError(t, err)
}
Expand Down

0 comments on commit c53ab20

Please sign in to comment.