Skip to content
forked from fzipp/gocyclo

Calculate cyclomatic complexities of functions in Go source code.

License

Notifications You must be signed in to change notification settings

gregoryv/gocyclo

 
 

Repository files navigation

Build Status codecov

Gocyclo calculates cyclomatic complexities of functions in Go source code.

The cyclomatic complexity of a function is calculated according to the following rules:

 1 is the base complexity of a function
+1 for each 'if', 'for', 'case', '&&' or '||'

To install, run

$ go get -u github.com/gregoryv/gocyclo/...

Usage:

$ gocyclo [<flag> ...] <Go file or directory> ...

Examples:

$ gocyclo .
$ gocyclo main.go
$ gocyclo -top 10 src/
$ gocyclo -over 5 *.go
$ gocyclo -avg .

The output fields for each line are:

<complexity> <package> <function> <file:row:column>

As unit test

func TestComplexity(t *testing.T) {
	files, err := filepath.Glob("*.go")
	if err != nil {
		t.Fatal(err)
	}
	max := 5
	result, ok := gocyclo.Assert(files, max)
	if !ok {
		for _, l := range result {
			t.Log(l)
		}
		t.Errorf("Exceeded maximum complexity %v", max)
	}
}

About

Calculate cyclomatic complexities of functions in Go source code.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%