Skip to content

Commit

Permalink
chore(tests): assert.Error->require.Error (util 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 26, 2024
1 parent 879d23a commit bfa8bc2
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 149 deletions.
56 changes: 28 additions & 28 deletions util/archive/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"testing"

log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func tempFile(dir, prefix, suffix string) (*os.File, error) {
Expand Down Expand Up @@ -67,22 +67,22 @@ func TestTarDirectory(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f, err := tempFile(os.TempDir()+"/argo-test", "dir-"+tt.name+"-", ".tgz")
assert.NoError(t, err)
require.NoError(t, err)

log.Infof("Taring to %s", f.Name())

err = TarGzToWriter(tt.src, tt.level, bufio.NewWriter(f))
if tt.wantErr {
assert.Error(t, err)
require.Error(t, err)
} else {
assert.NoError(t, err)
require.NoError(t, err)
}

err = f.Close()
assert.NoError(t, err)
require.NoError(t, err)

err = os.Remove(f.Name())
assert.NoError(t, err)
require.NoError(t, err)
})
}
}
Expand Down Expand Up @@ -112,31 +112,31 @@ func TestTarFile(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, err := tempFile(os.TempDir()+"/argo-test", "file-"+tt.name+"-", "")
assert.NoError(t, err)
require.NoError(t, err)
_, err = data.WriteString("hello world")
assert.NoError(t, err)
require.NoError(t, err)
err = data.Close()
assert.NoError(t, err)
require.NoError(t, err)

dataTarPath := data.Name() + ".tgz"
f, err := os.Create(dataTarPath)
assert.NoError(t, err)
require.NoError(t, err)

log.Infof("Taring to %s", f.Name())

err = TarGzToWriter(data.Name(), tt.level, bufio.NewWriter(f))
if tt.wantErr {
assert.Error(t, err)
require.Error(t, err)
} else {
assert.NoError(t, err)
require.NoError(t, err)
}

err = os.Remove(data.Name())
assert.NoError(t, err)
require.NoError(t, err)
err = f.Close()
assert.NoError(t, err)
require.NoError(t, err)
err = os.Remove(f.Name())
assert.NoError(t, err)
require.NoError(t, err)
})
}
}
Expand All @@ -161,47 +161,47 @@ func TestZipDirectory(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f, err := tempFile(os.TempDir()+"/argo-test", "dir-"+tt.name+"-", ".tgz")
assert.NoError(t, err)
require.NoError(t, err)

log.Infof("Zipping to %s", f.Name())

err = ZipToWriter(tt.src, zip.NewWriter(f))
if tt.wantErr {
assert.Error(t, err)
require.Error(t, err)
} else {
assert.NoError(t, err)
require.NoError(t, err)
}

err = f.Close()
assert.NoError(t, err)
require.NoError(t, err)

err = os.Remove(f.Name())
assert.NoError(t, err)
require.NoError(t, err)
})
}
}

