From eaacc110b0fba372aca5370704305d6550e2fc70 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Mon, 24 Jun 2024 10:29:36 +0200 Subject: [PATCH] error-nil: support empty and notempty error --- README.md | 4 ++++ .../checkers-default/error-nil/error_nil_test.go | 16 ++++++++++++++++ .../error-nil/error_nil_test.go.golden | 16 ++++++++++++++++ internal/checkers/error_nil.go | 8 ++++++-- internal/testgen/gen_error_nil.go | 8 ++++++++ 5 files changed, 50 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7fe6314f..02be709d 100644 --- a/README.md +++ b/README.md @@ -307,6 +307,10 @@ logic, but without autofix. ❌ assert.Nil(t, err) assert.NotNil(t, err) +assert.Empty(t, err) +assert.NotEmpty(t, err) +assert.Zero(t, err) +assert.NotZero(t, err) assert.Equal(t, nil, err) assert.EqualValues(t, nil, err) assert.Exactly(t, nil, err) diff --git a/analyzer/testdata/src/checkers-default/error-nil/error_nil_test.go b/analyzer/testdata/src/checkers-default/error-nil/error_nil_test.go index 407b90a3..43126b32 100644 --- a/analyzer/testdata/src/checkers-default/error-nil/error_nil_test.go +++ b/analyzer/testdata/src/checkers-default/error-nil/error_nil_test.go @@ -19,6 +19,14 @@ func TestErrorNilChecker(t *testing.T) { assert.Nilf(t, err, "msg with args %d %s", 42, "42") // want "error-nil: use assert\\.NoErrorf" assert.NotNil(t, err) // want "error-nil: use assert\\.Error" assert.NotNilf(t, err, "msg with args %d %s", 42, "42") // want "error-nil: use assert\\.Errorf" + assert.Empty(t, err) // want "error-nil: use assert\\.NoError" + assert.Emptyf(t, err, "msg with args %d %s", 42, "42") // want "error-nil: use assert\\.NoErrorf" + assert.NotEmpty(t, err) // want "error-nil: use assert\\.Error" + assert.NotEmptyf(t, err, "msg with args %d %s", 42, "42") // want "error-nil: use assert\\.Errorf" + assert.Zero(t, err) // want "error-nil: use assert\\.NoError" + assert.Zerof(t, err, "msg with args %d %s", 42, "42") // want "error-nil: use assert\\.NoErrorf" + assert.NotZero(t, err) // want "error-nil: use assert\\.Error" + assert.NotZerof(t, err, "msg with args %d %s", 42, "42") // want "error-nil: use assert\\.Errorf" assert.Equal(t, err, nil) // want "error-nil: use assert\\.NoError" assert.Equalf(t, err, nil, "msg with args %d %s", 42, "42") // want "error-nil: use assert\\.NoErrorf" assert.Equal(t, nil, err) // want "error-nil: use assert\\.NoError" @@ -67,6 +75,14 @@ func TestErrorNilChecker(t *testing.T) { assert.NotEqualf(t, err, err, "msg with args %d %s", 42, "42") assert.NotEqual(t, nil, nil) assert.NotEqualf(t, nil, nil, "msg with args %d %s", 42, "42") + assert.Empty(t, err.Error()) + assert.Emptyf(t, err.Error(), "msg with args %d %s", 42, "42") + assert.NotEmpty(t, err.Error()) + assert.NotEmptyf(t, err.Error(), "msg with args %d %s", 42, "42") + assert.Zero(t, err.Error()) + assert.Zerof(t, err.Error(), "msg with args %d %s", 42, "42") + assert.NotZero(t, err.Error()) + assert.NotZerof(t, err.Error(), "msg with args %d %s", 42, "42") } } diff --git a/analyzer/testdata/src/checkers-default/error-nil/error_nil_test.go.golden b/analyzer/testdata/src/checkers-default/error-nil/error_nil_test.go.golden index ba25565f..09f2abc4 100644 --- a/analyzer/testdata/src/checkers-default/error-nil/error_nil_test.go.golden +++ b/analyzer/testdata/src/checkers-default/error-nil/error_nil_test.go.golden @@ -15,6 +15,14 @@ func TestErrorNilChecker(t *testing.T) { // Invalid. { + assert.NoError(t, err) // want "error-nil: use assert\\.NoError" + assert.NoErrorf(t, err, "msg with args %d %s", 42, "42") // want "error-nil: use assert\\.NoErrorf" + assert.Error(t, err) // want "error-nil: use assert\\.Error" + assert.Errorf(t, err, "msg with args %d %s", 42, "42") // want "error-nil: use assert\\.Errorf" + assert.NoError(t, err) // want "error-nil: use assert\\.NoError" + assert.NoErrorf(t, err, "msg with args %d %s", 42, "42") // want "error-nil: use assert\\.NoErrorf" + assert.Error(t, err) // want "error-nil: use assert\\.Error" + assert.Errorf(t, err, "msg with args %d %s", 42, "42") // want "error-nil: use assert\\.Errorf" assert.NoError(t, err) // want "error-nil: use assert\\.NoError" assert.NoErrorf(t, err, "msg with args %d %s", 42, "42") // want "error-nil: use assert\\.NoErrorf" assert.Error(t, err) // want "error-nil: use assert\\.Error" @@ -67,6 +75,14 @@ func TestErrorNilChecker(t *testing.T) { assert.NotEqualf(t, err, err, "msg with args %d %s", 42, "42") assert.NotEqual(t, nil, nil) assert.NotEqualf(t, nil, nil, "msg with args %d %s", 42, "42") + assert.Empty(t, err.Error()) + assert.Emptyf(t, err.Error(), "msg with args %d %s", 42, "42") + assert.NotEmpty(t, err.Error()) + assert.NotEmptyf(t, err.Error(), "msg with args %d %s", 42, "42") + assert.Zero(t, err.Error()) + assert.Zerof(t, err.Error(), "msg with args %d %s", 42, "42") + assert.NotZero(t, err.Error()) + assert.NotZerof(t, err.Error(), "msg with args %d %s", 42, "42") } } diff --git a/internal/checkers/error_nil.go b/internal/checkers/error_nil.go index 1e56d222..0bacbb74 100644 --- a/internal/checkers/error_nil.go +++ b/internal/checkers/error_nil.go @@ -13,6 +13,10 @@ import ( // // assert.Nil(t, err) // assert.NotNil(t, err) +// assert.Empty(t, err) +// assert.NotEmpty(t, err) +// assert.Zero(t, err) +// assert.NotZero(t, err) // assert.Equal(t, nil, err) // assert.EqualValues(t, nil, err) // assert.Exactly(t, nil, err) @@ -40,12 +44,12 @@ func (checker ErrorNil) Check(pass *analysis.Pass, call *CallMeta) *analysis.Dia proposedFn, survivingArg, replacementEndPos := func() (string, ast.Expr, token.Pos) { switch call.Fn.NameFTrimmed { - case "Nil": + case "Nil", "Empty", "Zero": if len(call.Args) >= 1 && isError(pass, call.Args[0]) { return noErrorFn, call.Args[0], call.Args[0].End() } - case "NotNil": + case "NotNil", "NotEmpty", "NotZero": if len(call.Args) >= 1 && isError(pass, call.Args[0]) { return errorFn, call.Args[0], call.Args[0].End() } diff --git a/internal/testgen/gen_error_nil.go b/internal/testgen/gen_error_nil.go index 75168a64..92803aca 100644 --- a/internal/testgen/gen_error_nil.go +++ b/internal/testgen/gen_error_nil.go @@ -52,6 +52,10 @@ func (g ErrorNilTestsGenerator) TemplateData() any { InvalidAssertions: []Assertion{ {Fn: "Nil", Argsf: "err", ReportMsgf: report, ProposedFn: "NoError"}, {Fn: "NotNil", Argsf: "err", ReportMsgf: report, ProposedFn: "Error"}, + {Fn: "Empty", Argsf: "err", ReportMsgf: report, ProposedFn: "NoError"}, + {Fn: "NotEmpty", Argsf: "err", ReportMsgf: report, ProposedFn: "Error"}, + {Fn: "Zero", Argsf: "err", ReportMsgf: report, ProposedFn: "NoError"}, + {Fn: "NotZero", Argsf: "err", ReportMsgf: report, ProposedFn: "Error"}, {Fn: "Equal", Argsf: "err, nil", ReportMsgf: report, ProposedFn: "NoError", ProposedArgsf: "err"}, {Fn: "Equal", Argsf: "nil, err", ReportMsgf: report, ProposedFn: "NoError", ProposedArgsf: "err"}, {Fn: "EqualValues", Argsf: "err, nil", ReportMsgf: report, ProposedFn: "NoError", ProposedArgsf: "err"}, @@ -76,6 +80,10 @@ func (g ErrorNilTestsGenerator) TemplateData() any { {Fn: "Equal", Argsf: "nil, nil"}, {Fn: "NotEqual", Argsf: "err, err"}, {Fn: "NotEqual", Argsf: "nil, nil"}, + {Fn: "Empty", Argsf: "err.Error()"}, // Requested by https://github.com/Antonboom/testifylint/issues/138 + {Fn: "NotEmpty", Argsf: "err.Error()"}, // Requested by https://github.com/Antonboom/testifylint/issues/138 + {Fn: "Zero", Argsf: "err.Error()"}, // Requested by https://github.com/Antonboom/testifylint/issues/138 + {Fn: "NotZero", Argsf: "err.Error()"}, // Requested by https://github.com/Antonboom/testifylint/issues/138 }, } }