Skip to content

Commit

Permalink
Add flag to exclude generated code from coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
breml committed Mar 29, 2022
1 parent 3795772 commit 0e4312f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 33 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
20 changes: 11 additions & 9 deletions courtney.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 13 additions & 11 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 11 additions & 10 deletions shared/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0e4312f

Please sign in to comment.