Skip to content

Commit

Permalink
Makes markdown tables collapsible
Browse files Browse the repository at this point in the history
  • Loading branch information
kalverra committed Feb 4, 2025
1 parent 3528645 commit acb8567
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
2 changes: 1 addition & 1 deletion tools/flakeguard/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ var RunTestsCmd = &cobra.Command{
if len(flakyTests) > 0 {
log.Info().Int("count", len(flakyTests)).Str("pass ratio threshold", fmt.Sprintf("%.2f%%", maxPassRatio*100)).Msg("Found flaky tests")
fmt.Printf("\nFlakeguard Summary\n")
reports.RenderResults(os.Stdout, flakyTests, maxPassRatio, false)
reports.RenderResults(os.Stdout, flakyTests, maxPassRatio, false, false)
// Exit with error code if there are flaky tests
os.Exit(1)
}
Expand Down
44 changes: 33 additions & 11 deletions tools/flakeguard/reports/presentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func GenerateGitHubSummaryMarkdown(w io.Writer, testReport *TestReport, maxPassR
}

settingsTable := buildSettingsTable(testReport, maxPassRatio)
printTable(w, settingsTable)
printTable(w, settingsTable, false)
fmt.Fprintln(w)

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

RenderResults(w, testReport.Results, maxPassRatio, true)
RenderResults(w, testReport.Results, maxPassRatio, true, false)

if artifactLink != "" {
renderArtifactSection(w, artifactName, artifactLink)
}
}

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

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

resultsTable := GenerateFlakyTestsTable(testReport.Results, maxPassRatio, true)
renderTestResultsTable(w, resultsTable)
renderTestResultsTable(w, resultsTable, true)

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

// RenderResults renders the test results into a console or markdown format.
// If in markdown mode, the table results can also be made collapsible.
func RenderResults(
w io.Writer,
tests []TestResult,
maxPassRatio float64,
markdown bool,
collapsible bool,
) {
resultsTable := GenerateFlakyTestsTable(tests, maxPassRatio, markdown)
summary := GenerateSummaryData(tests, maxPassRatio)
renderSummaryTable(w, summary, markdown)
renderTestResultsTable(w, resultsTable)
renderSummaryTable(w, summary, markdown, false) // Don't make the summary collapsible
renderTestResultsTable(w, resultsTable, collapsible)
}

func renderSummaryTable(w io.Writer, summary SummaryData, markdown bool) {
// renderSummaryTable renders a summary table with the given data into a console or markdown format.
// If in markdown mode, the table can also be made collapsible.
func renderSummaryTable(w io.Writer, summary SummaryData, markdown bool, collapsible bool) {
summaryData := [][]string{
{"Category", "Total"},
{"Tests", fmt.Sprintf("%d", summary.TotalTests)},
Expand All @@ -205,16 +216,16 @@ func renderSummaryTable(w io.Writer, summary SummaryData, markdown bool) {
}
}
}
printTable(w, summaryData)
printTable(w, summaryData, collapsible && markdown)
fmt.Fprintln(w)
}

func renderTestResultsTable(w io.Writer, table [][]string) {
func renderTestResultsTable(w io.Writer, table [][]string, collapsible bool) {
if len(table) <= 1 {
fmt.Fprintln(w, "No tests found under the specified pass ratio threshold.")
return
}
printTable(w, table)
printTable(w, table, collapsible)
}

func renderArtifactSection(w io.Writer, artifactName, artifactLink string) {
Expand All @@ -226,16 +237,27 @@ func renderArtifactSection(w io.Writer, artifactName, artifactLink string) {
}
}

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

if collapsible {
numResults := len(table) - 1
fmt.Fprintln(w, "<details>")
fmt.Fprintf(w, "<summary>%d Results</summary>\n\n", numResults)
}

for i, row := range table {
printRow(w, row, colWidths)
if i == 0 {
fmt.Fprintln(w, separator)
}
}

if collapsible {
fmt.Fprintln(w, "</details>")
}
}

func calculateColumnWidths(table [][]string) []int {
Expand Down
4 changes: 2 additions & 2 deletions tools/flakeguard/reports/presentation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func TestPrintTable(t *testing.T) {
}

var buffer bytes.Buffer
printTable(&buffer, table)
printTable(&buffer, table, false)

output := buffer.String()

Expand Down Expand Up @@ -270,7 +270,7 @@ func TestRenderResults(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
var buf bytes.Buffer

RenderResults(&buf, tc.testResults, tc.maxPassRatio, false)
RenderResults(&buf, tc.testResults, tc.maxPassRatio, false, false)
output := buf.String()

// Generate the summary data
Expand Down
10 changes: 10 additions & 0 deletions tools/flakeguard/x.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<details>
<summary>## Toggle Me!</summary>

| Month | Savings |
| -------- | ------- |
| January | $250 |
| February | $80 |
| March | $420 |

</details>

0 comments on commit acb8567

Please sign in to comment.