Skip to content

Commit 12b3779

Browse files
committed
Fix
1 parent 5edc8bb commit 12b3779

File tree

4 files changed

+64
-73
lines changed

4 files changed

+64
-73
lines changed

tools/flakeguard/reports/data.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@ func GenerateSummaryData(tests []TestResult, maxPassRatio float64) SummaryData {
6363
passes += result.Successes
6464
fails += result.Failures
6565
skips += result.Skips
66+
6667
if result.Panic {
6768
panickedTests++
6869
flakyTests++
6970
} else if result.Race {
7071
racedTests++
7172
flakyTests++
72-
} else if result.PassRatio < maxPassRatio {
73+
} else if !result.Skipped && result.Runs > 0 && result.PassRatio < maxPassRatio {
74+
// Exclude skipped tests and tests with no runs
7375
flakyTests++
7476
}
7577
}
@@ -82,12 +84,14 @@ func GenerateSummaryData(tests []TestResult, maxPassRatio float64) SummaryData {
8284
passPercentage = math.Round((float64(passes)/float64(runs)*100)*100) / 100
8385
averagePassRatio = float64(passes) / float64(runs)
8486
}
85-
if len(tests) > 0 {
86-
flakePercentage = math.Round((float64(flakyTests)/float64(len(tests))*100)*100) / 100
87+
totalTests := len(tests) // Include skipped tests in total tests
88+
totalExecutedTests := totalTests - skips // Tests that were actually executed
89+
if totalExecutedTests > 0 {
90+
flakePercentage = math.Round((float64(flakyTests)/float64(totalExecutedTests)*100)*100) / 100
8791
}
8892

8993
return SummaryData{
90-
TotalTests: len(tests),
94+
TotalTests: totalTests,
9195
PanickedTests: panickedTests,
9296
RacedTests: racedTests,
9397
FlakyTests: flakyTests,
@@ -184,7 +188,7 @@ func mergeTestResults(a, b TestResult) TestResult {
184188
if a.Runs > 0 {
185189
a.PassRatio = float64(a.Successes) / float64(a.Runs)
186190
} else {
187-
a.PassRatio = 0.0
191+
a.PassRatio = -1.0 // Indicate undefined pass ratio for skipped tests
188192
}
189193

190194
return a

tools/flakeguard/reports/data_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ func TestAggregate_AllSkippedTests(t *testing.T) {
388388
Skipped: true,
389389
Runs: 0,
390390
Skips: 3,
391-
PassRatio: 0.0, // Or set to -1 to indicate undefined
391+
PassRatio: -1, // 1 indicate undefined
392392
},
393393
},
394394
}
@@ -403,7 +403,7 @@ func TestAggregate_AllSkippedTests(t *testing.T) {
403403
Skipped: true,
404404
Runs: 0,
405405
Skips: 2,
406-
PassRatio: 0.0,
406+
PassRatio: -1,
407407
},
408408
},
409409
}
@@ -419,7 +419,7 @@ func TestAggregate_AllSkippedTests(t *testing.T) {
419419
Skipped: true,
420420
Runs: 0,
421421
Skips: 5,
422-
PassRatio: 0.0,
422+
PassRatio: -1,
423423
}
424424

