diff --git a/README.md b/README.md index ad68dc8..4f383ec 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,9 @@ Blocks or files with a `// notest` comment are excluded. ### Generated code Generated code files, that contain the respective comment line that is specified by the [Go Team](https://github.com/golang/go/issues/41196) in -[`go generate`](https://cs.opensource.google/go/go/+/refs/tags/go1.18:src/cmd/go/internal/generate/generate.go;l=66-73). +[`go generate`](https://golang.org/s/generatedcode). + +This exclude is disabled by default. Use flag `-g` to enable this behavior. ### Blocks returning a error tested to be non-nil We only exclude blocks where the error being returned has been tested to be @@ -119,6 +121,11 @@ courtney -t="-count=2" -t="-parallel=4" All the output from the `go test -v` command is shown. +### Exclude generated code: -g +`Exclude generated code from coverage` + +All generated code is excluded from coverage. + # Output Courtney will fail if the tests fail. If the tests succeed, it will create or overwrite a `coverage.out` file in the current directory. diff --git a/courtney.go b/courtney.go index 28025a4..bd8d96c 100644 --- a/courtney.go +++ b/courtney.go @@ -27,19 +27,21 @@ func main() { argsFlag := new(argsValue) flag.Var(argsFlag, "t", "Argument to pass to the 'go test' command. Can be used more than once.") loadFlag := flag.String("l", "", "Load coverage file(s) instead of running 'go test'") + excludeGeneratedCodeFlag := flag.Bool("g", false, "Exclude generated code from coverage") flag.Parse() setup := &shared.Setup{ - Env: env, - Paths: patsy.NewCache(env), - Enforce: *enforceFlag, - Verbose: *verboseFlag, - Short: *shortFlag, - Timeout: *timeoutFlag, - Output: *outputFlag, - TestArgs: argsFlag.args, - Load: *loadFlag, + Env: env, + Paths: patsy.NewCache(env), + Enforce: *enforceFlag, + Verbose: *verboseFlag, + Short: *shortFlag, + Timeout: *timeoutFlag, + Output: *outputFlag, + TestArgs: argsFlag.args, + Load: *loadFlag, + ExcludeGeneratedCode: *excludeGeneratedCodeFlag, } if err := Run(setup); err != nil { fmt.Printf("%+v", err) diff --git a/scanner/scanner.go b/scanner/scanner.go index 95c62b8..1c186c1 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -132,17 +132,19 @@ func (p *PackageMap) ScanPackage() error { } } - // Exclude complete files, if the code is generated. - for _, f := range p.pkg.GoFiles { - body, err := os.ReadFile(f) - if err != nil { - return errors.WithStack(err) - } - if !isGenerated(body) { - continue - } - for i := range bytes.Split(body, []byte("\n")) { - p.addExclude(f, i+1) + if p.setup.ExcludeGeneratedCode { + // Exclude complete files, if the code is generated. + for _, f := range p.pkg.GoFiles { + body, err := os.ReadFile(f) + if err != nil { + return errors.WithStack(err) + } + if !isGenerated(body) { + continue + } + for i := range bytes.Split(body, []byte("\n")) { + p.addExclude(f, i+1) + } } } return nil diff --git a/scanner/scanner_test.go b/scanner/scanner_test.go index 7b5a60b..5e9bf38 100644 --- a/scanner/scanner_test.go +++ b/scanner/scanner_test.go @@ -715,8 +715,9 @@ func test(t *testing.T, tests map[string]string) { paths := patsy.NewCache(env) setup := &shared.Setup{ - Env: env, - Paths: paths, + Env: env, + Paths: paths, + ExcludeGeneratedCode: true, } if err := setup.Parse([]string{ppath}); err != nil { t.Fatalf("Error parsing args in %s: %+v", name, err) diff --git a/shared/shared.go b/shared/shared.go index 82be997..678ea2f 100644 --- a/shared/shared.go +++ b/shared/shared.go @@ -10,16 +10,17 @@ import ( // Setup holds globals, environment and command line flags for the courtney // command type Setup struct { - Env vos.Env - Paths *patsy.Cache - Enforce bool - Verbose bool - Short bool - Timeout string - Load string - Output string - TestArgs []string - Packages []PackageSpec + Env vos.Env + Paths *patsy.Cache + Enforce bool + Verbose bool + Short bool + Timeout string + Load string + Output string + TestArgs []string + ExcludeGeneratedCode bool + Packages []PackageSpec } // PackageSpec identifies a package by dir and path