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 all 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
18 changes: 18 additions & 0 deletions pkg/testcoverage/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,12 @@ func GenerateCoverageStats(cfg Config) ([]coverage.Stats, error) {
func Analyze(cfg Config, current, base []coverage.Stats) AnalyzeResult {
thr := cfg.Threshold
overrideRules := compileOverridePathRules(cfg)
hasFileOverrides, hasPackageOverrides := detectOverrides(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 +92,21 @@ 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") || strings.HasSuffix(override.Path, ".go$") {
hasFileOverrides = true
} else {
hasPackageOverrides = true
}
}

return hasFileOverrides, hasPackageOverrides
}

func saveCoverageBreakdown(cfg Config, stats []coverage.Stats) error {
if cfg.BreakdownFileName == "" {
return nil
Expand Down
34 changes: 33 additions & 1 deletion 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,6 +134,38 @@ func TestCheck(t *testing.T) {
pass := Check(buf, cfg)
assert.False(t, pass)
assertGithubActionErrorsCount(t, buf.String(), 0)
assertHumanReport(t, buf.String(), 0, 2)
assert.GreaterOrEqual(t, strings.Count(buf.String(), prefix), 0)
})

t.Run("valid profile - pass after file override", func(t *testing.T) {
t.Parallel()

buf := &bytes.Buffer{}
cfg := Config{
Profile: profileOK,
Threshold: Threshold{File: 70},
Override: []Override{{Threshold: 60, Path: "pkg/testcoverage/badgestorer/github.go"}},
}
pass := Check(buf, cfg)
assert.True(t, pass)
assertGithubActionErrorsCount(t, buf.String(), 0)
assertHumanReport(t, buf.String(), 1, 0)
assert.GreaterOrEqual(t, strings.Count(buf.String(), prefix), 0)
})

t.Run("valid profile - fail after file override", func(t *testing.T) {
t.Parallel()

buf := &bytes.Buffer{}
cfg := Config{
Profile: profileOK,
Threshold: Threshold{File: 70},
Override: []Override{{Threshold: 80, Path: "pkg/testcoverage/badgestorer/github.go"}},
}
pass := Check(buf, cfg)
assert.False(t, pass)
assertGithubActionErrorsCount(t, buf.String(), 0)
assertHumanReport(t, buf.String(), 0, 1)
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