Skip to content

Commit 5f5fc5e

Browse files
committed
Better printing
1 parent 82f7200 commit 5f5fc5e

File tree

3 files changed

+58
-48
lines changed

3 files changed

+58
-48
lines changed

tools/flakeguard/cmd/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ var RunTestsCmd = &cobra.Command{
7171
// Print all failed tests including flaky tests
7272
if printFailedTests {
7373
fmt.Printf("PassRatio threshold for flaky tests: %.2f\n", maxPassRatio)
74-
reports.PrintTests(os.Stdout, testReport.Results, maxPassRatio, false)
74+
reports.PrintResults(os.Stdout, testReport.Results, maxPassRatio, false)
7575
}
7676

7777
// Save the test results in JSON format

tools/flakeguard/reports/reports.go

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,13 @@ func Aggregate(reportsToAggregate ...*TestReport) (*TestReport, error) {
170170
return fullReport, nil
171171
}
172172

173-
// PrintTests prints tests in a pretty format
174-
func PrintTests(
175-
w io.Writer,
176-
tests []TestResult,
177-
maxPassRatio float64,
178-
includeCodeOwners bool, // Include code owners in the output. Set to true if test results have code owners
179-
) (runs, passes, fails, skips, panickedTests, racedTests, flakyTests int) {
173+
func TestResultsTable(
174+
results []TestResult,
175+
expectedPassRatio float64,
176+
includeCodeOwners bool,
177+
) (resultsTable [][]string, runs, passes, fails, skips, panickedTests, racedTests, flakyTests int) {
180178
p := message.NewPrinter(language.English) // For formatting numbers
181-
sortTestResults(tests)
179+
sortTestResults(results)
182180

183181
headers := []string{
184182
"**Name**",
@@ -199,55 +197,68 @@ func PrintTests(
199197
headers = append(headers, "**Code Owners**")
200198
}
201199

202-
// Build test rows and summary data
203-
rows := [][]string{}
204-
for _, test := range tests {
205-
if test.PassRatio < maxPassRatio {
200+
resultsTable = [][]string{}
201+
resultsTable = append(resultsTable, headers)
202+
for _, result := range results {
203+
if result.PassRatio < expectedPassRatio {
206204
row := []string{
207-
test.TestName,
208-
fmt.Sprintf("%.2f%%", test.PassRatio*100),
209-
fmt.Sprintf("%t", test.Panic),
210-
fmt.Sprintf("%t", test.Timeout),
211-
fmt.Sprintf("%t", test.Race),
212-
p.Sprintf("%d", test.Runs),
213-
p.Sprintf("%d", test.Successes),
214-
p.Sprintf("%d", test.Failures),
215-
p.Sprintf("%d", test.Skips),
216-
test.TestPackage,
217-
fmt.Sprintf("%t", test.PackagePanic),
218-
avgDuration(test.Durations).String(),
205+
result.TestName,
206+
fmt.Sprintf("%.2f%%", result.PassRatio*100),
207+
fmt.Sprintf("%t", result.Panic),
208+
fmt.Sprintf("%t", result.Timeout),
209+
fmt.Sprintf("%t", result.Race),
210+
p.Sprintf("%d", result.Runs),
211+
p.Sprintf("%d", result.Successes),
212+
p.Sprintf("%d", result.Failures),
213+
p.Sprintf("%d", result.Skips),
214+
result.TestPackage,
215+
fmt.Sprintf("%t", result.PackagePanic),
216+
avgDuration(result.Durations).String(),
219217
}
220218

221219
if includeCodeOwners {
222220
owners := "Unknown"
223-
if len(test.CodeOwners) > 0 {
224-
owners = strings.Join(test.CodeOwners, ", ")
221+
if len(result.CodeOwners) > 0 {
222+
owners = strings.Join(result.CodeOwners, ", ")
225223
}
226224
row = append(row, owners)
227225
}
228226

229-
rows = append(rows, row)
227+
resultsTable = append(resultsTable, row)
230228
}
231229

232-
runs += test.Runs
233-
passes += test.Successes
234-
fails += test.Failures
235-
skips += test.Skips
236-
if test.Panic {
230+
runs += result.Runs
231+
passes += result.Successes
232+
fails += result.Failures
233+
skips += result.Skips
234+
if result.Panic {
237235
panickedTests++
238236
flakyTests++
239-
} else if test.Race {
237+
} else if result.Race {
240238
racedTests++
241239
flakyTests++
242-
} else if test.PassRatio < maxPassRatio {
240+
} else if result.PassRatio < expectedPassRatio {
243241
flakyTests++
244242
}
245243
}
244+
return
245+
}
246246

247+
// PrintTests prints tests in a pretty format
248+
func PrintResults(
249+
w io.Writer,
250+
tests []TestResult,
251+
maxPassRatio float64,
252+
includeCodeOwners bool, // Include code owners in the output. Set to true if test results have code owners
253+
) (runs, passes, fails, skips, panickedTests, racedTests, flakyTests int) {
247254
var (
255+
resultsTable [][]string
248256
passRatioStr string
249257
flakeRatioStr string
258+
p = message.NewPrinter(language.English) // For formatting numbers
250259
)
260+
resultsTable, runs, passes, fails, skips, panickedTests, racedTests, flakyTests = TestResultsTable(tests, maxPassRatio, includeCodeOwners)
261+
// Print out summary data
251262
if runs == 0 || passes == runs {
252263
passRatioStr = "100%"
253264
flakeRatioStr = "0%"
@@ -259,8 +270,6 @@ func PrintTests(
259270
passRatioStr = fmt.Sprintf("%.2f%%", truncatedPassPercentage)
260271
flakeRatioStr = fmt.Sprintf("%.2f%%", truncatedFlakePercentage)
261272
}
262-
263-
// Print out summary data
264273
summaryData := [][]string{
265274
{"**Category**", "**Total**"},
266275
{"**Tests**", p.Sprint(len(tests))},
@@ -274,6 +283,7 @@ func PrintTests(
274283
{"**Skips**", p.Sprint(skips)},
275284
{"**Pass Ratio**", passRatioStr},
276285
}
286+
277287
colWidths := make([]int, len(summaryData[0]))
278288

279289
for _, row := range summaryData {
@@ -283,8 +293,7 @@ func PrintTests(
283293
}
284294
}
285295
}
286-
287-
if len(rows) == 0 {
296+
if len(resultsTable) <= 1 {
288297
fmt.Fprintf(w, "No tests found under pass ratio of %.2f%%\n", maxPassRatio*100)
289298
return
290299
}
@@ -303,12 +312,13 @@ func PrintTests(
303312
fmt.Fprintln(w)
304313

305314
// Print out test data
306-
colWidths = make([]int, len(headers))
307-
for i, header := range headers {
315+
resultsHeaders := resultsTable[0]
316+
colWidths = make([]int, len(resultsHeaders))
317+
for i, header := range resultsHeaders {
308318
colWidths[i] = len(header)
309319
}
310-
for _, row := range rows {
311-
for i, cell := range row {
320+
for rowNum := 1; rowNum < len(resultsTable); rowNum++ {
321+
for i, cell := range resultsTable[rowNum] {
312322
if len(cell) > colWidths[i] {
313323
colWidths[i] = len(cell)
314324
}
@@ -331,10 +341,10 @@ func PrintTests(
331341
fmt.Fprintln(w, "|"+buffer.String())
332342
}
333343

334-
printRow(headers)
344+
printRow(resultsHeaders)
335345
printSeparator()
336-
for _, row := range rows {
337-
printRow(row)
346+
for rowNum := 1; rowNum < len(resultsTable); rowNum++ {
347+
printRow(resultsTable[rowNum])
338348
}
339349
return
340350
}
@@ -391,7 +401,7 @@ func MarkdownSummary(w io.Writer, testReport *TestReport, maxPassRatio float64,
391401
return
392402
}
393403

394-
allRuns, passes, _, _, _, _, _ := PrintTests(testsData, tests, maxPassRatio, includeCodeOwners)
404+
allRuns, passes, _, _, _, _, _ := PrintResults(testsData, tests, maxPassRatio, includeCodeOwners)
395405
if allRuns > 0 {
396406
avgPassRatio = float64(passes) / float64(allRuns)
397407
}

tools/flakeguard/reports/reports_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func TestPrintTests(t *testing.T) {
142142
t.Parallel()
143143
var buf bytes.Buffer
144144

145-
runs, passes, fails, skips, panickedTests, racedTests, flakyTests := PrintTests(&buf, tc.testResults, tc.maxPassRatio, false)
145+
runs, passes, fails, skips, panickedTests, racedTests, flakyTests := PrintResults(&buf, tc.testResults, tc.maxPassRatio, false)
146146
assert.Equal(t, tc.expectedRuns, runs, "wrong number of runs")
147147
assert.Equal(t, tc.expectedPasses, passes, "wrong number of passes")
148148
assert.Equal(t, tc.expectedFails, fails, "wrong number of failures")

0 commit comments

Comments
 (0)