Skip to content

Commit 4262635

Browse files
committed
feat: add UseTesting linter
1 parent d74f1ae commit 4262635

15 files changed

+233
-2
lines changed

.golangci.next.reference.yml

+24
Original file line numberDiff line numberDiff line change
@@ -3533,6 +3533,30 @@ linters-settings:
35333533
# Default: false
35343534
constant-kind: true
35353535

3536+
usetesting:
3537+
# Enable/disable `context.Background()` detections.
3538+
# Disabled if Go < 1.24.
3539+
# Default: true
3540+
context-background: false
3541+
3542+
# Enable/disable `context.TODO()` detections.
3543+
# Disabled if Go < 1.24.
3544+
# Default: true
3545+
context-todo: false
3546+
3547+
# Enable/disable `os.Chdir()` detections.
3548+
# Disabled if Go < 1.24.
3549+
# Default: true
3550+
os-chdir: false
3551+
3552+
# Enable/disable `os.MkdirTemp()` detections.
3553+
# Default: true
3554+
os-mkdir-temp: false
3555+
3556+
# Enable/disable `os.Setenv()` detections.
3557+
# Default: false
3558+
os-setenv: true
3559+
35363560
unconvert:
35373561
# Remove conversions that force intermediate rounding.
35383562
# Default: false

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ require (
6868
github.com/lasiar/canonicalheader v1.1.2
6969
github.com/ldez/gomoddirectives v0.2.4
7070
github.com/ldez/tagliatelle v0.6.0
71+
github.com/ldez/usetesting v0.1.0
7172
github.com/leonklingele/grouper v1.1.2
7273
github.com/macabu/inamedparam v0.1.3
7374
github.com/maratori/testableexamples v1.0.0

go.sum

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jsonschema/golangci.next.jsonschema.json

+26
Original file line numberDiff line numberDiff line change
@@ -3325,6 +3325,32 @@
33253325
}
33263326
}
33273327
},
3328+
"usetesting": {
3329+
"type": "object",
3330+
"additionalProperties": false,
3331+
"properties": {
3332+
"context-background": {
3333+
"type": "boolean",
3334+
"default": true
3335+
},
3336+
"context-todo": {
3337+
"type": "boolean",
3338+
"default": true
3339+
},
3340+
"os-chdir": {
3341+
"type": "boolean",
3342+
"default": true
3343+
},
3344+
"os-mkdir-temp": {
3345+
"type": "boolean",
3346+
"default": true
3347+
},
3348+
"os-setenv": {
3349+
"type": "boolean",
3350+
"default": false
3351+
}
3352+
}
3353+
},
33283354
"unconvert": {
33293355
"type": "object",
33303356
"additionalProperties": false,

pkg/config/linters_settings.go

+15
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ var defaultLintersSettings = LintersSettings{
179179
HTTPMethod: true,
180180
HTTPStatusCode: true,
181181
},
182+
UseTesting: UseTestingSettings{
183+
ContextBackground: true,
184+
ContextTodo: true,
185+
OSChdir: true,
186+
OSMkdirTemp: true,
187+
},
182188
Varnamelen: VarnamelenSettings{
183189
MaxDistance: 5,
184190
MinNameLength: 3,
@@ -278,6 +284,7 @@ type LintersSettings struct {
278284
Unparam UnparamSettings
279285
Unused UnusedSettings
280286
UseStdlibVars UseStdlibVarsSettings
287+
UseTesting UseTestingSettings
281288
Varnamelen VarnamelenSettings
282289
Whitespace WhitespaceSettings
283290
Wrapcheck WrapcheckSettings
@@ -959,6 +966,14 @@ type UseStdlibVarsSettings struct {
959966
SyslogPriority bool `mapstructure:"syslog-priority"` // Deprecated
960967
}
961968

969+
type UseTestingSettings struct {
970+
ContextBackground bool `mapstructure:"context-background"`
971+
ContextTodo bool `mapstructure:"context-todo"`
972+
OSChdir bool `mapstructure:"os-chdir"`
973+
OSMkdirTemp bool `mapstructure:"os-mkdir-temp"`
974+
OSSetenv bool `mapstructure:"os-setenv"`
975+
}
976+
962977
type UnconvertSettings struct {
963978
FastMath bool `mapstructure:"fast-math"`
964979
Safe bool `mapstructure:"safe"`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//golangcitest:args -Eusetesting
2+
package testdata
3+
4+
import (
5+
"os"
6+
"testing"
7+
)
8+
9+
func Test_osMkdirTemp(t *testing.T) {
10+
os.MkdirTemp("", "") // want `os\.MkdirTemp\(\) could be replaced by <t/b/tb>\.TempDir\(\) in .+`
11+
}
12+
13+
func Test_osSetenv(t *testing.T) {
14+
os.Setenv("", "")
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//golangcitest:args -Eusetesting
2+
//golangcitest:config_path testdata/usetesting_configuration.yml
3+
package testdata
4+
5+
import (
6+
"os"
7+
"testing"
8+
)
9+
10+
func Test_osMkdirTemp(t *testing.T) {
11+
os.MkdirTemp("", "")
12+
}
13+
14+
func Test_osSetenv(t *testing.T) {
15+
os.Setenv("", "") // want `os\.Setenv\(\) could be replaced by <t/b/tb>\.Setenv\(\) in .+`
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
linters-settings:
2+
usetesting:
3+
context-background: false
4+
context-todo: false
5+
os-chdir: false
6+
os-mkdir-temp: false
7+
os-setenv: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//go:build go1.24
2+
3+
//golangcitest:args -Eusetesting
4+
package testdata
5+
6+
import (
7+
"context"
8+
"os"
9+
"testing"
10+
)
11+
12+
func Test_contextBackground(t *testing.T) {
13+
context.Background() // want `context\.Background\(\) could be replaced by <t/b/tb>\.Context\(\) in .+`
14+
}
15+
16+
func Test_contextTODO(t *testing.T) {
17+
context.TODO() // want `context\.TODO\(\) could be replaced by <t/b/tb>\.Context\(\) in .+`
18+
}
19+
20+
func Test_osChdir(t *testing.T) {
21+
os.Chdir("") // want `os\.Chdir\(\) could be replaced by <t/b/tb>\.Chdir\(\) in .+`
22+
}
23+
24+
func Test_osMkdirTemp(t *testing.T) {
25+
os.MkdirTemp("", "") // want `os\.MkdirTemp\(\) could be replaced by <t/b/tb>\.TempDir\(\) in .+`
26+
}
27+
28+
func Test_osSetenv(t *testing.T) {
29+
os.Setenv("", "")
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//go:build go1.24
2+
3+
//golangcitest:args -Eusetesting
4+
//golangcitest:config_path testdata/usetesting_go124_configuration.yml
5+
package testdata
6+
7+
import (
8+
"context"
9+
"os"
10+
"testing"
11+
)
12+
13+
func Test_contextBackground(t *testing.T) {
14+
context.Background()
15+
}
16+
17+
func Test_contextTODO(t *testing.T) {
18+
context.TODO()
19+
}
20+
21+
func Test_osChdir(t *testing.T) {
22+
os.Chdir("")
23+
}
24+
25+
func Test_osMkdirTemp(t *testing.T) {
26+
os.MkdirTemp("", "")
27+
}
28+
29+
func Test_osSetenv(t *testing.T) {
30+
os.Setenv("", "") // want `os\.Setenv\(\) could be replaced by <t/b/tb>\.Setenv\(\) in .+`
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
linters-settings:
2+
usetesting:
3+
context-background: false
4+
context-todo: false
5+
os-chdir: false
6+
os-mkdir-temp: false
7+
os-setenv: true
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package usetesting
2+
3+
import (
4+
"github.com/ldez/usetesting"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/config"
8+
"github.com/golangci/golangci-lint/pkg/goanalysis"
9+
)
10+
11+
func New(cfg *config.UseTestingSettings) *goanalysis.Linter {
12+
a := usetesting.NewAnalyzer()
13+
14+
cfgMap := make(map[string]map[string]any)
15+
if cfg != nil {
16+
cfgMap[a.Name] = map[string]any{
17+
"contextbackground": cfg.ContextBackground,
18+
"contexttodo": cfg.ContextTodo,
19+
"oschdir": cfg.OSChdir,
20+
"osmkdirtemp": cfg.OSMkdirTemp,
21+
"ossetenv": cfg.OSSetenv,
22+
}
23+
}
24+
25+
return goanalysis.NewLinter(
26+
a.Name,
27+
a.Doc,
28+
[]*analysis.Analyzer{a},
29+
cfgMap,
30+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package usetesting
2+
3+
import (
4+
"testing"
5+
6+
"github.com/golangci/golangci-lint/test/testshared/integration"
7+
)
8+
9+
func TestFromTestdata(t *testing.T) {
10+
integration.RunTestdata(t)
11+
}

pkg/lint/linter/config.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,20 @@ func (lc *Config) WithNoopFallback(cfg *config.Config, cond func(cfg *config.Con
164164
}
165165

166166
func IsGoLowerThanGo122() func(cfg *config.Config) error {
167+
return isGoLowerThanGo("1.22")
168+
}
169+
170+
func IsGoLowerThanGo124() func(cfg *config.Config) error {
171+
return isGoLowerThanGo("1.24")
172+
}
173+
174+
func isGoLowerThanGo(v string) func(cfg *config.Config) error {
167175
return func(cfg *config.Config) error {
168-
if cfg == nil || config.IsGoGreaterThanOrEqual(cfg.Run.Go, "1.22") {
176+
if cfg == nil || config.IsGoGreaterThanOrEqual(cfg.Run.Go, v) {
169177
return nil
170178
}
171179

172-
return fmt.Errorf("this linter is disabled because the Go version (%s) of your project is lower than Go 1.22", cfg.Run.Go)
180+
return fmt.Errorf("this linter is disabled because the Go version (%s) of your project is lower than Go %s", cfg.Run.Go, v)
173181
}
174182
}
175183

pkg/lint/lintersdb/builder_linter.go

+7
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ import (
105105
"github.com/golangci/golangci-lint/pkg/golinters/unparam"
106106
"github.com/golangci/golangci-lint/pkg/golinters/unused"
107107
"github.com/golangci/golangci-lint/pkg/golinters/usestdlibvars"
108+
"github.com/golangci/golangci-lint/pkg/golinters/usetesting"
108109
"github.com/golangci/golangci-lint/pkg/golinters/varnamelen"
109110
"github.com/golangci/golangci-lint/pkg/golinters/wastedassign"
110111
"github.com/golangci/golangci-lint/pkg/golinters/whitespace"
@@ -805,6 +806,12 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
805806
WithPresets(linter.PresetStyle).
806807
WithURL("https://github.com/sashamelentyev/usestdlibvars"),
807808

809+
linter.NewConfig(usetesting.New(&cfg.LintersSettings.UseTesting)).
810+
WithSince("v1.63.0").
811+
WithPresets(linter.PresetTest).
812+
WithLoadForGoAnalysis().
813+
WithURL("https://github.com/ldez/usetesting"),
814+
808815
linter.NewConfig(linter.NewNoopDeprecated("varcheck", cfg, linter.DeprecationError)).
809816
WithSince("v1.0.0").
810817
WithLoadForGoAnalysis().

0 commit comments

Comments
 (0)