Skip to content

Commit 9324521

Browse files
committed
more test
1 parent 1ed0dba commit 9324521

File tree

3 files changed

+129
-8
lines changed

3 files changed

+129
-8
lines changed

pkg/testcoverage/check_test.go

Lines changed: 101 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -265,16 +265,109 @@ func TestCheck(t *testing.T) {
265265
assert.Error(t, err)
266266
assert.Contains(t, err.Error(), "failed to load base coverage breakdown")
267267
})
268+
}
268269

269-
t.Run("valid profile - diff pass", func(t *testing.T) {
270-
t.Parallel()
271-
// add test
272-
})
270+
func TestCheckDiff(t *testing.T) {
271+
t.Parallel()
273272

274-
t.Run("valid profile - diff fail", func(t *testing.T) {
275-
t.Parallel()
276-
// add test
277-
})
273+
if testing.Short() {
274+
return
275+
}
276+
277+
brakedownFile := t.TempDir() + "/breakdown.testcoverage"
278+
brakedownCurrentFile := t.TempDir() + "/breakdown-current.testcoverage"
279+
brakedownFileEdited := "breakdown-edit.testcoverage"
280+
281+
// run check to generate brakedown file
282+
cfg := Config{
283+
Profile: profileOK,
284+
BreakdownFileName: brakedownFile,
285+
SourceDir: sourceDir,
286+
}
287+
buf := &bytes.Buffer{}
288+
pass, err := Check(buf, cfg)
289+
assert.True(t, pass)
290+
assert.NoError(t, err)
291+
292+
// should pass since brakedown is the same
293+
cfg = Config{
294+
Profile: profileOK,
295+
SourceDir: sourceDir,
296+
Diff: Diff{
297+
BaseBreakdownFileName: brakedownFile,
298+
Threshold: ptr(0.0),
299+
},
300+
}
301+
buf = &bytes.Buffer{}
302+
pass, err = Check(buf, cfg)
303+
assert.True(t, pass)
304+
assert.NoError(t, err)
305+
assertDiffNoChange(t, buf.String())
306+
assertDiffPercentage(t, buf.String(), 0.0)
307+
assertDiffThreshold(t, buf.String(), *cfg.Diff.Threshold, true)
308+
309+
// should pass since diff is negative
310+
cfg = Config{
311+
Profile: profileOK,
312+
SourceDir: sourceDir,
313+
Diff: Diff{
314+
BaseBreakdownFileName: brakedownFile,
315+
Threshold: ptr(-0.001),
316+
},
317+
}
318+
buf = &bytes.Buffer{}
319+
pass, err = Check(buf, cfg)
320+
assert.True(t, pass)
321+
assert.NoError(t, err)
322+
assertDiffNoChange(t, buf.String())
323+
assertDiffPercentage(t, buf.String(), 0.0)
324+
assertDiffThreshold(t, buf.String(), *cfg.Diff.Threshold, true)
325+
326+
// should NOT pass since brakedown is the same, and diff is positive
327+
cfg = Config{
328+
Profile: profileOK,
329+
SourceDir: sourceDir,
330+
Diff: Diff{
331+
BaseBreakdownFileName: brakedownFile,
332+
Threshold: ptr(0.1),
333+
},
334+
}
335+
buf = &bytes.Buffer{}
336+
pass, err = Check(buf, cfg)
337+
assert.False(t, pass)
338+
assert.NoError(t, err)
339+
assertDiffNoChange(t, buf.String())
340+
assertDiffPercentage(t, buf.String(), 0.0)
341+
assertDiffThreshold(t, buf.String(), *cfg.Diff.Threshold, false)
342+
343+
// change brakedown file to have positive difference
344+
base := readStats(t, brakedownFile)
345+
base[0].Covered = 0
346+
base[1].Covered = 0
347+
348+
tmpFile, err := os.CreateTemp(t.TempDir(), brakedownFileEdited)
349+
assert.NoError(t, err)
350+
_, err = tmpFile.Write(coverage.StatsSerialize(base))
351+
assert.NoError(t, err)
352+
353+
// check should now pass since difference has increased
354+
cfg = Config{
355+
Profile: profileOK,
356+
SourceDir: sourceDir,
357+
BreakdownFileName: brakedownCurrentFile,
358+
Diff: Diff{
359+
BaseBreakdownFileName: tmpFile.Name(),
360+
Threshold: ptr(1.0),
361+
},
362+
}
363+
buf = &bytes.Buffer{}
364+
pass, err = Check(buf, cfg)
365+
assert.True(t, pass)
366+
assert.NoError(t, err)
367+
368+
diff := TotalPercentageDiff(readStats(t, brakedownCurrentFile), base)
369+
assertDiffPercentage(t, buf.String(), diff)
370+
assertDiffThreshold(t, buf.String(), *cfg.Diff.Threshold, true)
278371
}
279372

280373
//nolint:paralleltest // must not be parallel because it uses env

pkg/testcoverage/export_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var (
1717
LoadBaseCoverageBreakdown = loadBaseCoverageBreakdown
1818
CompressUncoveredLines = compressUncoveredLines
1919
ReportUncoveredLines = reportUncoveredLines
20+
StatusStr = statusStr
2021
)
2122

2223
type (

pkg/testcoverage/helpers_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,21 @@ func assertDiffChange(t *testing.T, content string, lines int) {
208208
assert.Contains(t, content, str)
209209
}
210210

211+
func assertDiffThreshold(t *testing.T, content string, thr float64, isSatisfied bool) {
212+
t.Helper()
213+
214+
//nolint:lll //relax
215+
str := fmt.Sprintf("Coverage difference threshold (%.2f%%) satisfied:\t %s", thr, StatusStr(isSatisfied))
216+
assert.Contains(t, content, str)
217+
}
218+
219+
func assertDiffPercentage(t *testing.T, content string, p float64) {
220+
t.Helper()
221+
222+
str := fmt.Sprintf("Coverage difference: %.2f%%", p)
223+
assert.Contains(t, content, str)
224+
}
225+
211226
func assertGithubActionErrorsCount(t *testing.T, content string, count int) {
212227
t.Helper()
213228

@@ -264,3 +279,15 @@ func assertGithubOutputValues(t *testing.T, file string) {
264279
assertNonEmptyValue(t, content, GaOutputBadgeText)
265280
assertNonEmptyValue(t, content, GaOutputReport)
266281
}
282+
283+
func readStats(t *testing.T, file string) []coverage.Stats {
284+
t.Helper()
285+
286+
contentBytes, err := os.ReadFile(file)
287+
assert.NoError(t, err)
288+
assert.NotEmpty(t, contentBytes)
289+
stats, err := coverage.StatsDeserialize(contentBytes)
290+
assert.NoError(t, err)
291+
292+
return stats
293+
}

0 commit comments

Comments
 (0)