Skip to content

Commit

Permalink
Exclude generated code from coverage
Browse files Browse the repository at this point in the history
Fixes dave#15
  • Loading branch information
breml authored and bkmeneguello committed Nov 7, 2024
1 parent ccf8e7a commit f29e63b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ error rather than a panic.
### Notest comments
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).

### Blocks returning a error tested to be non-nil
We only exclude blocks where the error being returned has been tested to be
non-nil, so:
Expand Down
19 changes: 19 additions & 0 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package scanner

import (
"bytes"
"fmt"
"go/ast"
"go/constant"
"go/token"
"go/types"
"os"
"regexp"
"strings"

"github.com/dave/astrid"
Expand Down Expand Up @@ -117,6 +120,8 @@ func (c *CodeMap) ScanPackages() error {
return nil
}

var doNotEditRe = regexp.MustCompile(`(?m)^// Code generated .* DO NOT EDIT\.$`)

// ScanPackage scans a single package
func (p *PackageMap) ScanPackage() error {
for _, f := range p.pkg.Syntax {
Expand All @@ -130,6 +135,20 @@ func (p *PackageMap) ScanPackage() error {
return errors.WithStack(err)
}
}

// 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 !doNotEditRe.Match(body) {
continue
}
for i := range bytes.Split(body, []byte("\n")) {
p.addExclude(f, i+1)
}
}
return nil
}

Expand Down
15 changes: 14 additions & 1 deletion scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,17 @@ func TestComments(t *testing.T) {
test(t, tests)
}

func TestGenerated(t *testing.T) {
tests := map[string]string{
"generated": `// Code generated by courtney. DO NOT EDIT.
package foo // *
func Baz() int { // *
return 0 // *
} // *`,
}
test(t, tests)
}

func test(t *testing.T, tests map[string]string) {
for name, source := range tests {
env := vos.Mock()
Expand Down Expand Up @@ -746,7 +757,9 @@ func test(t *testing.T, tests map[string]string) {
notest := regexp.MustCompile("//\\s?notest(\\s//\\s?.*)?$")

for i, line := range strings.Split(source, "\n") {
expected := strings.HasSuffix(line, "// *") || notest.MatchString(line)
expected := strings.HasSuffix(line, "// *") ||
notest.MatchString(line) ||
strings.HasSuffix(line, "// Code generated by courtney. DO NOT EDIT.")
if result[i+1] != expected {
t.Fatalf("Unexpected state in %s, line %d: %s\n", name, i, strconv.Quote(strings.Trim(line, "\t")))
}
Expand Down

0 comments on commit f29e63b

Please sign in to comment.