Skip to content

Commit 8d1eb73

Browse files
committed
Cleans up printing
1 parent 9b2c859 commit 8d1eb73

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

tools/flakeguard/reports/reports.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func PrintTests(
243243
{"**Failures**", fmt.Sprint(fails)},
244244
{"**Skips**", fmt.Sprint(skips)},
245245
}
246-
colWidths := make([]int, len(rows[0]))
246+
colWidths := make([]int, len(summaryData[0]))
247247

248248
for _, row := range summaryData {
249249
for i, cell := range row {
@@ -253,6 +253,11 @@ func PrintTests(
253253
}
254254
}
255255

256+
if len(rows) == 0 {
257+
fmt.Fprintf(w, "No tests found under pass ratio of %.2f%%\n", maxPassRatio*100)
258+
return
259+
}
260+
256261
printRow := func(cells []string) {
257262
fmt.Fprintf(w, "| %-*s | %-*s |\n", colWidths[0], cells[0], colWidths[1], cells[1])
258263
}
@@ -295,12 +300,10 @@ func PrintTests(
295300
fmt.Fprintln(w, "|"+buffer.String())
296301
}
297302

298-
if len(rows) == 0 {
299-
printRow(headers)
300-
printSeparator()
301-
for _, row := range rows {
302-
printRow(row)
303-
}
303+
printRow(headers)
304+
printSeparator()
305+
for _, row := range rows {
306+
printRow(row)
304307
}
305308
return
306309
}

tools/flakeguard/reports/reports_test.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ func TestPrintTests(t *testing.T) {
7272
testcases := []struct {
7373
name string
7474
testResults []TestResult
75+
maxPassRatio float64
7576
expectedRuns int
7677
expectedPasses int
7778
expectedFails int
@@ -88,11 +89,14 @@ func TestPrintTests(t *testing.T) {
8889
TestName: "Test1",
8990
TestPackage: "package1",
9091
PassRatio: 0.75,
92+
Successes: 3,
93+
Failures: 1,
9194
Skipped: false,
9295
Runs: 4,
9396
Durations: []time.Duration{time.Millisecond * 1200, time.Millisecond * 900, time.Millisecond * 1100, time.Second},
9497
},
9598
},
99+
maxPassRatio: 1.0,
96100
expectedRuns: 4,
97101
expectedPasses: 3,
98102
expectedFails: 1,
@@ -102,6 +106,38 @@ func TestPrintTests(t *testing.T) {
102106
expectedFlakyTests: 1,
103107
expectedStringsContain: []string{"Test1", "package1", "75.00%", "false", "1.05s", "4", "0"},
104108
},
109+
{
110+
name: "multiple passing tests",
111+
testResults: []TestResult{
112+
{
113+
TestName: "Test1",
114+
TestPackage: "package1",
115+
PassRatio: 1.0,
116+
Skipped: false,
117+
Successes: 4,
118+
Runs: 4,
119+
Durations: []time.Duration{time.Millisecond * 1200, time.Millisecond * 900, time.Millisecond * 1100, time.Second},
120+
},
121+
{
122+
TestName: "Test2",
123+
TestPackage: "package1",
124+
PassRatio: 1.0,
125+
Skipped: false,
126+
Successes: 4,
127+
Runs: 4,
128+
Durations: []time.Duration{time.Millisecond * 1200, time.Millisecond * 900, time.Millisecond * 1100, time.Second},
129+
},
130+
},
131+
maxPassRatio: 1.0,
132+
expectedRuns: 8,
133+
expectedPasses: 8,
134+
expectedFails: 0,
135+
expectedSkippedTests: 0,
136+
expectedPanickedTests: 0,
137+
expectedRacedTests: 0,
138+
expectedFlakyTests: 0,
139+
expectedStringsContain: []string{},
140+
},
105141
}
106142

107143
for _, testCase := range testcases {
@@ -110,7 +146,7 @@ func TestPrintTests(t *testing.T) {
110146
t.Parallel()
111147
var buf bytes.Buffer
112148

113-
runs, passes, fails, skips, panickedTests, racedTests, flakyTests := PrintTests(&buf, tc.testResults, 1.0)
149+
runs, passes, fails, skips, panickedTests, racedTests, flakyTests := PrintTests(&buf, tc.testResults, tc.maxPassRatio)
114150
assert.Equal(t, tc.expectedRuns, runs, "wrong number of runs")
115151
assert.Equal(t, tc.expectedPasses, passes, "wrong number of passes")
116152
assert.Equal(t, tc.expectedFails, fails, "wrong number of failures")
@@ -121,8 +157,7 @@ func TestPrintTests(t *testing.T) {
121157

122158
// Get the output as a string
123159
output := buf.String()
124-
expectedContains := []string{"Test1", "package1", "75.00%", "false", "1.05s", "4", "0"}
125-
for _, expected := range expectedContains {
160+
for _, expected := range tc.expectedStringsContain {
126161
assert.Contains(t, output, expected, "output does not contain expected string")
127162
}
128163
})

0 commit comments

Comments
 (0)