Skip to content

Commit

Permalink
chore: enable testifylint linter
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 committed Dec 31, 2024
1 parent 23087ff commit f83eed2
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 39 deletions.
7 changes: 7 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 8 additions & 7 deletions exec/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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)
}

Expand All @@ -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)

Expand Down Expand Up @@ -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)
}

Expand Down
9 changes: 5 additions & 4 deletions file/file_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"

"github.com/argoproj/pkg/rand"
)
Expand All @@ -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))
}
15 changes: 8 additions & 7 deletions grpc/http/forwarders_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"
)

type testStruct struct {
Expand Down Expand Up @@ -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) {
Expand All @@ -51,16 +52,16 @@ 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) {
m := messageMarshaler{isSSE: true}

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))
Expand Down Expand Up @@ -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) {
Expand All @@ -98,5 +99,5 @@ func TestFlushFailed(t *testing.T) {
f := flusher{}
flush(&f)

assert.Equal(t, false, flushed)
assert.False(t, flushed)
}
7 changes: 4 additions & 3 deletions json/json_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"
)

// TestDisallowUnknownFields tests ability to disallow unknown fields
Expand All @@ -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)
}

Expand Down
15 changes: 8 additions & 7 deletions jwt/zjwt/zjwt_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"
)

func init() {
Expand All @@ -14,19 +15,19 @@ 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))
})
t.Run("Large", func(t *testing.T) {
jwt := `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6I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pY29tcG9uZW50czphcHAtdWljb21wb25lbnRzLWlkcy1kYXNoYm9hcmQtdWkiLCJhcHAtdWljb21wb25lbnRzOmlkcy1wbGF5Z3JvdW5kLXVpIiwiVVgtSW5mcmEiLCJVWC1JbmZyYTpDb3JlIiwiZGVzaWduLXN5c3RlbXMiXX0.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))
})
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion kubeclientmetrics/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
8 changes: 5 additions & 3 deletions rand/rand_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
9 changes: 5 additions & 4 deletions s3/s3_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"
)

// TestNewS3Client tests the s3 constructor
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions time/time_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"
)

// TestParseDuration tests TestParseDuration
Expand All @@ -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())
}

0 comments on commit f83eed2

Please sign in to comment.