Skip to content

Commit

Permalink
chore(tests): assert.Error->require.Error (pkg directory)
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 27, 2024
1 parent 5d8ee22 commit 953edcf
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 154 deletions.
6 changes: 3 additions & 3 deletions pkg/apiclient/http1/facade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestFacade_do(t *testing.T) {
f := Facade{baseUrl: "http://my-url"}
u, err := f.url("GET", "/{namespace}/{name}", &metav1.ObjectMeta{Namespace: "my-ns", Labels: map[string]string{"foo": "1"}})
if assert.NoError(t, err) {
assert.Equal(t, "http://my-url/my-ns/?labels.foo=1", u.String())
}
require.NoError(t, err)
assert.Equal(t, "http://my-url/my-ns/?labels.foo=1", u.String())
}
79 changes: 40 additions & 39 deletions pkg/apis/workflow/v1alpha1/anystring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,90 +5,91 @@ import (
"testing"

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

func TestAnyString(t *testing.T) {
t.Run("Empty", func(t *testing.T) {
x := AnyStringPtr("")
data, err := json.Marshal(x)
if assert.NoError(t, err) {
assert.Equal(t, `""`, string(data), "string value has quotes")
}
require.NoError(t, err)
assert.Equal(t, `""`, string(data), "string value has quotes")

i := AnyStringPtr("")
err = i.UnmarshalJSON([]byte(`""`))
if assert.NoError(t, err) {
assert.Equal(t, AnyStringPtr(""), i)
}
require.NoError(t, err)
assert.Equal(t, AnyStringPtr(""), i)

assert.Equal(t, "", i.String(), "string value does not have quotes")
})
t.Run("String", func(t *testing.T) {
x := AnyStringPtr("my-string")
data, err := json.Marshal(x)
if assert.NoError(t, err) {
assert.Equal(t, `"my-string"`, string(data), "string value has quotes")
}
require.NoError(t, err)
assert.Equal(t, `"my-string"`, string(data), "string value has quotes")

i := AnyStringPtr("")
err = i.UnmarshalJSON([]byte(`"my-string"`))
if assert.NoError(t, err) {
assert.Equal(t, AnyStringPtr("my-string"), i)
}
require.NoError(t, err)
assert.Equal(t, AnyStringPtr("my-string"), i)

assert.Equal(t, "my-string", i.String(), "string value does not have quotes")
})
t.Run("StringNumber", func(t *testing.T) {
x := AnyStringPtr(1)
data, err := json.Marshal(x)
if assert.NoError(t, err) {
assert.Equal(t, `"1"`, string(data), "number value has quotes")
}
require.NoError(t, err)
assert.Equal(t, `"1"`, string(data), "number value has quotes")

i := AnyStringPtr("")
err = i.UnmarshalJSON([]byte(`"1"`))
if assert.NoError(t, err) {
assert.Equal(t, AnyStringPtr("1"), i)
}
require.NoError(t, err)
assert.Equal(t, AnyStringPtr("1"), i)

assert.Equal(t, "1", i.String(), "number value does not have quotes")
})
t.Run("LargeNumber", func(t *testing.T) {
x := ParseAnyString(881217801864)
data, err := json.Marshal(x)
if assert.NoError(t, err) {
assert.Equal(t, `"881217801864"`, string(data))
}
require.NoError(t, err)
assert.Equal(t, `"881217801864"`, string(data))

i := AnyStringPtr("")
err = i.UnmarshalJSON([]byte("881217801864"))
if assert.NoError(t, err) {
assert.Equal(t, AnyStringPtr("881217801864"), i)
}
require.NoError(t, err)
assert.Equal(t, AnyStringPtr("881217801864"), i)

assert.Equal(t, "881217801864", i.String())
})
t.Run("FloatNumber", func(t *testing.T) {
x := ParseAnyString(0.2)
data, err := json.Marshal(x)
if assert.NoError(t, err) {
assert.Equal(t, `"0.2"`, string(data))
}
require.NoError(t, err)
assert.Equal(t, `"0.2"`, string(data))

i := AnyStringPtr("")
err = i.UnmarshalJSON([]byte("0.2"))
if assert.NoError(t, err) {
assert.Equal(t, AnyStringPtr("0.2"), i)
}
require.NoError(t, err)
assert.Equal(t, AnyStringPtr("0.2"), i)

assert.Equal(t, "0.2", i.String())
})
t.Run("Boolean", func(t *testing.T) {
x := ParseAnyString(true)
data, err := json.Marshal(x)
if assert.NoError(t, err) {
assert.Equal(t, `"true"`, string(data))
}
require.NoError(t, err)
assert.Equal(t, `"true"`, string(data))

x = ParseAnyString(false)
data, err = json.Marshal(x)
if assert.NoError(t, err) {
assert.Equal(t, `"false"`, string(data))
}
require.NoError(t, err)
assert.Equal(t, `"false"`, string(data))

i := AnyStringPtr("")
err = i.UnmarshalJSON([]byte("true"))
if assert.NoError(t, err) {
assert.Equal(t, AnyStringPtr("true"), i)
}
require.NoError(t, err)
assert.Equal(t, AnyStringPtr("true"), i)

assert.Equal(t, "true", i.String())
})
}
28 changes: 10 additions & 18 deletions pkg/apis/workflow/v1alpha1/container_set_template_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/wait"
Expand All @@ -29,7 +30,7 @@ func TestContainerSetGetRetryStrategy(t *testing.T) {
},
}
strategy, err := set.GetRetryStrategy()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, wait.Backoff{Steps: 100}, strategy)
})

