|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "os" |
| 10 | + "strings" |
| 11 | + "time" |
| 12 | + |
| 13 | + "golang.org/x/exp/slices" |
| 14 | + "golang.org/x/xerrors" |
| 15 | +) |
| 16 | + |
| 17 | +func main() { |
| 18 | + if len(os.Args) != 2 { |
| 19 | + _, _ = fmt.Println("usage: ci-report <gotests.json>") |
| 20 | + os.Exit(1) |
| 21 | + } |
| 22 | + name := os.Args[1] |
| 23 | + |
| 24 | + goTests, err := parseGoTestJSON(name) |
| 25 | + if err != nil { |
| 26 | + _, _ = fmt.Printf("error parsing gotestsum report: %v", err) |
| 27 | + os.Exit(1) |
| 28 | + } |
| 29 | + |
| 30 | + rep, err := parseCIReport(goTests) |
| 31 | + if err != nil { |
| 32 | + _, _ = fmt.Printf("error parsing ci report: %v", err) |
| 33 | + os.Exit(1) |
| 34 | + } |
| 35 | + |
| 36 | + err = printCIReport(os.Stdout, rep) |
| 37 | + if err != nil { |
| 38 | + _, _ = fmt.Printf("error printing report: %v", err) |
| 39 | + os.Exit(1) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func parseGoTestJSON(name string) (GotestsumReport, error) { |
| 44 | + f, err := os.Open(name) |
| 45 | + if err != nil { |
| 46 | + return GotestsumReport{}, xerrors.Errorf("error opening gotestsum json file: %w", err) |
| 47 | + } |
| 48 | + defer f.Close() |
| 49 | + |
| 50 | + dec := json.NewDecoder(f) |
| 51 | + var report GotestsumReport |
| 52 | + for { |
| 53 | + var e GotestsumReportEntry |
| 54 | + err = dec.Decode(&e) |
| 55 | + if errors.Is(err, io.EOF) { |
| 56 | + break |
| 57 | + } |
| 58 | + if err != nil { |
| 59 | + return GotestsumReport{}, xerrors.Errorf("error decoding json: %w", err) |
| 60 | + } |
| 61 | + e.Package = strings.TrimPrefix(e.Package, "github.com/coder/coder/") |
| 62 | + report = append(report, e) |
| 63 | + } |
| 64 | + |
| 65 | + return report, nil |
| 66 | +} |
| 67 | + |
| 68 | +func parseCIReport(report GotestsumReport) (CIReport, error) { |
| 69 | + packagesSortedByName := []string{} |
| 70 | + packageTimes := map[string]float64{} |
| 71 | + packageFail := map[string]int{} |
| 72 | + packageSkip := map[string]bool{} |
| 73 | + testTimes := map[string]float64{} |
| 74 | + testSkip := map[string]bool{} |
| 75 | + testOutput := map[string]string{} |
| 76 | + testSortedByName := []string{} |
| 77 | + timeouts := map[string]string{} |
| 78 | + timeoutRunningTests := map[string]bool{} |
| 79 | + for i, e := range report { |
| 80 | + switch e.Action { |
| 81 | + // A package/test may fail or pass. |
| 82 | + case Fail: |
| 83 | + if e.Test == "" { |
| 84 | + packageTimes[e.Package] = *e.Elapsed |
| 85 | + } else { |
| 86 | + packageFail[e.Package]++ |
| 87 | + name := e.Package + "." + e.Test |
| 88 | + testTimes[name] = *e.Elapsed |
| 89 | + } |
| 90 | + case Pass: |
| 91 | + if e.Test == "" { |
| 92 | + packageTimes[e.Package] = *e.Elapsed |
| 93 | + } else { |
| 94 | + name := e.Package + "." + e.Test |
| 95 | + delete(testOutput, name) |
| 96 | + testTimes[name] = *e.Elapsed |
| 97 | + } |
| 98 | + |
| 99 | + // Gather all output (deleted when irrelevant). |
| 100 | + case Output: |
| 101 | + name := e.Package + "." + e.Test // May be pkg.Test or pkg. |
| 102 | + if _, ok := timeouts[name]; ok || strings.HasPrefix(e.Output, "panic: test timed out") { |
| 103 | + timeouts[name] += e.Output |
| 104 | + continue |
| 105 | + } |
| 106 | + if e.Test != "" { |
| 107 | + name := e.Package + "." + e.Test |
| 108 | + testOutput[name] += e.Output |
| 109 | + } |
| 110 | + |
| 111 | + // Packages start, tests run and either may be skipped. |
| 112 | + case Start: |
| 113 | + packagesSortedByName = append(packagesSortedByName, e.Package) |
| 114 | + case Run: |
| 115 | + name := e.Package + "." + e.Test |
| 116 | + testSortedByName = append(testSortedByName, name) |
| 117 | + case Skip: |
| 118 | + if e.Test == "" { |
| 119 | + packageSkip[e.Package] = true |
| 120 | + } else { |
| 121 | + name := e.Package + "." + e.Test |
| 122 | + testSkip[name] = true |
| 123 | + delete(testOutput, name) |
| 124 | + } |
| 125 | + |
| 126 | + // Ignore. |
| 127 | + case Cont: |
| 128 | + case Pause: |
| 129 | + |
| 130 | + default: |
| 131 | + return CIReport{}, xerrors.Errorf("unknown action: %v in entry %d (%v)", e.Action, i, e) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // Normalize timeout from "pkg." or "pkg.Test" to "pkg". |
| 136 | + timeoutsNorm := make(map[string]string) |
| 137 | + for k, v := range timeouts { |
| 138 | + names := strings.SplitN(k, ".", 2) |
| 139 | + pkg := names[0] |
| 140 | + if _, ok := timeoutsNorm[pkg]; ok { |
| 141 | + panic("multiple timeouts for package: " + pkg) |
| 142 | + } |
| 143 | + timeoutsNorm[pkg] = v |
| 144 | + |
| 145 | + // Mark all running tests as timed out. |
| 146 | + // panic: test timed out after 2s\nrunning tests:\n\tTestAgent_Session_TTY_Hushlogin (0s)\n\n ... |
| 147 | + parts := strings.SplitN(v, "\n", 3) |
| 148 | + if len(parts) == 3 && strings.HasPrefix(parts[1], "running tests:") { |
| 149 | + s := bufio.NewScanner(strings.NewReader(parts[2])) |
| 150 | + for s.Scan() { |
| 151 | + name := s.Text() |
| 152 | + if !strings.HasPrefix(name, "\tTest") { |
| 153 | + break |
| 154 | + } |
| 155 | + name = strings.TrimPrefix(name, "\t") |
| 156 | + name = strings.SplitN(name, " ", 2)[0] |
| 157 | + timeoutRunningTests[pkg+"."+name] = true |
| 158 | + packageFail[pkg]++ |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + timeouts = timeoutsNorm |
| 163 | + |
| 164 | + sortAZ := func(a, b string) bool { return a < b } |
| 165 | + slices.SortFunc(packagesSortedByName, sortAZ) |
| 166 | + slices.SortFunc(testSortedByName, sortAZ) |
| 167 | + |
| 168 | + var rep CIReport |
| 169 | + |
| 170 | + for _, pkg := range packagesSortedByName { |
| 171 | + output, timeout := timeouts[pkg] |
| 172 | + rep.Packages = append(rep.Packages, PackageReport{ |
| 173 | + Name: pkg, |
| 174 | + Time: packageTimes[pkg], |
| 175 | + Skip: packageSkip[pkg], |
| 176 | + Fail: packageFail[pkg] > 0, |
| 177 | + Timeout: timeout, |
| 178 | + Output: output, |
| 179 | + NumFailed: packageFail[pkg], |
| 180 | + }) |
| 181 | + } |
| 182 | + |
| 183 | + for _, test := range testSortedByName { |
| 184 | + names := strings.SplitN(test, ".", 2) |
| 185 | + skip := testSkip[test] |
| 186 | + out, fail := testOutput[test] |
| 187 | + rep.Tests = append(rep.Tests, TestReport{ |
| 188 | + Package: names[0], |
| 189 | + Name: names[1], |
| 190 | + Time: testTimes[test], |
| 191 | + Skip: skip, |
| 192 | + Fail: fail, |
| 193 | + Timeout: timeoutRunningTests[test], |
| 194 | + Output: out, |
| 195 | + }) |
| 196 | + } |
| 197 | + |
| 198 | + return rep, nil |
| 199 | +} |
| 200 | + |
| 201 | +func printCIReport(dst io.Writer, rep CIReport) error { |
| 202 | + enc := json.NewEncoder(dst) |
| 203 | + enc.SetIndent("", " ") |
| 204 | + err := enc.Encode(rep) |
| 205 | + if err != nil { |
| 206 | + return xerrors.Errorf("error encoding json: %w", err) |
| 207 | + } |
| 208 | + return nil |
| 209 | +} |
| 210 | + |
| 211 | +type CIReport struct { |
| 212 | + Packages []PackageReport `json:"packages"` |
| 213 | + Tests []TestReport `json:"tests"` |
| 214 | +} |
| 215 | + |
| 216 | +type PackageReport struct { |
| 217 | + Name string `json:"name"` |
| 218 | + Time float64 `json:"time"` |
| 219 | + Skip bool `json:"skip,omitempty"` |
| 220 | + Fail bool `json:"fail,omitempty"` |
| 221 | + NumFailed int `json:"num_failed,omitempty"` |
| 222 | + Timeout bool `json:"timeout,omitempty"` |
| 223 | + Output string `json:"output,omitempty"` // Output present e.g. for timeout. |
| 224 | +} |
| 225 | + |
| 226 | +type TestReport struct { |
| 227 | + Package string `json:"package"` |
| 228 | + Name string `json:"name"` |
| 229 | + Time float64 `json:"time"` |
| 230 | + Skip bool `json:"skip,omitempty"` |
| 231 | + Fail bool `json:"fail,omitempty"` |
| 232 | + Timeout bool `json:"timeout,omitempty"` |
| 233 | + Output string `json:"output,omitempty"` |
| 234 | +} |
| 235 | + |
| 236 | +type GotestsumReport []GotestsumReportEntry |
| 237 | + |
| 238 | +type GotestsumReportEntry struct { |
| 239 | + Time time.Time `json:"Time"` |
| 240 | + Action Action `json:"Action"` |
| 241 | + Package string `json:"Package"` |
| 242 | + Test string `json:"Test,omitempty"` |
| 243 | + Output string `json:"Output,omitempty"` |
| 244 | + Elapsed *float64 `json:"Elapsed,omitempty"` |
| 245 | +} |
| 246 | + |
| 247 | +type Action string |
| 248 | + |
| 249 | +const ( |
| 250 | + Cont Action = "cont" |
| 251 | + Fail Action = "fail" |
| 252 | + Output Action = "output" |
| 253 | + Pass Action = "pass" |
| 254 | + Pause Action = "pause" |
| 255 | + Run Action = "run" |
| 256 | + Skip Action = "skip" |
| 257 | + Start Action = "start" |
| 258 | +) |
0 commit comments