Skip to content

Commit f7355b5

Browse files
authored
feat: add "-mincov" flag option (Issue #19) (#20)
* 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.
1 parent 7075de8 commit f7355b5

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ With `-256colors` option, shades of green indicate the level of coverage.
1515

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

18+
The `-mincov` option allows you to specify a coverage threshold to limit the files to be displayed.
19+
1820
Usage
1921
-----
2022

@@ -29,6 +31,8 @@ Usage
2931
comma-separated functions list (default: all functions)
3032
-include-vendor
3133
include vendor directories for show coverage (Godeps, vendor)
34+
-mincov float
35+
coverage threshold of the file to be displayed (in percent) (default 100)
3236
-summary
3337
only show summary for each file
3438
-version

go-carpet.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ func getCoverForDir(coverFileName string, filesFilter []string, config Config) (
160160
}
161161

162162
for _, fileProfile := range coverProfile {
163+
// Skip files if minimal coverage is set and is covered more than minimal coverage
164+
if config.minCoverage > 0 && config.minCoverage < 100.0 && getStatForProfileBlocks(fileProfile.Blocks) > config.minCoverage {
165+
continue
166+
}
167+
163168
var fileName string
164169
if strings.HasPrefix(fileProfile.FileName, "/") {
165170
// TODO: what about windows?
@@ -355,6 +360,7 @@ type Config struct {
355360
funcFilterRaw string
356361
funcFilter []string
357362
argsRaw string
363+
minCoverage float64
358364
colors256 bool
359365
includeVendor bool
360366
summary bool
@@ -369,6 +375,7 @@ func init() {
369375
flag.BoolVar(&config.summary, "summary", false, "only show summary for each file")
370376
flag.BoolVar(&config.includeVendor, "include-vendor", false, "include vendor directories for show coverage (Godeps, vendor)")
371377
flag.StringVar(&config.argsRaw, "args", "", "pass additional `arguments` for go test")
378+
flag.Float64Var(&config.minCoverage, "mincov", 100.0, "coverage threshold of the file to be displayed (in percent)")
372379
flag.Usage = func() {
373380
fmt.Println(usageMessage)
374381
flag.PrintDefaults()

unix_only_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,45 @@ func Test_getCoverForDir(t *testing.T) {
7979
}
8080
})
8181
}
82+
83+
func Test_getCoverForDir_mincov_flag(t *testing.T) {
84+
t.Run("covered 100% mincov 100%", func(t *testing.T) {
85+
conf := Config{
86+
colors256: false,
87+
minCoverage: 100.0,
88+
}
89+
90+
// cover_00.out has 100% coverage of 2 files
91+
_, profileBlocks, err := getCoverForDir("./testdata/cover_00.out", []string{"file_01.go"}, conf)
92+
if err != nil {
93+
t.Errorf("getCoverForDir() failed with error: %s", err)
94+
}
95+
96+
expectLen := 2
97+
actualLen := len(profileBlocks)
98+
99+
if expectLen != actualLen {
100+
t.Errorf("1. minimum coverage 100%% should print all the blocks. want %v, got: %v", expectLen, actualLen)
101+
}
102+
})
103+
104+
t.Run("covered 100% mincov 50%", func(t *testing.T) {
105+
conf := Config{
106+
colors256: false,
107+
minCoverage: 50.0,
108+
}
109+
110+
// cover_00.out has 100% coverage of 2 files
111+
_, profileBlocks, err := getCoverForDir("./testdata/cover_00.out", []string{"file_01.go"}, conf)
112+
if err != nil {
113+
t.Errorf("getCoverForDir() failed with error: %s", err)
114+
}
115+
116+
expectLen := 0
117+
actualLen := len(profileBlocks)
118+
119+
if expectLen != actualLen {
120+
t.Errorf("2. minimum coverage 50%% for 100%% covered source should print nothing. want %v, got: %v", expectLen, actualLen)
121+
}
122+
})
123+
}

0 commit comments

Comments
 (0)