Skip to content

Commit 52e254a

Browse files
committed
Cleanup
1 parent a4751e5 commit 52e254a

File tree

1 file changed

+58
-40
lines changed

1 file changed

+58
-40
lines changed

tools/flakeguard/reports/reports.go

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -185,22 +185,21 @@ func PrintTests(w io.Writer, tests []TestResult, maxPassRatio float64) (allRuns,
185185
rows := [][]string{}
186186

187187
for _, test := range tests {
188-
if test.PassRatio >= maxPassRatio {
189-
continue
188+
if test.PassRatio < maxPassRatio {
189+
rows = append(rows, []string{
190+
test.TestName,
191+
test.TestPackage,
192+
fmt.Sprintf("%.2f%%", test.PassRatio*100),
193+
fmt.Sprintf("%v", test.Skipped),
194+
fmt.Sprintf("%d", test.Runs),
195+
fmt.Sprintf("%d", test.Successes),
196+
fmt.Sprintf("%d", test.Failures),
197+
fmt.Sprintf("%d", test.Panics),
198+
fmt.Sprintf("%d", test.Races),
199+
fmt.Sprintf("%d", test.Skips),
200+
avgDuration(test.Durations).String(),
201+
})
190202
}
191-
rows = append(rows, []string{
192-
test.TestName,
193-
test.TestPackage,
194-
fmt.Sprintf("%.2f%%", test.PassRatio*100),
195-
fmt.Sprintf("%v", test.Skipped),
196-
fmt.Sprintf("%d", test.Runs),
197-
fmt.Sprintf("%d", test.Successes),
198-
fmt.Sprintf("%d", test.Failures),
199-
fmt.Sprintf("%d", test.Panics),
200-
fmt.Sprintf("%d", test.Races),
201-
fmt.Sprintf("%d", test.Skips),
202-
avgDuration(test.Durations).String(),
203-
})
204203

205204
allRuns += test.Runs
206205
passes += test.Successes
@@ -211,7 +210,7 @@ func PrintTests(w io.Writer, tests []TestResult, maxPassRatio float64) (allRuns,
211210
flakes += fails + races + panics
212211
}
213212

214-
// Determine column widths
213+
// Determine column widths for clean printing
215214
colWidths := make([]int, len(headers))
216215
for i, header := range headers {
217216
colWidths[i] = len(header)
@@ -224,7 +223,6 @@ func PrintTests(w io.Writer, tests []TestResult, maxPassRatio float64) (allRuns,
224223
}
225224
}
226225

227-
// Helper function to print a row
228226
printRow := func(cells []string) {
229227
var buffer bytes.Buffer
230228
for i, cell := range cells {
@@ -233,7 +231,6 @@ func PrintTests(w io.Writer, tests []TestResult, maxPassRatio float64) (allRuns,
233231
fmt.Fprintln(w, "|"+buffer.String())
234232
}
235233

236-
// Print header separator
237234
printSeparator := func() {
238235
var buffer bytes.Buffer
239236
for _, width := range colWidths {
@@ -259,30 +256,52 @@ func PrintTests(w io.Writer, tests []TestResult, maxPassRatio float64) (allRuns,
259256

260257
// MarkdownSummary builds a summary of test results in markdown format, handy for reporting in CI and Slack
261258
func MarkdownSummary(w io.Writer, testReport *TestReport, maxPassRatio float64) {
262-
tests := testReport.Results
263-
fmt.Fprintln(w, "# Flakeguard Summary")
264-
fmt.Fprintln(w, "| **Setting** | **Value** |")
265-
fmt.Fprintln(w, "|-------------|-----------|")
266-
fmt.Fprintf(w, "| Go Project | %s |\n", testReport.GoProject)
267-
fmt.Fprintf(w, "| Max Pass Ratio | %.2f%% |\n", maxPassRatio*100)
268-
fmt.Fprintf(w, "| Test Run Count | %d |\n", testReport.TestRunCount)
269-
fmt.Fprintf(w, "| Race Detection | %t |\n", testReport.RaceDetection)
270-
fmt.Fprintf(w, "| Excluded Tests | %s |\n", strings.Join(testReport.ExcludedTests, ", "))
271-
fmt.Fprintln(w, "|-------------|-----------|")
272-
if len(tests) == 0 {
273-
fmt.Fprintln(w, "## No tests ran :warning:")
274-
return
275-
}
276259
var (
277260
avgPassRatio = 1.0
278261
testsData = bytes.NewBuffer(nil)
262+
tests = testReport.Results
279263
)
280-
for _, test := range tests {
281-
fmt.Fprintf(testsData, "| %s | %s | %.2f%% | %v | %d | %d | %d | %d | %d | %d | %s |\n",
282-
test.TestName, test.TestPackage, test.PassRatio*100, test.Skipped, test.Runs, test.Successes,
283-
test.Failures, test.Panics, test.Races, test.Skips, avgDuration(test.Durations).String(),
284-
)
264+
265+
rows := [][]string{
266+
{"**Setting**", "**Value**"},
267+
{"Go Project", testReport.GoProject},
268+
{"Max Pass Ratio", fmt.Sprintf("%.2f%%", maxPassRatio*100)},
269+
{"Test Run Count", fmt.Sprintf("%d", testReport.TestRunCount)},
270+
{"Race Detection", fmt.Sprintf("%t", testReport.RaceDetection)},
271+
{"Excluded Tests", strings.Join(testReport.ExcludedTests, ", ")},
285272
}
273+
colWidths := make([]int, len(rows[0]))
274+
275+
// Calculate column widths
276+
for _, row := range rows {
277+
for i, cell := range row {
278+
if len(cell) > colWidths[i] {
279+
colWidths[i] = len(cell)
280+
}
281+
}
282+
}
283+
284+
printRow := func(cells []string) {
285+
fmt.Fprintf(w, "| %-*s | %-*s |\n", colWidths[0], cells[0], colWidths[1], cells[1])
286+
}
287+
printSeparator := func() {
288+
fmt.Fprintf(w, "|-%s-|-%s-|\n", strings.Repeat("-", colWidths[0]), strings.Repeat("-", colWidths[1]))
289+
}
290+
fmt.Fprint(w, "# Flakeguard Summary\n\n")
291+
// Print settings data
292+
printRow(rows[0])
293+
printSeparator()
294+
for _, row := range rows[1:] {
295+
printRow(row)
296+
}
297+
printSeparator()
298+
fmt.Fprintln(w)
299+
300+
if len(tests) == 0 {
301+
fmt.Fprintln(w, "## No tests ran :warning:")
302+
return
303+
}
304+
286305
allRuns, passes, _, _, _, _, flakes := PrintTests(testsData, tests, maxPassRatio)
287306
if allRuns > 0 {
288307
avgPassRatio = float64(passes) / float64(allRuns)
@@ -292,10 +311,9 @@ func MarkdownSummary(w io.Writer, testReport *TestReport, maxPassRatio float64)
292311
} else {
293312
fmt.Fprintln(w, "## No Flakes Found :white_check_mark:")
294313
}
295-
fmt.Fprintf(w, "Ran `%d` tests `%d` times with a `%.2f%%` pass ratio and found `%d` flaky tests\n", len(tests), allRuns, avgPassRatio*100, flakes)
296-
fmt.Fprintf(w, "### Results")
314+
fmt.Fprintf(w, "Ran `%d` tests `%d` times with a `%.2f%%` pass ratio and found `%d` flaky tests\n\n", len(tests), allRuns, avgPassRatio*100, flakes)
297315
if avgPassRatio < maxPassRatio {
298-
fmt.Fprintln(w, "### Flakes")
316+
fmt.Fprint(w, "### Flakes\n\n")
299317
fmt.Fprint(w, testsData.String())
300318
}
301319
}

0 commit comments

Comments
 (0)