diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 066057c..745924b 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -33,13 +33,7 @@ jobs: - name: Run Unit tests run: | - go test -race -covermode atomic -coverprofile=covprofile ./... - - name: Install goveralls - run: go get github.com/mattn/goveralls - - name: Send coverage - env: - COVERALLS_TOKEN: ${{ secrets.github_token }} - run: goveralls -coverprofile=covprofile -service=github + go test -race ./... - name: Check it's own cyclomatic complexity run: go run ./cmd/cyclop/ ./pkg/analyzer/ diff --git a/.golangci.yml b/.golangci.yml index 6c5ecf2..8294920 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,6 +1,5 @@ -run: - skip-dirs: - - pkg/analyzer/testdata +issues.exclude-dirs: + - pkg/analyzer/testdata linters-settings: golint: @@ -18,9 +17,10 @@ linters: - prealloc - dupl - wsl + - depguard + - nilnil + - exhaustruct - nlreturn - - goerr113 - - exhaustivestruct - paralleltest - testpackage - gomnd diff --git a/doc.go b/doc.go index 99b4747..bac7897 100644 --- a/doc.go +++ b/doc.go @@ -1,7 +1,7 @@ // Cyclop calculates cyclomatic complexities of functions in Go source code. // Simple usage: // -// cyclop . +// cyclop . // // Read more about usage in https://github.com/bkielbasa/cyclop/blob/master/README.md package main diff --git a/pkg/analyzer/analyzer.go b/pkg/analyzer/analyzer.go index 06bde5b..44607a3 100644 --- a/pkg/analyzer/analyzer.go +++ b/pkg/analyzer/analyzer.go @@ -19,9 +19,13 @@ var ( skipTests bool ) +const ( + defaultMaxComplexity = 10 +) + //nolint:gochecknoinits func init() { - flagSet.IntVar(&maxComplexity, "maxComplexity", 10, "max complexity the function can have") + flagSet.IntVar(&maxComplexity, "maxComplexity", defaultMaxComplexity, "max complexity the function can have") flagSet.Float64Var(&packageAverage, "packageAverage", 0, "max average complexity in package") flagSet.BoolVar(&skipTests, "skipTests", false, "should the linter execute on test files as well") } @@ -40,9 +44,9 @@ func run(pass *analysis.Pass) (interface{}, error) { var pkgName string var pkgPos token.Pos - for _, f := range pass.Files { - ast.Inspect(f, func(node ast.Node) bool { - f, ok := node.(*ast.FuncDecl) + for _, file := range pass.Files { + ast.Inspect(file, func(node ast.Node) bool { + funcDecl, ok := node.(*ast.FuncDecl) if !ok { if node == nil { return true @@ -55,15 +59,15 @@ func run(pass *analysis.Pass) (interface{}, error) { return true } - if skipTests && testFunc(f) { + if skipTests && testFunc(funcDecl) { return true } count++ - comp := complexity(f) + comp := complexity(funcDecl) sum += float64(comp) if comp > maxComplexity { - pass.Reportf(node.Pos(), "calculated cyclomatic complexity for function %s is %d, max is %d", f.Name.Name, comp, maxComplexity) + pass.Reportf(node.Pos(), "calculated cyclomatic complexity for function %s is %d, max is %d", file.Name.Name, comp, maxComplexity) } return true diff --git a/pkg/analyzer/analyzer_test.go b/pkg/analyzer/analyzer_test.go index 4db34d3..3f6fab4 100644 --- a/pkg/analyzer/analyzer_test.go +++ b/pkg/analyzer/analyzer_test.go @@ -9,31 +9,31 @@ import ( ) func TestAll(t *testing.T) { - wd, err := os.Getwd() + path, err := os.Getwd() if err != nil { t.Fatalf("Failed to get wd: %s", err) } skipTests = false - testdata := filepath.Join(filepath.Dir(filepath.Dir(wd)), "testdata") + testdata := filepath.Join(filepath.Dir(filepath.Dir(path)), "testdata") analysistest.Run(t, testdata, NewAnalyzer(), "p") } func TestIfTestFunctionsAreSkipped(t *testing.T) { - wd, err := os.Getwd() + path, err := os.Getwd() if err != nil { t.Fatalf("Failed to get wd: %s", err) } skipTests = true - testdata := filepath.Join(filepath.Dir(filepath.Dir(wd)), "testdata") + testdata := filepath.Join(filepath.Dir(filepath.Dir(path)), "testdata") analysistest.Run(t, testdata, NewAnalyzer(), "tests") } func TestAverageComplexityOfAPackage(t *testing.T) { - wd, err := os.Getwd() + path, err := os.Getwd() if err != nil { t.Fatalf("Failed to get wd: %s", err) } @@ -41,6 +41,6 @@ func TestAverageComplexityOfAPackage(t *testing.T) { skipTests = false packageAverage = 5 - testdata := filepath.Join(filepath.Dir(filepath.Dir(wd)), "testdata") + testdata := filepath.Join(filepath.Dir(filepath.Dir(path)), "testdata") analysistest.Run(t, testdata, NewAnalyzer(), "avg") }