Skip to content

Commit acb8567

Browse files
committed
Makes markdown tables collapsible
1 parent 3528645 commit acb8567

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

tools/flakeguard/cmd/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ var RunTestsCmd = &cobra.Command{
9797
if len(flakyTests) > 0 {
9898
log.Info().Int("count", len(flakyTests)).Str("pass ratio threshold", fmt.Sprintf("%.2f%%", maxPassRatio*100)).Msg("Found flaky tests")
9999
fmt.Printf("\nFlakeguard Summary\n")
100-
reports.RenderResults(os.Stdout, flakyTests, maxPassRatio, false)
100+
reports.RenderResults(os.Stdout, flakyTests, maxPassRatio, false, false)
101101
// Exit with error code if there are flaky tests
102102
os.Exit(1)
103103
}

tools/flakeguard/reports/presentation.go

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func GenerateGitHubSummaryMarkdown(w io.Writer, testReport *TestReport, maxPassR
9292
}
9393

9494
settingsTable := buildSettingsTable(testReport, maxPassRatio)
95-
printTable(w, settingsTable)
95+
printTable(w, settingsTable, false)
9696
fmt.Fprintln(w)
9797

9898
summary := GenerateSummaryData(testReport.Results, maxPassRatio)
@@ -102,14 +102,20 @@ func GenerateGitHubSummaryMarkdown(w io.Writer, testReport *TestReport, maxPassR
102102
fmt.Fprintln(w, "## No Flakes Found :white_check_mark:")
103103
}
104104

105-
RenderResults(w, testReport.Results, maxPassRatio, true)
105+
RenderResults(w, testReport.Results, maxPassRatio, true, false)
106106

107107
if artifactLink != "" {
108108
renderArtifactSection(w, artifactName, artifactLink)
109109
}
110110
}
111111

112-
func GeneratePRCommentMarkdown(w io.Writer, testReport *TestReport, maxPassRatio float64, baseBranch, currentBranch, currentCommitSHA, repoURL, actionRunID, artifactName, artifactLink string) {
112+
// GeneratePRCommentMarkdown generates a markdown summary of the test results for a GitHub PR comment.
113+
func GeneratePRCommentMarkdown(
114+
w io.Writer,
115+
testReport *TestReport,
116+
maxPassRatio float64,
117+
baseBranch, currentBranch, currentCommitSHA, repoURL, actionRunID, artifactName, artifactLink string,
118+
) {
113119
fmt.Fprint(w, "# Flakeguard Summary\n\n")
114120

115121
if len(testReport.Results) == 0 {
@@ -146,7 +152,7 @@ func GeneratePRCommentMarkdown(w io.Writer, testReport *TestReport, maxPassRatio
146152
}
147153

148154
resultsTable := GenerateFlakyTestsTable(testReport.Results, maxPassRatio, true)
149-
renderTestResultsTable(w, resultsTable)
155+
renderTestResultsTable(w, resultsTable, true)
150156

151157
if artifactLink != "" {
152158
renderArtifactSection(w, artifactName, artifactLink)
@@ -170,19 +176,24 @@ func buildSettingsTable(testReport *TestReport, maxPassRatio float64) [][]string
170176
return rows
171177
}
172178

179+
// RenderResults renders the test results into a console or markdown format.
180+
// If in markdown mode, the table results can also be made collapsible.
173181
func RenderResults(
174182
w io.Writer,
175183
tests []TestResult,
176184
maxPassRatio float64,
177185
markdown bool,
186+
collapsible bool,
178187
) {
179188
resultsTable := GenerateFlakyTestsTable(tests, maxPassRatio, markdown)
180189
summary := GenerateSummaryData(tests, maxPassRatio)
181-
renderSummaryTable(w, summary, markdown)
182-
renderTestResultsTable(w, resultsTable)
190+
renderSummaryTable(w, summary, markdown, false) // Don't make the summary collapsible
191+
renderTestResultsTable(w, resultsTable, collapsible)
183192
}
184193

185-
func renderSummaryTable(w io.Writer, summary SummaryData, markdown bool) {
194+
// renderSummaryTable renders a summary table with the given data into a console or markdown format.
195+
// If in markdown mode, the table can also be made collapsible.
196+
func renderSummaryTable(w io.Writer, summary SummaryData, markdown bool, collapsible bool) {
186197
summaryData := [][]string{
187198
{"Category", "Total"},
188199
{"Tests", fmt.Sprintf("%d", summary.TotalTests)},
@@ -205,16 +216,16 @@ func renderSummaryTable(w io.Writer, summary SummaryData, markdown bool) {
205216
}
206217
}
207218
}
208-
printTable(w, summaryData)
219+
printTable(w, summaryData, collapsible && markdown)
209220
fmt.Fprintln(w)
210221
}
211222

212-
func renderTestResultsTable(w io.Writer, table [][]string) {
223+
func renderTestResultsTable(w io.Writer, table [][]string, collapsible bool) {
213224
if len(table) <= 1 {
214225
fmt.Fprintln(w, "No tests found under the specified pass ratio threshold.")
215226
return
216227
}
217-
printTable(w, table)
228+
printTable(w, table, collapsible)
218229
}
219230

220231
func renderArtifactSection(w io.Writer, artifactName, artifactLink string) {
@@ -226,16 +237,27 @@ func renderArtifactSection(w io.Writer, artifactName, artifactLink string) {
226237
}
227238
}
228239

229-
func printTable(w io.Writer, table [][]string) {
240+
// printTable prints a markdown table to the given writer in a pretty format.
241+
func printTable(w io.Writer, table [][]string, collapsible bool) {
230242
colWidths := calculateColumnWidths(table)
231243
separator := buildSeparator(colWidths)
232244

245+
if collapsible {
246+
numResults := len(table) - 1
247+
fmt.Fprintln(w, "<details>")
248+
fmt.Fprintf(w, "<summary>%d Results</summary>\n\n", numResults)
249+
}
250+
233251
for i, row := range table {
234252
printRow(w, row, colWidths)
235253
if i == 0 {
236254
fmt.Fprintln(w, separator)
237255
}
238256
}
257+
258+
if collapsible {
259+
fmt.Fprintln(w, "</details>")
260+
}
239261
}
240262

241263
func calculateColumnWidths(table [][]string) []int {

tools/flakeguard/reports/presentation_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func TestPrintTable(t *testing.T) {
205205
}
206206

207207
var buffer bytes.Buffer
208-
printTable(&buffer, table)
208+
printTable(&buffer, table, false)
209209

210210
output := buffer.String()
211211

@@ -270,7 +270,7 @@ func TestRenderResults(t *testing.T) {
270270
t.Run(tc.name, func(t *testing.T) {
271271
var buf bytes.Buffer
272272

273-
RenderResults(&buf, tc.testResults, tc.maxPassRatio, false)
273+
RenderResults(&buf, tc.testResults, tc.maxPassRatio, false, false)
274274
output := buf.String()
275275

276276
// Generate the summary data

tools/flakeguard/x.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<details>
2+
<summary>## Toggle Me!</summary>
3+
4+
| Month | Savings |
5+
| -------- | ------- |
6+
| January | $250 |
7+
| February | $80 |
8+
| March | $420 |
9+
10+
</details>

0 commit comments

Comments
 (0)