func TestZipFile(t *testing.T) {
t.Run("test_zip_file", func(t *testing.T) {
data, err := tempFile(os.TempDir()+"/argo-test", "file-random-", "")
assert.NoError(t, err)
require.NoError(t, err)
_, err = data.WriteString("hello world")
assert.NoError(t, err)
require.NoError(t, err)
err = data.Close()
assert.NoError(t, err)
require.NoError(t, err)

dataZipPath := data.Name() + ".zip"
f, err := os.Create(dataZipPath)
assert.NoError(t, err)
require.NoError(t, err)

err = ZipToWriter(data.Name(), zip.NewWriter(f))
assert.NoError(t, err)
require.NoError(t, err)

err = os.Remove(data.Name())
assert.NoError(t, err)
require.NoError(t, err)
err = f.Close()
assert.NoError(t, err)
require.NoError(t, err)
err = os.Remove(f.Name())
assert.NoError(t, err)
require.NoError(t, err)
})
}
11 changes: 5 additions & 6 deletions util/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
authorizationv1 "k8s.io/api/authorization/v1"
"k8s.io/apimachinery/pkg/runtime"
kubefake "k8s.io/client-go/kubernetes/fake"
Expand All @@ -27,11 +28,9 @@ func TestCanI(t *testing.T) {

ctx := context.Background()
allowed, err := CanI(ctx, kubeClient, "get", "workflow", "", "")
if assert.NoError(t, err) {
assert.True(t, allowed)
}
require.NoError(t, err)
assert.True(t, allowed)
notAllowed, err := CanI(ctx, kubeClient, "list", "workflow", "", "")
if assert.NoError(t, err) {
assert.False(t, notAllowed)
}
require.NoError(t, err)
assert.False(t, notAllowed)
}
7 changes: 4 additions & 3 deletions util/fields/fields_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 Down Expand Up @@ -48,15 +49,15 @@ func TestCleaner_WithPrefix(t *testing.T) {
func TestCleanNoop(t *testing.T) {
var wf, cleanWf wfv1.Workflow
ok, err := NewCleaner("").Clean(wf, cleanWf)
assert.NoError(t, err)
require.NoError(t, err)
assert.False(t, ok)
}

func TestCleanFields(t *testing.T) {
var wf, cleanWf wfv1.Workflow
wfv1.MustUnmarshal([]byte(sampleWorkflow), &wf)
ok, err := NewCleaner("status.phase,metadata.name,spec.entrypoint").Clean(wf, &cleanWf)
assert.NoError(t, err)
require.NoError(t, err)
assert.True(t, ok)
assert.Equal(t, wfv1.WorkflowSucceeded, cleanWf.Status.Phase)
assert.Equal(t, "whalesay", cleanWf.Spec.Entrypoint)
Expand All @@ -68,7 +69,7 @@ func TestCleanFieldsExclude(t *testing.T) {
var wf, cleanWf wfv1.Workflow
wfv1.MustUnmarshal([]byte(sampleWorkflow), &wf)
ok, err := NewCleaner("-status.phase,metadata.name,spec.entrypoint").Clean(wf, &cleanWf)
assert.NoError(t, err)
require.NoError(t, err)
assert.True(t, ok)
assert.Empty(t, cleanWf.Status.Phase)
assert.Empty(t, cleanWf.Spec.Entrypoint)
Expand Down
14 changes: 8 additions & 6 deletions util/file/fileutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

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

"github.com/argoproj/argo-workflows/v3/util/file"
)
Expand Down Expand Up @@ -37,12 +38,12 @@ func TestGetGzipReader(t *testing.T) {
rawContent := "this is the content"
content := file.CompressEncodeString(rawContent)
buf, err := base64.StdEncoding.DecodeString(content)
assert.NoError(t, err)
require.NoError(t, err)
bufReader := bytes.NewReader(buf)
reader, err := file.GetGzipReader(bufReader)
assert.NoError(t, err)
require.NoError(t, err)
res, err := io.ReadAll(reader)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, rawContent, string(res))
}
}
Expand All @@ -63,12 +64,13 @@ func TestExistsInTar(t *testing.T) {
}
hdr := tar.Header{Name: f.name, Mode: int64(mode), Size: int64(len(f.body))}
err := writer.WriteHeader(&hdr)
assert.NoError(t, err)
require.NoError(t, err)
_, err = writer.Write([]byte(f.body))
assert.NoError(t, err)
require.NoError(t, err)
}
err := writer.Close()
assert.NoError(t, err)
require.NoError(t, err)

return tar.NewReader(&buf)
}

