Skip to content

Commit

Permalink
feat: add option to ignore specific packages
Browse files Browse the repository at this point in the history
  • Loading branch information
cancue committed Aug 6, 2024
1 parent a406ed7 commit 0664ad1
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 6 deletions.
1 change: 1 addition & 0 deletions reporter/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type Config struct {
Output string
Root string
Cutlines *Cutlines
Ignores []string
}

// Cutlines represents the values for safe, warning and danger.
Expand Down
11 changes: 10 additions & 1 deletion reporter/internal/dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ import (
"golang.org/x/tools/cover"
)

func NewGoProject(root string, cutlines *config.Cutlines) *GoProject {
func NewGoProject(root string, cutlines *config.Cutlines, ignores []string) *GoProject {
return &GoProject{
Dirs: make(map[string]*GoDir),
RootPath: root,
Cutlines: cutlines,
Ignores: ignores,
}
}

type GoProject struct {
Dirs map[string]*GoDir
RootPath string
Cutlines *config.Cutlines
Ignores []string
}

// Parse parses the input profiles filename and updates the GoProject's coverage report.
Expand All @@ -35,7 +37,14 @@ func (gp *GoProject) Parse(input string) error {
return err
}

PROFILE_LOOP:
for _, profile := range profiles {
for _, ignore := range gp.Ignores {
if strings.HasPrefix(profile.FileName, ignore) {
continue PROFILE_LOOP
}
}

dir := gp.SafeDir(filepath.Dir(profile.FileName))
var file *GoFile
for _, f := range dir.Files {
Expand Down
6 changes: 3 additions & 3 deletions reporter/internal/dirs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestGoListItemPercent(t *testing.T) {
}

func TestSafeDir(t *testing.T) {
gp := NewGoProject("a", nil)
gp := NewGoProject("a", nil, nil)
a := gp.SafeDir("a")
assert.Equal(t, gp.Root(), a)
assert.NotEqual(t, a, gp.SafeDir("."))
Expand All @@ -80,7 +80,7 @@ func TestSafeDir(t *testing.T) {
}

func TestAggregate(t *testing.T) {
gp := NewGoProject(".", nil)
gp := NewGoProject(".", nil, nil)
a := gp.Root()
a.AddFile(&GoFile{
GoListItem: &GoListItem{
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestGoProject_Parse(t *testing.T) {
_, err = temp.WriteString(tt.input)
assert.NoError(t, err)

gp := NewGoProject(curPkg, nil)
gp := NewGoProject(curPkg, nil, nil)
err = gp.Parse(input)
assert.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion reporter/internal/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

func TestReport(t *testing.T) {
t.Run("should return error when cannot read file", func(t *testing.T) {
gp := NewGoProject("/", &config.Cutlines{Safe: 70, Warning: 40})
gp := NewGoProject("/", &config.Cutlines{Safe: 70, Warning: 40}, nil)
file := &GoFile{GoListItem: NewGoListItem("not-exist.go")}
gp.Root().AddFile(file)
err := gp.Report(nil)
Expand Down
12 changes: 11 additions & 1 deletion reporter/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

// Report generates a coverage report using the given configuration.
func Report(cfg *config.Config) error {
gp := internal.NewGoProject(cfg.Root, cfg.Cutlines)
gp := internal.NewGoProject(cfg.Root, cfg.Cutlines, cfg.Ignores)
if err := gp.Parse(cfg.Input); err != nil {
return err
}
Expand All @@ -38,6 +38,7 @@ func NewCLIConfig() (*config.Config, error) {
output := flag.String("o", "cover.html", "output file name")
cutlines := flag.String("cutlines", "70,40", "cutlines (safe,warning)")
root := flag.String("root", ".", "root package name")
ignores := flag.String("ignores", "", "ignore packages (comma separated)")
flag.Parse()

parsedCutlines, err := ParseCutlines(*cutlines)
Expand All @@ -50,6 +51,7 @@ func NewCLIConfig() (*config.Config, error) {
Output: *output,
Cutlines: parsedCutlines,
Root: *root,
Ignores: ParseIgnores(*ignores),
}, nil
}

Expand All @@ -70,3 +72,11 @@ func ParseCutlines(cutlines string) (*config.Cutlines, error) {
Warning: warning,
}, nil
}

// ParseIgnores parses the ignores argument.
func ParseIgnores(ignores string) []string {
if ignores == "" {
return nil
}
return strings.Split(ignores, ",")
}
15 changes: 15 additions & 0 deletions reporter/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ func TestParseCutlines(t *testing.T) {
})
}

func TestParseIgnores(t *testing.T) {
t.Run("should return nil with empty string", func(t *testing.T) {
ignores := reporter.ParseIgnores("")
assert.Nil(t, ignores)
})

t.Run("should work", func(t *testing.T) {
ignores := reporter.ParseIgnores("example.txt,foobar,/.tmp")
assert.Equal(t, 3, len(ignores))
assert.Equal(t, "example.txt", ignores[0])
assert.Equal(t, "foobar", ignores[1])
assert.Equal(t, "/.tmp", ignores[2])
})
}

func TestNewCLIConfig(t *testing.T) {
t.Run("should have valid default values", func(t *testing.T) {
cfg, err := reporter.NewCLIConfig()
Expand Down

0 comments on commit 0664ad1

Please sign in to comment.