|
4 | 4 | "encoding/json" |
5 | 5 | "fmt" |
6 | 6 | "io" |
| 7 | + "log" |
7 | 8 | "os" |
8 | 9 | "path/filepath" |
9 | 10 | "sort" |
@@ -139,3 +140,87 @@ func PrintTests(tests []TestResult, w io.Writer) { |
139 | 140 | fmt.Fprintf(w, "Outputs:\n%s\n", strings.Join(test.Outputs, "")) |
140 | 141 | } |
141 | 142 | } |
| 143 | + |
| 144 | +// Helper function to save filtered results and logs to specified paths |
| 145 | +func SaveFilteredResultsAndLogs(outputResultsPath, outputLogsPath string, failedResults []TestResult) { |
| 146 | + if outputResultsPath != "" { |
| 147 | + if err := saveResults(outputResultsPath, failedResults); err != nil { |
| 148 | + log.Fatalf("Error writing failed results to file: %v", err) |
| 149 | + } |
| 150 | + fmt.Printf("Filtered failed test results saved to %s\n", outputResultsPath) |
| 151 | + } else { |
| 152 | + fmt.Println("No failed tests found based on the specified threshold and min pass ratio.") |
| 153 | + } |
| 154 | + |
| 155 | + if outputLogsPath != "" { |
| 156 | + if err := saveTestOutputs(outputLogsPath, failedResults); err != nil { |
| 157 | + log.Fatalf("Error writing failed logs to file: %v", err) |
| 158 | + } |
| 159 | + fmt.Printf("Filtered failed test logs saved to %s\n", outputLogsPath) |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +// Helper function to save results to JSON file |
| 164 | +func saveResults(filePath string, results []TestResult) error { |
| 165 | + // Define a struct type without Outputs and PackageOutputs |
| 166 | + type filteredTestResult struct { |
| 167 | + TestName string |
| 168 | + TestPackage string |
| 169 | + Panicked bool |
| 170 | + PackagePanicked bool |
| 171 | + PassRatio float64 |
| 172 | + PassRatioPercentage string |
| 173 | + Skipped bool |
| 174 | + Runs int |
| 175 | + Durations []float64 |
| 176 | + } |
| 177 | + |
| 178 | + var filteredResults []filteredTestResult |
| 179 | + for _, r := range results { |
| 180 | + filteredResults = append(filteredResults, filteredTestResult{ |
| 181 | + TestName: r.TestName, |
| 182 | + TestPackage: r.TestPackage, |
| 183 | + Panicked: r.Panicked, |
| 184 | + PackagePanicked: r.PackagePanicked, |
| 185 | + PassRatio: r.PassRatio, |
| 186 | + PassRatioPercentage: r.PassRatioPercentage, |
| 187 | + Skipped: r.Skipped, |
| 188 | + Runs: r.Runs, |
| 189 | + Durations: r.Durations, |
| 190 | + }) |
| 191 | + } |
| 192 | + |
| 193 | + data, err := json.MarshalIndent(filteredResults, "", " ") |
| 194 | + if err != nil { |
| 195 | + return fmt.Errorf("error marshaling results: %v", err) |
| 196 | + } |
| 197 | + return os.WriteFile(filePath, data, 0644) |
| 198 | +} |
| 199 | + |
| 200 | +// Helper function to save test names, packages, and outputs to JSON file |
| 201 | +func saveTestOutputs(filePath string, results []TestResult) error { |
| 202 | + // Define a struct type with only the required fields |
| 203 | + type outputOnlyResult struct { |
| 204 | + TestName string |
| 205 | + TestPackage string |
| 206 | + Outputs []string |
| 207 | + PackageOutputs []string |
| 208 | + } |
| 209 | + |
| 210 | + // Convert results to the filtered struct |
| 211 | + var outputResults []outputOnlyResult |
| 212 | + for _, r := range results { |
| 213 | + outputResults = append(outputResults, outputOnlyResult{ |
| 214 | + TestName: r.TestName, |
| 215 | + TestPackage: r.TestPackage, |
| 216 | + Outputs: r.Outputs, |
| 217 | + PackageOutputs: r.PackageOutputs, |
| 218 | + }) |
| 219 | + } |
| 220 | + |
| 221 | + data, err := json.MarshalIndent(outputResults, "", " ") |
| 222 | + if err != nil { |
| 223 | + return fmt.Errorf("error marshaling outputs: %v", err) |
| 224 | + } |
| 225 | + return os.WriteFile(filePath, data, 0644) |
| 226 | +} |
0 commit comments