Expand Down
11 changes: 6 additions & 5 deletions util/instanceid/service_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"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
Expand Down Expand Up @@ -50,14 +51,14 @@ func TestWith(t *testing.T) {
func TestValidate(t *testing.T) {
t.Run("NoInstanceID", func(t *testing.T) {
s := NewService("")
assert.NoError(t, s.Validate(&wfv1.Workflow{}))
assert.Error(t, s.Validate(&wfv1.Workflow{ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{common.LabelKeyControllerInstanceID: "bar"}}}))
require.NoError(t, s.Validate(&wfv1.Workflow{}))
require.Error(t, s.Validate(&wfv1.Workflow{ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{common.LabelKeyControllerInstanceID: "bar"}}}))
})
t.Run("InstanceID", func(t *testing.T) {
s := NewService("foo")
assert.Error(t, s.Validate(&wfv1.Workflow{}))
assert.Error(t, s.Validate(&wfv1.Workflow{ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{common.LabelKeyControllerInstanceID: "bar"}}}))
assert.NoError(t, s.Validate(&wfv1.Workflow{ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{common.LabelKeyControllerInstanceID: "foo"}}}))
require.Error(t, s.Validate(&wfv1.Workflow{}))
require.Error(t, s.Validate(&wfv1.Workflow{ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{common.LabelKeyControllerInstanceID: "bar"}}}))
require.NoError(t, s.Validate(&wfv1.Workflow{ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{common.LabelKeyControllerInstanceID: "foo"}}}))
})
}

Expand Down
25 changes: 13 additions & 12 deletions util/intstr/parametrizable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,57 @@ import (
"testing"

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

func TestInt(t *testing.T) {
i, err := Int(ParsePtr("2"))
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, 2, *i)

i, err = Int(ParsePtr("-1"))
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, -1, *i)

_, err = Int(ParsePtr("{{argo.variable}}"))
assert.Error(t, err)
require.Error(t, err)

i, err = Int(nil)
assert.NoError(t, err)
require.NoError(t, err)
assert.Nil(t, i)
}

func TestInt32(t *testing.T) {
i, err := Int32(ParsePtr("2"))
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, int32(2), *i)

i, err = Int32(ParsePtr("-1"))
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, int32(-1), *i)

_, err = Int32(ParsePtr("{{argo.variable}}"))
assert.Error(t, err)
require.Error(t, err)

i, err = Int32(nil)
assert.NoError(t, err)
require.NoError(t, err)
assert.Nil(t, i)
}

func TestInt64(t *testing.T) {
i, err := Int64(ParsePtr("2"))
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, int64(2), *i)

i, err = Int64(ParsePtr("-1"))
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, int64(-1), *i)

_, err = Int64(ParsePtr("{{argo.variable}}"))
assert.Error(t, err)
require.Error(t, err)

i, err = Int64(nil)
assert.NoError(t, err)
require.NoError(t, err)
assert.Nil(t, i)
}

Expand Down
18 changes: 9 additions & 9 deletions util/kubeconfig/kubeconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/client-go/tools/clientcmd"
)

Expand Down Expand Up @@ -34,9 +35,9 @@ users:
func Test_BasicAuthString(t *testing.T) {
t.Run("Basic Auth", func(t *testing.T) {
restConfig, err := clientcmd.RESTConfigFromKubeConfig([]byte(config))
assert.NoError(t, err)
require.NoError(t, err)
authString, err := GetAuthString(restConfig, "")
assert.NoError(t, err)
require.NoError(t, err)
assert.True(t, IsBasicAuthScheme(authString))
token := strings.TrimSpace(strings.TrimPrefix(authString, BasicAuthScheme))
uname, pwd, ok := decodeBasicAuthToken(token)
Expand All @@ -45,16 +46,15 @@ func Test_BasicAuthString(t *testing.T) {
assert.Equal(t, "admin", pwd)
}
file, err := os.CreateTemp("", "config.yaml")
assert.NoError(t, err)
require.NoError(t, err)
_, err = file.WriteString(config)
assert.NoError(t, err)
require.NoError(t, err)
err = file.Close()
assert.NoError(t, err)
require.NoError(t, err)
t.Setenv("KUBECONFIG", file.Name())
config, err := GetRestConfig(authString)
if assert.NoError(t, err) {
assert.Equal(t, "admin", config.Username)
assert.Equal(t, "admin", config.Password)
}
require.NoError(t, err)
assert.Equal(t, "admin", config.Username)
assert.Equal(t, "admin", config.Password)
})
}
Loading

0 comments on commit bfa8bc2

Please sign in to comment.