Expand All @@ -43,7 +44,7 @@ func TestContainerSetGetRetryStrategy(t *testing.T) {
},
}
strategy, err := set.GetRetryStrategy()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, wait.Backoff{
Steps: 100,
Duration: time.Duration(20 * time.Second),
Expand Down Expand Up @@ -86,9 +87,7 @@ volumeMounts:
mountPath: /workspace
`
err := validateContainerSetTemplate(invalidContainerSetEmpty)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "containers must have at least one container")
}
require.ErrorContains(t, err, "containers must have at least one container")
}

func TestInvalidContainerSetDuplicateVolumeMounting(t *testing.T) {
Expand All @@ -103,9 +102,7 @@ containers:
image: argoproj/argosay:v2
`
err := validateContainerSetTemplate(invalidContainerSetDuplicateNames)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "volumeMounts[1].mountPath '/workspace' already mounted in volumeMounts.workspace")
}
require.ErrorContains(t, err, "volumeMounts[1].mountPath '/workspace' already mounted in volumeMounts.workspace")
}

func TestInvalidContainerSetDuplicateNames(t *testing.T) {
Expand All @@ -120,9 +117,8 @@ containers:
image: argoproj/argosay:v2
`
err := validateContainerSetTemplate(invalidContainerSetDuplicateNames)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "containers[1].name 'a' is not unique")
}
require.ErrorContains(t, err, "containers[1].name 'a' is not unique")

}

func TestInvalidContainerSetDependencyNotFound(t *testing.T) {
Expand All @@ -139,9 +135,7 @@ containers:
- c
`
err := validateContainerSetTemplate(invalidContainerSetDependencyNotFound)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "containers.b dependency 'c' not defined")
}
require.ErrorContains(t, err, "containers.b dependency 'c' not defined")
}

func TestInvalidContainerSetDependencyCycle(t *testing.T) {
Expand All @@ -160,9 +154,7 @@ containers:
- a
`
err := validateContainerSetTemplate(invalidContainerSetDependencyCycle)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "containers dependency cycle detected: b->a->b")
}
require.ErrorContains(t, err, "containers dependency cycle detected: b->a->b")
}

func TestValidContainerSet(t *testing.T) {
Expand All @@ -188,5 +180,5 @@ containers:
- c
`
err := validateContainerSetTemplate(validContainerSet)
assert.NoError(t, err)
require.NoError(t, err)
}
5 changes: 3 additions & 2 deletions pkg/apis/workflow/v1alpha1/item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

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

func TestItem(t *testing.T) {
Expand All @@ -29,10 +30,10 @@ func TestItem(t *testing.T) {

func runItemTest(t *testing.T, data string, expectedType Type) {
itm, err := ParseItem(data)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, expectedType, itm.GetType())
jsonBytes, err := json.Marshal(itm)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, data, string(jsonBytes), "marshalling is symmetric")
if strings.HasPrefix(data, `"`) {
assert.Equal(t, data, fmt.Sprintf("\"%v\"", itm))
Expand Down
8 changes: 4 additions & 4 deletions pkg/apis/workflow/v1alpha1/plugin_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ package v1alpha1
import (
"testing"

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

func TestPlugin_UnmarshalJSON(t *testing.T) {
t.Run("Invalid", func(t *testing.T) {
p := Plugin{}
assert.EqualError(t, p.UnmarshalJSON([]byte(`1`)), "json: cannot unmarshal number into Go value of type map[string]interface {}")
require.EqualError(t, p.UnmarshalJSON([]byte(`1`)), "json: cannot unmarshal number into Go value of type map[string]interface {}")
})
t.Run("NoKeys", func(t *testing.T) {
p := Plugin{}
assert.EqualError(t, p.UnmarshalJSON([]byte(`{}`)), "expected exactly one key, got 0")
require.EqualError(t, p.UnmarshalJSON([]byte(`{}`)), "expected exactly one key, got 0")
})
t.Run("OneKey", func(t *testing.T) {
p := Plugin{}
assert.NoError(t, p.UnmarshalJSON([]byte(`{"foo":1}`)))
require.NoError(t, p.UnmarshalJSON([]byte(`{"foo":1}`)))
})
}
10 changes: 5 additions & 5 deletions pkg/apis/workflow/v1alpha1/validation_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

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

func TestValidateWorkflowFieldNames(t *testing.T) {
Expand Down Expand Up @@ -55,9 +55,9 @@ func TestValidateWorkflowFieldNames(t *testing.T) {
t.Run(name, func(t *testing.T) {
err := validateWorkflowFieldNames(tc.names, tc.isParamOrArtifact)
if tc.expectedErr != nil {
assert.EqualError(t, err, tc.expectedErr.Error())
require.EqualError(t, err, tc.expectedErr.Error())
} else {
assert.NoError(t, err)
require.NoError(t, err)
}
})
}
Expand Down Expand Up @@ -101,9 +101,9 @@ func TestVerifyNoCycles(t *testing.T) {
t.Run(name, func(t *testing.T) {
err := validateNoCycles(tc.depGraph)
if tc.expectedErr != nil {
assert.Errorf(t, err, tc.expectedErr.Error())
require.Errorf(t, err, tc.expectedErr.Error())
} else {
assert.NoError(t, err)
require.NoError(t, err)
}
})
}
Expand Down
Loading

0 comments on commit 953edcf

Please sign in to comment.