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+
}

0 commit comments

Comments
 (0)