Skip to content

Commit 1320cd1

Browse files
authored
reports are added to output only if thresholds are specified (#111)
1 parent 71bdf74 commit 1320cd1

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

pkg/testcoverage/check_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func TestCheck(t *testing.T) {
5454
pass := Check(buf, cfg)
5555
assert.True(t, pass)
5656
assertGithubActionErrorsCount(t, buf.String(), 0)
57-
assertHumanReport(t, buf.String(), 3, 0)
57+
assertHumanReport(t, buf.String(), 1, 0)
5858
})
5959

6060
t.Run("valid profile with exclude - pass", func(t *testing.T) {
@@ -71,7 +71,7 @@ func TestCheck(t *testing.T) {
7171
pass := Check(buf, cfg)
7272
assert.True(t, pass)
7373
assertGithubActionErrorsCount(t, buf.String(), 0)
74-
assertHumanReport(t, buf.String(), 3, 0)
74+
assertHumanReport(t, buf.String(), 1, 0)
7575
})
7676

7777
t.Run("valid profile - fail", func(t *testing.T) {
@@ -82,7 +82,7 @@ func TestCheck(t *testing.T) {
8282
pass := Check(buf, cfg)
8383
assert.False(t, pass)
8484
assertGithubActionErrorsCount(t, buf.String(), 0)
85-
assertHumanReport(t, buf.String(), 2, 1)
85+
assertHumanReport(t, buf.String(), 0, 1)
8686
assert.GreaterOrEqual(t, strings.Count(buf.String(), prefix), 0)
8787
})
8888

@@ -95,7 +95,7 @@ func TestCheck(t *testing.T) {
9595
pass := Check(buf, cfg)
9696
assert.True(t, pass)
9797
assertGithubActionErrorsCount(t, buf.String(), 0)
98-
assertHumanReport(t, buf.String(), 3, 0)
98+
assertHumanReport(t, buf.String(), 1, 0)
9999
assert.Equal(t, 0, strings.Count(buf.String(), prefix))
100100
})
101101

@@ -111,7 +111,7 @@ func TestCheck(t *testing.T) {
111111
pass := Check(buf, cfg)
112112
assert.True(t, pass)
113113
assertGithubActionErrorsCount(t, buf.String(), 0)
114-
assertHumanReport(t, buf.String(), 3, 0)
114+
assertHumanReport(t, buf.String(), 1, 0)
115115
assert.GreaterOrEqual(t, strings.Count(buf.String(), prefix), 0)
116116
})
117117

@@ -127,7 +127,7 @@ func TestCheck(t *testing.T) {
127127
pass := Check(buf, cfg)
128128
assert.False(t, pass)
129129
assertGithubActionErrorsCount(t, buf.String(), 0)
130-
assertHumanReport(t, buf.String(), 1, 2)
130+
assertHumanReport(t, buf.String(), 0, 1)
131131
assert.GreaterOrEqual(t, strings.Count(buf.String(), prefix), 0)
132132
})
133133

@@ -172,7 +172,7 @@ func TestCheckNoParallel(t *testing.T) {
172172
pass := Check(buf, cfg)
173173
assert.True(t, pass)
174174
assertGithubActionErrorsCount(t, buf.String(), 0)
175-
assertHumanReport(t, buf.String(), 3, 0)
175+
assertHumanReport(t, buf.String(), 1, 0)
176176
assertGithubOutputValues(t, testFile)
177177
})
178178

@@ -185,7 +185,7 @@ func TestCheckNoParallel(t *testing.T) {
185185
pass := Check(buf, cfg)
186186
assert.False(t, pass)
187187
assertGithubActionErrorsCount(t, buf.String(), 1)
188-
assertHumanReport(t, buf.String(), 2, 1)
188+
assertHumanReport(t, buf.String(), 0, 1)
189189
assertGithubOutputValues(t, testFile)
190190
})
191191
}

