Skip to content

Commit 5cfb711

Browse files
committed
Merge remote-tracking branch 'origin/main' into tt-1923-retry-gas-estimation
2 parents 10f1961 + d9a9a59 commit 5cfb711

File tree

3 files changed

+109
-47
lines changed

3 files changed

+109
-47
lines changed

tools/flakeguard/reports/data.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package reports
22

33
import (
44
"fmt"
5-
"math"
65
"sort"
76
"strings"
87
"time"
@@ -121,6 +120,7 @@ type SplunkTestResultEvent struct {
121120

122121
func GenerateSummaryData(tests []TestResult, maxPassRatio float64) SummaryData {
123122
var runs, passes, fails, skips, panickedTests, racedTests, flakyTests, skippedTests int
123+
124124
for _, result := range tests {
125125
runs += result.Runs
126126
passes += result.Successes
@@ -143,30 +143,47 @@ func GenerateSummaryData(tests []TestResult, maxPassRatio float64) SummaryData {
143143
}
144144
}
145145

146+
// Calculate the raw pass ratio
146147
passPercentage := 100.0
147-
flakePercentage := 0.0
148-
149148
if runs > 0 {
150-
passPercentage = math.Floor((float64(passes)/float64(runs)*100)*100) / 100 // Truncate to 2 decimal places
149+
passPercentage = (float64(passes) / float64(runs)) * 100
151150
}
152151

152+
// Calculate the raw flake ratio
153153
totalTests := len(tests)
154+
flakePercentage := 0.0
154155
if totalTests > 0 {
155-
flakePercentage = math.Floor((float64(flakyTests)/float64(totalTests)*100)*100) / 100 // Truncate to 2 decimal places
156+
flakePercentage = (float64(flakyTests) / float64(totalTests)) * 100
156157
}
157158

159+
// Helper function to convert a float ratio into a trimmed string
160+
formatRatio := func(val float64) string {
161+
// Format with 4 decimal places
162+
s := fmt.Sprintf("%.4f", val)
163+
// Trim trailing zeros
164+
s = strings.TrimRight(s, "0")
165+
// Trim trailing '.' if needed (in case we have an integer)
166+
s = strings.TrimRight(s, ".")
167+
return s + "%"
168+
}
169+
170+
passRatioStr := formatRatio(passPercentage)
171+
flakeTestRatioStr := formatRatio(flakePercentage)
172+
158173
return SummaryData{
159174
TotalTests: totalTests,
160175
PanickedTests: panickedTests,
161176
RacedTests: racedTests,
162177
FlakyTests: flakyTests,
163-
FlakyTestRatio: fmt.Sprintf("%.2f%%", flakePercentage),
164-
TotalRuns: runs,
165-
PassedRuns: passes,
166-
FailedRuns: fails,
167-
SkippedRuns: skips,
168-
PassRatio: fmt.Sprintf("%.2f%%", passPercentage),
169-
MaxPassRatio: maxPassRatio,
178+
FlakyTestRatio: flakeTestRatioStr,
179+
180+
TotalRuns: runs,
181+
PassedRuns: passes,
182+
FailedRuns: fails,
183+
SkippedRuns: skips,
184+
185+
PassRatio: passRatioStr,
186+
MaxPassRatio: maxPassRatio,
170187
}
171188
}
172189

tools/flakeguard/reports/data_test.go

Lines changed: 77 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ func TestGenerateSummaryData(t *testing.T) {
2828
PanickedTests: 0,
2929
RacedTests: 0,
3030
FlakyTests: 0,
31-
FlakyTestRatio: "0.00%",
31+
FlakyTestRatio: "0%", // no flaky tests
3232
TotalRuns: 15,
3333
PassedRuns: 15,
3434
FailedRuns: 0,
3535
SkippedRuns: 0,
36-
PassRatio: "100.00%",
36+
PassRatio: "100%",
3737
MaxPassRatio: 1.0,
3838
},
3939
},
@@ -46,17 +46,19 @@ func TestGenerateSummaryData(t *testing.T) {
4646
},
4747
maxPassRatio: 0.9,
4848
expected: SummaryData{
49-
TotalTests: 3,
50-
PanickedTests: 0,
51-
RacedTests: 0,
52-
FlakyTests: 2,
53-
FlakyTestRatio: "66.66%",
49+
TotalTests: 3,
50+
PanickedTests: 0,
51+
RacedTests: 0,
52+
FlakyTests: 2,
53+
// 2/3 => 66.666...%
54+
FlakyTestRatio: "66.6667%",
5455
TotalRuns: 19,
5556
PassedRuns: 15,
56-
FailedRuns: 4,
57+
FailedRuns: 4, // total failures
5758
SkippedRuns: 0,
58-
PassRatio: "78.94%",
59-
MaxPassRatio: 0.9,
59+
// 15/19 => ~78.947...
60+
PassRatio: "78.9474%",
61+
MaxPassRatio: 0.9,
6062
},
6163
},
6264
{
@@ -68,17 +70,19 @@ func TestGenerateSummaryData(t *testing.T) {
6870
},
6971
maxPassRatio: 1.0,
7072
expected: SummaryData{
71-
TotalTests: 3,
72-
PanickedTests: 1,
73-
RacedTests: 1,
74-
FlakyTests: 2,
75-
FlakyTestRatio: "66.66%",
73+
TotalTests: 3,
74+
PanickedTests: 1,
75+
RacedTests: 1,
76+
FlakyTests: 2,
77+
// 2/3 => ~66.666...
78+
FlakyTestRatio: "66.6667%",
7679
TotalRuns: 18,
7780
PassedRuns: 17,
7881
FailedRuns: 1,
7982
SkippedRuns: 0,
80-
PassRatio: "94.44%",
81-
MaxPassRatio: 1.0,
83+
// 17/18 => ~94.444...
84+
PassRatio: "94.4444%",
85+
MaxPassRatio: 1.0,
8286
},
8387
},
8488
{
@@ -90,13 +94,14 @@ func TestGenerateSummaryData(t *testing.T) {
9094
PanickedTests: 0,
9195
RacedTests: 0,
9296
FlakyTests: 0,
93-
FlakyTestRatio: "0.00%",
97+
FlakyTestRatio: "0%", // no tests => 0%
9498
TotalRuns: 0,
9599
PassedRuns: 0,
96100
FailedRuns: 0,
97101
SkippedRuns: 0,
98-
PassRatio: "100.00%",
99-
MaxPassRatio: 1.0,
102+
// With zero runs, we default passRatio to "100%"
103+
PassRatio: "100%",
104+
MaxPassRatio: 1.0,
100105
},
101106
},
102107
{
@@ -110,13 +115,13 @@ func TestGenerateSummaryData(t *testing.T) {
110115
TotalTests: 2,
111116
PanickedTests: 0,
112117
RacedTests: 0,
113-
FlakyTests: 1,
114-
FlakyTestRatio: "50.00%",
118+
FlakyTests: 1, // second test has ratio=0.7 => "flaky"
119+
FlakyTestRatio: "50%", // 1 out of 2 => 50%
115120
TotalRuns: 10,
116121
PassedRuns: 7,
117122
FailedRuns: 3,
118-
SkippedRuns: 1,
119-
PassRatio: "70.00%",
123+
SkippedRuns: 1, // from first test
124+
PassRatio: "70%",
120125
MaxPassRatio: 0.8,
121126
},
122127
},
@@ -132,14 +137,53 @@ func TestGenerateSummaryData(t *testing.T) {
132137
TotalTests: 3,
133138
PanickedTests: 0,
134139
RacedTests: 0,
140+
FlakyTests: 1, // last test has ratio=0.5 => "flaky"
141+
FlakyTestRatio: "33.3333%", // 1 out of 3 => 33.333...
142+
TotalRuns: 14, // 10 + 4
143+
PassedRuns: 11, // 9 + 2
144+
FailedRuns: 3, // 1 + 2
145+
SkippedRuns: 1, // from first test
146+
// 11/14 => 78.5714...
147+
PassRatio: "78.5714%",
148+
MaxPassRatio: 0.85,
149+
},
150+
},
151+
{
152+
name: "Tiny flake ratio that is exactly 0.01%",
153+
testResults: func() []TestResult {
154+
// 9,999 total:
155+
// - 9,998 stable => pass=1.0
156+
// - 1 flaky => pass=0.5
157+
const total = 9999
158+
tests := make([]TestResult, total)
159+
for i := 0; i < total-1; i++ {
160+
tests[i] = TestResult{
161+
PassRatio: 1.0,
162+
Runs: 10,
163+
Successes: 10,
164+
}
165+
}
166+
tests[total-1] = TestResult{
167+
PassRatio: 0.5, // 1 success, 1 fail
168+
Runs: 2,
169+
Successes: 1,
170+
Failures: 1,
171+
}
172+
return tests
173+
}(),
174+
maxPassRatio: 1.0,
175+
expected: SummaryData{
176+
TotalTests: 9999,
177+
PanickedTests: 0,
178+
RacedTests: 0,
135179
FlakyTests: 1,
136-
FlakyTestRatio: "33.33%",
137-
TotalRuns: 14,
138-
PassedRuns: 11,
139-
FailedRuns: 3,
140-
SkippedRuns: 1,
141-
PassRatio: "78.57%",
142-
MaxPassRatio: 0.85,
180+
FlakyTestRatio: "0.01%",
181+
TotalRuns: (9998 * 10) + 2,
182+
PassedRuns: (9998 * 10) + 1,
183+
FailedRuns: 1,
184+
SkippedRuns: 0,
185+
PassRatio: "99.999%",
186+
MaxPassRatio: 1.0,
143187
},
144188
},
145189
}
@@ -148,7 +192,8 @@ func TestGenerateSummaryData(t *testing.T) {
148192
t.Run(tc.name, func(t *testing.T) {
149193
summary := GenerateSummaryData(tc.testResults, tc.maxPassRatio)
150194
if !reflect.DeepEqual(summary, tc.expected) {
151-
t.Errorf("Test %s failed. Expected %+v, got %+v", tc.name, tc.expected, summary)
195+
t.Errorf("Test %q failed.\nExpected: %+v\nGot: %+v",
196+
tc.name, tc.expected, summary)
152197
}
153198
})
154199
}

tools/flakeguard/reports/presentation_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,15 @@ func TestRenderResults(t *testing.T) {
253253
PanickedTests: 0,
254254
RacedTests: 0,
255255
FlakyTests: 1,
256-
FlakyTestRatio: "100.00%",
256+
FlakyTestRatio: "100%",
257257
TotalRuns: 4,
258258
PassedRuns: 3,
259259
FailedRuns: 1,
260260
SkippedRuns: 0,
261-
PassRatio: "75.00%",
261+
PassRatio: "75%",
262262
MaxPassRatio: 0.9,
263263
},
264-
expectedStringsContain: []string{"Test1", "package1", "75.00%", "false", "1.05s", "4", "0"},
264+
expectedStringsContain: []string{"Test1", "package1", "75%", "false", "1.05s", "4", "0"},
265265
},
266266
// Add more test cases as needed
267267
}

0 commit comments

Comments
 (0)