Skip to content

Commit

Permalink
fix linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
bkielbasa committed Oct 9, 2024
1 parent 845eb92 commit 529618c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 26 deletions.
8 changes: 1 addition & 7 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/
10 changes: 5 additions & 5 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
run:
skip-dirs:
- pkg/analyzer/testdata
issues.exclude-dirs:
- pkg/analyzer/testdata

linters-settings:
golint:
Expand All @@ -18,9 +17,10 @@ linters:
- prealloc
- dupl
- wsl
- depguard
- nilnil
- exhaustruct
- nlreturn
- goerr113
- exhaustivestruct
- paralleltest
- testpackage
- gomnd
Expand Down
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
@@ -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
18 changes: 11 additions & 7 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions pkg/analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,38 @@ 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)
}

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")
}

0 comments on commit 529618c

Please sign in to comment.