pkg/testcoverage/report.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,27 @@ func ReportForHuman(w io.Writer, result AnalyzeResult) {
3131

3232
thr := result.Threshold
3333

34-
// File threshold report
35-
fmt.Fprintf(tabber, "File coverage threshold (%d%%) satisfied:\t", thr.File)
36-
fmt.Fprint(tabber, statusStr(len(result.FilesBelowThreshold) == 0))
37-
reportIssuesForHuman(tabber, result.FilesBelowThreshold)
34+
if thr.File > 0 { // File threshold report
35+
fmt.Fprintf(tabber, "File coverage threshold (%d%%) satisfied:\t", thr.File)
36+
fmt.Fprint(tabber, statusStr(len(result.FilesBelowThreshold) == 0))
37+
reportIssuesForHuman(tabber, result.FilesBelowThreshold)
38+
fmt.Fprint(tabber, "\n")
39+
}
3840

39-
// Package threshold report
40-
fmt.Fprintf(tabber, "\nPackage coverage threshold (%d%%) satisfied:\t", thr.Package)
41-
fmt.Fprint(tabber, statusStr(len(result.PackagesBelowThreshold) == 0))
42-
reportIssuesForHuman(tabber, result.PackagesBelowThreshold)
41+
if thr.Package > 0 { // Package threshold report
42+
fmt.Fprintf(tabber, "Package coverage threshold (%d%%) satisfied:\t", thr.Package)
43+
fmt.Fprint(tabber, statusStr(len(result.PackagesBelowThreshold) == 0))
44+
reportIssuesForHuman(tabber, result.PackagesBelowThreshold)
45+
fmt.Fprint(tabber, "\n")
46+
}
4347

44-
// Total threshold report
45-
fmt.Fprintf(tabber, "\nTotal coverage threshold (%d%%) satisfied:\t", thr.Total)
46-
fmt.Fprint(tabber, statusStr(result.MeetsTotalCoverage))
48+
if thr.Total > 0 { // Total threshold report
49+
fmt.Fprintf(tabber, "Total coverage threshold (%d%%) satisfied:\t", thr.Total)
50+
fmt.Fprint(tabber, statusStr(result.MeetsTotalCoverage))
51+
fmt.Fprint(tabber, "\n")
52+
}
4753

48-
fmt.Fprintf(tabber, "\nTotal test coverage: %d%%\n", result.TotalCoverage)
54+
fmt.Fprintf(tabber, "Total test coverage: %d%%\n", result.TotalCoverage)
4955
}
5056

5157
func reportIssuesForHuman(w io.Writer, coverageStats []coverage.Stats) {

pkg/testcoverage/report_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,21 @@ func Test_ReportForHuman(t *testing.T) {
1616
t.Parallel()
1717

1818
prefix := "organization.org"
19+
thr := Threshold{100, 100, 100}
1920

2021
t.Run("all - pass", func(t *testing.T) {
2122
t.Parallel()
2223

2324
buf := &bytes.Buffer{}
24-
ReportForHuman(buf, AnalyzeResult{MeetsTotalCoverage: true})
25+
ReportForHuman(buf, AnalyzeResult{Threshold: thr, MeetsTotalCoverage: true})
2526
assertHumanReport(t, buf.String(), 3, 0)
2627
})
2728

2829
t.Run("total coverage - fail", func(t *testing.T) {
2930
t.Parallel()
3031

3132
buf := &bytes.Buffer{}
32-
ReportForHuman(buf, AnalyzeResult{MeetsTotalCoverage: false})
33+
ReportForHuman(buf, AnalyzeResult{Threshold: thr, MeetsTotalCoverage: false})
3334
assertHumanReport(t, buf.String(), 2, 1)
3435
})
3536

@@ -42,7 +43,7 @@ func Test_ReportForHuman(t *testing.T) {
4243
statsNoError := randStats(prefix, 10, 100)
4344
result := Analyze(cfg, mergeStats(statsWithError, statsNoError))
4445
ReportForHuman(buf, result)
45-
assertHumanReport(t, buf.String(), 2, 1)
46+
assertHumanReport(t, buf.String(), 0, 1)
4647
assertContainStats(t, buf.String(), statsWithError)
4748
assertNotContainStats(t, buf.String(), statsNoError)
4849
})
@@ -56,7 +57,7 @@ func Test_ReportForHuman(t *testing.T) {
5657
statsNoError := randStats(prefix, 10, 100)
5758
result := Analyze(cfg, mergeStats(statsWithError, statsNoError))
5859
ReportForHuman(buf, result)
59-
assertHumanReport(t, buf.String(), 2, 1)
60+
assertHumanReport(t, buf.String(), 0, 1)
6061
assertContainStats(t, buf.String(), MakePackageStats(statsWithError))
6162
assertNotContainStats(t, buf.String(), MakePackageStats(statsNoError))
6263
assertNotContainStats(t, buf.String(), statsWithError)

0 commit comments

Comments
 (0)