|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "sort" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +// models single line from "go test -json" output |
| 13 | +type RawTestStep struct { |
| 14 | + Time time.Time `json:"Time"` |
| 15 | + Action string `json:"Action"` |
| 16 | + Package string `json:"Package"` |
| 17 | + Test string `json:"Test"` |
| 18 | + Output string `json:"Output"` |
| 19 | + Elapsed float32 `json:"Elapsed"` |
| 20 | +} |
| 21 | + |
| 22 | +// models full summary of a test run from "go test -json" |
| 23 | +type TestRun struct { |
| 24 | + CommitSha string `json:"commit_sha"` |
| 25 | + CommitDate time.Time `json:"commit_date"` |
| 26 | + JobRunDate time.Time `json:"job_run_date"` |
| 27 | + PackageResults []PackageResult `json:"results"` |
| 28 | +} |
| 29 | + |
| 30 | +// save TestRun to local JSON file |
| 31 | +func (testRun *TestRun) save(fileName string) { |
| 32 | + testRunBytes, err := json.MarshalIndent(testRun, "", " ") |
| 33 | + |
| 34 | + if err != nil { |
| 35 | + panic("error marshalling json" + err.Error()) |
| 36 | + } |
| 37 | + |
| 38 | + file, err := os.Create(fileName) |
| 39 | + if err != nil { |
| 40 | + panic("error creating filename: " + err.Error()) |
| 41 | + } |
| 42 | + defer file.Close() |
| 43 | + |
| 44 | + _, err = file.Write(testRunBytes) |
| 45 | + if err != nil { |
| 46 | + panic("error saving test run to file: " + err.Error()) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// models test result of an entire package which can have multiple tests |
| 51 | +type PackageResult struct { |
| 52 | + Package string `json:"package"` |
| 53 | + Result string `json:"result"` |
| 54 | + Elapsed float32 `json:"elapsed"` |
| 55 | + Output []string `json:"output"` |
| 56 | + Tests []TestResult `json:"tests"` |
| 57 | + TestMap map[string][]TestResult `json:"-"` |
| 58 | +} |
| 59 | + |
| 60 | +// models result of a single test that's part of a larger package result |
| 61 | +type TestResult struct { |
| 62 | + Test string `json:"test"` |
| 63 | + Package string `json:"package"` |
| 64 | + Output []string `json:"output"` |
| 65 | + Result string `json:"result"` |
| 66 | + Elapsed float32 `json:"elapsed"` |
| 67 | +} |
| 68 | + |
| 69 | +// this interface gives us the flexibility to read test results in multiple ways - from stdin (for production) and from a local file (for testing) |
| 70 | +type ResultReader interface { |
| 71 | + getReader() *os.File |
| 72 | + close() |
| 73 | + |
| 74 | + // where to save results - will be different for tests vs production |
| 75 | + getResultsFileName() string |
| 76 | +} |
| 77 | + |
| 78 | +type StdinResultReader struct { |
| 79 | +} |
| 80 | + |
| 81 | +// return reader for reading from stdin - for production |
| 82 | +func (stdinResultReader StdinResultReader) getReader() *os.File { |
| 83 | + return os.Stdin |
| 84 | +} |
| 85 | + |
| 86 | +// nothing to close when reading from stdin |
| 87 | +func (stdinResultReader StdinResultReader) close() { |
| 88 | +} |
| 89 | + |
| 90 | +func (stdinResultReader StdinResultReader) getResultsFileName() string { |
| 91 | + return os.Args[1] |
| 92 | +} |
| 93 | + |
| 94 | +func processTestRun(resultReader ResultReader) TestRun { |
| 95 | + reader := resultReader.getReader() |
| 96 | + scanner := bufio.NewScanner(reader) |
| 97 | + |
| 98 | + defer resultReader.close() |
| 99 | + |
| 100 | + packageResultMap := processTestRunLineByLine(scanner) |
| 101 | + |
| 102 | + err := scanner.Err() |
| 103 | + if err != nil { |
| 104 | + panic("error returning EOF for scanner: " + err.Error()) |
| 105 | + } |
| 106 | + |
| 107 | + postProcessTestRun(packageResultMap) |
| 108 | + |
| 109 | + testRun := finalizeTestRun(packageResultMap) |
| 110 | + testRun.save(resultReader.getResultsFileName()) |
| 111 | + |
| 112 | + return testRun |
| 113 | +} |
| 114 | + |
| 115 | +// Raw JSON result step from `go test -json` execution |
| 116 | +// Sequence of result steps (specified by Action value) per test: |
| 117 | +// 1. run (once) |
| 118 | +// 2. output (one to many) |
| 119 | +// 3. pause (zero or once) - for tests using t.Parallel() |
| 120 | +// 4. cont (zero or once) - for tests using t.Parallel() |
| 121 | +// 5. pass OR fail OR skip (once) |
| 122 | +func processTestRunLineByLine(scanner *bufio.Scanner) map[string]*PackageResult { |
| 123 | + packageResultMap := make(map[string]*PackageResult) |
| 124 | + // reuse the same package result over and over |
| 125 | + for scanner.Scan() { |
| 126 | + var rawTestStep RawTestStep |
| 127 | + err := json.Unmarshal(scanner.Bytes(), &rawTestStep) |
| 128 | + if err != nil { |
| 129 | + panic("error unmarshalling raw test step: " + err.Error()) |
| 130 | + } |
| 131 | + |
| 132 | + // check if package result exists to hold test results |
| 133 | + packageResult, packageResultExists := packageResultMap[rawTestStep.Package] |
| 134 | + if !packageResultExists { |
| 135 | + packageResult = &PackageResult{ |
| 136 | + Package: rawTestStep.Package, |
| 137 | + |
| 138 | + // package result will hold map of test results |
| 139 | + TestMap: make(map[string][]TestResult), |
| 140 | + |
| 141 | + // store outputs as a slice of strings - that's how "go test -json" outputs each output string on a separate line |
| 142 | + // there are usually 2 or more outputs for a package |
| 143 | + Output: make([]string, 0), |
| 144 | + } |
| 145 | + packageResultMap[rawTestStep.Package] = packageResult |
| 146 | + } |
| 147 | + |
| 148 | + // most raw test steps will have Test value - only package specific steps won't |
| 149 | + if rawTestStep.Test != "" { |
| 150 | + |
| 151 | + // "run" is the very first test step and it needs special treatment - to create all the data structures that will be used by subsequent test steps for the same test |
| 152 | + if rawTestStep.Action == "run" { |
| 153 | + var newTestResult TestResult |
| 154 | + newTestResult.Test = rawTestStep.Test |
| 155 | + newTestResult.Package = rawTestStep.Package |
| 156 | + |
| 157 | + // store outputs as a slice of strings - that's how "go test -json" outputs each output string on a separate line |
| 158 | + // for passing tests, there are usually 2 outputs for a passing test and more outputs for a failing test |
| 159 | + newTestResult.Output = make([]string, 0) |
| 160 | + |
| 161 | + // append to test result slice, whether it's the first or subsequent test result |
| 162 | + packageResult.TestMap[rawTestStep.Test] = append(packageResult.TestMap[rawTestStep.Test], newTestResult) |
| 163 | + continue |
| 164 | + } |
| 165 | + |
| 166 | + lastTestResultIndex := len(packageResult.TestMap[rawTestStep.Test]) - 1 |
| 167 | + if lastTestResultIndex < 0 { |
| 168 | + lastTestResultIndex = 0 |
| 169 | + } |
| 170 | + |
| 171 | + testResults, ok := packageResult.TestMap[rawTestStep.Test] |
| 172 | + if !ok { |
| 173 | + panic(fmt.Sprintf("no test result for test %s", rawTestStep.Test)) |
| 174 | + } |
| 175 | + lastTestResultPointer := &testResults[lastTestResultIndex] |
| 176 | + |
| 177 | + // subsequent raw json outputs will have different data about the test - whether it passed/failed, what the test output was, etc |
| 178 | + switch rawTestStep.Action { |
| 179 | + case "output": |
| 180 | + lastTestResultPointer.Output = append(lastTestResultPointer.Output, rawTestStep.Output) |
| 181 | + |
| 182 | + case "pass", "fail", "skip": |
| 183 | + lastTestResultPointer.Result = rawTestStep.Action |
| 184 | + lastTestResultPointer.Elapsed = rawTestStep.Elapsed |
| 185 | + |
| 186 | + case "pause", "cont": |
| 187 | + // tests using t.Parallel() will have these values |
| 188 | + // nothing to do - test will continue to run normally and have a pass/fail result at the end |
| 189 | + |
| 190 | + default: |
| 191 | + panic(fmt.Sprintf("unexpected action: %s", rawTestStep.Action)) |
| 192 | + } |
| 193 | + |
| 194 | + } else { |
| 195 | + // package level raw messages won't have a Test value |
| 196 | + switch rawTestStep.Action { |
| 197 | + case "output": |
| 198 | + packageResult.Output = append(packageResult.Output, rawTestStep.Output) |
| 199 | + case "pass", "fail", "skip": |
| 200 | + packageResult.Result = rawTestStep.Action |
| 201 | + packageResult.Elapsed = rawTestStep.Elapsed |
| 202 | + default: |
| 203 | + panic(fmt.Sprintf("unexpected action (package): %s", rawTestStep.Action)) |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + return packageResultMap |
| 208 | +} |
| 209 | + |
| 210 | +func postProcessTestRun(packageResultMap map[string]*PackageResult) { |
| 211 | + // transfer each test result map in each package result to a test result slice |
| 212 | + for packageName, packageResult := range packageResultMap { |
| 213 | + |
| 214 | + // delete skipped packages since they don't have any tests - won't be adding it to result map |
| 215 | + if packageResult.Result == "skip" { |
| 216 | + delete(packageResultMap, packageName) |
| 217 | + continue |
| 218 | + } |
| 219 | + |
| 220 | + for _, testResults := range packageResult.TestMap { |
| 221 | + packageResult.Tests = append(packageResult.Tests, testResults...) |
| 222 | + } |
| 223 | + |
| 224 | + // clear test result map once all values transfered to slice - needed for testing so will check against an empty map |
| 225 | + for k := range packageResultMap[packageName].TestMap { |
| 226 | + delete(packageResultMap[packageName].TestMap, k) |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + // sort all the test results in each package result slice - needed for testing so it's easy to compare ordered tests |
| 231 | + for _, pr := range packageResultMap { |
| 232 | + sort.SliceStable(pr.Tests, func(i, j int) bool { |
| 233 | + return pr.Tests[i].Test < pr.Tests[j].Test |
| 234 | + }) |
| 235 | + } |
| 236 | +} |
| 237 | + |
| 238 | +func finalizeTestRun(packageResultMap map[string]*PackageResult) TestRun { |
| 239 | + commitSha := os.Getenv("COMMIT_SHA") |
| 240 | + if commitSha == "" { |
| 241 | + panic("COMMIT_SHA can't be empty") |
| 242 | + } |
| 243 | + |
| 244 | + commitDate, err := time.Parse(time.RFC3339, os.Getenv("COMMIT_DATE")) |
| 245 | + if err != nil { |
| 246 | + panic("error parsing COMMIT_DATE: " + err.Error()) |
| 247 | + } |
| 248 | + |
| 249 | + jobStarted, err := time.Parse(time.RFC3339, os.Getenv("JOB_STARTED")) |
| 250 | + if err != nil { |
| 251 | + panic("error parsing JOB_STARTED: " + err.Error()) |
| 252 | + } |
| 253 | + |
| 254 | + var testRun TestRun |
| 255 | + testRun.CommitDate = commitDate.UTC() |
| 256 | + testRun.CommitSha = commitSha |
| 257 | + testRun.JobRunDate = jobStarted.UTC() |
| 258 | + |
| 259 | + // add all the package results to the test run |
| 260 | + for _, pr := range packageResultMap { |
| 261 | + testRun.PackageResults = append(testRun.PackageResults, *pr) |
| 262 | + } |
| 263 | + |
| 264 | + // sort all package results in the test run |
| 265 | + sort.SliceStable(testRun.PackageResults, func(i, j int) bool { |
| 266 | + return testRun.PackageResults[i].Package < testRun.PackageResults[j].Package |
| 267 | + }) |
| 268 | + |
| 269 | + return testRun |
| 270 | +} |
| 271 | + |
| 272 | +func main() { |
| 273 | + processTestRun(StdinResultReader{}) |
| 274 | +} |
0 commit comments