Skip to content

Commit

Permalink
Add UseTesting linter (#5170)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored Nov 30, 2024
1 parent d74f1ae commit 0640d3f
Show file tree
Hide file tree
Showing 15 changed files with 307 additions and 2 deletions.
34 changes: 34 additions & 0 deletions .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ linters:
- unparam
- unused
- usestdlibvars
- usetesting
- varnamelen
- wastedassign
- whitespace
Expand Down Expand Up @@ -230,6 +231,7 @@ linters:
- unparam
- unused
- usestdlibvars
- usetesting
- varnamelen
- wastedassign
- whitespace
Expand Down Expand Up @@ -3533,6 +3535,38 @@ linters-settings:
# Default: false
constant-kind: true

usetesting:
# Enable/disable `os.CreateTemp("", ...)` detections.
# Default: true
os-create-temp: false

# Enable/disable `os.MkdirTemp()` detections.
# Default: true
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 `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

unconvert:
# Remove conversions that force intermediate rounding.
# Default: false
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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.2.0
github.com/leonklingele/grouper v1.1.2
github.com/macabu/inamedparam v0.1.3
github.com/maratori/testableexamples v1.0.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions jsonschema/golangci.next.jsonschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@
"unparam",
"unused",
"usestdlibvars",
"usetesting",
"varnamelen",
"wastedassign",
"whitespace",
Expand Down Expand Up @@ -3325,6 +3326,40 @@
}
}
},
"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
},
"os-create-temp": {
"type": "boolean",
"default": true
},
"os-temp-dir": {
"type": "boolean",
"default": false
}
}
},
"unconvert": {
"type": "object",
"additionalProperties": false,
Expand Down
20 changes: 20 additions & 0 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ var defaultLintersSettings = LintersSettings{
HTTPMethod: true,
HTTPStatusCode: true,
},
UseTesting: UseTestingSettings{
ContextBackground: true,
ContextTodo: true,
OSChdir: true,
OSMkdirTemp: true,
OSSetenv: false,
OSTempDir: false,
OSCreateTemp: true,
},
Varnamelen: VarnamelenSettings{
MaxDistance: 5,
MinNameLength: 3,
Expand Down Expand Up @@ -278,6 +287,7 @@ type LintersSettings struct {
Unparam UnparamSettings
Unused UnusedSettings
UseStdlibVars UseStdlibVarsSettings
UseTesting UseTestingSettings
Varnamelen VarnamelenSettings
Whitespace WhitespaceSettings
Wrapcheck WrapcheckSettings
Expand Down Expand Up @@ -959,6 +969,16 @@ 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"`
OSTempDir bool `mapstructure:"os-temp-dir"`
OSCreateTemp bool `mapstructure:"os-create-temp"`
}

type UnconvertSettings struct {
FastMath bool `mapstructure:"fast-math"`
Safe bool `mapstructure:"safe"`
Expand Down
26 changes: 26 additions & 0 deletions pkg/golinters/usetesting/testdata/usetesting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//golangcitest:args -Eusetesting
package testdata

import (
"os"
"testing"
)

func Test_osMkdirTemp(t *testing.T) {
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")
}
27 changes: 27 additions & 0 deletions pkg/golinters/usetesting/testdata/usetesting_configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//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_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 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")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
linters-settings:
usetesting:
os-create-temp: false
os-mkdir-temp: false
os-setenv: true
os-temp-dir: true
os-chdir: false
context-background: false
context-todo: false
41 changes: 41 additions & 0 deletions pkg/golinters/usetesting/testdata/usetesting_go124.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//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 t\.Context\(\) in .+`
}

func Test_contextTODO(t *testing.T) {
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 t\.Chdir\(\) in .+`
}

func Test_osMkdirTemp(t *testing.T) {
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")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//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 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")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
linters-settings:
usetesting:
os-create-temp: false
os-mkdir-temp: false
os-setenv: true
os-temp-dir: true
os-chdir: false
context-background: false
context-todo: false
33 changes: 33 additions & 0 deletions pkg/golinters/usetesting/usetesting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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(settings *config.UseTestingSettings) *goanalysis.Linter {
a := usetesting.NewAnalyzer()

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,
"ostempdir": settings.OSTempDir,
"oscreatetemp": settings.OSCreateTemp,
}
}

return goanalysis.NewLinter(
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
cfg,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
11 changes: 11 additions & 0 deletions pkg/golinters/usetesting/usetesting_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package usetesting

import (
"testing"

"github.com/golangci/golangci-lint/test/testshared/integration"
)

func TestFromTestdata(t *testing.T) {
integration.RunTestdata(t)
}
12 changes: 10 additions & 2 deletions pkg/lint/linter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
Loading

0 comments on commit 0640d3f

Please sign in to comment.