diff --git a/analyzer/testdata/src/checkers-default/blank-import/blank_import_test.go b/analyzer/testdata/src/checkers-default/blank-import/blank_import_test.go index 1ddec82..707cfa5 100644 --- a/analyzer/testdata/src/checkers-default/blank-import/blank_import_test.go +++ b/analyzer/testdata/src/checkers-default/blank-import/blank_import_test.go @@ -2,41 +2,55 @@ package blankimport +import "testing" +import _ "github.com/stretchr/testify" // want "blank-import: avoid blank import of github.com/stretchr/testify as it does nothing" +import _ "github.com/stretchr/testify/assert" // want "blank-import: avoid blank import of github.com/stretchr/testify/assert as it does nothing" +import _ "github.com/stretchr/testify/http" // want "blank-import: avoid blank import of github.com/stretchr/testify/http as it does nothing" +import _ "github.com/stretchr/testify/mock" // want "blank-import: avoid blank import of github.com/stretchr/testify/mock as it does nothing" +import _ "github.com/stretchr/testify/require" // want "blank-import: avoid blank import of github.com/stretchr/testify/require as it does nothing" +import _ "github.com/stretchr/testify/suite" // want "blank-import: avoid blank import of github.com/stretchr/testify/suite as it does nothing" +import "strings" + import ( - . "database/sql" + _ "github.com/stretchr/testify" // want "blank-import: avoid blank import of github.com/stretchr/testify as it does nothing" + _ "github.com/stretchr/testify/assert" // want "blank-import: avoid blank import of github.com/stretchr/testify/assert as it does nothing" + _ "github.com/stretchr/testify/http" // want "blank-import: avoid blank import of github.com/stretchr/testify/http as it does nothing" + _ "github.com/stretchr/testify/mock" // want "blank-import: avoid blank import of github.com/stretchr/testify/mock as it does nothing" + _ "github.com/stretchr/testify/require" // want "blank-import: avoid blank import of github.com/stretchr/testify/require as it does nothing" + _ "github.com/stretchr/testify/suite" // want "blank-import: avoid blank import of github.com/stretchr/testify/suite as it does nothing" +) + +import ( + _ "gopkg.in/yaml.v3" "net/url" - "strings" - "testing" - _ "github.com/pmezard/go-difflib/difflib" - _ "github.com/stretchr/testify" // want "blank-import: avoid blank import of github.com/stretchr/testify as it does nothing" // Testing. + _ "github.com/stretchr/testify" // want "blank-import: avoid blank import of github.com/stretchr/testify as it does nothing" + _ "github.com/stretchr/testify/assert" // want "blank-import: avoid blank import of github.com/stretchr/testify/assert as it does nothing" + _ "github.com/stretchr/testify/http" // want "blank-import: avoid blank import of github.com/stretchr/testify/http as it does nothing" + _ "github.com/stretchr/testify/mock" // want "blank-import: avoid blank import of github.com/stretchr/testify/mock as it does nothing" + _ "github.com/stretchr/testify/require" // want "blank-import: avoid blank import of github.com/stretchr/testify/require as it does nothing" + _ "github.com/stretchr/testify/suite" // want "blank-import: avoid blank import of github.com/stretchr/testify/suite as it does nothing" + + . "database/sql" + _ "github.com/pmezard/go-difflib/difflib" +) + +import ( _ "github.com/stretchr/testify" // want "blank-import: avoid blank import of github.com/stretchr/testify as it does nothing" +) + +import ( // Test dependencies so that it doesn't get cleaned by glide vc _ "github.com/stretchr/testify" // want "blank-import: avoid blank import of github.com/stretchr/testify as it does nothing" - _ "github.com/stretchr/testify" // want "blank-import: avoid blank import of github.com/stretchr/testify as it does nothing" - _ "github.com/stretchr/testify" // want "blank-import: avoid blank import of github.com/stretchr/testify as it does nothing" +) + +import ( "github.com/stretchr/testify/assert" - _ "github.com/stretchr/testify/assert" // want "blank-import: avoid blank import of github.com/stretchr/testify/assert as it does nothing" - _ "github.com/stretchr/testify/assert" // want "blank-import: avoid blank import of github.com/stretchr/testify/assert as it does nothing" - _ "github.com/stretchr/testify/assert" // want "blank-import: avoid blank import of github.com/stretchr/testify/assert as it does nothing" "github.com/stretchr/testify/http" - _ "github.com/stretchr/testify/http" // want "blank-import: avoid blank import of github.com/stretchr/testify/http as it does nothing" - _ "github.com/stretchr/testify/http" // want "blank-import: avoid blank import of github.com/stretchr/testify/http as it does nothing" - _ "github.com/stretchr/testify/http" // want "blank-import: avoid blank import of github.com/stretchr/testify/http as it does nothing" "github.com/stretchr/testify/mock" - _ "github.com/stretchr/testify/mock" // want "blank-import: avoid blank import of github.com/stretchr/testify/mock as it does nothing" - _ "github.com/stretchr/testify/mock" // want "blank-import: avoid blank import of github.com/stretchr/testify/mock as it does nothing" - _ "github.com/stretchr/testify/mock" // want "blank-import: avoid blank import of github.com/stretchr/testify/mock as it does nothing" "github.com/stretchr/testify/require" - _ "github.com/stretchr/testify/require" // want "blank-import: avoid blank import of github.com/stretchr/testify/require as it does nothing" - _ "github.com/stretchr/testify/require" // want "blank-import: avoid blank import of github.com/stretchr/testify/require as it does nothing" - _ "github.com/stretchr/testify/require" // want "blank-import: avoid blank import of github.com/stretchr/testify/require as it does nothing" "github.com/stretchr/testify/suite" - _ "github.com/stretchr/testify/suite" // want "blank-import: avoid blank import of github.com/stretchr/testify/suite as it does nothing" - _ "github.com/stretchr/testify/suite" // want "blank-import: avoid blank import of github.com/stretchr/testify/suite as it does nothing" - _ "github.com/stretchr/testify/suite" // want "blank-import: avoid blank import of github.com/stretchr/testify/suite as it does nothing" - _ "gopkg.in/yaml.v3" ) func TestDummy(t *testing.T) { diff --git a/analyzer/testdata/src/checkers-default/formatter/formatter_test.go b/analyzer/testdata/src/checkers-default/formatter/formatter_test.go index c55c65d..29db750 100644 --- a/analyzer/testdata/src/checkers-default/formatter/formatter_test.go +++ b/analyzer/testdata/src/checkers-default/formatter/formatter_test.go @@ -363,6 +363,7 @@ func TestFormatterChecker_AllAssertions(t *testing.T) { } func TestFormatterChecker_Ignored(t *testing.T) { + assert.Equal(nil, nil, new(time.Time)) assert.ObjectsAreEqual(nil, nil) assert.ObjectsAreEqualValues(nil, nil) assert.ObjectsExportedFieldsAreEqual(nil, nil) diff --git a/analyzer/testdata/src/checkers-default/formatter/formatter_test.go.golden b/analyzer/testdata/src/checkers-default/formatter/formatter_test.go.golden index 319c417..0e0101d 100644 --- a/analyzer/testdata/src/checkers-default/formatter/formatter_test.go.golden +++ b/analyzer/testdata/src/checkers-default/formatter/formatter_test.go.golden @@ -363,6 +363,7 @@ func TestFormatterChecker_AllAssertions(t *testing.T) { } func TestFormatterChecker_Ignored(t *testing.T) { + assert.Equal(nil, nil, new(time.Time)) assert.ObjectsAreEqual(nil, nil) assert.ObjectsAreEqualValues(nil, nil) assert.ObjectsExportedFieldsAreEqual(nil, nil) diff --git a/analyzer/testdata/src/checkers-default/suite-dont-use-pkg/suite_dont_use_pkg_test.go b/analyzer/testdata/src/checkers-default/suite-dont-use-pkg/suite_dont_use_pkg_test.go index 1ecc91d..b133515 100644 --- a/analyzer/testdata/src/checkers-default/suite-dont-use-pkg/suite_dont_use_pkg_test.go +++ b/analyzer/testdata/src/checkers-default/suite-dont-use-pkg/suite_dont_use_pkg_test.go @@ -5,10 +5,11 @@ package suitedontusepkg import ( "testing" - "github.com/stretchr/testify/assert" a "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" r "github.com/stretchr/testify/require" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" ) diff --git a/analyzer/testdata/src/debug/formatter_test.go b/analyzer/testdata/src/debug/formatter_test.go new file mode 100644 index 0000000..f9ca761 --- /dev/null +++ b/analyzer/testdata/src/debug/formatter_test.go @@ -0,0 +1,19 @@ +package debug + +import ( + "net/http" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFormatter(t *testing.T) { + assert.False(t, true) + assert.False(t, true, new(http.Response)) + // assert.False(t, true, new(http.Response), 1, 2, 3) // panic + assert.False(t, true, "hello") + assert.False(t, true, "hello", 1, 2) + assert.False(t, true, "hello_%v_%d", 3, 4) + assert.Falsef(t, true, "world") + assert.Falsef(t, true, "world_%d_%v", 5, 6) +} diff --git a/analyzer/testdata/src/formatter-not-defaults/formatter_not_defaults_test.go b/analyzer/testdata/src/formatter-not-defaults/formatter_not_defaults_test.go index 079b38b..f92f90c 100644 --- a/analyzer/testdata/src/formatter-not-defaults/formatter_not_defaults_test.go +++ b/analyzer/testdata/src/formatter-not-defaults/formatter_not_defaults_test.go @@ -15,6 +15,7 @@ func TestFormatterChecker(t *testing.T) { var args []any assert.Error(t, err, "Parse(%v) should fail.", args) // want "formatter: use assert\\.Errorf$" assert.Equal(t, 1, 2) + assert.Equal(t, 1, 2, new(time.Time)) assert.Equal(t, 1, 2, "msg") // want "formatter: use assert\\.Equalf" assert.Equal(t, 1, 2, "msg with arg %d", 42) // want "formatter: use assert\\.Equalf" assert.Equal(t, 1, 2, "msg with args %d %s", 42, "42") // want "formatter: use assert\\.Equalf" diff --git a/analyzer/testdata/src/formatter-not-defaults/formatter_not_defaults_test.go.golden b/analyzer/testdata/src/formatter-not-defaults/formatter_not_defaults_test.go.golden index 86bdfaa..adda687 100644 --- a/analyzer/testdata/src/formatter-not-defaults/formatter_not_defaults_test.go.golden +++ b/analyzer/testdata/src/formatter-not-defaults/formatter_not_defaults_test.go.golden @@ -15,6 +15,7 @@ func TestFormatterChecker(t *testing.T) { var args []any assert.Errorf(t, err, "Parse(%v) should fail.", args) // want "formatter: use assert\\.Errorf$" assert.Equal(t, 1, 2) + assert.Equal(t, 1, 2, new(time.Time)) assert.Equalf(t, 1, 2, "msg") // want "formatter: use assert\\.Equalf" assert.Equalf(t, 1, 2, "msg with arg %d", 42) // want "formatter: use assert\\.Equalf" assert.Equalf(t, 1, 2, "msg with args %d %s", 42, "42") // want "formatter: use assert\\.Equalf" diff --git a/internal/checkers/formatter.go b/internal/checkers/formatter.go index 896b6bf..7ff4de4 100644 --- a/internal/checkers/formatter.go +++ b/internal/checkers/formatter.go @@ -115,7 +115,11 @@ func (checker Formatter) checkFmtAssertion(pass *analysis.Pass, call *CallMeta) func isPrintfLikeCall(pass *analysis.Pass, call *CallMeta) (int, bool) { msgAndArgsPos := getMsgAndArgsPosition(call.Fn.Signature) - if msgAndArgsPos < 0 { + if msgAndArgsPos <= 0 { + return -1, false + } + + if !(len(call.ArgsRaw) > msgAndArgsPos && hasStringType(pass, call.ArgsRaw[msgAndArgsPos])) { return -1, false } @@ -123,7 +127,7 @@ func isPrintfLikeCall(pass *analysis.Pass, call *CallMeta) (int, bool) { return -1, false } - return msgAndArgsPos, len(call.ArgsRaw) > msgAndArgsPos + return msgAndArgsPos, true } func assertHasFormattedAnalogue(pass *analysis.Pass, call *CallMeta) bool { diff --git a/internal/testgen/gen_formatter.go b/internal/testgen/gen_formatter.go index 6b7757c..d02be3a 100644 --- a/internal/testgen/gen_formatter.go +++ b/internal/testgen/gen_formatter.go @@ -114,6 +114,7 @@ func (g FormatterTestsGenerator) TemplateData() any { SprintfAssertions: sprintfAssertions, AllAssertions: assertions, IgnoredAssertions: []Assertion{ + {Fn: "Equal", Argsf: "nil, nil, new(time.Time)"}, {Fn: "ObjectsAreEqual", Argsf: "nil, nil"}, {Fn: "ObjectsAreEqualValues", Argsf: "nil, nil"}, {Fn: "ObjectsExportedFieldsAreEqual", Argsf: "nil, nil"}, diff --git a/internal/testgen/gen_formatter_not_defaults.go b/internal/testgen/gen_formatter_not_defaults.go index bd599ca..caa1c29 100644 --- a/internal/testgen/gen_formatter_not_defaults.go +++ b/internal/testgen/gen_formatter_not_defaults.go @@ -19,6 +19,8 @@ func (g FormatterNotDefaultsTestsGenerator) TemplateData() any { baseAssertions := []Assertion{ {Fn: "Equal", Argsf: "1, 2"}, + {Fn: "Equal", Argsf: "1, 2, new(time.Time)"}, + // {Fn: "Equal", Argsf: "1, 2, new(time.Time), 2"}, // Panics. {Fn: "Equal", Argsf: `1, 2, "msg"`, ReportMsgf: reportUse, ProposedFn: "Equalf"}, {Fn: "Equal", Argsf: `1, 2, "msg with arg %d", 42`, ReportMsgf: reportUse, ProposedFn: "Equalf"}, {Fn: "Equal", Argsf: `1, 2, "msg with args %d %s", 42, "42"`, ReportMsgf: reportUse, ProposedFn: "Equalf"},