Skip to content

Commit 9a2c7d6

Browse files
committed
ttempdir added
1 parent a7817a1 commit 9a2c7d6

File tree

9 files changed

+149
-0
lines changed

9 files changed

+149
-0
lines changed

.golangci.reference.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2289,6 +2289,15 @@ linters-settings:
22892289
# Default: true
22902290
begin: false
22912291

2292+
ttempdir:
2293+
# The option `all` will run against whole test files (`_test.go`) regardless of method/function signatures.
2294+
# Otherwise, only methods that take `*testing.T`, `*testing.B`, and `testing.TB` as arguments are checked.
2295+
# Default: false
2296+
all: false
2297+
# The option `max-recursion-level` is uses to analyze function calls in recursion.
2298+
# Default: 5
2299+
max-recursion-level: 5
2300+
22922301
usestdlibvars:
22932302
# Suggest the use of http.MethodXX.
22942303
# Default: true

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ require (
8383
github.com/nishanths/exhaustive v0.12.0
8484
github.com/nishanths/predeclared v0.2.2
8585
github.com/nunnatsa/ginkgolinter v0.16.2
86+
github.com/peczenyj/ttempdir v0.2.1
8687
github.com/pelletier/go-toml/v2 v2.2.2
8788
github.com/polyfloyd/go-errorlint v1.5.2
8889
github.com/quasilyte/go-ruleguard/dsl v0.3.22

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.

pkg/config/linters_settings.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ type LintersSettings struct {
269269
Testifylint TestifylintSettings
270270
Testpackage TestpackageSettings
271271
Thelper ThelperSettings
272+
Ttempdir TtempdirSettings
272273
Unconvert UnconvertSettings
273274
Unparam UnparamSettings
274275
Unused UnusedSettings
@@ -914,6 +915,11 @@ type TenvSettings struct {
914915
All bool `mapstructure:"all"`
915916
}
916917

918+
type TtempdirSettings struct {
919+
All bool `mapstructure:"all"`
920+
MaxRecursionLevel uint `mapstructure:"max-recursion-level"`
921+
}
922+
917923
type UseStdlibVarsSettings struct {
918924
HTTPMethod bool `mapstructure:"http-method"`
919925
HTTPStatusCode bool `mapstructure:"http-status-code"`
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//golangcitest:args -Ettempdir
2+
//golangcitest:config_path testdata/ttempdir_all.yml
3+
package testdata
4+
5+
import (
6+
"os"
7+
"testing"
8+
)
9+
10+
var (
11+
_, e = os.MkdirTemp("a", "b") // never seen
12+
)
13+
14+
func testsetup() {
15+
os.MkdirTemp("a", "b") // if -all = true, want "os\\.MkdirTemp\\(\\) can be replaced by `testing\\.TempDir\\(\\)` in testsetup"
16+
_, err := os.MkdirTemp("a", "b") // if -all = true, want "os\\.MkdirTemp\\(\\) can be replaced by `testing\\.TempDir\\(\\)` in testsetup"
17+
if err != nil {
18+
_ = err
19+
}
20+
os.MkdirTemp("a", "b") // if -all = true, "func setup is not using testing.TempDir"
21+
}
22+
23+
func TestF(t *testing.T) {
24+
testsetup()
25+
os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `t\\.TempDir\\(\\)` in TestF"
26+
_, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `t\\.TempDir\\(\\)` in TestF"
27+
_ = err
28+
if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) can be replaced by `t\\.TempDir\\(\\)` in TestF"
29+
_ = err
30+
}
31+
}
32+
33+
func BenchmarkF(b *testing.B) {
34+
TB(b)
35+
os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `b\\.TempDir\\(\\)` in BenchmarkF"
36+
_, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `b\\.TempDir\\(\\)` in BenchmarkF"
37+
_ = err
38+
if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) can be replaced by `b\\.TempDir\\(\\)` in BenchmarkF"
39+
_ = err
40+
}
41+
}
42+
43+
func TB(tb testing.TB) {
44+
os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `tb\\.TempDir\\(\\)` in TB"
45+
_, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `tb\\.TempDir\\(\\)` in TB"
46+
_ = err
47+
if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) can be replaced by `tb\\.TempDir\\(\\)` in TB"
48+
_ = err
49+
}
50+
}
51+
52+
func FuzzF(f *testing.F) {
53+
os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `f\\.TempDir\\(\\)` in FuzzF"
54+
_, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `f\\.TempDir\\(\\)` in FuzzF"
55+
_ = err
56+
if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) can be replaced by `f\\.TempDir\\(\\)` in FuzzF"
57+
_ = err
58+
}
59+
}
60+
61+
func TestFunctionLiteral(t *testing.T) {
62+
testsetup()
63+
t.Run("test", func(t *testing.T) {
64+
os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `t\\.TempDir\\(\\)` in anonymous function"
65+
_, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) can be replaced by `t\\.TempDir\\(\\)` in anonymous function"
66+
_ = err
67+
if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) can be replaced by `t\\.TempDir\\(\\)` in anonymous function"
68+
_ = err
69+
}
70+
})
71+
}
72+
73+
func TestEmpty(t *testing.T) {
74+
t.Run("test", func(*testing.T) {})
75+
}
76+
77+
func TestEmptyTB(t *testing.T) {
78+
func(testing.TB) {}(t)
79+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
linters-settings:
2+
ttempdir:
3+
all: true
4+
max-recursion-level: 5

pkg/golinters/ttempdir/ttempdir.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package ttempdir
2+
3+
import (
4+
"github.com/peczenyj/ttempdir"
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(settings *config.TtempdirSettings) *goanalysis.Linter {
12+
a := ttempdir.Analyzer
13+
14+
var cfg map[string]map[string]any
15+
if settings != nil {
16+
cfg = map[string]map[string]any{
17+
a.Name: {
18+
ttempdir.A: settings.All,
19+
ttempdir.MRL: settings.MaxRecursionLevel,
20+
},
21+
}
22+
}
23+
24+
return goanalysis.NewLinter(
25+
a.Name,
26+
a.Doc,
27+
[]*analysis.Analyzer{a},
28+
cfg,
29+
).WithLoadMode(goanalysis.LoadModeSyntax)
30+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ttempdir
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/lintersdb/builder_linter.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ import (
100100
"github.com/golangci/golangci-lint/pkg/golinters/testpackage"
101101
"github.com/golangci/golangci-lint/pkg/golinters/thelper"
102102
"github.com/golangci/golangci-lint/pkg/golinters/tparallel"
103+
"github.com/golangci/golangci-lint/pkg/golinters/ttempdir"
103104
"github.com/golangci/golangci-lint/pkg/golinters/unconvert"
104105
"github.com/golangci/golangci-lint/pkg/golinters/unparam"
105106
"github.com/golangci/golangci-lint/pkg/golinters/unused"
@@ -756,6 +757,12 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
756757
WithLoadForGoAnalysis().
757758
WithURL("https://github.com/moricho/tparallel"),
758759

760+
linter.NewConfig(ttempdir.New(&cfg.LintersSettings.Ttempdir)).
761+
WithSince("v1.60.0").
762+
WithPresets(linter.PresetStyle).
763+
WithLoadForGoAnalysis().
764+
WithURL("https://github.com/peczenyj/ttempdir"),
765+
759766
linter.NewConfig(golinters.NewTypecheck()).
760767
WithInternal().
761768
WithEnabledByDefault().

0 commit comments

Comments
 (0)