From 4262635fc5d0d33c9dfd5fc360144a3ba3a1c70a Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Fri, 29 Nov 2024 00:38:54 +0100 Subject: [PATCH 1/4] feat: add UseTesting linter --- .golangci.next.reference.yml | 24 ++++++++++++++ go.mod | 1 + go.sum | 2 ++ jsonschema/golangci.next.jsonschema.json | 26 ++++++++++++++++ pkg/config/linters_settings.go | 15 +++++++++ .../usetesting/testdata/usetesting.go | 15 +++++++++ .../testdata/usetesting_configuration.go | 16 ++++++++++ .../testdata/usetesting_configuration.yml | 7 +++++ .../usetesting/testdata/usetesting_go124.go | 30 ++++++++++++++++++ .../usetesting_go124_configuration.go | 31 +++++++++++++++++++ .../usetesting_go124_configuration.yml | 7 +++++ pkg/golinters/usetesting/usetesting.go | 31 +++++++++++++++++++ .../usetesting/usetesting_integration_test.go | 11 +++++++ pkg/lint/linter/config.go | 12 +++++-- pkg/lint/lintersdb/builder_linter.go | 7 +++++ 15 files changed, 233 insertions(+), 2 deletions(-) create mode 100644 pkg/golinters/usetesting/testdata/usetesting.go create mode 100644 pkg/golinters/usetesting/testdata/usetesting_configuration.go create mode 100644 pkg/golinters/usetesting/testdata/usetesting_configuration.yml create mode 100644 pkg/golinters/usetesting/testdata/usetesting_go124.go create mode 100644 pkg/golinters/usetesting/testdata/usetesting_go124_configuration.go create mode 100644 pkg/golinters/usetesting/testdata/usetesting_go124_configuration.yml create mode 100644 pkg/golinters/usetesting/usetesting.go create mode 100644 pkg/golinters/usetesting/usetesting_integration_test.go diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index fcb9fa4aa8b9..1f8c8d797567 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -3533,6 +3533,30 @@ linters-settings: # Default: false constant-kind: true + usetesting: + # Enable/disable `context.Background()` detections. + # Disabled if Go < 1.24. + # Default: true + context-background: false + + # Enable/disable `context.TODO()` detections. + # Disabled if Go < 1.24. + # Default: true + context-todo: false + + # Enable/disable `os.Chdir()` detections. + # Disabled if Go < 1.24. + # Default: true + os-chdir: false + + # Enable/disable `os.MkdirTemp()` detections. + # Default: true + os-mkdir-temp: false + + # Enable/disable `os.Setenv()` detections. + # Default: false + os-setenv: true + unconvert: # Remove conversions that force intermediate rounding. # Default: false diff --git a/go.mod b/go.mod index 028cb143b97a..467a8cda7705 100644 --- a/go.mod +++ b/go.mod @@ -68,6 +68,7 @@ require ( github.com/lasiar/canonicalheader v1.1.2 github.com/ldez/gomoddirectives v0.2.4 github.com/ldez/tagliatelle v0.6.0 + github.com/ldez/usetesting v0.1.0 github.com/leonklingele/grouper v1.1.2 github.com/macabu/inamedparam v0.1.3 github.com/maratori/testableexamples v1.0.0 diff --git a/go.sum b/go.sum index dd6bf7214ebb..b072d240ca45 100644 --- a/go.sum +++ b/go.sum @@ -355,6 +355,8 @@ github.com/ldez/gomoddirectives v0.2.4 h1:j3YjBIjEBbqZ0NKtBNzr8rtMHTOrLPeiwTkfUJ github.com/ldez/gomoddirectives v0.2.4/go.mod h1:oWu9i62VcQDYp9EQ0ONTfqLNh+mDLWWDO+SO0qSQw5g= github.com/ldez/tagliatelle v0.6.0 h1:1Muumft/shmQ0x96vA6a/OUgTjamRt8jUlZPLm1ruwA= github.com/ldez/tagliatelle v0.6.0/go.mod h1:WeZ7TgEqq7fw/0Zj8BuQhh4+4KX1/+g0O11eygvClRA= +github.com/ldez/usetesting v0.1.0 h1:Hk2geZK7Vgf6rSCTZDMJvW/qZHvv+OW9cSCeoFzykMM= +github.com/ldez/usetesting v0.1.0/go.mod h1:9Tk8OH9NL3Xi7lpvkGRpuY/cOmWmatxwVjRc+TkgLDg= github.com/leonklingele/grouper v1.1.2 h1:o1ARBDLOmmasUaNDesWqWCIFH3u7hoFlM84YrjT3mIY= github.com/leonklingele/grouper v1.1.2/go.mod h1:6D0M/HVkhs2yRKRFZUoGjeDy7EZTfFBE9gl4kjmIGkA= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4= diff --git a/jsonschema/golangci.next.jsonschema.json b/jsonschema/golangci.next.jsonschema.json index 3b0e72330dd3..59e10c4ba102 100644 --- a/jsonschema/golangci.next.jsonschema.json +++ b/jsonschema/golangci.next.jsonschema.json @@ -3325,6 +3325,32 @@ } } }, + "usetesting": { + "type": "object", + "additionalProperties": false, + "properties": { + "context-background": { + "type": "boolean", + "default": true + }, + "context-todo": { + "type": "boolean", + "default": true + }, + "os-chdir": { + "type": "boolean", + "default": true + }, + "os-mkdir-temp": { + "type": "boolean", + "default": true + }, + "os-setenv": { + "type": "boolean", + "default": false + } + } + }, "unconvert": { "type": "object", "additionalProperties": false, diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index 98e6bccb313f..700bf4d671f0 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -179,6 +179,12 @@ var defaultLintersSettings = LintersSettings{ HTTPMethod: true, HTTPStatusCode: true, }, + UseTesting: UseTestingSettings{ + ContextBackground: true, + ContextTodo: true, + OSChdir: true, + OSMkdirTemp: true, + }, Varnamelen: VarnamelenSettings{ MaxDistance: 5, MinNameLength: 3, @@ -278,6 +284,7 @@ type LintersSettings struct { Unparam UnparamSettings Unused UnusedSettings UseStdlibVars UseStdlibVarsSettings + UseTesting UseTestingSettings Varnamelen VarnamelenSettings Whitespace WhitespaceSettings Wrapcheck WrapcheckSettings @@ -959,6 +966,14 @@ type UseStdlibVarsSettings struct { SyslogPriority bool `mapstructure:"syslog-priority"` // Deprecated } +type UseTestingSettings struct { + ContextBackground bool `mapstructure:"context-background"` + ContextTodo bool `mapstructure:"context-todo"` + OSChdir bool `mapstructure:"os-chdir"` + OSMkdirTemp bool `mapstructure:"os-mkdir-temp"` + OSSetenv bool `mapstructure:"os-setenv"` +} + type UnconvertSettings struct { FastMath bool `mapstructure:"fast-math"` Safe bool `mapstructure:"safe"` diff --git a/pkg/golinters/usetesting/testdata/usetesting.go b/pkg/golinters/usetesting/testdata/usetesting.go new file mode 100644 index 000000000000..e6c88c2742f9 --- /dev/null +++ b/pkg/golinters/usetesting/testdata/usetesting.go @@ -0,0 +1,15 @@ +//golangcitest:args -Eusetesting +package testdata + +import ( + "os" + "testing" +) + +func Test_osMkdirTemp(t *testing.T) { + os.MkdirTemp("", "") // want `os\.MkdirTemp\(\) could be replaced by \.TempDir\(\) in .+` +} + +func Test_osSetenv(t *testing.T) { + os.Setenv("", "") +} diff --git a/pkg/golinters/usetesting/testdata/usetesting_configuration.go b/pkg/golinters/usetesting/testdata/usetesting_configuration.go new file mode 100644 index 000000000000..43945bd10bbb --- /dev/null +++ b/pkg/golinters/usetesting/testdata/usetesting_configuration.go @@ -0,0 +1,16 @@ +//golangcitest:args -Eusetesting +//golangcitest:config_path testdata/usetesting_configuration.yml +package testdata + +import ( + "os" + "testing" +) + +func Test_osMkdirTemp(t *testing.T) { + os.MkdirTemp("", "") +} + +func Test_osSetenv(t *testing.T) { + os.Setenv("", "") // want `os\.Setenv\(\) could be replaced by \.Setenv\(\) in .+` +} diff --git a/pkg/golinters/usetesting/testdata/usetesting_configuration.yml b/pkg/golinters/usetesting/testdata/usetesting_configuration.yml new file mode 100644 index 000000000000..b18354fd0aa9 --- /dev/null +++ b/pkg/golinters/usetesting/testdata/usetesting_configuration.yml @@ -0,0 +1,7 @@ +linters-settings: + usetesting: + context-background: false + context-todo: false + os-chdir: false + os-mkdir-temp: false + os-setenv: true diff --git a/pkg/golinters/usetesting/testdata/usetesting_go124.go b/pkg/golinters/usetesting/testdata/usetesting_go124.go new file mode 100644 index 000000000000..1bd308584904 --- /dev/null +++ b/pkg/golinters/usetesting/testdata/usetesting_go124.go @@ -0,0 +1,30 @@ +//go:build go1.24 + +//golangcitest:args -Eusetesting +package testdata + +import ( + "context" + "os" + "testing" +) + +func Test_contextBackground(t *testing.T) { + context.Background() // want `context\.Background\(\) could be replaced by \.Context\(\) in .+` +} + +func Test_contextTODO(t *testing.T) { + context.TODO() // want `context\.TODO\(\) could be replaced by \.Context\(\) in .+` +} + +func Test_osChdir(t *testing.T) { + os.Chdir("") // want `os\.Chdir\(\) could be replaced by \.Chdir\(\) in .+` +} + +func Test_osMkdirTemp(t *testing.T) { + os.MkdirTemp("", "") // want `os\.MkdirTemp\(\) could be replaced by \.TempDir\(\) in .+` +} + +func Test_osSetenv(t *testing.T) { + os.Setenv("", "") +} diff --git a/pkg/golinters/usetesting/testdata/usetesting_go124_configuration.go b/pkg/golinters/usetesting/testdata/usetesting_go124_configuration.go new file mode 100644 index 000000000000..78b402cf5a99 --- /dev/null +++ b/pkg/golinters/usetesting/testdata/usetesting_go124_configuration.go @@ -0,0 +1,31 @@ +//go:build go1.24 + +//golangcitest:args -Eusetesting +//golangcitest:config_path testdata/usetesting_go124_configuration.yml +package testdata + +import ( + "context" + "os" + "testing" +) + +func Test_contextBackground(t *testing.T) { + context.Background() +} + +func Test_contextTODO(t *testing.T) { + context.TODO() +} + +func Test_osChdir(t *testing.T) { + os.Chdir("") +} + +func Test_osMkdirTemp(t *testing.T) { + os.MkdirTemp("", "") +} + +func Test_osSetenv(t *testing.T) { + os.Setenv("", "") // want `os\.Setenv\(\) could be replaced by \.Setenv\(\) in .+` +} diff --git a/pkg/golinters/usetesting/testdata/usetesting_go124_configuration.yml b/pkg/golinters/usetesting/testdata/usetesting_go124_configuration.yml new file mode 100644 index 000000000000..b18354fd0aa9 --- /dev/null +++ b/pkg/golinters/usetesting/testdata/usetesting_go124_configuration.yml @@ -0,0 +1,7 @@ +linters-settings: + usetesting: + context-background: false + context-todo: false + os-chdir: false + os-mkdir-temp: false + os-setenv: true diff --git a/pkg/golinters/usetesting/usetesting.go b/pkg/golinters/usetesting/usetesting.go new file mode 100644 index 000000000000..abc21e30c273 --- /dev/null +++ b/pkg/golinters/usetesting/usetesting.go @@ -0,0 +1,31 @@ +package usetesting + +import ( + "github.com/ldez/usetesting" + "golang.org/x/tools/go/analysis" + + "github.com/golangci/golangci-lint/pkg/config" + "github.com/golangci/golangci-lint/pkg/goanalysis" +) + +func New(cfg *config.UseTestingSettings) *goanalysis.Linter { + a := usetesting.NewAnalyzer() + + cfgMap := make(map[string]map[string]any) + if cfg != nil { + cfgMap[a.Name] = map[string]any{ + "contextbackground": cfg.ContextBackground, + "contexttodo": cfg.ContextTodo, + "oschdir": cfg.OSChdir, + "osmkdirtemp": cfg.OSMkdirTemp, + "ossetenv": cfg.OSSetenv, + } + } + + return goanalysis.NewLinter( + a.Name, + a.Doc, + []*analysis.Analyzer{a}, + cfgMap, + ).WithLoadMode(goanalysis.LoadModeTypesInfo) +} diff --git a/pkg/golinters/usetesting/usetesting_integration_test.go b/pkg/golinters/usetesting/usetesting_integration_test.go new file mode 100644 index 000000000000..3ea6e1ffdaa5 --- /dev/null +++ b/pkg/golinters/usetesting/usetesting_integration_test.go @@ -0,0 +1,11 @@ +package usetesting + +import ( + "testing" + + "github.com/golangci/golangci-lint/test/testshared/integration" +) + +func TestFromTestdata(t *testing.T) { + integration.RunTestdata(t) +} diff --git a/pkg/lint/linter/config.go b/pkg/lint/linter/config.go index 6d6d4b17e7d4..1e438a0f0368 100644 --- a/pkg/lint/linter/config.go +++ b/pkg/lint/linter/config.go @@ -164,12 +164,20 @@ func (lc *Config) WithNoopFallback(cfg *config.Config, cond func(cfg *config.Con } func IsGoLowerThanGo122() func(cfg *config.Config) error { + return isGoLowerThanGo("1.22") +} + +func IsGoLowerThanGo124() func(cfg *config.Config) error { + return isGoLowerThanGo("1.24") +} + +func isGoLowerThanGo(v string) func(cfg *config.Config) error { return func(cfg *config.Config) error { - if cfg == nil || config.IsGoGreaterThanOrEqual(cfg.Run.Go, "1.22") { + if cfg == nil || config.IsGoGreaterThanOrEqual(cfg.Run.Go, v) { return nil } - return fmt.Errorf("this linter is disabled because the Go version (%s) of your project is lower than Go 1.22", cfg.Run.Go) + return fmt.Errorf("this linter is disabled because the Go version (%s) of your project is lower than Go %s", cfg.Run.Go, v) } } diff --git a/pkg/lint/lintersdb/builder_linter.go b/pkg/lint/lintersdb/builder_linter.go index f09fd951ce0e..2a6f46c135b0 100644 --- a/pkg/lint/lintersdb/builder_linter.go +++ b/pkg/lint/lintersdb/builder_linter.go @@ -105,6 +105,7 @@ import ( "github.com/golangci/golangci-lint/pkg/golinters/unparam" "github.com/golangci/golangci-lint/pkg/golinters/unused" "github.com/golangci/golangci-lint/pkg/golinters/usestdlibvars" + "github.com/golangci/golangci-lint/pkg/golinters/usetesting" "github.com/golangci/golangci-lint/pkg/golinters/varnamelen" "github.com/golangci/golangci-lint/pkg/golinters/wastedassign" "github.com/golangci/golangci-lint/pkg/golinters/whitespace" @@ -805,6 +806,12 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) { WithPresets(linter.PresetStyle). WithURL("https://github.com/sashamelentyev/usestdlibvars"), + linter.NewConfig(usetesting.New(&cfg.LintersSettings.UseTesting)). + WithSince("v1.63.0"). + WithPresets(linter.PresetTest). + WithLoadForGoAnalysis(). + WithURL("https://github.com/ldez/usetesting"), + linter.NewConfig(linter.NewNoopDeprecated("varcheck", cfg, linter.DeprecationError)). WithSince("v1.0.0"). WithLoadForGoAnalysis(). From 2ee494491cc425232f43384611d965ca3cfc3dd9 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Fri, 29 Nov 2024 20:33:21 +0100 Subject: [PATCH 2/4] review --- .golangci.next.reference.yml | 2 ++ pkg/golinters/usetesting/usetesting.go | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index 1f8c8d797567..9e34ef87d06e 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -114,6 +114,7 @@ linters: - unparam - unused - usestdlibvars + - usetesting - varnamelen - wastedassign - whitespace @@ -230,6 +231,7 @@ linters: - unparam - unused - usestdlibvars + - usetesting - varnamelen - wastedassign - whitespace diff --git a/pkg/golinters/usetesting/usetesting.go b/pkg/golinters/usetesting/usetesting.go index abc21e30c273..eea03c908255 100644 --- a/pkg/golinters/usetesting/usetesting.go +++ b/pkg/golinters/usetesting/usetesting.go @@ -8,17 +8,17 @@ import ( "github.com/golangci/golangci-lint/pkg/goanalysis" ) -func New(cfg *config.UseTestingSettings) *goanalysis.Linter { +func New(settings *config.UseTestingSettings) *goanalysis.Linter { a := usetesting.NewAnalyzer() - cfgMap := make(map[string]map[string]any) - if cfg != nil { - cfgMap[a.Name] = map[string]any{ - "contextbackground": cfg.ContextBackground, - "contexttodo": cfg.ContextTodo, - "oschdir": cfg.OSChdir, - "osmkdirtemp": cfg.OSMkdirTemp, - "ossetenv": cfg.OSSetenv, + cfg := make(map[string]map[string]any) + if settings != nil { + cfg[a.Name] = map[string]any{ + "contextbackground": settings.ContextBackground, + "contexttodo": settings.ContextTodo, + "oschdir": settings.OSChdir, + "osmkdirtemp": settings.OSMkdirTemp, + "ossetenv": settings.OSSetenv, } } @@ -26,6 +26,6 @@ func New(cfg *config.UseTestingSettings) *goanalysis.Linter { a.Name, a.Doc, []*analysis.Analyzer{a}, - cfgMap, + cfg, ).WithLoadMode(goanalysis.LoadModeTypesInfo) } From 40944eab06cd4a2cc2a1fab4e68e0a2b4b352774 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sat, 30 Nov 2024 16:31:17 +0100 Subject: [PATCH 3/4] feat: v0.2.0 --- .golangci.next.reference.yml | 30 ++++++++++++------- go.mod | 2 +- go.sum | 4 +-- jsonschema/golangci.next.jsonschema.json | 9 ++++++ pkg/config/linters_settings.go | 5 ++++ .../usetesting/testdata/usetesting.go | 13 +++++++- .../testdata/usetesting_configuration.go | 13 +++++++- .../testdata/usetesting_configuration.yml | 8 +++-- .../usetesting/testdata/usetesting_go124.go | 19 +++++++++--- .../usetesting_go124_configuration.go | 13 +++++++- .../usetesting_go124_configuration.yml | 8 +++-- pkg/golinters/usetesting/usetesting.go | 2 ++ 12 files changed, 99 insertions(+), 27 deletions(-) diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index 9e34ef87d06e..402ac6511373 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -3536,28 +3536,36 @@ linters-settings: constant-kind: true usetesting: - # Enable/disable `context.Background()` detections. - # Disabled if Go < 1.24. + # Enable/disable `os.CreateTemp("", ...)` detections. # Default: true - context-background: false + os-create-temp: false - # Enable/disable `context.TODO()` detections. - # Disabled if Go < 1.24. + # Enable/disable `os.MkdirTemp()` detections. # Default: true - context-todo: false + os-mkdir-temp: false + + # Enable/disable `os.Setenv()` detections. + # Default: false + os-setenv: true + + # Enable/disable `os.TempDir()` detections. + # Default: false + os-temp-dir: true # Enable/disable `os.Chdir()` detections. # Disabled if Go < 1.24. # Default: true os-chdir: false - # Enable/disable `os.MkdirTemp()` detections. + # Enable/disable `context.Background()` detections. + # Disabled if Go < 1.24. # Default: true - os-mkdir-temp: false + context-background: false - # Enable/disable `os.Setenv()` detections. - # Default: false - os-setenv: true + # Enable/disable `context.TODO()` detections. + # Disabled if Go < 1.24. + # Default: true + context-todo: false unconvert: # Remove conversions that force intermediate rounding. diff --git a/go.mod b/go.mod index 467a8cda7705..60545739c267 100644 --- a/go.mod +++ b/go.mod @@ -68,7 +68,7 @@ require ( github.com/lasiar/canonicalheader v1.1.2 github.com/ldez/gomoddirectives v0.2.4 github.com/ldez/tagliatelle v0.6.0 - github.com/ldez/usetesting v0.1.0 + github.com/ldez/usetesting v0.2.0 github.com/leonklingele/grouper v1.1.2 github.com/macabu/inamedparam v0.1.3 github.com/maratori/testableexamples v1.0.0 diff --git a/go.sum b/go.sum index b072d240ca45..e9bdaeef62ce 100644 --- a/go.sum +++ b/go.sum @@ -355,8 +355,8 @@ github.com/ldez/gomoddirectives v0.2.4 h1:j3YjBIjEBbqZ0NKtBNzr8rtMHTOrLPeiwTkfUJ github.com/ldez/gomoddirectives v0.2.4/go.mod h1:oWu9i62VcQDYp9EQ0ONTfqLNh+mDLWWDO+SO0qSQw5g= github.com/ldez/tagliatelle v0.6.0 h1:1Muumft/shmQ0x96vA6a/OUgTjamRt8jUlZPLm1ruwA= github.com/ldez/tagliatelle v0.6.0/go.mod h1:WeZ7TgEqq7fw/0Zj8BuQhh4+4KX1/+g0O11eygvClRA= -github.com/ldez/usetesting v0.1.0 h1:Hk2geZK7Vgf6rSCTZDMJvW/qZHvv+OW9cSCeoFzykMM= -github.com/ldez/usetesting v0.1.0/go.mod h1:9Tk8OH9NL3Xi7lpvkGRpuY/cOmWmatxwVjRc+TkgLDg= +github.com/ldez/usetesting v0.2.0 h1:RnQ7HRVInPwsuX3FPesE/HxngIk3UrqEuDu20LMd2tI= +github.com/ldez/usetesting v0.2.0/go.mod h1:9Tk8OH9NL3Xi7lpvkGRpuY/cOmWmatxwVjRc+TkgLDg= github.com/leonklingele/grouper v1.1.2 h1:o1ARBDLOmmasUaNDesWqWCIFH3u7hoFlM84YrjT3mIY= github.com/leonklingele/grouper v1.1.2/go.mod h1:6D0M/HVkhs2yRKRFZUoGjeDy7EZTfFBE9gl4kjmIGkA= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4= diff --git a/jsonschema/golangci.next.jsonschema.json b/jsonschema/golangci.next.jsonschema.json index 59e10c4ba102..91366feef0c5 100644 --- a/jsonschema/golangci.next.jsonschema.json +++ b/jsonschema/golangci.next.jsonschema.json @@ -428,6 +428,7 @@ "unparam", "unused", "usestdlibvars", + "usetesting", "varnamelen", "wastedassign", "whitespace", @@ -3348,6 +3349,14 @@ "os-setenv": { "type": "boolean", "default": false + }, + "os-create-temp": { + "type": "boolean", + "default": true + }, + "os-temp-dir": { + "type": "boolean", + "default": false } } }, diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index 700bf4d671f0..d6120482d0a5 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -184,6 +184,9 @@ var defaultLintersSettings = LintersSettings{ ContextTodo: true, OSChdir: true, OSMkdirTemp: true, + OSSetenv: false, + OSTempDir: false, + OSSCreateTemp: true, }, Varnamelen: VarnamelenSettings{ MaxDistance: 5, @@ -972,6 +975,8 @@ type UseTestingSettings struct { OSChdir bool `mapstructure:"os-chdir"` OSMkdirTemp bool `mapstructure:"os-mkdir-temp"` OSSetenv bool `mapstructure:"os-setenv"` + OSTempDir bool `mapstructure:"os-temp-dir"` + OSSCreateTemp bool `mapstructure:"os-create-temp"` } type UnconvertSettings struct { diff --git a/pkg/golinters/usetesting/testdata/usetesting.go b/pkg/golinters/usetesting/testdata/usetesting.go index e6c88c2742f9..579908d48be5 100644 --- a/pkg/golinters/usetesting/testdata/usetesting.go +++ b/pkg/golinters/usetesting/testdata/usetesting.go @@ -7,9 +7,20 @@ import ( ) func Test_osMkdirTemp(t *testing.T) { - os.MkdirTemp("", "") // want `os\.MkdirTemp\(\) could be replaced by \.TempDir\(\) in .+` + os.MkdirTemp("", "") // want `os\.MkdirTemp\(\) could be replaced by t\.TempDir\(\) in .+` } func Test_osSetenv(t *testing.T) { os.Setenv("", "") } + +func Test_osTempDir(t *testing.T) { + os.TempDir() +} + +func Test_osCreateTemp(t *testing.T) { + os.CreateTemp("", "") // want `os\.CreateTemp\("", \.\.\.\) could be replaced by os\.CreateTemp\(t\.TempDir\(\), \.\.\.\) in .+` + os.CreateTemp("", "xx") // want `os\.CreateTemp\("", \.\.\.\) could be replaced by os\.CreateTemp\(t\.TempDir\(\), \.\.\.\) in .+` + os.CreateTemp(os.TempDir(), "xx") + os.CreateTemp(t.TempDir(), "xx") +} diff --git a/pkg/golinters/usetesting/testdata/usetesting_configuration.go b/pkg/golinters/usetesting/testdata/usetesting_configuration.go index 43945bd10bbb..332e4ea9b12c 100644 --- a/pkg/golinters/usetesting/testdata/usetesting_configuration.go +++ b/pkg/golinters/usetesting/testdata/usetesting_configuration.go @@ -11,6 +11,17 @@ func Test_osMkdirTemp(t *testing.T) { os.MkdirTemp("", "") } +func Test_osTempDir(t *testing.T) { + os.TempDir() // want `os\.TempDir\(\) could be replaced by t\.TempDir\(\) in .+` +} + func Test_osSetenv(t *testing.T) { - os.Setenv("", "") // want `os\.Setenv\(\) could be replaced by \.Setenv\(\) in .+` + os.Setenv("", "") // want `os\.Setenv\(\) could be replaced by t\.Setenv\(\) in .+` +} + +func Test_osCreateTemp(t *testing.T) { + os.CreateTemp("", "") + os.CreateTemp("", "xx") + os.CreateTemp(os.TempDir(), "xx") // want `os\.TempDir\(\) could be replaced by t\.TempDir\(\) in .+` + os.CreateTemp(t.TempDir(), "xx") } diff --git a/pkg/golinters/usetesting/testdata/usetesting_configuration.yml b/pkg/golinters/usetesting/testdata/usetesting_configuration.yml index b18354fd0aa9..fc877dd17f20 100644 --- a/pkg/golinters/usetesting/testdata/usetesting_configuration.yml +++ b/pkg/golinters/usetesting/testdata/usetesting_configuration.yml @@ -1,7 +1,9 @@ linters-settings: usetesting: - context-background: false - context-todo: false - os-chdir: false + os-create-temp: false os-mkdir-temp: false os-setenv: true + os-temp-dir: true + os-chdir: false + context-background: false + context-todo: false diff --git a/pkg/golinters/usetesting/testdata/usetesting_go124.go b/pkg/golinters/usetesting/testdata/usetesting_go124.go index 1bd308584904..0ac122da1caa 100644 --- a/pkg/golinters/usetesting/testdata/usetesting_go124.go +++ b/pkg/golinters/usetesting/testdata/usetesting_go124.go @@ -10,21 +10,32 @@ import ( ) func Test_contextBackground(t *testing.T) { - context.Background() // want `context\.Background\(\) could be replaced by \.Context\(\) in .+` + context.Background() // want `context\.Background\(\) could be replaced by t\.Context\(\) in .+` } func Test_contextTODO(t *testing.T) { - context.TODO() // want `context\.TODO\(\) could be replaced by \.Context\(\) in .+` + context.TODO() // want `context\.TODO\(\) could be replaced by t\.Context\(\) in .+` } func Test_osChdir(t *testing.T) { - os.Chdir("") // want `os\.Chdir\(\) could be replaced by \.Chdir\(\) in .+` + os.Chdir("") // want `os\.Chdir\(\) could be replaced by t\.Chdir\(\) in .+` } func Test_osMkdirTemp(t *testing.T) { - os.MkdirTemp("", "") // want `os\.MkdirTemp\(\) could be replaced by \.TempDir\(\) in .+` + os.MkdirTemp("", "") // want `os\.MkdirTemp\(\) could be replaced by t\.TempDir\(\) in .+` } func Test_osSetenv(t *testing.T) { os.Setenv("", "") } + +func Test_osTempDir(t *testing.T) { + os.TempDir() +} + +func Test_osCreateTemp(t *testing.T) { + os.CreateTemp("", "") // want `os\.CreateTemp\("", \.\.\.\) could be replaced by os\.CreateTemp\(t\.TempDir\(\), \.\.\.\) in .+` + os.CreateTemp("", "xx") // want `os\.CreateTemp\("", \.\.\.\) could be replaced by os\.CreateTemp\(t\.TempDir\(\), \.\.\.\) in .+` + os.CreateTemp(os.TempDir(), "xx") + os.CreateTemp(t.TempDir(), "xx") +} diff --git a/pkg/golinters/usetesting/testdata/usetesting_go124_configuration.go b/pkg/golinters/usetesting/testdata/usetesting_go124_configuration.go index 78b402cf5a99..44774f3cb399 100644 --- a/pkg/golinters/usetesting/testdata/usetesting_go124_configuration.go +++ b/pkg/golinters/usetesting/testdata/usetesting_go124_configuration.go @@ -27,5 +27,16 @@ func Test_osMkdirTemp(t *testing.T) { } func Test_osSetenv(t *testing.T) { - os.Setenv("", "") // want `os\.Setenv\(\) could be replaced by \.Setenv\(\) in .+` + os.Setenv("", "") // want `os\.Setenv\(\) could be replaced by t\.Setenv\(\) in .+` +} + +func Test_osTempDir(t *testing.T) { + os.TempDir() // want `os\.TempDir\(\) could be replaced by t\.TempDir\(\) in .+` +} + +func Test_osCreateTemp(t *testing.T) { + os.CreateTemp("", "") + os.CreateTemp("", "xx") + os.CreateTemp(os.TempDir(), "xx") // want `os\.TempDir\(\) could be replaced by t\.TempDir\(\) in .+` + os.CreateTemp(t.TempDir(), "xx") } diff --git a/pkg/golinters/usetesting/testdata/usetesting_go124_configuration.yml b/pkg/golinters/usetesting/testdata/usetesting_go124_configuration.yml index b18354fd0aa9..fc877dd17f20 100644 --- a/pkg/golinters/usetesting/testdata/usetesting_go124_configuration.yml +++ b/pkg/golinters/usetesting/testdata/usetesting_go124_configuration.yml @@ -1,7 +1,9 @@ linters-settings: usetesting: - context-background: false - context-todo: false - os-chdir: false + os-create-temp: false os-mkdir-temp: false os-setenv: true + os-temp-dir: true + os-chdir: false + context-background: false + context-todo: false diff --git a/pkg/golinters/usetesting/usetesting.go b/pkg/golinters/usetesting/usetesting.go index eea03c908255..ec20be5f868d 100644 --- a/pkg/golinters/usetesting/usetesting.go +++ b/pkg/golinters/usetesting/usetesting.go @@ -19,6 +19,8 @@ func New(settings *config.UseTestingSettings) *goanalysis.Linter { "oschdir": settings.OSChdir, "osmkdirtemp": settings.OSMkdirTemp, "ossetenv": settings.OSSetenv, + "ostempdir": settings.OSTempDir, + "oscreatetemp": settings.OSSCreateTemp, } } From b9a3f2e7dd08e13e7106310315915c03619a74df Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sat, 30 Nov 2024 17:14:41 +0100 Subject: [PATCH 4/4] review --- pkg/config/linters_settings.go | 4 ++-- pkg/golinters/usetesting/usetesting.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index d6120482d0a5..6fca1003539f 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -186,7 +186,7 @@ var defaultLintersSettings = LintersSettings{ OSMkdirTemp: true, OSSetenv: false, OSTempDir: false, - OSSCreateTemp: true, + OSCreateTemp: true, }, Varnamelen: VarnamelenSettings{ MaxDistance: 5, @@ -976,7 +976,7 @@ type UseTestingSettings struct { OSMkdirTemp bool `mapstructure:"os-mkdir-temp"` OSSetenv bool `mapstructure:"os-setenv"` OSTempDir bool `mapstructure:"os-temp-dir"` - OSSCreateTemp bool `mapstructure:"os-create-temp"` + OSCreateTemp bool `mapstructure:"os-create-temp"` } type UnconvertSettings struct { diff --git a/pkg/golinters/usetesting/usetesting.go b/pkg/golinters/usetesting/usetesting.go index ec20be5f868d..a21742fbd6d4 100644 --- a/pkg/golinters/usetesting/usetesting.go +++ b/pkg/golinters/usetesting/usetesting.go @@ -20,7 +20,7 @@ func New(settings *config.UseTestingSettings) *goanalysis.Linter { "osmkdirtemp": settings.OSMkdirTemp, "ossetenv": settings.OSSetenv, "ostempdir": settings.OSTempDir, - "oscreatetemp": settings.OSSCreateTemp, + "oscreatetemp": settings.OSCreateTemp, } }