Skip to content

Commit bcd9ec9

Browse files
committed
Fix reports.go
1 parent ca12154 commit bcd9ec9

File tree

1 file changed

+63
-4
lines changed

1 file changed

+63
-4
lines changed

tools/flakeguard/reports/reports.go

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"io"
8+
"math"
89
"os"
910
"path/filepath"
1011
"sort"
@@ -261,8 +262,66 @@ func PrintTests(
261262
}
262263
}
263264

264-
// Adjust column widths and print
265-
colWidths := make([]int, len(headers))
265+
var (
266+
passRatioStr string
267+
flakeRatioStr string
268+
)
269+
if runs == 0 || passes == runs {
270+
passRatioStr = "100%"
271+
flakeRatioStr = "0%"
272+
} else {
273+
passPercentage := float64(passes) / float64(runs) * 100
274+
truncatedPassPercentage := math.Floor(passPercentage*100) / 100 // Truncate to 2 decimal places
275+
flakePercentage := float64(flakyTests) / float64(len(tests)) * 100
276+
truncatedFlakePercentage := math.Floor(flakePercentage*100) / 100 // Truncate to 2 decimal places
277+
passRatioStr = fmt.Sprintf("%.2f%%", truncatedPassPercentage)
278+
flakeRatioStr = fmt.Sprintf("%.2f%%", truncatedFlakePercentage)
279+
}
280+
281+
// Print out summary data
282+
summaryData := [][]string{
283+
{"**Category**", "**Total**"},
284+
{"**Tests**", p.Sprint(len(tests))},
285+
{"**Panicked Tests**", p.Sprint(panickedTests)},
286+
{"**Raced Tests**", p.Sprint(racedTests)},
287+
{"**Flaky Tests**", p.Sprint(flakyTests)},
288+
{"**Flaky Test Ratio**", flakeRatioStr},
289+
{"**Runs**", p.Sprint(runs)},
290+
{"**Passes**", p.Sprint(passes)},
291+
{"**Failures**", p.Sprint(fails)},
292+
{"**Skips**", p.Sprint(skips)},
293+
{"**Pass Ratio**", passRatioStr},
294+
}
295+
colWidths := make([]int, len(summaryData[0]))
296+
297+
for _, row := range summaryData {
298+
for i, cell := range row {
299+
if len(cell) > colWidths[i] {
300+
colWidths[i] = len(cell)
301+
}
302+
}
303+
}
304+
305+
if len(rows) == 0 {
306+
fmt.Fprintf(w, "No tests found under pass ratio of %.2f%%\n", maxPassRatio*100)
307+
return
308+
}
309+
310+
printRow := func(cells []string) {
311+
fmt.Fprintf(w, "| %-*s | %-*s |\n", colWidths[0], cells[0], colWidths[1], cells[1])
312+
}
313+
printSeparator := func() {
314+
fmt.Fprintf(w, "|-%s-|-%s-|\n", strings.Repeat("-", colWidths[0]), strings.Repeat("-", colWidths[1]))
315+
}
316+
printRow(summaryData[0])
317+
printSeparator()
318+
for _, row := range summaryData[1:] {
319+
printRow(row)
320+
}
321+
fmt.Fprintln(w)
322+
323+
// Print out test data
324+
colWidths = make([]int, len(headers))
266325
for i, header := range headers {
267326
colWidths[i] = len(header)
268327
}
@@ -274,15 +333,15 @@ func PrintTests(
274333
}
275334
}
276335

277-
printRow := func(cells []string) {
336+
printRow = func(cells []string) {
278337
var buffer bytes.Buffer
279338
for i, cell := range cells {
280339
buffer.WriteString(fmt.Sprintf(" %-*s |", colWidths[i], cell))
281340
}
282341
fmt.Fprintln(w, "|"+buffer.String())
283342
}
284343

285-
printSeparator := func() {
344+
printSeparator = func() {
286345
var buffer bytes.Buffer
287346
for _, width := range colWidths {
288347
buffer.WriteString(" " + strings.Repeat("-", width) + " |")

0 commit comments

Comments
 (0)