|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/briandowns/spinner" |
| 10 | + "github.com/smartcontractkit/chainlink-testing-framework/tools/flakeguard/reports" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +var ReportCmd = &cobra.Command{ |
| 15 | + Use: "report", |
| 16 | + Short: "Aggregate test results and generate reports", |
| 17 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 18 | + fs := reports.OSFileSystem{} |
| 19 | + |
| 20 | + // Get flag values directly using cmd.Flags().Get* methods |
| 21 | + reportResultsPath, _ := cmd.Flags().GetString("results-path") |
| 22 | + reportOutputPath, _ := cmd.Flags().GetString("output-path") |
| 23 | + reportFormats, _ := cmd.Flags().GetString("format") |
| 24 | + reportMaxPassRatio, _ := cmd.Flags().GetFloat64("max-pass-ratio") |
| 25 | + reportCodeOwnersPath, _ := cmd.Flags().GetString("codeowners-path") |
| 26 | + reportRepoPath, _ := cmd.Flags().GetString("repo-path") |
| 27 | + |
| 28 | + // Split the formats into a slice |
| 29 | + formats := strings.Split(reportFormats, ",") |
| 30 | + |
| 31 | + // Start spinner for loading test reports |
| 32 | + s := spinner.New(spinner.CharSets[11], 100*time.Millisecond) |
| 33 | + s.Suffix = " Loading test reports..." |
| 34 | + s.Start() |
| 35 | + |
| 36 | + // Load test reports from JSON files |
| 37 | + testReports, err := reports.LoadReports(reportResultsPath) |
| 38 | + if err != nil { |
| 39 | + s.Stop() |
| 40 | + return fmt.Errorf("error loading test reports: %w", err) |
| 41 | + } |
| 42 | + s.Stop() |
| 43 | + fmt.Println("Test reports loaded successfully.") |
| 44 | + |
| 45 | + // Start spinner for aggregating reports |
| 46 | + s = spinner.New(spinner.CharSets[11], 100*time.Millisecond) |
| 47 | + s.Suffix = " Aggregating test reports..." |
| 48 | + s.Start() |
| 49 | + |
| 50 | + // Aggregate the reports |
| 51 | + aggregatedReport, err := reports.Aggregate(testReports...) |
| 52 | + if err != nil { |
| 53 | + s.Stop() |
| 54 | + return fmt.Errorf("error aggregating test reports: %w", err) |
| 55 | + } |
| 56 | + s.Stop() |
| 57 | + fmt.Println("Test reports aggregated successfully.") |
| 58 | + |
| 59 | + // Start spinner for mapping test results to paths |
| 60 | + s = spinner.New(spinner.CharSets[11], 100*time.Millisecond) |
| 61 | + s.Suffix = " Mapping test results to paths..." |
| 62 | + s.Start() |
| 63 | + |
| 64 | + // Map test results to test paths |
| 65 | + err = reports.MapTestResultsToPaths(aggregatedReport, reportRepoPath) |
| 66 | + if err != nil { |
| 67 | + s.Stop() |
| 68 | + return fmt.Errorf("error mapping test results to paths: %w", err) |
| 69 | + } |
| 70 | + s.Stop() |
| 71 | + fmt.Println("Test results mapped to paths successfully.") |
| 72 | + |
| 73 | + // Map test results to code owners if codeOwnersPath is provided |
| 74 | + if reportCodeOwnersPath != "" { |
| 75 | + s = spinner.New(spinner.CharSets[11], 100*time.Millisecond) |
| 76 | + s.Suffix = " Mapping test results to code owners..." |
| 77 | + s.Start() |
| 78 | + |
| 79 | + err = reports.MapTestResultsToOwners(aggregatedReport, reportCodeOwnersPath) |
| 80 | + if err != nil { |
| 81 | + s.Stop() |
| 82 | + return fmt.Errorf("error mapping test results to code owners: %w", err) |
| 83 | + } |
| 84 | + s.Stop() |
| 85 | + fmt.Println("Test results mapped to code owners successfully.") |
| 86 | + } |
| 87 | + |
| 88 | + // Exclude outputs and package outputs from the aggregated report of all tests |
| 89 | + for i := range aggregatedReport.Results { |
| 90 | + aggregatedReport.Results[i].Outputs = nil |
| 91 | + aggregatedReport.Results[i].PackageOutputs = nil |
| 92 | + } |
| 93 | + |
| 94 | + // Create output directory if it doesn't exist |
| 95 | + outputDir := reportOutputPath |
| 96 | + if err := fs.MkdirAll(outputDir, 0755); err != nil { |
| 97 | + return fmt.Errorf("error creating output directory: %w", err) |
| 98 | + } |
| 99 | + |
| 100 | + // Save the aggregated report (all tests) |
| 101 | + allTestsReportPath := filepath.Join(outputDir, "all-tests-report.json") |
| 102 | + if err := reports.SaveReport(fs, allTestsReportPath, *aggregatedReport); err != nil { |
| 103 | + return fmt.Errorf("error saving all tests report: %w", err) |
| 104 | + } |
| 105 | + fmt.Printf("All tests report saved to %s\n", allTestsReportPath) |
| 106 | + |
| 107 | + // Generate and save the reports (all tests) in specified formats |
| 108 | + for _, format := range formats { |
| 109 | + s = spinner.New(spinner.CharSets[11], 100*time.Millisecond) |
| 110 | + s.Suffix = fmt.Sprintf(" Generating all tests report in format %s...", format) |
| 111 | + s.Start() |
| 112 | + |
| 113 | + if err := generateReport(aggregatedReport, format, filepath.Join(outputDir, "all-tests")); err != nil { |
| 114 | + s.Stop() |
| 115 | + return fmt.Errorf("error generating all tests report in format %s: %w", format, err) |
| 116 | + } |
| 117 | + s.Stop() |
| 118 | + fmt.Printf("All tests report in format %s generated successfully.\n", format) |
| 119 | + } |
| 120 | + |
| 121 | + // Filter failed tests (PassRatio < maxPassRatio and not skipped) |
| 122 | + s = spinner.New(spinner.CharSets[11], 100*time.Millisecond) |
| 123 | + s.Suffix = " Filtering failed tests..." |
| 124 | + s.Start() |
| 125 | + |
| 126 | + failedTests := reports.FilterTests(aggregatedReport.Results, func(tr reports.TestResult) bool { |
| 127 | + return !tr.Skipped && tr.PassRatio < reportMaxPassRatio |
| 128 | + }) |
| 129 | + s.Stop() |
| 130 | + fmt.Println("Failed tests filtered successfully.") |
| 131 | + |
| 132 | + // For failed tests, include outputs and package outputs |
| 133 | + for i := range failedTests { |
| 134 | + // Retrieve outputs and package outputs from original reports |
| 135 | + failedTests[i].Outputs = getOriginalOutputs(testReports, failedTests[i].TestName, failedTests[i].TestPackage) |
| 136 | + failedTests[i].PackageOutputs = getOriginalPackageOutputs(testReports, failedTests[i].TestName, failedTests[i].TestPackage) |
| 137 | + } |
| 138 | + |
| 139 | + // Create a new report for failed tests |
| 140 | + failedReport := &reports.TestReport{ |
| 141 | + GoProject: aggregatedReport.GoProject, |
| 142 | + TestRunCount: aggregatedReport.TestRunCount, |
| 143 | + RaceDetection: aggregatedReport.RaceDetection, |
| 144 | + ExcludedTests: aggregatedReport.ExcludedTests, |
| 145 | + SelectedTests: aggregatedReport.SelectedTests, |
| 146 | + Results: failedTests, |
| 147 | + } |
| 148 | + |
| 149 | + // Save the failed tests report |
| 150 | + failedTestsReportPath := filepath.Join(outputDir, "failed-tests-report.json") |
| 151 | + if err := reports.SaveReport(fs, failedTestsReportPath, *failedReport); err != nil { |
| 152 | + return fmt.Errorf("error saving failed tests report: %w", err) |
| 153 | + } |
| 154 | + fmt.Printf("Failed tests report saved to %s\n", failedTestsReportPath) |
| 155 | + |
| 156 | + // Generate and save the reports for failed tests in specified formats |
| 157 | + for _, format := range formats { |
| 158 | + s = spinner.New(spinner.CharSets[11], 100*time.Millisecond) |
| 159 | + s.Suffix = fmt.Sprintf(" Generating failed tests report in format %s...", format) |
| 160 | + s.Start() |
| 161 | + |
| 162 | + if err := generateReport(failedReport, format, filepath.Join(outputDir, "failed-tests")); err != nil { |
| 163 | + s.Stop() |
| 164 | + return fmt.Errorf("error generating failed tests report in format %s: %w", format, err) |
| 165 | + } |
| 166 | + s.Stop() |
| 167 | + fmt.Printf("Failed tests report in format %s generated successfully.\n", format) |
| 168 | + } |
| 169 | + |
| 170 | + fmt.Printf("Reports generated at: %s\n", reportOutputPath) |
| 171 | + |
| 172 | + return nil |
| 173 | + }, |
| 174 | +} |
| 175 | + |
| 176 | +func init() { |
| 177 | + ReportCmd.Flags().StringP("results-path", "p", "", "Path to the folder containing JSON test result files (required)") |
| 178 | + ReportCmd.Flags().StringP("output-path", "o", "./report", "Path to output the generated report files") |
| 179 | + ReportCmd.Flags().StringP("format", "f", "markdown,json", "Comma-separated list of report formats (markdown,json)") |
| 180 | + ReportCmd.Flags().Float64P("max-pass-ratio", "", 1.0, "The maximum pass ratio threshold for a test to be considered flaky") |
| 181 | + ReportCmd.Flags().StringP("codeowners-path", "", "", "Path to the CODEOWNERS file") |
| 182 | + ReportCmd.Flags().StringP("repo-path", "", ".", "The path to the root of the repository/project") |
| 183 | + ReportCmd.MarkFlagRequired("results-path") |
| 184 | +} |
| 185 | + |
| 186 | +func generateReport(report *reports.TestReport, format, outputPath string) error { |
| 187 | + fs := reports.OSFileSystem{} |
| 188 | + format = strings.ToLower(strings.TrimSpace(format)) |
| 189 | + switch format { |
| 190 | + case "markdown": |
| 191 | + mdFileName := outputPath + ".md" |
| 192 | + mdFile, err := fs.Create(mdFileName) |
| 193 | + if err != nil { |
| 194 | + return fmt.Errorf("error creating markdown file: %w", err) |
| 195 | + } |
| 196 | + defer mdFile.Close() |
| 197 | + reports.GenerateMarkdownSummary(mdFile, report, 1.0) |
| 198 | + case "json": |
| 199 | + jsonFileName := outputPath + ".json" |
| 200 | + if err := reports.SaveReportNoLogs(fs, jsonFileName, *report); err != nil { |
| 201 | + return fmt.Errorf("error saving JSON report: %w", err) |
| 202 | + } |
| 203 | + default: |
| 204 | + return fmt.Errorf("unsupported report format: %s", format) |
| 205 | + } |
| 206 | + |
| 207 | + // Generate summary JSON |
| 208 | + summaryData := reports.GenerateSummaryData(report.Results, 1.0) |
| 209 | + summaryFileName := outputPath + "-summary.json" |
| 210 | + if err := reports.SaveSummaryAsJSON(fs, summaryFileName, summaryData); err != nil { |
| 211 | + return fmt.Errorf("error saving summary JSON: %w", err) |
| 212 | + } |
| 213 | + |
| 214 | + return nil |
| 215 | +} |
| 216 | + |
| 217 | +// Helper functions to retrieve original outputs and package outputs |
| 218 | +func getOriginalOutputs(reports []*reports.TestReport, testName, testPackage string) []string { |
| 219 | + for _, report := range reports { |
| 220 | + for _, result := range report.Results { |
| 221 | + if result.TestName == testName && result.TestPackage == testPackage { |
| 222 | + return result.Outputs |
| 223 | + } |
| 224 | + } |
| 225 | + } |
| 226 | + return nil |
| 227 | +} |
| 228 | + |
| 229 | +func getOriginalPackageOutputs(reports []*reports.TestReport, testName, testPackage string) []string { |
| 230 | + for _, report := range reports { |
| 231 | + for _, result := range report.Results { |
| 232 | + if result.TestName == testName && result.TestPackage == testPackage { |
| 233 | + return result.PackageOutputs |
| 234 | + } |
| 235 | + } |
| 236 | + } |
| 237 | + return nil |
| 238 | +} |
0 commit comments