425425
if len(aggregatedReport.Results) != 1 {

tools/flakeguard/reports/presentation.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ import (
1010
"golang.org/x/text/message"
1111
)
1212

13-
func GenerateResultsTable(
13+
func GenerateFlakyTestsTable(
1414
results []TestResult,
1515
expectedPassRatio float64,
1616
markdown bool,
1717
) [][]string {
1818
p := message.NewPrinter(language.English)
1919
sortTestResults(results)
2020

21+
// Headers in the requested order
2122
headers := []string{
2223
"Name",
2324
"Pass Ratio",
@@ -34,18 +35,22 @@ func GenerateResultsTable(
3435
"Code Owners",
3536
}
3637

38+
// Format headers for Markdown if needed
3739
if markdown {
3840
for i, header := range headers {
3941
headers[i] = fmt.Sprintf("**%s**", header)
4042
}
4143
}
4244

45+
// Initialize the table with headers
4346
table := [][]string{headers}
47+
4448
for _, result := range results {
45-
if result.PassRatio < expectedPassRatio {
49+
// Exclude skipped tests and only include tests below the expected pass ratio
50+
if !result.Skipped && result.PassRatio < expectedPassRatio {
4651
row := []string{
4752
result.TestName,
48-
fmt.Sprintf("%.2f%%", result.PassRatio*100),
53+
formatPassRatio(result.PassRatio),
4954
fmt.Sprintf("%t", result.Panic),
5055
fmt.Sprintf("%t", result.Timeout),
5156
fmt.Sprintf("%t", result.Race),
@@ -58,7 +63,7 @@ func GenerateResultsTable(
5863
avgDuration(result.Durations).String(),
5964
}
6065

61-
// Code owners
66+
// Add code owners
6267
owners := "Unknown"
6368
if len(result.CodeOwners) > 0 {
6469
owners = strings.Join(result.CodeOwners, ", ")
@@ -71,6 +76,13 @@ func GenerateResultsTable(
7176
return table
7277
}
7378

79+
func formatPassRatio(passRatio float64) string {
80+
if passRatio < 0 {
81+
return "N/A" // Handle undefined pass ratios (e.g., skipped tests)
82+
}
83+
return fmt.Sprintf("%.2f%%", passRatio*100)
84+
}
85+
7486
func GenerateMarkdownSummary(w io.Writer, testReport *TestReport, maxPassRatio float64) {
7587
settingsTable := buildSettingsTable(testReport, maxPassRatio)
7688
fmt.Fprint(w, "# Flakeguard Summary\n\n")
@@ -115,7 +127,7 @@ func RenderResults(
115127
maxPassRatio float64,
116128
markdown bool,
117129
) {
118-
resultsTable := GenerateResultsTable(tests, maxPassRatio, markdown)
130+
resultsTable := GenerateFlakyTestsTable(tests, maxPassRatio, markdown)
119131
summary := GenerateSummaryData(tests, maxPassRatio)
120132
renderSummaryTable(w, summary, markdown)
121133
renderTestResultsTable(w, resultsTable, markdown)

tools/flakeguard/reports/presentation_test.go

Lines changed: 35 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -8,91 +8,66 @@ import (
88
"time"
99
)
1010

11-
// TestGenerateResultsTable tests the GenerateResultsTable function.
12-
func TestGenerateResultsTable(t *testing.T) {
13-
testResults := []TestResult{
11+
func TestGenerateFlakyTestsTable(t *testing.T) {
12+
results := []TestResult{
1413
{
15-
TestName: "TestA",
16-
PassRatio: 0.8,
17-
Panic: false,
18-
Timeout: false,
19-
Race: false,
20-
Runs: 5,
21-
Successes: 4,
22-
Failures: 1,
23-
Skips: 0,
24-
TestPackage: "pkg1",
25-
PackagePanic: false,
26-
Durations: []time.Duration{time.Second, time.Second, time.Second, time.Second, time.Second},
27-
CodeOwners: []string{"owner1"},
14+
TestName: "TestFlaky",
15+
PassRatio: 0.5,
16+
Skipped: false,
17+
Runs: 2,
18+
Successes: 1,
19+
Failures: 1,
20+
TestPackage: "pkg1",
21+
CodeOwners: []string{"owner1"},
2822
},
2923
{
30-
TestName: "TestB",
31-
PassRatio: 1.0,
32-
Panic: false,
33-
Timeout: false,
34-
Race: false,
35-
Runs: 3,
36-
Successes: 3,
37-
Failures: 0,
38-
Skips: 0,
39-
TestPackage: "pkg2",
40-
PackagePanic: false,
41-
Durations: []time.Duration{2 * time.Second, 2 * time.Second, 2 * time.Second},
42-
CodeOwners: []string{"owner2"},
24+
TestName: "TestSkipped",
25+
PassRatio: -1.0,
26+
Skipped: true,
27+
Runs: 0,
28+
Skips: 1,
29+
TestPackage: "pkg2",
30+
CodeOwners: []string{"owner2"},
4331
},
4432
}
4533

4634
expectedPassRatio := 0.9
4735
markdown := false
4836

49-
table := GenerateResultsTable(testResults, expectedPassRatio, markdown)
50-
51-
// Only TestA should be included since its PassRatio is below 0.9
52-
if len(table) != 2 {
53-
t.Fatalf("Expected table length 2 (headers + 1 row), got %d", len(table))
54-
}
37+
table := GenerateFlakyTestsTable(results, expectedPassRatio, markdown)
5538

5639
// Verify headers
57-
headers := table[0]
5840
expectedHeaders := []string{
59-
"Name",
60-
"Pass Ratio",
61-
"Panicked?",
62-
"Timed Out?",
63-
"Race?",
64-
"Runs",
65-
"Successes",
66-
"Failures",
67-
"Skips",
68-
"Package",
69-
"Package Panicked?",
70-
"Avg Duration",
71-
"Code Owners",
41+
"Name", "Pass Ratio", "Panicked?", "Timed Out?", "Race?", "Runs",
42+
"Successes", "Failures", "Skips", "Package", "Package Panicked?",
43+
"Avg Duration", "Code Owners",
7244
}
73-
if !reflect.DeepEqual(headers, expectedHeaders) {
74-
t.Errorf("Expected headers %+v, got %+v", expectedHeaders, headers)
45+
if !reflect.DeepEqual(table[0], expectedHeaders) {
46+
t.Errorf("Expected headers %+v, got %+v", expectedHeaders, table[0])
47+
}
48+
49+
// Verify rows (only TestFlaky should appear)
50+
if len(table) != 2 { // 1 header row + 1 data row
51+
t.Fatalf("Expected table length 2 (headers + 1 row), got %d", len(table))
7552
}
7653

77-
// Verify row data
78-
row := table[1]
7954
expectedRow := []string{
80-
"TestA",
81-
"80.00%",
55+
"TestFlaky",
56+
"50.00%",
8257
"false",
8358
"false",
8459
"false",
85-
"5",
86-
"4",
60+
"2",
61+
"1",
8762
"1",
8863
"0",
8964
"pkg1",
9065
"false",
91-
"1s",
66+
"0s",
9267
"owner1",
9368
}
94-
if !reflect.DeepEqual(row, expectedRow) {
95-
t.Errorf("Expected row %+v, got %+v", expectedRow, row)
69+
if !reflect.DeepEqual(table[1], expectedRow) {
70+
t.Errorf("Expected row %+v, got %+v", expectedRow, table[1])
9671
}
9772
}
9873

0 commit comments

Comments
 (0)