Skip to content

Commit

Permalink
feat: add "-mincov" flag option (Issue #19) (#20)
Browse files Browse the repository at this point in the history
* chore: add test for `-mincov` option

* feat: "-mincov" option to set coverage threshold (issue #19)

* docs: add description of "-mincov" option

* chore: add more test for `-mincov` option (getCoverForDir)

* fix: skip files early on `getCoverForDir()`

See: #20 (comment)

* fix: remove redundant filtering

Filtering in `getCoverForDir()` is enough.
  • Loading branch information
KEINOS authored Sep 8, 2022
1 parent 7075de8 commit f7355b5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ With `-256colors` option, shades of green indicate the level of coverage.

By default skip vendor directories (Godeps,vendor), otherwise use `-include-vendor` option.

The `-mincov` option allows you to specify a coverage threshold to limit the files to be displayed.

Usage
-----

Expand All @@ -29,6 +31,8 @@ Usage
comma-separated functions list (default: all functions)
-include-vendor
include vendor directories for show coverage (Godeps, vendor)
-mincov float
coverage threshold of the file to be displayed (in percent) (default 100)
-summary
only show summary for each file
-version
Expand Down
7 changes: 7 additions & 0 deletions go-carpet.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ func getCoverForDir(coverFileName string, filesFilter []string, config Config) (
}

for _, fileProfile := range coverProfile {
// Skip files if minimal coverage is set and is covered more than minimal coverage
if config.minCoverage > 0 && config.minCoverage < 100.0 && getStatForProfileBlocks(fileProfile.Blocks) > config.minCoverage {
continue
}

var fileName string
if strings.HasPrefix(fileProfile.FileName, "/") {
// TODO: what about windows?
Expand Down Expand Up @@ -355,6 +360,7 @@ type Config struct {
funcFilterRaw string
funcFilter []string
argsRaw string
minCoverage float64
colors256 bool
includeVendor bool
summary bool
Expand All @@ -369,6 +375,7 @@ func init() {
flag.BoolVar(&config.summary, "summary", false, "only show summary for each file")
flag.BoolVar(&config.includeVendor, "include-vendor", false, "include vendor directories for show coverage (Godeps, vendor)")
flag.StringVar(&config.argsRaw, "args", "", "pass additional `arguments` for go test")
flag.Float64Var(&config.minCoverage, "mincov", 100.0, "coverage threshold of the file to be displayed (in percent)")
flag.Usage = func() {
fmt.Println(usageMessage)
flag.PrintDefaults()
Expand Down
42 changes: 42 additions & 0 deletions unix_only_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,45 @@ func Test_getCoverForDir(t *testing.T) {
}
})
}

func Test_getCoverForDir_mincov_flag(t *testing.T) {
t.Run("covered 100% mincov 100%", func(t *testing.T) {
conf := Config{
colors256: false,
minCoverage: 100.0,
}

// cover_00.out has 100% coverage of 2 files
_, profileBlocks, err := getCoverForDir("./testdata/cover_00.out", []string{"file_01.go"}, conf)
if err != nil {
t.Errorf("getCoverForDir() failed with error: %s", err)
}

expectLen := 2
actualLen := len(profileBlocks)

if expectLen != actualLen {
t.Errorf("1. minimum coverage 100%% should print all the blocks. want %v, got: %v", expectLen, actualLen)
}
})

t.Run("covered 100% mincov 50%", func(t *testing.T) {
conf := Config{
colors256: false,
minCoverage: 50.0,
}

// cover_00.out has 100% coverage of 2 files
_, profileBlocks, err := getCoverForDir("./testdata/cover_00.out", []string{"file_01.go"}, conf)
if err != nil {
t.Errorf("getCoverForDir() failed with error: %s", err)
}

expectLen := 0
actualLen := len(profileBlocks)

if expectLen != actualLen {
t.Errorf("2. minimum coverage 50%% for 100%% covered source should print nothing. want %v, got: %v", expectLen, actualLen)
}
})
}

0 comments on commit f7355b5

Please sign in to comment.