diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 4c214c3..9b040a1 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -27,10 +27,10 @@ jobs: go-version: ${{ matrix.go }} - name: Test - run: go test -v ./... + run: make test - name: Coverage - run: go test -v -race -cover -covermode=atomic -coverprofile coverage.out ./... + run: make coverage - name: Upload coverage reports to Codecov uses: codecov/codecov-action@v4.0.1 diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 628e637..bd72628 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -20,10 +20,7 @@ jobs: - uses: actions/setup-go@v5 with: go-version: stable - - run: go mod tidy - - run: go version - - run: go fix ./... - - run: go vet -all ./... + - run: make info tidy go_fix go_vet - name: golangci-lint uses: golangci/golangci-lint-action@v6 with: diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e335e07 --- /dev/null +++ b/Makefile @@ -0,0 +1,34 @@ +.PHONY: info fmt goimports gofumpt lint tidy go_fix go_vet golangci tests coverage + +info: + go version + +fmt: goimports gofumpt + $(info === format done) + +goimports: + goimports -e -l -w -local github.com/peczenyj/ttempdir . + +gofumpt: + gofumpt -l -w -extra . + +lint: tidy go_fix go_vet golangci + $(info === lint done) + +tidy: + go mod tidy + +go_fix: + go fix ./... + +go_vet: + go vet -all ./... + +golangci: + golangci-lint run ./... + +tests: + go test -v ./... + +coverage: + go test -v -race -cover -covermode=atomic -coverprofile coverage.out ./... diff --git a/ttestdir.go b/ttestdir.go index 06c3ad8..4e60c7b 100644 --- a/ttestdir.go +++ b/ttestdir.go @@ -10,7 +10,11 @@ import ( "golang.org/x/tools/go/ast/inspector" ) -const doc = "ttempdir is analyzer that detects using os.MkdirTemp, ioutil.TempDir or os.TempDir instead of t.TempDir since Go1.17" +const ( + doc = "ttempdir is analyzer that detects using os.MkdirTemp, ioutil.TempDir or os.TempDir instead of t.TempDir since Go1.17" + + defaultMaxCheckCallExprRecursionLevel = 50 // arbitrary value, just to avoid too many recursion calls +) // Analyzer is ttempdir analyzer var Analyzer = &analysis.Analyzer{ @@ -23,12 +27,15 @@ var Analyzer = &analysis.Analyzer{ } var ( - A = "all" - aflag bool + A = "all" + aflag bool + MRL = "max-recursion-level" + mrlFlag int ) func init() { Analyzer.Flags.BoolVar(&aflag, A, false, "the all option will run against all method in test file") + Analyzer.Flags.IntVar(&mrlFlag, MRL, defaultMaxCheckCallExprRecursionLevel, "max recursion level when checking nested arg calls") } func run(pass *analysis.Pass) (interface{}, error) { @@ -92,15 +99,23 @@ func checkExprStmt(pass *analysis.Pass, stmt *ast.ExprStmt, funcName, argName st return false } - checkCallExprRecursive(pass, callExpr, funcName, argName) + checkCallExprRecursive(pass, callExpr, funcName, argName, mrlFlag) return true } -func checkCallExprRecursive(pass *analysis.Pass, callExpr *ast.CallExpr, funcName, argName string) { +func checkCallExprRecursive(pass *analysis.Pass, + callExpr *ast.CallExpr, + funcName, argName string, + currentRecursionLevel int, +) { + if currentRecursionLevel == 0 { + return + } + for _, arg := range callExpr.Args { if argCallExpr, ok := arg.(*ast.CallExpr); ok { - checkCallExprRecursive(pass, argCallExpr, funcName, argName) + checkCallExprRecursive(pass, argCallExpr, funcName, argName, currentRecursionLevel-1) } } @@ -114,7 +129,6 @@ func checkCallExprRecursive(pass *analysis.Pass, callExpr *ast.CallExpr, funcNam } checkTargetNames(pass, callExpr, funcName, argName, fun, x) - } func checkIfStmt(pass *analysis.Pass, stmt *ast.IfStmt, funcName, argName string) bool {