Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

report when overall threshold present (or) overrides are present #141

Merged
merged 4 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions pkg/testcoverage/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,19 @@ func GenerateCoverageStats(cfg Config) ([]coverage.Stats, error) {
}

func Analyze(cfg Config, current, base []coverage.Stats) AnalyzeResult {
var hasFileOverrides, hasPackageOverrides bool

thr := cfg.Threshold
overrideRules := compileOverridePathRules(cfg)

if len(cfg.Override) > 0 {
hasFileOverrides, hasPackageOverrides = detectOverrides(cfg.Override)
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if len(cfg.Override) > 0 { seems unnecessary, detectOverrides should return correct result for any size of cfg.Override


return AnalyzeResult{
Threshold: thr,
HasFileOverrides: hasFileOverrides,
HasPackageOverrides: hasPackageOverrides,
FilesBelowThreshold: checkCoverageStatsBelowThreshold(current, thr.File, overrideRules),
PackagesBelowThreshold: checkCoverageStatsBelowThreshold(
makePackageStats(current), thr.Package, overrideRules,
Expand All @@ -89,6 +97,27 @@ func Analyze(cfg Config, current, base []coverage.Stats) AnalyzeResult {
}
}

func detectOverrides(overrides []Override) (bool, bool) {
hasFileOverrides := false
hasPackageOverrides := false

for _, override := range overrides {
if strings.HasSuffix(override.Path, ".go") {
hasFileOverrides = true
}

if strings.HasPrefix(override.Path, "^") {
hasPackageOverrides = true
}

if hasFileOverrides && hasPackageOverrides {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't have anything against of merging this if, but it seems unnecessary. these overrides are usually very small, lets say there is 100 of them (99.99% of users will have less then 10). with this size, this for cycle will finish in 1ns or less, so this if wont produce any noticeably benefits. and on another hand because this repo requires 100% coverage, this if will required to have additional tests case that needs to be added or ignored.

return hasFileOverrides, hasPackageOverrides
}
}

return hasFileOverrides, hasPackageOverrides
}

func saveCoverageBreakdown(cfg Config, stats []coverage.Stats) error {
if cfg.BreakdownFileName == "" {
return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/testcoverage/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func TestCheck(t *testing.T) {
pass := Check(buf, cfg)
assert.True(t, pass)
assertGithubActionErrorsCount(t, buf.String(), 0)
assertHumanReport(t, buf.String(), 1, 0)
assertHumanReport(t, buf.String(), 2, 0)
assert.GreaterOrEqual(t, strings.Count(buf.String(), prefix), 0)
})

Expand All @@ -134,7 +134,7 @@ func TestCheck(t *testing.T) {
pass := Check(buf, cfg)
assert.False(t, pass)
assertGithubActionErrorsCount(t, buf.String(), 0)
assertHumanReport(t, buf.String(), 0, 1)
assertHumanReport(t, buf.String(), 0, 2)
assert.GreaterOrEqual(t, strings.Count(buf.String(), prefix), 0)
})

Expand Down
4 changes: 2 additions & 2 deletions pkg/testcoverage/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ func reportCoverage(w io.Writer, result AnalyzeResult) {

thr := result.Threshold

if thr.File > 0 { // File threshold report
if thr.File > 0 || result.HasFileOverrides { // File threshold report
fmt.Fprintf(tabber, "File coverage threshold (%d%%) satisfied:\t", thr.File)
fmt.Fprint(tabber, statusStr(len(result.FilesBelowThreshold) == 0))
reportIssuesForHuman(tabber, result.FilesBelowThreshold)
fmt.Fprint(tabber, "\n")
}

if thr.Package > 0 { // Package threshold report
if thr.Package > 0 || result.HasPackageOverrides { // Package threshold report
fmt.Fprintf(tabber, "Package coverage threshold (%d%%) satisfied:\t", thr.Package)
fmt.Fprint(tabber, statusStr(len(result.PackagesBelowThreshold) == 0))
reportIssuesForHuman(tabber, result.PackagesBelowThreshold)
Expand Down
2 changes: 2 additions & 0 deletions pkg/testcoverage/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type AnalyzeResult struct {
TotalStats coverage.Stats
HasBaseBreakdown bool
Diff []FileCoverageDiff
HasFileOverrides bool
HasPackageOverrides bool
}

func (r *AnalyzeResult) Pass() bool {
Expand Down
Loading