Skip to content

Commit 7c6ad23

Browse files
authored
reporting improvements (#18)
1 parent 210ef93 commit 7c6ad23

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

pkg/testcoverage/report.go

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,43 @@ func ReportForHuman(w io.Writer, result AnalyzeResult, thr Threshold) {
1414
out := bufio.NewWriter(w)
1515
defer out.Flush()
1616

17-
{
18-
fmt.Fprintf(out, "Files meeting coverage threshold of (%d%%):\t", thr.File)
19-
if len(result.FilesBelowThreshold) > 0 {
20-
fmt.Fprintf(out, "FAIL")
21-
reportIssuesForHuman(out, result.FilesBelowThreshold)
22-
} else {
23-
fmt.Fprintf(out, "PASS")
17+
statusStr := func(passing bool) string {
18+
if passing {
19+
return "PASS"
2420
}
25-
}
2621

27-
{
28-
fmt.Fprintf(out, "\nPackages meeting coverage threshold of (%d%%):\t", thr.Package)
29-
if len(result.PackagesBelowThreshold) > 0 {
30-
fmt.Fprintf(out, "FAIL")
31-
reportIssuesForHuman(out, result.PackagesBelowThreshold)
32-
} else {
33-
fmt.Fprintf(out, "PASS")
34-
}
22+
return "FAIL"
3523
}
3624

37-
{
38-
fmt.Fprintf(out, "\nTotal coverage meeting the threshold of (%d%%):\t", thr.Total)
39-
if !result.MeetsTotalCoverage {
40-
fmt.Fprintf(out, "FAIL")
41-
} else {
42-
fmt.Fprintf(out, "PASS")
43-
}
44-
}
25+
// File threshold report
26+
fmt.Fprintf(out, "File coverage threshold (%d%%) satisfied:\t", thr.File)
27+
fmt.Fprint(out, statusStr(len(result.FilesBelowThreshold) == 0))
28+
reportIssuesForHuman(out, result.FilesBelowThreshold)
29+
30+
// Package threshold report
31+
fmt.Fprintf(out, "\nPackage coverage threshold (%d%%) satisfied:\t", thr.Package)
32+
fmt.Fprint(out, statusStr(len(result.PackagesBelowThreshold) == 0))
33+
reportIssuesForHuman(out, result.PackagesBelowThreshold)
34+
35+
// Total threshold report
36+
fmt.Fprintf(out, "\nTotal coverage threshold (%d%%) satisfied:\t", thr.Total)
37+
fmt.Fprint(out, statusStr(result.MeetsTotalCoverage))
4538

4639
fmt.Fprintf(out, "\nTotal test coverage: %d%%\n", result.TotalCoverage)
4740
}
4841

4942
func reportIssuesForHuman(w io.Writer, coverageStats []CoverageStats) {
50-
tabber := tabwriter.NewWriter(w, 1, 8, 1, '\t', 0) //nolint:gomnd // relax
43+
if len(coverageStats) == 0 {
44+
return
45+
}
46+
47+
tabber := tabwriter.NewWriter(w, 1, 8, 2, '\t', 0) //nolint:gomnd // relax
5148
defer tabber.Flush()
5249

53-
fmt.Fprintf(tabber, "\n\nIssues with:")
50+
fmt.Fprintf(tabber, "\n below threshold:\tcoverage:")
5451

5552
for _, stats := range coverageStats {
56-
fmt.Fprintf(tabber, "\n%s\t%d%%", stats.Name, stats.CoveredPercentage())
53+
fmt.Fprintf(tabber, "\n %s\t%d%%", stats.Name, stats.CoveredPercentage())
5754
}
5855

5956
fmt.Fprintf(tabber, "\n")
@@ -72,25 +69,28 @@ func ReportForGithubAction(w io.Writer, result AnalyzeResult, thr Threshold) {
7269

7370
for _, stats := range result.FilesBelowThreshold {
7471
title := "File test coverage below threshold"
75-
c := stats.CoveredPercentage()
76-
t := thr.File
77-
msg := fmt.Sprintf("coverage: %d%%; threshold: %d%%", c, t)
72+
msg := fmt.Sprintf(
73+
"%s: coverage: %d%%; threshold: %d%%",
74+
title, stats.CoveredPercentage(), thr.File,
75+
)
7876
reportLineError(stats.Name, title, msg)
7977
}
8078

8179
for _, stats := range result.PackagesBelowThreshold {
8280
title := "Package test coverage below threshold"
83-
c := stats.CoveredPercentage()
84-
t := thr.Package
85-
msg := fmt.Sprintf("package: %s; coverage: %d%%; threshold: %d%%", stats.Name, c, t)
81+
msg := fmt.Sprintf(
82+
"%s: package: %s; coverage: %d%%; threshold: %d%%",
83+
title, stats.Name, stats.CoveredPercentage(), thr.Package,
84+
)
8685
reportError(title, msg)
8786
}
8887

8988
if !result.MeetsTotalCoverage {
9089
title := "Total test coverage below threshold"
91-
c := result.TotalCoverage
92-
t := thr.Total
93-
msg := fmt.Sprintf("coverage: %d%%; threshold: %d%%", c, t)
90+
msg := fmt.Sprintf(
91+
"%s: coverage: %d%%; threshold: %d%%",
92+
title, result.TotalCoverage, thr.Total,
93+
)
9494
reportError(title, msg)
9595
}
9696
}

0 commit comments

Comments
 (0)