diff --git a/.golangci.yml b/.golangci.yml index 2446e07f6..b7a7db08a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -5,6 +5,13 @@ run: issues: exclude-dirs: - vendor +linters: + enable: + - testifylint linters-settings: goimports: local-prefixes: github.com/argoproj/pkg + testifylint: + enable-all: true + disable: + - float-compare diff --git a/exec/exec_test.go b/exec/exec_test.go index 987fe861a..18a2f64ec 100644 --- a/exec/exec_test.go +++ b/exec/exec_test.go @@ -9,6 +9,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/sirupsen/logrus/hooks/test" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestRunCommand(t *testing.T) { @@ -17,7 +18,7 @@ func TestRunCommand(t *testing.T) { defer log.SetLevel(log.InfoLevel) message, err := RunCommand("echo", CmdOpts{Redactor: Redact([]string{"world"})}, "hello world") - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, "hello world", message) assert.Len(t, hook.Entries, 2) @@ -41,8 +42,8 @@ func TestRunCommandTimeout(t *testing.T) { defer log.SetLevel(log.InfoLevel) output, err := RunCommand("sh", CmdOpts{Timeout: 500 * time.Millisecond}, "-c", "echo hello world && echo my-error >&2 && sleep 2") - assert.Equal(t, output, "hello world") - assert.EqualError(t, err, "`sh -c echo hello world && echo my-error >&2 && sleep 2` failed timeout after 500ms") + assert.Equal(t, "hello world", output) + require.EqualError(t, err, "`sh -c echo hello world && echo my-error >&2 && sleep 2` failed timeout after 500ms") assert.Len(t, hook.Entries, 3) @@ -72,14 +73,14 @@ func TestRunCommandSignal(t *testing.T) { var timeoutBehavior = TimeoutBehavior{Signal: syscall.SIGTERM, ShouldWait: true} output, err := RunCommand("sh", CmdOpts{Timeout: 200 * time.Millisecond, TimeoutBehavior: timeoutBehavior}, "-c", "trap 'trap - 15 && echo captured && exit' 15 && sleep 2") assert.Equal(t, "captured", output) - assert.EqualError(t, err, "`sh -c trap 'trap - 15 && echo captured && exit' 15 && sleep 2` failed timeout after 200ms") + require.EqualError(t, err, "`sh -c trap 'trap - 15 && echo captured && exit' 15 && sleep 2` failed timeout after 200ms") assert.Len(t, hook.Entries, 3) } func TestTrimmedOutput(t *testing.T) { message, err := RunCommand("printf", CmdOpts{}, "hello world") - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, "hello world", message) } @@ -90,7 +91,7 @@ func TestRunCommandExitErr(t *testing.T) { output, err := RunCommand("sh", CmdOpts{Redactor: Redact([]string{"world"})}, "-c", "echo hello world && echo my-error >&2 && exit 1") assert.Equal(t, "hello world", output) - assert.EqualError(t, err, "`sh -c echo hello ****** && echo my-error >&2 && exit 1` failed exit status 1: my-error") + require.EqualError(t, err, "`sh -c echo hello ****** && echo my-error >&2 && exit 1` failed exit status 1: my-error") assert.Len(t, hook.Entries, 3) @@ -125,7 +126,7 @@ func TestRunInDir(t *testing.T) { cmd := exec.Command("pwd") cmd.Dir = "/" message, err := RunCommandExt(cmd, CmdOpts{}) - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, "/", message) } diff --git a/file/file_test.go b/file/file_test.go index c661aa6e7..90fbbff81 100644 --- a/file/file_test.go +++ b/file/file_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/argoproj/pkg/rand" ) @@ -20,22 +21,22 @@ func TestIsDirectory(t *testing.T) { testDir := filepath.Dir(filename) isDir, err := IsDirectory(testDir) - assert.Nil(t, err) + require.NoError(t, err) assert.True(t, isDir) isDir, err = IsDirectory(filename) - assert.Nil(t, err) + require.NoError(t, err) assert.False(t, isDir) isDir, err = IsDirectory("doesnt-exist") - assert.NotNil(t, err) + require.Error(t, err) assert.False(t, isDir) } func TestExists(t *testing.T) { assert.True(t, Exists("/")) path, err := rand.RandString(10) - assert.NoError(t, err) + require.NoError(t, err) randFilePath := fmt.Sprintf("/%s", path) assert.False(t, Exists(randFilePath)) } diff --git a/grpc/http/forwarders_test.go b/grpc/http/forwarders_test.go index 40a6a603e..8d6d9b2dd 100644 --- a/grpc/http/forwarders_test.go +++ b/grpc/http/forwarders_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) type testStruct struct { @@ -40,8 +41,8 @@ func TestMarshalerIncludeFields(t *testing.T) { out, err := m.Marshal(testVal) - assert.Nil(t, err) - assert.Equal(t, `{"metadata":{"name":"test"},"spec":{"source":{"path":"test_path"}}}`, string(out)) + require.NoError(t, err) + assert.JSONEq(t, `{"metadata":{"name":"test"},"spec":{"source":{"path":"test_path"}}}`, string(out)) } func TestMarshalerExcludeFields(t *testing.T) { @@ -51,8 +52,8 @@ func TestMarshalerExcludeFields(t *testing.T) { out, err := m.Marshal(testVal) - assert.Nil(t, err) - assert.Equal(t, `{"metadata":{},"spec":{"source":{"path":"test_path"}},"status":{"message":"Failed"}}`, string(out)) + require.NoError(t, err) + assert.JSONEq(t, `{"metadata":{},"spec":{"source":{"path":"test_path"}},"status":{"message":"Failed"}}`, string(out)) } func TestMarshalerSSE(t *testing.T) { @@ -60,7 +61,7 @@ func TestMarshalerSSE(t *testing.T) { out, err := m.Marshal(testVal) - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, `data: {"metadata":{"name":"test"},"spec":{"source":{"path":"test_path"}},"status":{"message":"Failed"}} `, string(out)) @@ -89,7 +90,7 @@ func TestFlushSuccess(t *testing.T) { f := flusher{w: bufio.NewWriter(&buf)} flush(&f) - assert.Equal(t, true, flushed) + assert.True(t, flushed) } func TestFlushFailed(t *testing.T) { @@ -98,5 +99,5 @@ func TestFlushFailed(t *testing.T) { f := flusher{} flush(&f) - assert.Equal(t, false, flushed) + assert.False(t, flushed) } diff --git a/json/json_test.go b/json/json_test.go index 434c28ded..2bbccdf99 100644 --- a/json/json_test.go +++ b/json/json_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) // TestDisallowUnknownFields tests ability to disallow unknown fields @@ -20,17 +21,17 @@ func TestDisallowUnknownFields(t *testing.T) { var obj mystruct err := Unmarshal(jsonWithUnknownField, &obj) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, "foo", obj.MyField) obj = mystruct{} err = Unmarshal(jsonWithUnknownField, &obj, DisallowUnknownFields) - assert.Error(t, err) + require.Error(t, err) assert.Equal(t, "foo", obj.MyField) obj = mystruct{} err = UnmarshalStrict(jsonWithUnknownField, &obj) - assert.Error(t, err) + require.Error(t, err) assert.Equal(t, "foo", obj.MyField) } diff --git a/jwt/zjwt/zjwt_test.go b/jwt/zjwt/zjwt_test.go index fb986ee75..7e5810d7c 100644 --- a/jwt/zjwt/zjwt_test.go +++ b/jwt/zjwt/zjwt_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func init() { @@ -14,9 +15,9 @@ func TestCompactor(t *testing.T) { t.Run("Small", func(t *testing.T) { jwt := `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.EpM5XBzTJZ4J8AfoJEcJrjth8pfH28LWdjLo90sYb9g` compactJWT, err := ZJWT(jwt) - assert.NoError(t, err) + require.NoError(t, err) expandedJWT, err := JWT(compactJWT) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, jwt, expandedJWT) assert.Equal(t, jwt, compactJWT) assert.Equal(t, 100, 100*len(compactJWT)/len(jwt)) @@ -24,9 +25,9 @@ func TestCompactor(t *testing.T) { t.Run("Large", func(t *testing.T) { jwt := `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJncm91cHMiOlsiU0JHIiwiU0JHOk1lbWJlcnMiLCJTQkc6dWkiLCJTQkc6emlvbiIsIlNCRzpoYXJtb255LXV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CJVWC1JbmZyYTpDb3JlIiwiZGVzaWduLXN5c3RlbXMiXX0.XDozAEiz9AIVkA3DFbPOKG6msM43gT5zup3oxsHg_4Q` compactJWT, err := ZJWT(jwt) - assert.NoError(t, err) + require.NoError(t, err) expandedJWT, err := JWT(compactJWT) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, jwt, expandedJWT) assert.Equal(t, 37, 100*len(compactJWT)/len(jwt)) }) @@ -39,11 +40,11 @@ func TestCompact(t *testing.T) { func TestExpand(t *testing.T) { _, err := JWT(".") - assert.Error(t, err) + require.Error(t, err) _, err = JWT("...") - assert.Error(t, err) + require.Error(t, err) _, err = JWT("zJWT/v1:...") - assert.Error(t, err) + require.Error(t, err) _, err = JWT("zJWT/v1:..!.") assert.Error(t, err) } diff --git a/kubeclientmetrics/metric_test.go b/kubeclientmetrics/metric_test.go index 31c7187b5..228d95ae9 100644 --- a/kubeclientmetrics/metric_test.go +++ b/kubeclientmetrics/metric_test.go @@ -23,7 +23,7 @@ type fakeWrapper struct { func (f fakeWrapper) RoundTrip(r *http.Request) (*http.Response, error) { resp := httptest.NewRecorder() resp.Code = 201 - assert.Equal(f.t, f.currentCount, f.expectedCount) + assert.Equal(f.t, f.expectedCount, f.currentCount) return resp.Result(), nil } diff --git a/rand/rand_test.go b/rand/rand_test.go index 54a057d74..1f17ab03c 100644 --- a/rand/rand_test.go +++ b/rand/rand_test.go @@ -1,15 +1,17 @@ package rand import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestRandString(t *testing.T) { ss, err := RandString(10) - assert.NoError(t, err) + require.NoError(t, err) assert.Len(t, ss, 10) ss, err = RandString(5) - assert.NoError(t, err) + require.NoError(t, err) assert.Len(t, ss, 5) } diff --git a/s3/s3_test.go b/s3/s3_test.go index 82615e0e9..d38c443a4 100644 --- a/s3/s3_test.go +++ b/s3/s3_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) // TestNewS3Client tests the s3 constructor @@ -25,7 +26,7 @@ func TestNewS3Client(t *testing.T) { EncryptOpts: EncryptOpts{Enabled: true, ServerSideCustomerKey: "", KmsKeyId: "", KmsEncryptionContext: ""}, } s3If, err := NewS3Client(context.Background(), opts) - assert.NoError(t, err) + require.NoError(t, err) s3cli := s3If.(*s3client) assert.Equal(t, opts.Endpoint, s3cli.Endpoint) assert.Equal(t, opts.Region, s3cli.Region) @@ -50,7 +51,7 @@ func TestNewS3ClientEphemeral(t *testing.T) { SessionToken: "sessionToken", } s3If, err := NewS3Client(context.Background(), opts) - assert.NoError(t, err) + require.NoError(t, err) s3cli := s3If.(*s3client) assert.Equal(t, opts.Endpoint, s3cli.Endpoint) assert.Equal(t, opts.Region, s3cli.Region) @@ -69,7 +70,7 @@ func TestNewS3ClientWithDiff(t *testing.T) { Trace: true, } s3If, err := NewS3Client(context.Background(), opts) - assert.NoError(t, err) + require.NoError(t, err) s3cli := s3If.(*s3client) assert.Equal(t, opts.Endpoint, s3cli.Endpoint) assert.Equal(t, opts.Region, s3cli.Region) @@ -86,7 +87,7 @@ func TestNewS3ClientWithDiff(t *testing.T) { RoleARN: "01234567890123456789", } s3If, err := NewS3Client(context.Background(), opts) - assert.NoError(t, err) + require.NoError(t, err) s3cli := s3If.(*s3client) assert.Equal(t, opts.Endpoint, s3cli.Endpoint) assert.Equal(t, opts.Region, s3cli.Region) diff --git a/time/time_test.go b/time/time_test.go index 3509ce98f..70a88991c 100644 --- a/time/time_test.go +++ b/time/time_test.go @@ -5,6 +5,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) // TestParseDuration tests TestParseDuration @@ -24,17 +25,17 @@ func TestParseDuration(t *testing.T) { } for _, data := range testdata { dur, err := ParseDuration(data.duration) - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, dur.Nanoseconds(), data.xVal.Nanoseconds()) } _, err := ParseDuration("1z") - assert.NotNil(t, err) + assert.Error(t, err) } // TestParseSince tests parsing of since strings func TestParseSince(t *testing.T) { oneDayAgo, err := ParseSince("1d") - assert.Nil(t, err) + require.NoError(t, err) yesterday := time.Now().UTC().Add(-24 * time.Hour) assert.Equal(t, yesterday.Minute(), oneDayAgo.Minute()) }