Skip to content

Commit

Permalink
chore(tests): assert.Error->require.Error (other unit tests)
Browse files Browse the repository at this point in the history
This is part of a series of test tidies started by #13365.

The aim is to enable the testifylint golangci-lint checker.

This commit converts assert.Error checks into require.Error for the
part of the codebase, as per #13270 (comment)

In some places checks have been coaleced - in particular the pattern

```go
if assert.Error() {
    assert.Contains(..., "message")
}
```

is now
```go
require.ErrorContains(..., "message")
```

Getting this wrong and missing the Contains is still valid go, so
that's a mistake I may have made.

Signed-off-by: Alan Clucas <[email protected]>
  • Loading branch information
Joibel committed Jul 26, 2024
1 parent 879d23a commit e4ec409
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 27 deletions.
5 changes: 3 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
)
Expand All @@ -25,9 +26,9 @@ func TestSanitize(t *testing.T) {
for _, tt := range tests {
err := tt.c.Sanitize([]string{"http", "https"})
if tt.err != "" {
assert.Equal(t, err.Error(), tt.err)
require.EqualError(t, err, tt.err)
} else {
assert.NoError(t, err)
require.NoError(t, err)
}
}
}
10 changes: 5 additions & 5 deletions config/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
apiv1 "k8s.io/api/core/v1"
)

func Test_parseConfigMap(t *testing.T) {
t.Run("Empty", func(t *testing.T) {
c := &Config{}
err := parseConfigMap(&apiv1.ConfigMap{}, c)
assert.NoError(t, err)
require.NoError(t, err)
})
t.Run("Complex", func(t *testing.T) {
c := &Config{}
Expand All @@ -26,13 +27,12 @@ func Test_parseConfigMap(t *testing.T) {
secretKeySecret:
name: my-minio-cred
key: secretkey`}}, c)
if assert.NoError(t, err) {
assert.NotEmpty(t, c.ArtifactRepository)
}
require.NoError(t, err)
assert.NotEmpty(t, c.ArtifactRepository)
})
t.Run("Garbage", func(t *testing.T) {
c := &Config{}
err := parseConfigMap(&apiv1.ConfigMap{Data: map[string]string{"garbage": "garbage"}}, c)
assert.Error(t, err)
require.Error(t, err)
})
}
16 changes: 7 additions & 9 deletions config/ttl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,26 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestTTL(t *testing.T) {
t.Run("Empty", func(t *testing.T) {
ttl := TTL(-1)
err := ttl.UnmarshalJSON([]byte(`""`))
if assert.NoError(t, err) {
assert.Equal(t, TTL(0), ttl)
}
require.NoError(t, err)
assert.Equal(t, TTL(0), ttl)
})
t.Run("1h", func(t *testing.T) {
ttl := TTL(-1)
err := ttl.UnmarshalJSON([]byte(`"1h"`))
if assert.NoError(t, err) {
assert.Equal(t, TTL(1*time.Hour), ttl)
}
require.NoError(t, err)
assert.Equal(t, TTL(1*time.Hour), ttl)
})
t.Run("1d", func(t *testing.T) {
ttl := TTL(-1)
err := ttl.UnmarshalJSON([]byte(`"1d"`))
if assert.NoError(t, err) {
assert.Equal(t, TTL(24*time.Hour), ttl)
}
require.NoError(t, err)
assert.Equal(t, TTL(24*time.Hour), ttl)
})
}
6 changes: 3 additions & 3 deletions persist/sqldb/archived_workflow_labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/upper/db/v4"
"k8s.io/apimachinery/pkg/labels"
)
Expand Down Expand Up @@ -32,9 +33,8 @@ func Test_labelsClause(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
for _, req := range tt.requirements {
got, err := requirementToCondition(tt.dbType, req, archiveTableName, archiveLabelsTableName, true)
if assert.NoError(t, err) {
assert.Equal(t, tt.want, *got)
}
require.NoError(t, err)
assert.Equal(t, tt.want, *got)
}
})
}
Expand Down
15 changes: 7 additions & 8 deletions persist/sqldb/offload_node_status_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
)

func Test_nodeStatusVersion(t *testing.T) {
t.Run("Empty", func(t *testing.T) {
marshalled, version, err := nodeStatusVersion(nil)
if assert.NoError(t, err) {
assert.NotEmpty(t, marshalled)
assert.Equal(t, "fnv:784127654", version)
}
require.NoError(t, err)
assert.NotEmpty(t, marshalled)
assert.Equal(t, "fnv:784127654", version)
})
t.Run("NonEmpty", func(t *testing.T) {
marshalled, version, err := nodeStatusVersion(wfv1.Nodes{"my-node": wfv1.NodeStatus{}})
if assert.NoError(t, err) {
assert.NotEmpty(t, marshalled)
assert.Equal(t, "fnv:2308444803", version)
}
require.NoError(t, err)
assert.NotEmpty(t, marshalled)
assert.Equal(t, "fnv:2308444803", version)
})
}

0 comments on commit e4ec409

Please sign in to comment.