Skip to content

Commit

Permalink
refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
peczenyj committed Jun 4, 2024
1 parent 923949a commit dda36a6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
Expand Down
5 changes: 1 addition & 4 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
34 changes: 34 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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 ./...
28 changes: 21 additions & 7 deletions ttestdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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 {
Expand Down

0 comments on commit dda36a6

Please sign in to comment.