Skip to content

Commit 6064563

Browse files
committed
feat: add UseTesting linter
1 parent 6b000ab commit 6064563

15 files changed

+233
-2
lines changed

.golangci.next.reference.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3493,6 +3493,30 @@ linters-settings:
34933493
# Default: false
34943494
constant-kind: true
34953495

3496+
usetesting:
3497+
# Enable/disable `context.Background()` detections.
3498+
# Disabled if Go < 1.24.
3499+
# Default: true
3500+
context-background: false
3501+
3502+
# Enable/disable `context.TODO()` detections.
3503+
# Disabled if Go < 1.24.
3504+
# Default: true
3505+
context-todo: false
3506+
3507+
# Enable/disable `os.Chdir()` detections.
3508+
# Disabled if Go < 1.24.
3509+
# Default: true
3510+
os-chdir: false
3511+
3512+
# Enable/disable `os.MkdirTemp()` detections.
3513+
# Default: true
3514+
os-mkdir-temp: false
3515+
3516+
# Enable/disable `os.Setenv()` detections.
3517+
# Default: false
3518+
os-setenv: true
3519+
34963520
unconvert:
34973521
# Remove conversions that force intermediate rounding.
34983522
# Default: false

go.mod

Lines changed: 1 addition & 0 deletions
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.5.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

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jsonschema/golangci.next.jsonschema.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3273,6 +3273,32 @@
32733273
}
32743274
}
32753275
},
3276+
"usetesting": {
3277+
"type": "object",
3278+
"additionalProperties": false,
3279+
"properties": {
3280+
"context-background": {
3281+
"type": "boolean",
3282+
"default": true
3283+
},
3284+
"context-todo": {
3285+
"type": "boolean",
3286+
"default": true
3287+
},
3288+
"os-chdir": {
3289+
"type": "boolean",
3290+
"default": true
3291+
},
3292+
"os-mkdir-temp": {
3293+
"type": "boolean",
3294+
"default": true
3295+
},
3296+
"os-setenv": {
3297+
"type": "boolean",
3298+
"default": false
3299+
}
3300+
}
3301+
},
32763302
"unconvert": {
32773303
"type": "object",
32783304
"additionalProperties": false,

pkg/config/linters_settings.go

Lines changed: 15 additions & 0 deletions
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
@@ -945,6 +952,14 @@ type UseStdlibVarsSettings struct {
945952
SyslogPriority bool `mapstructure:"syslog-priority"` // Deprecated
946953
}
947954

955+
type UseTestingSettings struct {
956+
ContextBackground bool `mapstructure:"context-background"`
957+
ContextTodo bool `mapstructure:"context-todo"`
958+
OSChdir bool `mapstructure:"os-chdir"`
959+
OSMkdirTemp bool `mapstructure:"os-mkdir-temp"`
960+
OSSetenv bool `mapstructure:"os-setenv"`
961+
}
962+
948963
type UnconvertSettings struct {
949964
FastMath bool `mapstructure:"fast-math"`
950965
Safe bool `mapstructure:"safe"`
Lines changed: 15 additions & 0 deletions
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+
}
Lines changed: 16 additions & 0 deletions
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+
}
Lines changed: 7 additions & 0 deletions
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
Lines changed: 30 additions & 0 deletions
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+
}
Lines changed: 31 additions & 0 deletions
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+
}
Lines changed: 7 additions & 0 deletions
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
Lines changed: 31 additions & 0 deletions
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+
}
Lines changed: 11 additions & 0 deletions
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

Lines changed: 10 additions & 2 deletions
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

Lines changed: 7 additions & 0 deletions
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"
@@ -804,6 +805,12 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
804805
WithPresets(linter.PresetStyle).
805806
WithURL("https://github.com/sashamelentyev/usestdlibvars"),
806807

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

0 commit comments

Comments
 (0)