Skip to content

Commit adbaa9f

Browse files
committed
Prettier printing
1 parent 69e3904 commit adbaa9f

File tree

5 files changed

+80
-30
lines changed

5 files changed

+80
-30
lines changed

tools/flakeguard/.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
tmp_test_flaky_state
22
test_results_*.json
3-
debug_outputs/
3+
debug_outputs/
4+
example_results.json
5+
example_results.md

tools/flakeguard/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ test:
77
go install github.com/gotesttools/gotestfmt/v2/cmd/gotestfmt@latest
88
set -euo pipefail
99
go list ./... | grep -v 'example_test_package' | xargs go test -json -cover -coverprofile unit-test-coverage.out -v 2>&1 | tee /tmp/gotest.log | gotestfmt
10+
11+
.PHONY: example
12+
example:
13+
go run . run --project-path=./runner --test-packages=./example_test_package --run-count=5 --skip-tests=TestPanic --max-pass-ratio=1 --race=false --output-json=example_results.json

tools/flakeguard/cmd/run.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,15 @@ var RunTestsCmd = &cobra.Command{
5858
os.Exit(1)
5959
}
6060

61-
passedTests := reports.FilterPassedTests(testReport.Results, maxPassRatio)
62-
failedTests := reports.FilterFailedTests(testReport.Results, maxPassRatio)
63-
skippedTests := reports.FilterSkippedTests(testReport.Results)
64-
flakyTests := reports.FilterFlakyTests(testReport.Results, maxPassRatio)
65-
6661
// Print all failed tests including flaky tests
67-
if len(failedTests) > 0 && printFailedTests {
68-
fmt.Printf("Maximum threshold for flaky tests: %.2f\n", maxPassRatio)
62+
if printFailedTests {
6963
fmt.Printf("PassRatio threshold for flaky tests: %.2f\n", maxPassRatio)
70-
fmt.Printf("%d failed tests:\n", len(failedTests))
71-
reports.PrintTests(os.Stdout, failedTests, 0.0)
64+
reports.PrintTests(os.Stdout, testReport.Results, maxPassRatio)
7265
}
7366

74-
fmt.Printf("Summary: %d passed, %d skipped, %d failed, %d flaky\n", len(passedTests), len(skippedTests), len(failedTests), len(flakyTests))
75-
7667
// Save the test results in JSON format
7768
if outputPath != "" && len(testReport.Results) > 0 {
78-
jsonData, err := json.MarshalIndent(testReport.Results, "", " ")
69+
jsonData, err := json.MarshalIndent(testReport, "", " ")
7970
if err != nil {
8071
log.Fatalf("Error marshaling test results to JSON: %v", err)
8172
}
@@ -85,6 +76,7 @@ var RunTestsCmd = &cobra.Command{
8576
fmt.Printf("All test results saved to %s\n", outputPath)
8677
}
8778

79+
flakyTests := reports.FilterFlakyTests(testReport.Results, maxPassRatio)
8880
if len(flakyTests) > 0 {
8981
// Exit with error code if there are flaky tests
9082
os.Exit(1)

tools/flakeguard/reports/reports.go

Lines changed: 67 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,29 @@ func AggregateTestResults(folderPath string) (*TestReport, error) {
179179

180180
// PrintTests prints tests in a pretty format
181181
func PrintTests(w io.Writer, tests []TestResult, maxPassRatio float64) (allRuns, passes, fails, skips, races, panics, flakes int) {
182-
fmt.Fprintln(w, "| Test Name | Test Package | Pass Ratio | Skipped | Runs | Successes | Failures | Panics | Races | Skips | Avg Duration |")
183-
fmt.Fprintln(w, "|-----------|--------------|------------|---------|------|-----------|----------|--------|-------|-------|--------------|")
182+
headers := []string{
183+
"Test Name", "Test Package", "Pass Ratio", "Skipped", "Runs", "Successes", "Failures", "Panics", "Races", "Skips", "Avg Duration",
184+
}
185+
rows := [][]string{}
186+
184187
for _, test := range tests {
185188
if test.PassRatio >= maxPassRatio {
186189
continue
187190
}
188-
fmt.Fprintf(w, "| %s | %s | %.2f%% | %v | %d | %d | %d | %d | %d | %d | %s |\n",
189-
test.TestName, test.TestPackage, test.PassRatio*100, test.Skipped, test.Runs, test.Successes, test.Failures, test.Panics, test.Races, test.Skips, avgDuration(test.Durations).String())
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+
})
204+
190205
allRuns += test.Runs
191206
passes += test.Successes
192207
fails += test.Failures
@@ -195,11 +210,55 @@ func PrintTests(w io.Writer, tests []TestResult, maxPassRatio float64) (allRuns,
195210
panics += test.Panics
196211
flakes += fails + races + panics
197212
}
213+
214+
// Determine column widths
215+
colWidths := make([]int, len(headers))
216+
for i, header := range headers {
217+
colWidths[i] = len(header)
218+
}
219+
for _, row := range rows {
220+
for i, cell := range row {
221+
if len(cell) > colWidths[i] {
222+
colWidths[i] = len(cell)
223+
}
224+
}
225+
}
226+
227+
// Helper function to print a row
228+
printRow := func(cells []string) {
229+
var buffer bytes.Buffer
230+
for i, cell := range cells {
231+
buffer.WriteString(fmt.Sprintf(" %-*s |", colWidths[i], cell))
232+
}
233+
fmt.Fprintln(w, "|"+buffer.String())
234+
}
235+
236+
// Print header separator
237+
printSeparator := func() {
238+
var buffer bytes.Buffer
239+
for _, width := range colWidths {
240+
buffer.WriteString(" " + strings.Repeat("-", width) + " |")
241+
}
242+
fmt.Fprintln(w, "|"+buffer.String())
243+
}
244+
245+
// Print table
246+
printSeparator()
247+
printRow(headers)
248+
printSeparator()
249+
for _, row := range rows {
250+
printRow(row)
251+
}
252+
printSeparator()
253+
254+
// Print totals
255+
fmt.Fprintf(w, "\nSummary:\n")
256+
fmt.Fprintf(w, "Total Runs: %d, Passes: %d, Failures: %d, Panics: %d, Races: %d, Skips: %d\n", allRuns, passes, fails, panics, races, skips)
198257
return
199258
}
200259

201-
// TestsSummary builds a summary of test results in markdown format, handy for reporting in CI and Slack
202-
func TestsSummary(w io.Writer, testReport *TestReport, maxPassRatio float64) {
260+
// MarkdownSummary builds a summary of test results in markdown format, handy for reporting in CI and Slack
261+
func MarkdownSummary(w io.Writer, testReport *TestReport, maxPassRatio float64) {
203262
tests := testReport.Results
204263
fmt.Fprintln(w, "# Flakeguard Summary")
205264
fmt.Fprintln(w, "| **Setting** | **Value** |")
@@ -224,7 +283,7 @@ func TestsSummary(w io.Writer, testReport *TestReport, maxPassRatio float64) {
224283
test.Failures, test.Panics, test.Races, test.Skips, avgDuration(test.Durations).String(),
225284
)
226285
}
227-
allRuns, passes, fails, skips, races, panics, flakes := PrintTests(testsData, tests, maxPassRatio)
286+
allRuns, passes, _, _, _, _, flakes := PrintTests(testsData, tests, maxPassRatio)
228287
if allRuns > 0 {
229288
avgPassRatio = float64(passes) / float64(allRuns)
230289
}
@@ -235,18 +294,9 @@ func TestsSummary(w io.Writer, testReport *TestReport, maxPassRatio float64) {
235294
}
236295
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)
237296
fmt.Fprintf(w, "### Results")
238-
separator := "|----------------|------------------|--------------------|------------------|-----------------|-----------------|"
239-
fmt.Fprintln(w, "| **Total Runs** | **Total Passes** | **Total Failures** | **Total Panics** | **Total Races** | **Total Skips** |")
240-
fmt.Fprintln(w, separator)
241-
fmt.Fprintf(w, "| %d | %d | %d | %d | %d | %d |\n", allRuns, passes, fails, panics, races, skips)
242-
fmt.Fprintln(w, separator)
243297
if avgPassRatio < maxPassRatio {
244298
fmt.Fprintln(w, "### Flakes")
245-
separator = "|---------------|------------------|----------------|-------------|----------|---------------|--------------|------------|-----------|-----------|------------------|"
246-
fmt.Fprintln(w, "| **Test Name** | **Test Package** | **Pass Ratio** | **Skipped** | **Runs** | **Successes** | **Failures** | **Panics** | **Races** | **Skips** | **Avg Duration** |")
247-
fmt.Fprintln(w, separator)
248299
fmt.Fprint(w, testsData.String())
249-
fmt.Fprintln(w, separator)
250300
}
251301
}
252302

@@ -266,7 +316,7 @@ func SaveFilteredResultsAndLogs(outputResultsPath, outputLogsPath string, report
266316
return fmt.Errorf("error opening markdown file: %w", err)
267317
}
268318
defer summaryFile.Close()
269-
TestsSummary(summaryFile, report, 1.0)
319+
MarkdownSummary(summaryFile, report, 1.0)
270320
fmt.Printf("Test results saved to %s and summary to %s\n", jsonFileName, mdFileName)
271321
} else {
272322
fmt.Println("No failed tests found based on the specified threshold and min pass ratio.")

tools/flakeguard/runner/runner.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,8 @@ func parseTestResults(filePaths []string) ([]reports.TestResult, error) {
320320
result.Runs = result.Successes + result.Failures + result.Panics + result.Races
321321
if result.Runs > 0 {
322322
result.PassRatio = float64(result.Successes) / float64(result.Runs)
323+
} else {
324+
result.PassRatio = 1
323325
}
324326
}
325327
}

0 commit comments

Comments
 (0)