Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ttempdir linter #4794

Draft
wants to merge 22 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2316,6 +2316,15 @@ linters-settings:
# Default: true
begin: false

ttempdir:
# The option `all` will run against whole test files (`_test.go`) regardless of method/function signatures.
# Otherwise, only methods that take `*testing.T`, `*testing.B`, and `testing.TB` as arguments are checked.
# Default: false
all: true
# The option `max-recursion-level` is uses to analyze function calls in recursion.
# Default: 5
max-recursion-level: 10

usestdlibvars:
# Suggest the use of http.MethodXX.
# Default: true
Expand Down Expand Up @@ -2659,6 +2668,7 @@ linters:
- testpackage
- thelper
- tparallel
- ttempdir
- typecheck
- unconvert
- unparam
Expand Down Expand Up @@ -2774,6 +2784,7 @@ linters:
- testpackage
- thelper
- tparallel
- ttempdir
- typecheck
- unconvert
- unparam
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ require (
github.com/nishanths/exhaustive v0.12.0
github.com/nishanths/predeclared v0.2.2
github.com/nunnatsa/ginkgolinter v0.16.2
github.com/peczenyj/ttempdir v0.4.1
github.com/pelletier/go-toml/v2 v2.2.2
github.com/polyfloyd/go-errorlint v1.5.2
github.com/quasilyte/go-ruleguard/dsl v0.3.22
Expand Down Expand Up @@ -193,7 +194,7 @@ require (
golang.org/x/mod v0.18.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum

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

17 changes: 17 additions & 0 deletions jsonschema/golangci.next.jsonschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@
"testpackage",
"thelper",
"tparallel",
"ttempdir",
"typecheck",
"unconvert",
"unparam",
Expand Down Expand Up @@ -3137,6 +3138,22 @@
}
}
},
"ttempdir": {
"type": "object",
"additionalProperties": false,
"properties": {
"all": {
"description": "The option `all` will run against whole test files (`_test.go`) regardless of method/function signatures.",
"type": "boolean",
"default": false
},
"max-recursion-level": {
"description": "Max recursion level when checking nested arg calls",
"type": "integer",
"default": 5
}
}
},
"usestdlibvars": {
"type": "object",
"additionalProperties": false,
Expand Down
10 changes: 10 additions & 0 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ var defaultLintersSettings = LintersSettings{
SkipRegexp: `(export|internal)_test\.go`,
AllowPackages: []string{"main"},
},
Ttempdir: TtempdirSettings{
All: false,
MaxRecursionLevel: 5,
},
Unparam: UnparamSettings{
Algo: "cha",
},
Expand Down Expand Up @@ -269,6 +273,7 @@ type LintersSettings struct {
Testifylint TestifylintSettings
Testpackage TestpackageSettings
Thelper ThelperSettings
Ttempdir TtempdirSettings
Unconvert UnconvertSettings
Unparam UnparamSettings
Unused UnusedSettings
Expand Down Expand Up @@ -919,6 +924,11 @@ type TenvSettings struct {
All bool `mapstructure:"all"`
}

type TtempdirSettings struct {
All bool `mapstructure:"all"`
MaxRecursionLevel uint `mapstructure:"max-recursion-level"`
}

type UseStdlibVarsSettings struct {
HTTPMethod bool `mapstructure:"http-method"`
HTTPStatusCode bool `mapstructure:"http-status-code"`
Expand Down
59 changes: 59 additions & 0 deletions pkg/golinters/ttempdir/testdata/ttempdir_all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//golangcitest:args -Ettempdir
//golangcitest:config_path testdata/ttempdir_all.yml
package testdata

import (
"os"
"testing"
)

var (
_, ee = os.MkdirTemp("a", "b") // never seen
)

func setup() {
os.MkdirTemp("a", "b") // never seen
_, err := os.MkdirTemp("a", "b") // never seen
if err != nil {
_ = err
}
os.MkdirTemp("a", "b") // never seen
}

func F(t *testing.T) {
setup()
os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in F"
_, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in F"
_ = err
if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in F"
_ = err
}
}

func BF(b *testing.B) {
TBF(b)
os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BF"
_, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BF"
_ = err
if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BF"
_ = err
}
}

func TBF(tb testing.TB) {
os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TBF"
_, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TBF"
_ = err
if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TBF"
_ = err
}
}

func FF(f *testing.F) {
os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FF"
_, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FF"
_ = err
if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FF"
_ = err
}
}
4 changes: 4 additions & 0 deletions pkg/golinters/ttempdir/testdata/ttempdir_all.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
linters-settings:
ttempdir:
all: true
max-recursion-level: 5
82 changes: 82 additions & 0 deletions pkg/golinters/ttempdir/testdata/ttempdir_all_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//golangcitest:args -Ettempdir
//golangcitest:config_path testdata/ttempdir_all.yml
package testdata

import (
"os"
"testing"
)

var (
_, e = os.MkdirTemp("a", "b") // never seen
)

func testsetup() {
os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `testing\\.TempDir\\(\\)` in testsetup"
_, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `testing\\.TempDir\\(\\)` in testsetup"
if err != nil {
_ = err
}
os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `testing\\.TempDir\\(\\)` in testsetup"
}

func TestF(t *testing.T) {
testsetup()
os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestF"
_, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestF"
_ = err
if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestF"
_ = err
}
t.Cleanup(func() {
_, _ = os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `testing\\.TempDir\\(\\)` in anonymous function"
})
}

func BenchmarkF(b *testing.B) {
TB(b)
os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BenchmarkF"
_, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BenchmarkF"
_ = err
if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BenchmarkF"
_ = err
}
}

func TB(tb testing.TB) {
os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TB"
_, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TB"
_ = err
if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TB"
_ = err
}
}

func FuzzF(f *testing.F) {
os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FuzzF"
_, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FuzzF"
_ = err
if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FuzzF"
_ = err
}
}

func TestFunctionLiteral(t *testing.T) {
testsetup()
t.Run("test", func(t *testing.T) {
os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in anonymous function"
_, err := os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in anonymous function"
_ = err
if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in anonymous function"
_ = err
}
})
}

func TestEmpty(t *testing.T) {
t.Run("test", func(*testing.T) {})
}

func TestEmptyTB(t *testing.T) {
func(testing.TB) {}(t)
}
54 changes: 54 additions & 0 deletions pkg/golinters/ttempdir/testdata/ttempdir_default_happy_path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//golangcitest:args -Ettempdir
//golangcitest:expected_exitcode 0
package testdata

import (
"os"
"testing"
)

var (
dir = os.TempDir() // never seen
)

func setup() {
os.TempDir() // never seen
dir := os.TempDir() // never seen
_ = dir
_ = os.TempDir() // never seen
}

func F(t *testing.T) {
setup()
t.TempDir() // never seen
t.Log(t.TempDir()) // never seen
_ = t.TempDir() // never seen
if dir := t.TempDir(); dir != "" { // never seen
_ = dir
}
}

func BF(b *testing.B) {
TBF(b)
b.TempDir() // never seen
_ = b.TempDir() // never seen
if dir := b.TempDir(); dir != "" { // never seen
_ = dir
}
}

func TBF(tb testing.TB) {
tb.TempDir() // never seen
_ = tb.TempDir() // never seen
if dir := tb.TempDir(); dir != "" { // never seen
_ = dir
}
}

func FF(f *testing.F) {
f.TempDir() // never seen
_ = f.TempDir() // never seen
if dir := f.TempDir(); dir != "" { // never seen
_ = dir
}
}
Loading
Loading