Skip to content

Commit 1639ae9

Browse files
committed
initial effort
1 parent 4147bce commit 1639ae9

File tree

6 files changed

+51
-15
lines changed

6 files changed

+51
-15
lines changed

pkg/testcoverage/check.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ func Analyze(cfg Config, current, base []coverage.Stats) AnalyzeResult {
105105

106106
return AnalyzeResult{
107107
Threshold: thr,
108+
DiffThreshold: cfg.Diff.Threshold,
108109
HasFileOverrides: hasFileOverrides,
109110
HasPackageOverrides: hasPackageOverrides,
110111
FilesBelowThreshold: checkCoverageStatsBelowThreshold(current, thr.File, overrideRules),
@@ -115,6 +116,7 @@ func Analyze(cfg Config, current, base []coverage.Stats) AnalyzeResult {
115116
TotalStats: coverage.StatsCalcTotal(current),
116117
HasBaseBreakdown: len(base) > 0,
117118
Diff: calculateStatsDiff(current, base),
119+
DiffPercentage: TotalPercentageDiff(current, base),
118120
}
119121
}
120122

pkg/testcoverage/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ type Exclude struct {
5353
}
5454

5555
type Diff struct {
56-
BaseBreakdownFileName string `yaml:"base-breakdown-file-name"`
56+
BaseBreakdownFileName string `yaml:"base-breakdown-file-name"`
57+
Threshold *float64 `yaml:"threshold"`
5758
}
5859

5960
type Badge struct {

pkg/testcoverage/config_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ func nonZeroConfig() Config {
240240
BreakdownFileName: "breakdown.testcoverage",
241241
Diff: Diff{
242242
BaseBreakdownFileName: "breakdown.testcoverage",
243+
Threshold: ptr(-1.1),
243244
},
244245
GithubActionOutput: true,
245246
}
@@ -261,7 +262,8 @@ exclude:
261262
- path2
262263
breakdown-file-name: 'breakdown.testcoverage'
263264
diff:
264-
base-breakdown-file-name: 'breakdown.testcoverage'
265+
base-breakdown-file-name: 'breakdown.testcoverage'
266+
threshold: -1.1
265267
github-action-output: true`
266268
}
267269

pkg/testcoverage/helpers_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import (
1414
"github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage/coverage"
1515
)
1616

17+
func ptr[T any](t T) *T {
18+
return &t
19+
}
20+
1721
func mergeStats(a, b []coverage.Stats) []coverage.Stats {
1822
r := make([]coverage.Stats, 0, len(a)+len(b))
1923
r = append(r, a...)

pkg/testcoverage/report.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,6 @@ func reportCoverage(w io.Writer, result AnalyzeResult) {
2727
tabber := tabwriter.NewWriter(w, 1, 8, 2, '\t', 0) //nolint:mnd // relax
2828
defer tabber.Flush()
2929

30-
statusStr := func(passing bool) string {
31-
if passing {
32-
return "PASS"
33-
}
34-
35-
return "FAIL"
36-
}
37-
3830
thr := result.Threshold
3931

4032
if thr.File > 0 || result.HasFileOverrides { // File threshold report
@@ -103,13 +95,19 @@ func reportDiff(w io.Writer, result AnalyzeResult) {
10395
tabber := tabwriter.NewWriter(w, 1, 8, 2, '\t', 0) //nolint:mnd // relax
10496
defer tabber.Flush()
10597

98+
if result.DiffThreshold != nil {
99+
status := statusStr(result.MeetsDiffThreshold())
100+
fmt.Fprintf(tabber, "\nCoverage difference threshold (%.2f%%) satisfied:\t %s", *result.DiffThreshold, status)
101+
fmt.Fprintf(tabber, "\nCoverage difference: %.2f%%\n", result.DiffPercentage)
102+
}
103+
106104
if len(result.Diff) == 0 {
107-
fmt.Fprintf(tabber, "\nCurrent tests coverage has not changed.\n")
105+
fmt.Fprintf(tabber, "\nNo coverage changes in any files compared to the base.\n")
108106
return
109107
}
110108

111-
td := TotalLinesDiff(result.Diff)
112-
fmt.Fprintf(tabber, "\nCurrent tests coverage has changed with %d lines missing coverage.", td)
109+
td := TotalLinesMissingCoverage(result.Diff)
110+
fmt.Fprintf(tabber, "\nTest coverage has changed in the current files, with %d lines missing coverage.", td)
113111
fmt.Fprintf(tabber, "\n file:\tuncovered:\tcurrent coverage:\tbase coverage:")
114112

115113
for _, d := range result.Diff {
@@ -242,3 +240,11 @@ func compressUncoveredLines(w io.Writer, ull []int) {
242240
printRange(last, ull[len(ull)-1])
243241
}
244242
}
243+
244+
func statusStr(passing bool) string {
245+
if passing {
246+
return "PASS"
247+
}
248+
249+
return "FAIL"
250+
}

pkg/testcoverage/types.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,31 @@ import (
1010

1111
type AnalyzeResult struct {
1212
Threshold Threshold
13+
DiffThreshold *float64
1314
FilesBelowThreshold []coverage.Stats
1415
PackagesBelowThreshold []coverage.Stats
1516
FilesWithUncoveredLines []coverage.Stats
1617
TotalStats coverage.Stats
1718
HasBaseBreakdown bool
1819
Diff []FileCoverageDiff
20+
DiffPercentage float64
1921
HasFileOverrides bool
2022
HasPackageOverrides bool
2123
}
2224

2325
func (r *AnalyzeResult) Pass() bool {
2426
return r.MeetsTotalCoverage() &&
2527
len(r.FilesBelowThreshold) == 0 &&
26-
len(r.PackagesBelowThreshold) == 0
28+
len(r.PackagesBelowThreshold) == 0 &&
29+
r.MeetsDiffThreshold()
30+
}
31+
32+
func (r *AnalyzeResult) MeetsDiffThreshold() bool {
33+
if r.DiffThreshold == nil || !r.HasBaseBreakdown {
34+
return true
35+
}
36+
37+
return *r.DiffThreshold <= r.DiffPercentage
2738
}
2839

2940
func (r *AnalyzeResult) MeetsTotalCoverage() bool {
@@ -109,11 +120,21 @@ func calculateStatsDiff(current, base []coverage.Stats) []FileCoverageDiff {
109120
return res
110121
}
111122

112-
func TotalLinesDiff(diff []FileCoverageDiff) int {
123+
func TotalLinesMissingCoverage(diff []FileCoverageDiff) int {
113124
r := 0
114125
for _, d := range diff {
115126
r += d.Current.UncoveredLinesCount()
116127
}
117128

118129
return r
119130
}
131+
132+
func TotalPercentageDiff(current, base []coverage.Stats) float64 {
133+
curretStats := coverage.StatsCalcTotal(current)
134+
baseStats := coverage.StatsCalcTotal(base)
135+
136+
cp := curretStats.CoveredPercentageF()
137+
bp := baseStats.CoveredPercentageF()
138+
139+
return cp - bp
140+
}

0 commit comments

Comments
 (0)