From 9a2c7d6080feeecff359dfd1808cd892ca4a6b7e Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Tue, 4 Jun 2024 15:55:48 +0200 Subject: [PATCH 01/18] ttempdir added --- .golangci.reference.yml | 9 +++ go.mod | 1 + go.sum | 2 + pkg/config/linters_settings.go | 6 ++ pkg/golinters/ttempdir/testdata/ttempdir.go | 79 +++++++++++++++++++ .../ttempdir/testdata/ttempdir_all.yml | 4 + pkg/golinters/ttempdir/ttempdir.go | 30 +++++++ .../ttempdir/ttempdir_integration_test.go | 11 +++ pkg/lint/lintersdb/builder_linter.go | 7 ++ 9 files changed, 149 insertions(+) create mode 100644 pkg/golinters/ttempdir/testdata/ttempdir.go create mode 100644 pkg/golinters/ttempdir/testdata/ttempdir_all.yml create mode 100644 pkg/golinters/ttempdir/ttempdir.go create mode 100644 pkg/golinters/ttempdir/ttempdir_integration_test.go diff --git a/.golangci.reference.yml b/.golangci.reference.yml index 1875b4b6f428..9502e2c93218 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -2289,6 +2289,15 @@ linters-settings: # Default: true begin: false + ttempdir: + # The option `all` will run against whole test files (`_test.go`) regardless of method/function signatures. + # Otherwise, only methods that take `*testing.T`, `*testing.B`, and `testing.TB` as arguments are checked. + # Default: false + all: false + # The option `max-recursion-level` is uses to analyze function calls in recursion. + # Default: 5 + max-recursion-level: 5 + usestdlibvars: # Suggest the use of http.MethodXX. # Default: true diff --git a/go.mod b/go.mod index 8d2077a96945..37e638343a78 100644 --- a/go.mod +++ b/go.mod @@ -83,6 +83,7 @@ require ( github.com/nishanths/exhaustive v0.12.0 github.com/nishanths/predeclared v0.2.2 github.com/nunnatsa/ginkgolinter v0.16.2 + github.com/peczenyj/ttempdir v0.2.1 github.com/pelletier/go-toml/v2 v2.2.2 github.com/polyfloyd/go-errorlint v1.5.2 github.com/quasilyte/go-ruleguard/dsl v0.3.22 diff --git a/go.sum b/go.sum index 07670ebf28f9..ca9ce5de1d83 100644 --- a/go.sum +++ b/go.sum @@ -413,6 +413,8 @@ github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJ github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs= github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo= github.com/otiai10/mint v1.3.1/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc= +github.com/peczenyj/ttempdir v0.2.1 h1:leuAE4nRtgw2LMI0MzP/4ZP7OyDhbXzSDkbZSMCdR0c= +github.com/peczenyj/ttempdir v0.2.1/go.mod h1:geqyoDCcBig6BF+XMMorghAjk8EqgmavNJOsng7AVOs= github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index b2f4567d494d..9a567ecbf28a 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -269,6 +269,7 @@ type LintersSettings struct { Testifylint TestifylintSettings Testpackage TestpackageSettings Thelper ThelperSettings + Ttempdir TtempdirSettings Unconvert UnconvertSettings Unparam UnparamSettings Unused UnusedSettings @@ -914,6 +915,11 @@ type TenvSettings struct { All bool `mapstructure:"all"` } +type TtempdirSettings struct { + All bool `mapstructure:"all"` + MaxRecursionLevel uint `mapstructure:"max-recursion-level"` +} + type UseStdlibVarsSettings struct { HTTPMethod bool `mapstructure:"http-method"` HTTPStatusCode bool `mapstructure:"http-status-code"` diff --git a/pkg/golinters/ttempdir/testdata/ttempdir.go b/pkg/golinters/ttempdir/testdata/ttempdir.go new file mode 100644 index 000000000000..94ff47f559ff --- /dev/null +++ b/pkg/golinters/ttempdir/testdata/ttempdir.go @@ -0,0 +1,79 @@ +//golangcitest:args -Ettempdir +//golangcitest:config_path testdata/ttempdir_all.yml +package testdata + +import ( + "os" + "testing" +) + +var ( + _, e = os.MkdirTemp("a", "b") // never seen +) + +func testsetup() { + os.MkdirTemp("a", "b") // if -all = true, want "os\\.MkdirTemp\\(\\) can be replaced by `testing\\.TempDir\\(\\)` in testsetup" + _, err := os.MkdirTemp("a", "b") // if -all = true, want "os\\.MkdirTemp\\(\\) can be replaced by `testing\\.TempDir\\(\\)` in testsetup" + if err != nil { + _ = err + } + os.MkdirTemp("a", "b") // if -all = true, "func setup is not using testing.TempDir" +} + +func TestF(t *testing.T) { + testsetup() + os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `t\\.TempDir\\(\\)` in TestF" + _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `t\\.TempDir\\(\\)` in TestF" + _ = err + if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) can be replaced by `t\\.TempDir\\(\\)` in TestF" + _ = err + } +} + +func BenchmarkF(b *testing.B) { + TB(b) + os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `b\\.TempDir\\(\\)` in BenchmarkF" + _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `b\\.TempDir\\(\\)` in BenchmarkF" + _ = err + if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) can be replaced by `b\\.TempDir\\(\\)` in BenchmarkF" + _ = err + } +} + +func TB(tb testing.TB) { + os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `tb\\.TempDir\\(\\)` in TB" + _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `tb\\.TempDir\\(\\)` in TB" + _ = err + if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) can be replaced by `tb\\.TempDir\\(\\)` in TB" + _ = err + } +} + +func FuzzF(f *testing.F) { + os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `f\\.TempDir\\(\\)` in FuzzF" + _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `f\\.TempDir\\(\\)` in FuzzF" + _ = err + if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) can be replaced by `f\\.TempDir\\(\\)` in FuzzF" + _ = err + } +} + +func TestFunctionLiteral(t *testing.T) { + testsetup() + t.Run("test", func(t *testing.T) { + os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `t\\.TempDir\\(\\)` in anonymous function" + _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `t\\.TempDir\\(\\)` in anonymous function" + _ = err + if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) can be replaced by `t\\.TempDir\\(\\)` in anonymous function" + _ = err + } + }) +} + +func TestEmpty(t *testing.T) { + t.Run("test", func(*testing.T) {}) +} + +func TestEmptyTB(t *testing.T) { + func(testing.TB) {}(t) +} diff --git a/pkg/golinters/ttempdir/testdata/ttempdir_all.yml b/pkg/golinters/ttempdir/testdata/ttempdir_all.yml new file mode 100644 index 000000000000..200b4482a441 --- /dev/null +++ b/pkg/golinters/ttempdir/testdata/ttempdir_all.yml @@ -0,0 +1,4 @@ +linters-settings: + ttempdir: + all: true + max-recursion-level: 5 diff --git a/pkg/golinters/ttempdir/ttempdir.go b/pkg/golinters/ttempdir/ttempdir.go new file mode 100644 index 000000000000..16f70caea9cf --- /dev/null +++ b/pkg/golinters/ttempdir/ttempdir.go @@ -0,0 +1,30 @@ +package ttempdir + +import ( + "github.com/peczenyj/ttempdir" + "golang.org/x/tools/go/analysis" + + "github.com/golangci/golangci-lint/pkg/config" + "github.com/golangci/golangci-lint/pkg/goanalysis" +) + +func New(settings *config.TtempdirSettings) *goanalysis.Linter { + a := ttempdir.Analyzer + + var cfg map[string]map[string]any + if settings != nil { + cfg = map[string]map[string]any{ + a.Name: { + ttempdir.A: settings.All, + ttempdir.MRL: settings.MaxRecursionLevel, + }, + } + } + + return goanalysis.NewLinter( + a.Name, + a.Doc, + []*analysis.Analyzer{a}, + cfg, + ).WithLoadMode(goanalysis.LoadModeSyntax) +} diff --git a/pkg/golinters/ttempdir/ttempdir_integration_test.go b/pkg/golinters/ttempdir/ttempdir_integration_test.go new file mode 100644 index 000000000000..590790e8a2fd --- /dev/null +++ b/pkg/golinters/ttempdir/ttempdir_integration_test.go @@ -0,0 +1,11 @@ +package ttempdir + +import ( + "testing" + + "github.com/golangci/golangci-lint/test/testshared/integration" +) + +func TestFromTestdata(t *testing.T) { + integration.RunTestdata(t) +} diff --git a/pkg/lint/lintersdb/builder_linter.go b/pkg/lint/lintersdb/builder_linter.go index 7b9d2a559bab..6a9b9c45618e 100644 --- a/pkg/lint/lintersdb/builder_linter.go +++ b/pkg/lint/lintersdb/builder_linter.go @@ -100,6 +100,7 @@ import ( "github.com/golangci/golangci-lint/pkg/golinters/testpackage" "github.com/golangci/golangci-lint/pkg/golinters/thelper" "github.com/golangci/golangci-lint/pkg/golinters/tparallel" + "github.com/golangci/golangci-lint/pkg/golinters/ttempdir" "github.com/golangci/golangci-lint/pkg/golinters/unconvert" "github.com/golangci/golangci-lint/pkg/golinters/unparam" "github.com/golangci/golangci-lint/pkg/golinters/unused" @@ -756,6 +757,12 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) { WithLoadForGoAnalysis(). WithURL("https://github.com/moricho/tparallel"), + linter.NewConfig(ttempdir.New(&cfg.LintersSettings.Ttempdir)). + WithSince("v1.60.0"). + WithPresets(linter.PresetStyle). + WithLoadForGoAnalysis(). + WithURL("https://github.com/peczenyj/ttempdir"), + linter.NewConfig(golinters.NewTypecheck()). WithInternal(). WithEnabledByDefault(). From 5ad72af5ada2c9031967777483e6e71e09dfa67e Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Tue, 4 Jun 2024 16:15:04 +0200 Subject: [PATCH 02/18] update reference yml files --- .golangci.next.reference.yml | 11 +++++++++++ .golangci.reference.yml | 2 ++ 2 files changed, 13 insertions(+) diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index 1875b4b6f428..49f0907e8d0a 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -2289,6 +2289,15 @@ linters-settings: # Default: true begin: false + ttempdir: + # The option `all` will run against whole test files (`_test.go`) regardless of method/function signatures. + # Otherwise, only methods that take `*testing.T`, `*testing.B`, and `testing.TB` as arguments are checked. + # Default: false + all: false + # The option `max-recursion-level` is uses to analyze function calls in recursion. + # Default: 5 + max-recursion-level: 5 + usestdlibvars: # Suggest the use of http.MethodXX. # Default: true @@ -2632,6 +2641,7 @@ linters: - testpackage - thelper - tparallel + - ttempdir - typecheck - unconvert - unparam @@ -2747,6 +2757,7 @@ linters: - testpackage - thelper - tparallel + - ttempdir - typecheck - unconvert - unparam diff --git a/.golangci.reference.yml b/.golangci.reference.yml index 9502e2c93218..49f0907e8d0a 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -2641,6 +2641,7 @@ linters: - testpackage - thelper - tparallel + - ttempdir - typecheck - unconvert - unparam @@ -2756,6 +2757,7 @@ linters: - testpackage - thelper - tparallel + - ttempdir - typecheck - unconvert - unparam From dd0ce0114b729df3ac8a4d1b89d74e1a2813bc00 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Tue, 4 Jun 2024 16:17:31 +0200 Subject: [PATCH 03/18] revert changes --- .golangci.reference.yml | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/.golangci.reference.yml b/.golangci.reference.yml index 49f0907e8d0a..1875b4b6f428 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -2289,15 +2289,6 @@ linters-settings: # Default: true begin: false - ttempdir: - # The option `all` will run against whole test files (`_test.go`) regardless of method/function signatures. - # Otherwise, only methods that take `*testing.T`, `*testing.B`, and `testing.TB` as arguments are checked. - # Default: false - all: false - # The option `max-recursion-level` is uses to analyze function calls in recursion. - # Default: 5 - max-recursion-level: 5 - usestdlibvars: # Suggest the use of http.MethodXX. # Default: true @@ -2641,7 +2632,6 @@ linters: - testpackage - thelper - tparallel - - ttempdir - typecheck - unconvert - unparam @@ -2757,7 +2747,6 @@ linters: - testpackage - thelper - tparallel - - ttempdir - typecheck - unconvert - unparam From 05a27d10f9675b69046ac6dafc095f873b515df7 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Tue, 4 Jun 2024 16:21:17 +0200 Subject: [PATCH 04/18] add dexample with different values --- .golangci.next.reference.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index 49f0907e8d0a..5d57782abca3 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -2293,10 +2293,10 @@ linters-settings: # The option `all` will run against whole test files (`_test.go`) regardless of method/function signatures. # Otherwise, only methods that take `*testing.T`, `*testing.B`, and `testing.TB` as arguments are checked. # Default: false - all: false + all: true # The option `max-recursion-level` is uses to analyze function calls in recursion. # Default: 5 - max-recursion-level: 5 + max-recursion-level: 10 usestdlibvars: # Suggest the use of http.MethodXX. From e279d6b1c5fd8b1ecd28c1ddaac519b6c92ce066 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Tue, 4 Jun 2024 19:01:12 +0200 Subject: [PATCH 05/18] update dependency --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 37e638343a78..25cc5ae9a64e 100644 --- a/go.mod +++ b/go.mod @@ -83,7 +83,7 @@ require ( github.com/nishanths/exhaustive v0.12.0 github.com/nishanths/predeclared v0.2.2 github.com/nunnatsa/ginkgolinter v0.16.2 - github.com/peczenyj/ttempdir v0.2.1 + github.com/peczenyj/ttempdir v0.2.2 github.com/pelletier/go-toml/v2 v2.2.2 github.com/polyfloyd/go-errorlint v1.5.2 github.com/quasilyte/go-ruleguard/dsl v0.3.22 diff --git a/go.sum b/go.sum index ca9ce5de1d83..255bc06741ca 100644 --- a/go.sum +++ b/go.sum @@ -413,8 +413,8 @@ github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJ github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs= github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo= github.com/otiai10/mint v1.3.1/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc= -github.com/peczenyj/ttempdir v0.2.1 h1:leuAE4nRtgw2LMI0MzP/4ZP7OyDhbXzSDkbZSMCdR0c= -github.com/peczenyj/ttempdir v0.2.1/go.mod h1:geqyoDCcBig6BF+XMMorghAjk8EqgmavNJOsng7AVOs= +github.com/peczenyj/ttempdir v0.2.2 h1:t/v9+OGaCDKZxwsxjGqVL+/bCIZ0WXOl6skqwPtXBXA= +github.com/peczenyj/ttempdir v0.2.2/go.mod h1:geqyoDCcBig6BF+XMMorghAjk8EqgmavNJOsng7AVOs= github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= From a0aacf9808cf2e02e1ff6429aaad4268c98c6b3f Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Wed, 5 Jun 2024 07:35:13 +0200 Subject: [PATCH 06/18] use ttempdir v0.3.0 --- go.mod | 8 ++++---- go.sum | 20 ++++++++++---------- pkg/golinters/ttempdir/ttempdir.go | 16 ++++++++-------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/go.mod b/go.mod index 25cc5ae9a64e..68f234388022 100644 --- a/go.mod +++ b/go.mod @@ -83,7 +83,7 @@ require ( github.com/nishanths/exhaustive v0.12.0 github.com/nishanths/predeclared v0.2.2 github.com/nunnatsa/ginkgolinter v0.16.2 - github.com/peczenyj/ttempdir v0.2.2 + github.com/peczenyj/ttempdir v0.3.0 github.com/pelletier/go-toml/v2 v2.2.2 github.com/polyfloyd/go-errorlint v1.5.2 github.com/quasilyte/go-ruleguard/dsl v0.3.22 @@ -126,7 +126,7 @@ require ( go-simpler.org/sloglint v0.7.1 go.uber.org/automaxprocs v1.5.3 golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc - golang.org/x/tools v0.21.0 + golang.org/x/tools v0.22.0 gopkg.in/yaml.v3 v3.0.1 honnef.co/go/tools v0.4.7 mvdan.cc/gofumpt v0.6.0 @@ -191,9 +191,9 @@ require ( go.uber.org/multierr v1.6.0 // indirect go.uber.org/zap v1.24.0 // indirect golang.org/x/exp/typeparams v0.0.0-20240314144324-c7f7c6466f7f // indirect - golang.org/x/mod v0.17.0 // indirect + golang.org/x/mod v0.18.0 // indirect golang.org/x/sync v0.7.0 // indirect - golang.org/x/sys v0.20.0 // indirect + golang.org/x/sys v0.21.0 // indirect golang.org/x/text v0.15.0 // indirect google.golang.org/protobuf v1.33.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect diff --git a/go.sum b/go.sum index 255bc06741ca..6cf378dd6544 100644 --- a/go.sum +++ b/go.sum @@ -413,8 +413,8 @@ github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJ github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs= github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo= github.com/otiai10/mint v1.3.1/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc= -github.com/peczenyj/ttempdir v0.2.2 h1:t/v9+OGaCDKZxwsxjGqVL+/bCIZ0WXOl6skqwPtXBXA= -github.com/peczenyj/ttempdir v0.2.2/go.mod h1:geqyoDCcBig6BF+XMMorghAjk8EqgmavNJOsng7AVOs= +github.com/peczenyj/ttempdir v0.3.0 h1:xMUqiPBueA0/5IIs28KoS48Zdbwf5PgkJ43hPCg8FPY= +github.com/peczenyj/ttempdir v0.3.0/go.mod h1:jXgHoYn33YfuzChRNf5t0d76cTlzY+7Le+Fp+9cmzwY= github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= @@ -665,8 +665,8 @@ golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91 golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI= golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= -golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0= +golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -706,8 +706,8 @@ golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco= golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= -golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= -golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= +golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ= +golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -788,8 +788,8 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= -golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= +golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= @@ -872,8 +872,8 @@ golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA= golang.org/x/tools v0.3.0/go.mod h1:/rWhSS2+zyEVwoJf8YAX6L2f0ntZ7Kn/mGgAWcipA5k= golang.org/x/tools v0.5.0/go.mod h1:N+Kgy78s5I24c24dU8OfWNEotWjutIs8SnJvn5IDq+k= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= -golang.org/x/tools v0.21.0 h1:qc0xYgIbsSDt9EyWz05J5wfa7LOVW0YTLOXrqdLAWIw= -golang.org/x/tools v0.21.0/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= +golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA= +golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/pkg/golinters/ttempdir/ttempdir.go b/pkg/golinters/ttempdir/ttempdir.go index 16f70caea9cf..0423bb91cf5c 100644 --- a/pkg/golinters/ttempdir/ttempdir.go +++ b/pkg/golinters/ttempdir/ttempdir.go @@ -1,7 +1,7 @@ package ttempdir import ( - "github.com/peczenyj/ttempdir" + "github.com/peczenyj/ttempdir/analyzer" "golang.org/x/tools/go/analysis" "github.com/golangci/golangci-lint/pkg/config" @@ -9,22 +9,22 @@ import ( ) func New(settings *config.TtempdirSettings) *goanalysis.Linter { - a := ttempdir.Analyzer + ttempdirAnalyzer := analyzer.New() var cfg map[string]map[string]any if settings != nil { cfg = map[string]map[string]any{ - a.Name: { - ttempdir.A: settings.All, - ttempdir.MRL: settings.MaxRecursionLevel, + ttempdirAnalyzer.Name: { + analyzer.FlagAllName: settings.All, + analyzer.FlagMaxRecursionLevelName: settings.MaxRecursionLevel, }, } } return goanalysis.NewLinter( - a.Name, - a.Doc, - []*analysis.Analyzer{a}, + ttempdirAnalyzer.Name, + ttempdirAnalyzer.Doc, + []*analysis.Analyzer{ttempdirAnalyzer}, cfg, ).WithLoadMode(goanalysis.LoadModeSyntax) } From 7abfa9b8ec4df4fb0ea40c2bdd99491da58cb64d Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Wed, 5 Jun 2024 09:09:20 +0200 Subject: [PATCH 07/18] update jsonschema --- jsonschema/golangci.next.jsonschema.json | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/jsonschema/golangci.next.jsonschema.json b/jsonschema/golangci.next.jsonschema.json index 7b658258ab0f..09c8ebb96100 100644 --- a/jsonschema/golangci.next.jsonschema.json +++ b/jsonschema/golangci.next.jsonschema.json @@ -316,6 +316,7 @@ "testpackage", "thelper", "tparallel", + "ttempdir", "typecheck", "unconvert", "unparam", @@ -3030,6 +3031,22 @@ } } }, + "ttempdir": { + "type": "object", + "additionalProperties": false, + "properties": { + "all": { + "description": "The option `all` will run against whole test files (`_test.go`) regardless of method/function signatures.", + "type": "boolean", + "default": false + }, + "max-recursion-level": { + "description": "Max recursion level when checking nested arg calls", + "type": "integer", + "default": 5 + } + } + }, "usestdlibvars": { "type": "object", "additionalProperties": false, From 7b39ece52c4615b88b56d900df8e34a61e03d0d5 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Wed, 5 Jun 2024 10:07:28 +0200 Subject: [PATCH 08/18] bump dependency ttempdir to v0.3.1 --- go.mod | 2 +- go.sum | 4 +-- pkg/golinters/ttempdir/testdata/ttempdir.go | 34 ++++++++++----------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/go.mod b/go.mod index 68f234388022..e7ab4df677d8 100644 --- a/go.mod +++ b/go.mod @@ -83,7 +83,7 @@ require ( github.com/nishanths/exhaustive v0.12.0 github.com/nishanths/predeclared v0.2.2 github.com/nunnatsa/ginkgolinter v0.16.2 - github.com/peczenyj/ttempdir v0.3.0 + github.com/peczenyj/ttempdir v0.3.1 github.com/pelletier/go-toml/v2 v2.2.2 github.com/polyfloyd/go-errorlint v1.5.2 github.com/quasilyte/go-ruleguard/dsl v0.3.22 diff --git a/go.sum b/go.sum index 6cf378dd6544..f4d9b738cad9 100644 --- a/go.sum +++ b/go.sum @@ -413,8 +413,8 @@ github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJ github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs= github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo= github.com/otiai10/mint v1.3.1/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc= -github.com/peczenyj/ttempdir v0.3.0 h1:xMUqiPBueA0/5IIs28KoS48Zdbwf5PgkJ43hPCg8FPY= -github.com/peczenyj/ttempdir v0.3.0/go.mod h1:jXgHoYn33YfuzChRNf5t0d76cTlzY+7Le+Fp+9cmzwY= +github.com/peczenyj/ttempdir v0.3.1 h1:BYj2gpOcjdR/VVjNZgWNTT5jpo2HKj1NwpFZ/KJ3IME= +github.com/peczenyj/ttempdir v0.3.1/go.mod h1:jXgHoYn33YfuzChRNf5t0d76cTlzY+7Le+Fp+9cmzwY= github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= diff --git a/pkg/golinters/ttempdir/testdata/ttempdir.go b/pkg/golinters/ttempdir/testdata/ttempdir.go index 94ff47f559ff..a0f4648cbcba 100644 --- a/pkg/golinters/ttempdir/testdata/ttempdir.go +++ b/pkg/golinters/ttempdir/testdata/ttempdir.go @@ -12,8 +12,8 @@ var ( ) func testsetup() { - os.MkdirTemp("a", "b") // if -all = true, want "os\\.MkdirTemp\\(\\) can be replaced by `testing\\.TempDir\\(\\)` in testsetup" - _, err := os.MkdirTemp("a", "b") // if -all = true, want "os\\.MkdirTemp\\(\\) can be replaced by `testing\\.TempDir\\(\\)` in testsetup" + os.MkdirTemp("a", "b") // if -all = true, want "os\\.MkdirTemp\\(\\) should be replaced by `testing\\.TempDir\\(\\)` in testsetup" + _, err := os.MkdirTemp("a", "b") // if -all = true, want "os\\.MkdirTemp\\(\\) should be replaced by `testing\\.TempDir\\(\\)` in testsetup" if err != nil { _ = err } @@ -22,38 +22,38 @@ func testsetup() { func TestF(t *testing.T) { testsetup() - os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `t\\.TempDir\\(\\)` in TestF" - _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `t\\.TempDir\\(\\)` in TestF" + os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestF" + _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestF" _ = err - if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) can be replaced by `t\\.TempDir\\(\\)` in TestF" + if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestF" _ = err } } func BenchmarkF(b *testing.B) { TB(b) - os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `b\\.TempDir\\(\\)` in BenchmarkF" - _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `b\\.TempDir\\(\\)` in BenchmarkF" + os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BenchmarkF" + _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BenchmarkF" _ = err - if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) can be replaced by `b\\.TempDir\\(\\)` in BenchmarkF" + if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BenchmarkF" _ = err } } func TB(tb testing.TB) { - os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `tb\\.TempDir\\(\\)` in TB" - _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `tb\\.TempDir\\(\\)` in TB" + os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TB" + _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TB" _ = err - if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) can be replaced by `tb\\.TempDir\\(\\)` in TB" + if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TB" _ = err } } func FuzzF(f *testing.F) { - os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `f\\.TempDir\\(\\)` in FuzzF" - _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `f\\.TempDir\\(\\)` in FuzzF" + os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FuzzF" + _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FuzzF" _ = err - if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) can be replaced by `f\\.TempDir\\(\\)` in FuzzF" + if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FuzzF" _ = err } } @@ -61,10 +61,10 @@ func FuzzF(f *testing.F) { func TestFunctionLiteral(t *testing.T) { testsetup() t.Run("test", func(t *testing.T) { - os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `t\\.TempDir\\(\\)` in anonymous function" - _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `t\\.TempDir\\(\\)` in anonymous function" + os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in anonymous function" + _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in anonymous function" _ = err - if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) can be replaced by `t\\.TempDir\\(\\)` in anonymous function" + if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in anonymous function" _ = err } }) From e4a55e02a922ffa505f0c44c72a89c4f75c543f2 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Wed, 5 Jun 2024 15:21:59 +0200 Subject: [PATCH 09/18] bump ttempdir to version v0.3.2 --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index e7ab4df677d8..e0517d9b5773 100644 --- a/go.mod +++ b/go.mod @@ -83,7 +83,7 @@ require ( github.com/nishanths/exhaustive v0.12.0 github.com/nishanths/predeclared v0.2.2 github.com/nunnatsa/ginkgolinter v0.16.2 - github.com/peczenyj/ttempdir v0.3.1 + github.com/peczenyj/ttempdir v0.3.2 github.com/pelletier/go-toml/v2 v2.2.2 github.com/polyfloyd/go-errorlint v1.5.2 github.com/quasilyte/go-ruleguard/dsl v0.3.22 diff --git a/go.sum b/go.sum index f4d9b738cad9..2e3df3d9e7bb 100644 --- a/go.sum +++ b/go.sum @@ -413,8 +413,8 @@ github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJ github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs= github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo= github.com/otiai10/mint v1.3.1/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc= -github.com/peczenyj/ttempdir v0.3.1 h1:BYj2gpOcjdR/VVjNZgWNTT5jpo2HKj1NwpFZ/KJ3IME= -github.com/peczenyj/ttempdir v0.3.1/go.mod h1:jXgHoYn33YfuzChRNf5t0d76cTlzY+7Le+Fp+9cmzwY= +github.com/peczenyj/ttempdir v0.3.2 h1:caE8Y7SMZD+zTnP/AQOMmLBo89u1zWvHDqVk/W4wrL8= +github.com/peczenyj/ttempdir v0.3.2/go.mod h1:jXgHoYn33YfuzChRNf5t0d76cTlzY+7Le+Fp+9cmzwY= github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= From 152c26eac7baf1f66f0e09b9faaa949a7ed47dd1 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Mon, 10 Jun 2024 10:03:19 +0200 Subject: [PATCH 10/18] fix default configuration --- pkg/config/linters_settings.go | 4 +++ .../{ttempdir.go => os_mkdirtemp_test.go} | 36 ++++++++++++++++--- .../ttempdir/testdata/ttempdir_mrl.yml | 4 +++ 3 files changed, 40 insertions(+), 4 deletions(-) rename pkg/golinters/ttempdir/testdata/{ttempdir.go => os_mkdirtemp_test.go} (67%) create mode 100644 pkg/golinters/ttempdir/testdata/ttempdir_mrl.yml diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index 40cb58f4ba7d..7d13452a17f6 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -159,6 +159,10 @@ var defaultLintersSettings = LintersSettings{ SkipRegexp: `(export|internal)_test\.go`, AllowPackages: []string{"main"}, }, + Ttempdir: TtempdirSettings{ + All: false, + MaxRecursionLevel: 5, + }, Unparam: UnparamSettings{ Algo: "cha", }, diff --git a/pkg/golinters/ttempdir/testdata/ttempdir.go b/pkg/golinters/ttempdir/testdata/os_mkdirtemp_test.go similarity index 67% rename from pkg/golinters/ttempdir/testdata/ttempdir.go rename to pkg/golinters/ttempdir/testdata/os_mkdirtemp_test.go index a0f4648cbcba..3b2ee8451a9c 100644 --- a/pkg/golinters/ttempdir/testdata/ttempdir.go +++ b/pkg/golinters/ttempdir/testdata/os_mkdirtemp_test.go @@ -1,8 +1,8 @@ //golangcitest:args -Ettempdir -//golangcitest:config_path testdata/ttempdir_all.yml package testdata import ( + "fmt" "os" "testing" ) @@ -12,12 +12,12 @@ var ( ) func testsetup() { - os.MkdirTemp("a", "b") // if -all = true, want "os\\.MkdirTemp\\(\\) should be replaced by `testing\\.TempDir\\(\\)` in testsetup" - _, err := os.MkdirTemp("a", "b") // if -all = true, want "os\\.MkdirTemp\\(\\) should be replaced by `testing\\.TempDir\\(\\)` in testsetup" + os.MkdirTemp("a", "b") // never seen + _, err := os.MkdirTemp("a", "b") // never seen if err != nil { _ = err } - os.MkdirTemp("a", "b") // if -all = true, "func setup is not using testing.TempDir" + os.MkdirTemp("a", "b") // never seen } func TestF(t *testing.T) { @@ -77,3 +77,31 @@ func TestEmpty(t *testing.T) { func TestEmptyTB(t *testing.T) { func(testing.TB) {}(t) } + +func TestTDD(t *testing.T) { + for _, tt := range []struct { + name string + }{ + {"test"}, + } { + t.Run(tt.name, func(t *testing.T) { + os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in anonymous function" + _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in anonymous function" + _ = err + if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in anonymous function" + _ = err + } + }) + } +} + +func TestLoop(t *testing.T) { + for i := 0; i < 3; i++ { + os.MkdirTemp(fmt.Sprintf("a%d", i), "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestLoop" + _, err := os.MkdirTemp(fmt.Sprintf("a%d", i), "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestLoop" + _ = err + if _, err := os.MkdirTemp(fmt.Sprintf("a%d", i), "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestLoop" + _ = err + } + } +} diff --git a/pkg/golinters/ttempdir/testdata/ttempdir_mrl.yml b/pkg/golinters/ttempdir/testdata/ttempdir_mrl.yml new file mode 100644 index 000000000000..199fb2a1e999 --- /dev/null +++ b/pkg/golinters/ttempdir/testdata/ttempdir_mrl.yml @@ -0,0 +1,4 @@ +linters-settings: + ttempdir: + all: false + max-recursion-level: 10 From 463e125f9cabf5260ff768b702202eed5247e201 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Mon, 10 Jun 2024 10:18:15 +0200 Subject: [PATCH 11/18] add more integration tests --- .../ttempdir/testdata/ioutil_tempdir.go | 58 +++++++++ .../ttempdir/testdata/ioutil_tempdir_test.go | 78 ++++++++++++ .../ttempdir/testdata/os_mkdirtemp.go | 79 ++++++++++++ pkg/golinters/ttempdir/testdata/os_tempdir.go | 53 ++++++++ .../ttempdir/testdata/os_tempdir_test.go | 114 ++++++++++++++++++ .../ttempdir/testdata/ttempdir_all.go | 59 +++++++++ .../ttempdir/testdata/ttempdir_all_test.go | 82 +++++++++++++ ...l.yml => ttempdir_max_recursion_level.yml} | 0 .../ttempdir_max_recursion_level_test.go | 51 ++++++++ 9 files changed, 574 insertions(+) create mode 100644 pkg/golinters/ttempdir/testdata/ioutil_tempdir.go create mode 100644 pkg/golinters/ttempdir/testdata/ioutil_tempdir_test.go create mode 100644 pkg/golinters/ttempdir/testdata/os_mkdirtemp.go create mode 100644 pkg/golinters/ttempdir/testdata/os_tempdir.go create mode 100644 pkg/golinters/ttempdir/testdata/os_tempdir_test.go create mode 100644 pkg/golinters/ttempdir/testdata/ttempdir_all.go create mode 100644 pkg/golinters/ttempdir/testdata/ttempdir_all_test.go rename pkg/golinters/ttempdir/testdata/{ttempdir_mrl.yml => ttempdir_max_recursion_level.yml} (100%) create mode 100644 pkg/golinters/ttempdir/testdata/ttempdir_max_recursion_level_test.go diff --git a/pkg/golinters/ttempdir/testdata/ioutil_tempdir.go b/pkg/golinters/ttempdir/testdata/ioutil_tempdir.go new file mode 100644 index 000000000000..7a71e598bc3f --- /dev/null +++ b/pkg/golinters/ttempdir/testdata/ioutil_tempdir.go @@ -0,0 +1,58 @@ +//golangcitest:args -Ettempdir +package testdata + +import ( + "io/ioutil" + "testing" +) + +var ( + _, ee = ioutil.TempDir("a", "b") // never seen +) + +func setup() { + ioutil.TempDir("a", "b") // never seen + _, err := ioutil.TempDir("a", "b") // never seen + if err != nil { + _ = err + } + ioutil.TempDir("a", "b") // never seen +} + +func F(t *testing.T) { + setup() + ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in F" + _, err := ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in F" + _ = err + if _, err := ioutil.TempDir("a", "b"); err != nil { // want "ioutil\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in F" + _ = err + } +} + +func BF(b *testing.B) { + TBF(b) + ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BF" + _, err := ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BF" + _ = err + if _, err := ioutil.TempDir("a", "b"); err != nil { // want "ioutil\\.TempDir\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BF" + _ = err + } +} + +func TBF(tb testing.TB) { + ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TBF" + _, err := ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TBF" + _ = err + if _, err := ioutil.TempDir("a", "b"); err != nil { // want "ioutil\\.TempDir\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TBF" + _ = err + } +} + +func FF(f *testing.F) { + ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FF" + _, err := ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FF" + _ = err + if _, err := ioutil.TempDir("a", "b"); err != nil { // want "ioutil\\.TempDir\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FF" + _ = err + } +} diff --git a/pkg/golinters/ttempdir/testdata/ioutil_tempdir_test.go b/pkg/golinters/ttempdir/testdata/ioutil_tempdir_test.go new file mode 100644 index 000000000000..8b402a860e41 --- /dev/null +++ b/pkg/golinters/ttempdir/testdata/ioutil_tempdir_test.go @@ -0,0 +1,78 @@ +//golangcitest:args -Ettempdir +package testdata + +import ( + "io/ioutil" + "testing" +) + +var ( + _, e = ioutil.TempDir("a", "b") // never seen +) + +func testsetup() { + ioutil.TempDir("a", "b") // if -all = true, want "ioutil\\.TempDir\\(\\) should be replaced by `testing\\.TempDir\\(\\)` in testsetup" + _, err := ioutil.TempDir("a", "b") // if -all = true, want "ioutil\\.TempDir\\(\\) should be replaced by `testing\\.TempDir\\(\\)` in testsetup" + if err != nil { + _ = err + } + ioutil.TempDir("a", "b") // if -all = true, "func setup is not using testing.TempDir" +} + +func TestF(t *testing.T) { + testsetup() + ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestF" + _, err := ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestF" + _ = err + if _, err := ioutil.TempDir("a", "b"); err != nil { // want "ioutil\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestF" + _ = err + } +} + +func BenchmarkF(b *testing.B) { + TB(b) + ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BenchmarkF" + _, err := ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BenchmarkF" + _ = err + if _, err := ioutil.TempDir("a", "b"); err != nil { // want "ioutil\\.TempDir\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BenchmarkF" + _ = err + } +} + +func TB(tb testing.TB) { + ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TB" + _, err := ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TB" + _ = err + if _, err := ioutil.TempDir("a", "b"); err != nil { // want "ioutil\\.TempDir\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TB" + _ = err + } +} + +func FuzzF(f *testing.F) { + ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FuzzF" + _, err := ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FuzzF" + _ = err + if _, err := ioutil.TempDir("a", "b"); err != nil { // want "ioutil\\.TempDir\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FuzzF" + _ = err + } +} + +func TestFunctionLiteral(t *testing.T) { + testsetup() + t.Run("test", func(t *testing.T) { + ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in anonymous function" + _, err := ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in anonymous function" + _ = err + if _, err := ioutil.TempDir("a", "b"); err != nil { // want "ioutil\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in anonymous function" + _ = err + } + }) +} + +func TestEmpty(t *testing.T) { + t.Run("test", func(*testing.T) {}) +} + +func TestEmptyTB(t *testing.T) { + func(testing.TB) {}(t) +} diff --git a/pkg/golinters/ttempdir/testdata/os_mkdirtemp.go b/pkg/golinters/ttempdir/testdata/os_mkdirtemp.go new file mode 100644 index 000000000000..b2d2b94aa37e --- /dev/null +++ b/pkg/golinters/ttempdir/testdata/os_mkdirtemp.go @@ -0,0 +1,79 @@ +//golangcitest:args -Ettempdir +package testdata + +import ( + "os" + "testing" +) + +var ( + _, ee = os.MkdirTemp("a", "b") // never seen +) + +func setup() { + os.MkdirTemp("a", "b") // never seen + _, err := os.MkdirTemp("a", "b") // never seen + if err != nil { + _ = err + } + os.MkdirTemp("a", "b") // never seen +} + +func F(t *testing.T) { + setup() + os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in F" + _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in F" + _ = err + if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in F" + _ = err + } + + _ = func(t *testing.T) { + _ = t + _, _ = os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in anonymous function" + } + + t.Cleanup(func() { + _, _ = os.MkdirTemp("a", "b") + }) +} + +func BF(b *testing.B) { + TBF(b) + os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BF" + _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BF" + _ = err + if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BF" + _ = err + } + + func(b *testing.B) { + _ = b + _, _ = os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `b\\.TempDir\\(\\)` in anonymous function" + }(b) +} + +func TBF(tb testing.TB) { + os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TBF" + _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TBF" + _ = err + if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TBF" + _ = err + } + + defer func(tb testing.TB) { + _ = tb + _, _ = os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in anonymous function" + }(tb) +} + +func FF(f *testing.F) { + os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FF" + _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FF" + _ = err + if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FF" + _ = err + } + + defer os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FF" +} diff --git a/pkg/golinters/ttempdir/testdata/os_tempdir.go b/pkg/golinters/ttempdir/testdata/os_tempdir.go new file mode 100644 index 000000000000..993cc73267b7 --- /dev/null +++ b/pkg/golinters/ttempdir/testdata/os_tempdir.go @@ -0,0 +1,53 @@ +//golangcitest:args -Ettempdir +package testdata + +import ( + "os" + "testing" +) + +var ( + dir = os.TempDir() // never seen +) + +func setup() { + os.TempDir() // never seen + dir := os.TempDir() // never seen + _ = dir + _ = os.TempDir() // never seen +} + +func F(t *testing.T) { + setup() + os.TempDir() // want "os\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in F" + t.Log(os.TempDir()) // want "os\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in F" + _ = os.TempDir() // want "os\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in F" + if dir := os.TempDir(); dir != "" { // want "os\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in F" + _ = dir + } +} + +func BF(b *testing.B) { + TBF(b) + os.TempDir() // want "os\\.TempDir\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BF" + _ = os.TempDir() // want "os\\.TempDir\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BF" + if dir := os.TempDir(); dir != "" { // want "os\\.TempDir\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BF" + _ = dir + } +} + +func TBF(tb testing.TB) { + os.TempDir() // want "os\\.TempDir\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TBF" + _ = os.TempDir() // want "os\\.TempDir\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TBF" + if dir := os.TempDir(); dir != "" { // want "os\\.TempDir\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TBF" + _ = dir + } +} + +func FF(f *testing.F) { + os.TempDir() // want "os\\.TempDir\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FF" + _ = os.TempDir() // want "os\\.TempDir\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FF" + if dir := os.TempDir(); dir != "" { // want "os\\.TempDir\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FF" + _ = dir + } +} diff --git a/pkg/golinters/ttempdir/testdata/os_tempdir_test.go b/pkg/golinters/ttempdir/testdata/os_tempdir_test.go new file mode 100644 index 000000000000..7a1941d3abab --- /dev/null +++ b/pkg/golinters/ttempdir/testdata/os_tempdir_test.go @@ -0,0 +1,114 @@ +//golangcitest:args -Ettempdir +package testdata + +import ( + "fmt" + "os" + "path/filepath" + "testing" +) + +var ( + tdir = os.TempDir() // never seen +) + +func testsetup() { + os.TempDir() // if -all = true, want "os\\.TempDir\\(\\) should be replaced by `testing\\.TempDir\\(\\)` in testsetup" + dir := os.TempDir() // if -all = true, want "os\\.TempDir\\(\\) should be replaced by `testing\\.TempDir\\(\\)` in testsetup" + _ = dir + _ = os.TempDir() // if -all = true, "func setup is not using testing.TempDir" +} + +func TestF(t *testing.T) { + testsetup() + os.TempDir() // want "os\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestF" + _ = os.TempDir() // want "os\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestF" + if dir = os.TempDir(); dir != "" { // want "os\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestF" + _ = dir + } +} + +func BenchmarkF(b *testing.B) { + TB(b) + os.TempDir() // want "os\\.TempDir\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BenchmarkF" + _ = os.TempDir() // want "os\\.TempDir\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BenchmarkF" + if dir = os.TempDir(); dir != "" { // want "os\\.TempDir\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BenchmarkF" + _ = dir + } +} + +func TB(tb testing.TB) { + os.TempDir() // want "os\\.TempDir\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TB" + _ = os.TempDir() // want "os\\.TempDir\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TB" + if dir = os.TempDir(); dir != "" { // want "os\\.TempDir\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TB" + _ = dir + } +} + +func FuzzF(f *testing.F) { + os.TempDir() // want "os\\.TempDir\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FuzzF" + _ = os.TempDir() // want "os\\.TempDir\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FuzzF" + if dir = os.TempDir(); dir != "" { // want "os\\.TempDir\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FuzzF" + _ = dir + } +} + +func TestFunctionLiteral(t *testing.T) { + testsetup() + t.Run("test", func(t *testing.T) { + os.TempDir() // want "os\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in anonymous function" + _ = os.TempDir() // want "os\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in anonymous function" + if dir = os.TempDir(); dir != "" { // want "os\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in anonymous function" + _ = dir + } + }) +} + +func TestEmpty(t *testing.T) { + t.Run("test", func(*testing.T) {}) +} + +func TestEmptyTB(t *testing.T) { + func(testing.TB) {}(t) +} + +func TestRecursive(t *testing.T) { + t.Log( // recursion level 1 + os.TempDir(), // want "os\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestRecursive" + ) + t.Log( // recursion level 1 + fmt.Sprintf("%s", // recursion level 2 + os.TempDir(), // want "os\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestRecursive" + ), + ) + t.Log( // recursion level 1 + filepath.Clean( // recursion level 2 + fmt.Sprintf("%s", // recursion level 3 + os.TempDir(), // want "os\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestRecursive" + ), + ), + ) + t.Log( // recursion level 1 + filepath.Join( // recursion level 2 + filepath.Clean( // recursion level 3 + fmt.Sprintf("%s", // recursion level 4 + os.TempDir(), // want "os\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestRecursive" + ), + ), + "test", + ), + ) + t.Log( // recursion level 1 + fmt.Sprintf("%s/foo-%d", // recursion level 2 + filepath.Join( // recursion level 3 + filepath.Clean( // recursion level 4 + fmt.Sprintf("%s", // recursion level 5 + os.TempDir(), // max recursion level reached. + ), + ), + "test", + ), + 1024, + ), + ) +} diff --git a/pkg/golinters/ttempdir/testdata/ttempdir_all.go b/pkg/golinters/ttempdir/testdata/ttempdir_all.go new file mode 100644 index 000000000000..de60d74f4287 --- /dev/null +++ b/pkg/golinters/ttempdir/testdata/ttempdir_all.go @@ -0,0 +1,59 @@ +//golangcitest:args -Ettempdir +//golangcitest:config_path testdata/ttempdir_all.yml +package testdata + +import ( + "os" + "testing" +) + +var ( + _, ee = os.MkdirTemp("a", "b") // never seen +) + +func setup() { + os.MkdirTemp("a", "b") // never seen + _, err := os.MkdirTemp("a", "b") // never seen + if err != nil { + _ = err + } + os.MkdirTemp("a", "b") // never seen +} + +func F(t *testing.T) { + setup() + os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in F" + _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in F" + _ = err + if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in F" + _ = err + } +} + +func BF(b *testing.B) { + TBF(b) + os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BF" + _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BF" + _ = err + if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BF" + _ = err + } +} + +func TBF(tb testing.TB) { + os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TBF" + _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TBF" + _ = err + if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TBF" + _ = err + } +} + +func FF(f *testing.F) { + os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FF" + _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FF" + _ = err + if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FF" + _ = err + } +} diff --git a/pkg/golinters/ttempdir/testdata/ttempdir_all_test.go b/pkg/golinters/ttempdir/testdata/ttempdir_all_test.go new file mode 100644 index 000000000000..d41db499597d --- /dev/null +++ b/pkg/golinters/ttempdir/testdata/ttempdir_all_test.go @@ -0,0 +1,82 @@ +//golangcitest:args -Ettempdir +//golangcitest:config_path testdata/ttempdir_all.yml +package testdata + +import ( + "os" + "testing" +) + +var ( + _, e = os.MkdirTemp("a", "b") // never seen +) + +func testsetup() { + os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `testing\\.TempDir\\(\\)` in testsetup" + _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `testing\\.TempDir\\(\\)` in testsetup" + if err != nil { + _ = err + } + os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `testing\\.TempDir\\(\\)` in testsetup" +} + +func TestF(t *testing.T) { + testsetup() + os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestF" + _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestF" + _ = err + if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestF" + _ = err + } + t.Cleanup(func() { + _, _ = os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `testing\\.TempDir\\(\\)` in anonymous function" + }) +} + +func BenchmarkF(b *testing.B) { + TB(b) + os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BenchmarkF" + _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BenchmarkF" + _ = err + if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BenchmarkF" + _ = err + } +} + +func TB(tb testing.TB) { + os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TB" + _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TB" + _ = err + if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TB" + _ = err + } +} + +func FuzzF(f *testing.F) { + os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FuzzF" + _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FuzzF" + _ = err + if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FuzzF" + _ = err + } +} + +func TestFunctionLiteral(t *testing.T) { + testsetup() + t.Run("test", func(t *testing.T) { + os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in anonymous function" + _, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in anonymous function" + _ = err + if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in anonymous function" + _ = err + } + }) +} + +func TestEmpty(t *testing.T) { + t.Run("test", func(*testing.T) {}) +} + +func TestEmptyTB(t *testing.T) { + func(testing.TB) {}(t) +} diff --git a/pkg/golinters/ttempdir/testdata/ttempdir_mrl.yml b/pkg/golinters/ttempdir/testdata/ttempdir_max_recursion_level.yml similarity index 100% rename from pkg/golinters/ttempdir/testdata/ttempdir_mrl.yml rename to pkg/golinters/ttempdir/testdata/ttempdir_max_recursion_level.yml diff --git a/pkg/golinters/ttempdir/testdata/ttempdir_max_recursion_level_test.go b/pkg/golinters/ttempdir/testdata/ttempdir_max_recursion_level_test.go new file mode 100644 index 000000000000..2b44b4278f91 --- /dev/null +++ b/pkg/golinters/ttempdir/testdata/ttempdir_max_recursion_level_test.go @@ -0,0 +1,51 @@ +//golangcitest:args -Ettempdir +//golangcitest:config_path testdata/ttempdir_max_recursion_level.yml +package testdata + +import ( + "fmt" + "os" + "path/filepath" + "testing" +) + +func TestRecursive(t *testing.T) { + t.Log( // recursion level 1 + os.TempDir(), // want "os\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestRecursive" + ) + t.Log( // recursion level 1 + fmt.Sprintf("%s", // recursion level 2 + os.TempDir(), // want "os\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestRecursive" + ), + ) + t.Log( // recursion level 1 + filepath.Clean( // recursion level 2 + fmt.Sprintf("%s", // recursion level 3 + os.TempDir(), // want "os\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestRecursive" + ), + ), + ) + t.Log( // recursion level 1 + filepath.Join( // recursion level 2 + filepath.Clean( // recursion level 3 + fmt.Sprintf("%s", // recursion level 4 + os.TempDir(), // want "os\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestRecursive" + ), + ), + "test", + ), + ) + t.Log( // recursion level 1 + fmt.Sprintf("%s/foo-%d", // recursion level 2 + filepath.Join( // recursion level 3 + filepath.Clean( // recursion level 4 + fmt.Sprintf("%s", // recursion level 5 + os.TempDir(), // want "os\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestRecursive" + ), + ), + "test", + ), + 1024, + ), + ) +} From e6d2071b87077121255e294ae334eb3b6e128d76 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Mon, 10 Jun 2024 10:26:26 +0200 Subject: [PATCH 12/18] add negative / happy path tests --- .../ttempdir/testdata/ttempdir_happy_path.go | 54 ++++++++ .../testdata/ttempdir_happy_path_test.go | 115 ++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 pkg/golinters/ttempdir/testdata/ttempdir_happy_path.go create mode 100644 pkg/golinters/ttempdir/testdata/ttempdir_happy_path_test.go diff --git a/pkg/golinters/ttempdir/testdata/ttempdir_happy_path.go b/pkg/golinters/ttempdir/testdata/ttempdir_happy_path.go new file mode 100644 index 000000000000..f5544b99d51a --- /dev/null +++ b/pkg/golinters/ttempdir/testdata/ttempdir_happy_path.go @@ -0,0 +1,54 @@ +//golangcitest:args -Ettempdir +//golangcitest:expected_exitcode 0 +package testdata + +import ( + "os" + "testing" +) + +var ( + dir = os.TempDir() // never seen +) + +func setup() { + os.TempDir() // never seen + dir := os.TempDir() // never seen + _ = dir + _ = os.TempDir() // never seen +} + +func F(t *testing.T) { + setup() + t.TempDir() // never seen + t.Log(t.TempDir()) // never seen + _ = t.TempDir() // never seen + if dir := t.TempDir(); dir != "" { // never seen + _ = dir + } +} + +func BF(b *testing.B) { + TBF(b) + b.TempDir() // never seen + _ = b.TempDir() // never seen + if dir := b.TempDir(); dir != "" { // never seen + _ = dir + } +} + +func TBF(tb testing.TB) { + tb.TempDir() // never seen + _ = tb.TempDir() // never seen + if dir := tb.TempDir(); dir != "" { // never seen + _ = dir + } +} + +func FF(f *testing.F) { + f.TempDir() // never seen + _ = f.TempDir() // never seen + if dir := f.TempDir(); dir != "" { // never seen + _ = dir + } +} diff --git a/pkg/golinters/ttempdir/testdata/ttempdir_happy_path_test.go b/pkg/golinters/ttempdir/testdata/ttempdir_happy_path_test.go new file mode 100644 index 000000000000..047dc982e9f6 --- /dev/null +++ b/pkg/golinters/ttempdir/testdata/ttempdir_happy_path_test.go @@ -0,0 +1,115 @@ +//golangcitest:args -Ettempdir +//golangcitest:expected_exitcode 0 +package testdata + +import ( + "fmt" + "os" + "path/filepath" + "testing" +) + +var ( + tdir = os.TempDir() // never seen +) + +func testsetup() { + os.TempDir() // never seen + dir := os.TempDir() // never seen + _ = dir + _ = os.TempDir() // never seen +} + +func TestF(t *testing.T) { + testsetup() + t.TempDir() // never seen + _ = t.TempDir() // never seen + if dir = t.TempDir(); dir != "" { // never seen + _ = dir + } +} + +func BenchmarkF(b *testing.B) { + TB(b) + b.TempDir() // never seen + _ = b.TempDir() // never seen + if dir = b.TempDir(); dir != "" { // never seen + _ = dir + } +} + +func TB(tb testing.TB) { + tb.TempDir() // never seen + _ = tb.TempDir() // never seen + if dir = tb.TempDir(); dir != "" { // never seen + _ = dir + } +} + +func FuzzF(f *testing.F) { + f.TempDir() // never seen + _ = f.TempDir() // never seen + if dir = f.TempDir(); dir != "" { // never seen + _ = dir + } +} + +func TestFunctionLiteral(t *testing.T) { + testsetup() + t.Run("test", func(t *testing.T) { + t.TempDir() // never seen + _ = t.TempDir() // never seen + if dir = t.TempDir(); dir != "" { // never seen + _ = dir + } + }) +} + +func TestEmpty(t *testing.T) { + t.Run("test", func(*testing.T) {}) +} + +func TestEmptyTB(t *testing.T) { + func(testing.TB) {}(t) +} + +func TestRecursive(t *testing.T) { + t.Log( // recursion level 1 + t.TempDir(), // never seen + ) + t.Log( // recursion level 1 + fmt.Sprintf("%s", // recursion level 2 + t.TempDir(), // never seen + ), + ) + t.Log( // recursion level 1 + filepath.Clean( // recursion level 2 + fmt.Sprintf("%s", // recursion level 3 + t.TempDir(), // never seen + ), + ), + ) + t.Log( // recursion level 1 + filepath.Join( // recursion level 2 + filepath.Clean( // recursion level 3 + fmt.Sprintf("%s", // recursion level 4 + t.TempDir(), // never seen + ), + ), + "test", + ), + ) + t.Log( // recursion level 1 + fmt.Sprintf("%s/foo-%d", // recursion level 2 + filepath.Join( // recursion level 3 + filepath.Clean( // recursion level 4 + fmt.Sprintf("%s", // recursion level 5 + os.TempDir(), // max recursion level reached. + ), + ), + "test", + ), + 1024, + ), + ) +} From 4a42fce043a19a06ab071c27d59c48ac15a34612 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Mon, 10 Jun 2024 10:26:39 +0200 Subject: [PATCH 13/18] bump version to ttempdir to v0.4.1 --- go.mod | 4 ++-- go.sum | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index f663fe97d11e..652f85596c62 100644 --- a/go.mod +++ b/go.mod @@ -83,7 +83,7 @@ require ( github.com/nishanths/exhaustive v0.12.0 github.com/nishanths/predeclared v0.2.2 github.com/nunnatsa/ginkgolinter v0.16.2 - github.com/peczenyj/ttempdir v0.3.2 + github.com/peczenyj/ttempdir v0.4.1 github.com/pelletier/go-toml/v2 v2.2.2 github.com/polyfloyd/go-errorlint v1.5.2 github.com/quasilyte/go-ruleguard/dsl v0.3.22 @@ -194,7 +194,7 @@ require ( golang.org/x/mod v0.18.0 // indirect golang.org/x/sync v0.7.0 // indirect golang.org/x/sys v0.21.0 // indirect - golang.org/x/text v0.15.0 // indirect + golang.org/x/text v0.16.0 // indirect google.golang.org/protobuf v1.33.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect diff --git a/go.sum b/go.sum index 797b07fc6215..8693835fc6c7 100644 --- a/go.sum +++ b/go.sum @@ -413,8 +413,8 @@ github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJ github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs= github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo= github.com/otiai10/mint v1.3.1/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc= -github.com/peczenyj/ttempdir v0.3.2 h1:caE8Y7SMZD+zTnP/AQOMmLBo89u1zWvHDqVk/W4wrL8= -github.com/peczenyj/ttempdir v0.3.2/go.mod h1:jXgHoYn33YfuzChRNf5t0d76cTlzY+7Le+Fp+9cmzwY= +github.com/peczenyj/ttempdir v0.4.1 h1:fOl4T3ugtb4Pinmzk+t3ShvBCQYIqF/juRTBZW69QXI= +github.com/peczenyj/ttempdir v0.4.1/go.mod h1:zK2cEgOq73piefMNWglvwqc7w6r4VTcAv2KJA2emj9k= github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= @@ -806,8 +806,8 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= -golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= +golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= From fbac5f327d1a05b8408c21d724b5a571cb0335f5 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Mon, 10 Jun 2024 10:32:19 +0200 Subject: [PATCH 14/18] remove WithLoadForGoAnalysis from lintersdb/builder_linter --- pkg/lint/lintersdb/builder_linter.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/lint/lintersdb/builder_linter.go b/pkg/lint/lintersdb/builder_linter.go index 732663bec170..09e14c28e5ed 100644 --- a/pkg/lint/lintersdb/builder_linter.go +++ b/pkg/lint/lintersdb/builder_linter.go @@ -761,7 +761,6 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) { linter.NewConfig(ttempdir.New(&cfg.LintersSettings.Ttempdir)). WithSince("v1.60.0"). WithPresets(linter.PresetStyle). - WithLoadForGoAnalysis(). WithURL("https://github.com/peczenyj/ttempdir"), linter.NewConfig(golinters.NewTypecheck()). From 9cb7fb2f0a6f839e1eda3c7c17efe30f5590e985 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Mon, 10 Jun 2024 10:40:05 +0200 Subject: [PATCH 15/18] include preset test --- pkg/lint/lintersdb/builder_linter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/lint/lintersdb/builder_linter.go b/pkg/lint/lintersdb/builder_linter.go index 09e14c28e5ed..a710a2e23caa 100644 --- a/pkg/lint/lintersdb/builder_linter.go +++ b/pkg/lint/lintersdb/builder_linter.go @@ -760,7 +760,7 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) { linter.NewConfig(ttempdir.New(&cfg.LintersSettings.Ttempdir)). WithSince("v1.60.0"). - WithPresets(linter.PresetStyle). + WithPresets(linter.PresetStyle, linter.PresetTest). WithURL("https://github.com/peczenyj/ttempdir"), linter.NewConfig(golinters.NewTypecheck()). From 6e85554ea7fee7f414a6c2c45ada2b53626700e0 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Mon, 10 Jun 2024 13:07:09 +0200 Subject: [PATCH 16/18] review --- .../ttempdir/testdata/ioutil_tempdir.go | 58 -------------- .../ttempdir/testdata/ioutil_tempdir_test.go | 78 ------------------- ...path.go => ttempdir_default_happy_path.go} | 0 ...go => ttempdir_default_happy_path_test.go} | 0 ...rtemp.go => ttempdir_default_mkdirtemp.go} | 0 ....go => ttempdir_default_mkdirtemp_test.go} | 0 ...tempdir.go => ttempdir_default_tempdir.go} | 0 ...st.go => ttempdir_default_tempdir_test.go} | 0 .../testdata/ttempdir_max_recursion_level.yml | 4 - .../ttempdir_max_recursion_level_test.go | 51 ------------ 10 files changed, 191 deletions(-) delete mode 100644 pkg/golinters/ttempdir/testdata/ioutil_tempdir.go delete mode 100644 pkg/golinters/ttempdir/testdata/ioutil_tempdir_test.go rename pkg/golinters/ttempdir/testdata/{ttempdir_happy_path.go => ttempdir_default_happy_path.go} (100%) rename pkg/golinters/ttempdir/testdata/{ttempdir_happy_path_test.go => ttempdir_default_happy_path_test.go} (100%) rename pkg/golinters/ttempdir/testdata/{os_mkdirtemp.go => ttempdir_default_mkdirtemp.go} (100%) rename pkg/golinters/ttempdir/testdata/{os_mkdirtemp_test.go => ttempdir_default_mkdirtemp_test.go} (100%) rename pkg/golinters/ttempdir/testdata/{os_tempdir.go => ttempdir_default_tempdir.go} (100%) rename pkg/golinters/ttempdir/testdata/{os_tempdir_test.go => ttempdir_default_tempdir_test.go} (100%) delete mode 100644 pkg/golinters/ttempdir/testdata/ttempdir_max_recursion_level.yml delete mode 100644 pkg/golinters/ttempdir/testdata/ttempdir_max_recursion_level_test.go diff --git a/pkg/golinters/ttempdir/testdata/ioutil_tempdir.go b/pkg/golinters/ttempdir/testdata/ioutil_tempdir.go deleted file mode 100644 index 7a71e598bc3f..000000000000 --- a/pkg/golinters/ttempdir/testdata/ioutil_tempdir.go +++ /dev/null @@ -1,58 +0,0 @@ -//golangcitest:args -Ettempdir -package testdata - -import ( - "io/ioutil" - "testing" -) - -var ( - _, ee = ioutil.TempDir("a", "b") // never seen -) - -func setup() { - ioutil.TempDir("a", "b") // never seen - _, err := ioutil.TempDir("a", "b") // never seen - if err != nil { - _ = err - } - ioutil.TempDir("a", "b") // never seen -} - -func F(t *testing.T) { - setup() - ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in F" - _, err := ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in F" - _ = err - if _, err := ioutil.TempDir("a", "b"); err != nil { // want "ioutil\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in F" - _ = err - } -} - -func BF(b *testing.B) { - TBF(b) - ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BF" - _, err := ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BF" - _ = err - if _, err := ioutil.TempDir("a", "b"); err != nil { // want "ioutil\\.TempDir\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BF" - _ = err - } -} - -func TBF(tb testing.TB) { - ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TBF" - _, err := ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TBF" - _ = err - if _, err := ioutil.TempDir("a", "b"); err != nil { // want "ioutil\\.TempDir\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TBF" - _ = err - } -} - -func FF(f *testing.F) { - ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FF" - _, err := ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FF" - _ = err - if _, err := ioutil.TempDir("a", "b"); err != nil { // want "ioutil\\.TempDir\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FF" - _ = err - } -} diff --git a/pkg/golinters/ttempdir/testdata/ioutil_tempdir_test.go b/pkg/golinters/ttempdir/testdata/ioutil_tempdir_test.go deleted file mode 100644 index 8b402a860e41..000000000000 --- a/pkg/golinters/ttempdir/testdata/ioutil_tempdir_test.go +++ /dev/null @@ -1,78 +0,0 @@ -//golangcitest:args -Ettempdir -package testdata - -import ( - "io/ioutil" - "testing" -) - -var ( - _, e = ioutil.TempDir("a", "b") // never seen -) - -func testsetup() { - ioutil.TempDir("a", "b") // if -all = true, want "ioutil\\.TempDir\\(\\) should be replaced by `testing\\.TempDir\\(\\)` in testsetup" - _, err := ioutil.TempDir("a", "b") // if -all = true, want "ioutil\\.TempDir\\(\\) should be replaced by `testing\\.TempDir\\(\\)` in testsetup" - if err != nil { - _ = err - } - ioutil.TempDir("a", "b") // if -all = true, "func setup is not using testing.TempDir" -} - -func TestF(t *testing.T) { - testsetup() - ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestF" - _, err := ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestF" - _ = err - if _, err := ioutil.TempDir("a", "b"); err != nil { // want "ioutil\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestF" - _ = err - } -} - -func BenchmarkF(b *testing.B) { - TB(b) - ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BenchmarkF" - _, err := ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BenchmarkF" - _ = err - if _, err := ioutil.TempDir("a", "b"); err != nil { // want "ioutil\\.TempDir\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BenchmarkF" - _ = err - } -} - -func TB(tb testing.TB) { - ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TB" - _, err := ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TB" - _ = err - if _, err := ioutil.TempDir("a", "b"); err != nil { // want "ioutil\\.TempDir\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TB" - _ = err - } -} - -func FuzzF(f *testing.F) { - ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FuzzF" - _, err := ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FuzzF" - _ = err - if _, err := ioutil.TempDir("a", "b"); err != nil { // want "ioutil\\.TempDir\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FuzzF" - _ = err - } -} - -func TestFunctionLiteral(t *testing.T) { - testsetup() - t.Run("test", func(t *testing.T) { - ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in anonymous function" - _, err := ioutil.TempDir("a", "b") // want "ioutil\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in anonymous function" - _ = err - if _, err := ioutil.TempDir("a", "b"); err != nil { // want "ioutil\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in anonymous function" - _ = err - } - }) -} - -func TestEmpty(t *testing.T) { - t.Run("test", func(*testing.T) {}) -} - -func TestEmptyTB(t *testing.T) { - func(testing.TB) {}(t) -} diff --git a/pkg/golinters/ttempdir/testdata/ttempdir_happy_path.go b/pkg/golinters/ttempdir/testdata/ttempdir_default_happy_path.go similarity index 100% rename from pkg/golinters/ttempdir/testdata/ttempdir_happy_path.go rename to pkg/golinters/ttempdir/testdata/ttempdir_default_happy_path.go diff --git a/pkg/golinters/ttempdir/testdata/ttempdir_happy_path_test.go b/pkg/golinters/ttempdir/testdata/ttempdir_default_happy_path_test.go similarity index 100% rename from pkg/golinters/ttempdir/testdata/ttempdir_happy_path_test.go rename to pkg/golinters/ttempdir/testdata/ttempdir_default_happy_path_test.go diff --git a/pkg/golinters/ttempdir/testdata/os_mkdirtemp.go b/pkg/golinters/ttempdir/testdata/ttempdir_default_mkdirtemp.go similarity index 100% rename from pkg/golinters/ttempdir/testdata/os_mkdirtemp.go rename to pkg/golinters/ttempdir/testdata/ttempdir_default_mkdirtemp.go diff --git a/pkg/golinters/ttempdir/testdata/os_mkdirtemp_test.go b/pkg/golinters/ttempdir/testdata/ttempdir_default_mkdirtemp_test.go similarity index 100% rename from pkg/golinters/ttempdir/testdata/os_mkdirtemp_test.go rename to pkg/golinters/ttempdir/testdata/ttempdir_default_mkdirtemp_test.go diff --git a/pkg/golinters/ttempdir/testdata/os_tempdir.go b/pkg/golinters/ttempdir/testdata/ttempdir_default_tempdir.go similarity index 100% rename from pkg/golinters/ttempdir/testdata/os_tempdir.go rename to pkg/golinters/ttempdir/testdata/ttempdir_default_tempdir.go diff --git a/pkg/golinters/ttempdir/testdata/os_tempdir_test.go b/pkg/golinters/ttempdir/testdata/ttempdir_default_tempdir_test.go similarity index 100% rename from pkg/golinters/ttempdir/testdata/os_tempdir_test.go rename to pkg/golinters/ttempdir/testdata/ttempdir_default_tempdir_test.go diff --git a/pkg/golinters/ttempdir/testdata/ttempdir_max_recursion_level.yml b/pkg/golinters/ttempdir/testdata/ttempdir_max_recursion_level.yml deleted file mode 100644 index 199fb2a1e999..000000000000 --- a/pkg/golinters/ttempdir/testdata/ttempdir_max_recursion_level.yml +++ /dev/null @@ -1,4 +0,0 @@ -linters-settings: - ttempdir: - all: false - max-recursion-level: 10 diff --git a/pkg/golinters/ttempdir/testdata/ttempdir_max_recursion_level_test.go b/pkg/golinters/ttempdir/testdata/ttempdir_max_recursion_level_test.go deleted file mode 100644 index 2b44b4278f91..000000000000 --- a/pkg/golinters/ttempdir/testdata/ttempdir_max_recursion_level_test.go +++ /dev/null @@ -1,51 +0,0 @@ -//golangcitest:args -Ettempdir -//golangcitest:config_path testdata/ttempdir_max_recursion_level.yml -package testdata - -import ( - "fmt" - "os" - "path/filepath" - "testing" -) - -func TestRecursive(t *testing.T) { - t.Log( // recursion level 1 - os.TempDir(), // want "os\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestRecursive" - ) - t.Log( // recursion level 1 - fmt.Sprintf("%s", // recursion level 2 - os.TempDir(), // want "os\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestRecursive" - ), - ) - t.Log( // recursion level 1 - filepath.Clean( // recursion level 2 - fmt.Sprintf("%s", // recursion level 3 - os.TempDir(), // want "os\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestRecursive" - ), - ), - ) - t.Log( // recursion level 1 - filepath.Join( // recursion level 2 - filepath.Clean( // recursion level 3 - fmt.Sprintf("%s", // recursion level 4 - os.TempDir(), // want "os\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestRecursive" - ), - ), - "test", - ), - ) - t.Log( // recursion level 1 - fmt.Sprintf("%s/foo-%d", // recursion level 2 - filepath.Join( // recursion level 3 - filepath.Clean( // recursion level 4 - fmt.Sprintf("%s", // recursion level 5 - os.TempDir(), // want "os\\.TempDir\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestRecursive" - ), - ), - "test", - ), - 1024, - ), - ) -} From d6a8cb62de970e96d72f2349368c6536209d5a59 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Mon, 10 Jun 2024 13:43:38 +0200 Subject: [PATCH 17/18] review: fix presets --- pkg/lint/lintersdb/builder_linter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/lint/lintersdb/builder_linter.go b/pkg/lint/lintersdb/builder_linter.go index a710a2e23caa..960647b9bad2 100644 --- a/pkg/lint/lintersdb/builder_linter.go +++ b/pkg/lint/lintersdb/builder_linter.go @@ -760,7 +760,7 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) { linter.NewConfig(ttempdir.New(&cfg.LintersSettings.Ttempdir)). WithSince("v1.60.0"). - WithPresets(linter.PresetStyle, linter.PresetTest). + WithPresets(linter.PresetTest). WithURL("https://github.com/peczenyj/ttempdir"), linter.NewConfig(golinters.NewTypecheck()). From f86b00c3546e23d309b16580cd5a08332d0daabe Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Mon, 10 Jun 2024 13:47:30 +0200 Subject: [PATCH 18/18] review --- pkg/golinters/ttempdir/ttempdir.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/golinters/ttempdir/ttempdir.go b/pkg/golinters/ttempdir/ttempdir.go index 0423bb91cf5c..0df141d0b385 100644 --- a/pkg/golinters/ttempdir/ttempdir.go +++ b/pkg/golinters/ttempdir/ttempdir.go @@ -9,12 +9,12 @@ import ( ) func New(settings *config.TtempdirSettings) *goanalysis.Linter { - ttempdirAnalyzer := analyzer.New() + a := analyzer.New() var cfg map[string]map[string]any if settings != nil { cfg = map[string]map[string]any{ - ttempdirAnalyzer.Name: { + a.Name: { analyzer.FlagAllName: settings.All, analyzer.FlagMaxRecursionLevelName: settings.MaxRecursionLevel, }, @@ -22,9 +22,9 @@ func New(settings *config.TtempdirSettings) *goanalysis.Linter { } return goanalysis.NewLinter( - ttempdirAnalyzer.Name, - ttempdirAnalyzer.Doc, - []*analysis.Analyzer{ttempdirAnalyzer}, + a.Name, + "Detects the use of os.MkdirTemp, ioutil.TempDir or os.TempDir instead of t.TempDir", + []*analysis.Analyzer{a}, cfg, ).WithLoadMode(goanalysis.LoadModeSyntax) }