diff --git a/tools/flakeguard/cmd/run.go b/tools/flakeguard/cmd/run.go index 7312c3204..0bdf9756b 100644 --- a/tools/flakeguard/cmd/run.go +++ b/tools/flakeguard/cmd/run.go @@ -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) } diff --git a/tools/flakeguard/reports/presentation.go b/tools/flakeguard/reports/presentation.go index ebd846c31..0a0babfa9 100644 --- a/tools/flakeguard/reports/presentation.go +++ b/tools/flakeguard/reports/presentation.go @@ -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) @@ -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 { @@ -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) @@ -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)}, @@ -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) { @@ -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, "
") + fmt.Fprintf(w, "%d Results\n\n", numResults) + } + for i, row := range table { printRow(w, row, colWidths) if i == 0 { fmt.Fprintln(w, separator) } } + + if collapsible { + fmt.Fprintln(w, "
") + } } func calculateColumnWidths(table [][]string) []int { diff --git a/tools/flakeguard/reports/presentation_test.go b/tools/flakeguard/reports/presentation_test.go index d443e1f72..b3881a1f4 100644 --- a/tools/flakeguard/reports/presentation_test.go +++ b/tools/flakeguard/reports/presentation_test.go @@ -205,7 +205,7 @@ func TestPrintTable(t *testing.T) { } var buffer bytes.Buffer - printTable(&buffer, table) + printTable(&buffer, table, false) output := buffer.String() @@ -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 diff --git a/tools/flakeguard/x.md b/tools/flakeguard/x.md new file mode 100644 index 000000000..8fb92c12a --- /dev/null +++ b/tools/flakeguard/x.md @@ -0,0 +1,10 @@ +
+## Toggle Me! + +| Month | Savings | +| -------- | ------- | +| January | $250 | +| February | $80 | +| March | $420 | + +
\ No newline at end of file