|
| 1 | +package ctrf |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +type Report struct { |
| 13 | + Results *Results `json:"results"` |
| 14 | +} |
| 15 | + |
| 16 | +func NewReport(toolName string, env *Environment) *Report { |
| 17 | + return &Report{ |
| 18 | + Results: &Results{ |
| 19 | + Tool: &Tool{Name: toolName}, |
| 20 | + Environment: env, |
| 21 | + Summary: &Summary{}, |
| 22 | + }, |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func (report *Report) ToJson() (string, error) { |
| 27 | + stringBuilder := &strings.Builder{} |
| 28 | + |
| 29 | + err := report.Write(stringBuilder, false) |
| 30 | + if err != nil { |
| 31 | + return "", err |
| 32 | + } |
| 33 | + |
| 34 | + return stringBuilder.String(), nil |
| 35 | +} |
| 36 | + |
| 37 | +func (report *Report) ToJsonPretty() (string, error) { |
| 38 | + stringBuilder := &strings.Builder{} |
| 39 | + |
| 40 | + err := report.Write(stringBuilder, true) |
| 41 | + if err != nil { |
| 42 | + return "", err |
| 43 | + } |
| 44 | + |
| 45 | + return stringBuilder.String(), nil |
| 46 | +} |
| 47 | + |
| 48 | +func (report *Report) Write(w io.Writer, pretty bool) error { |
| 49 | + if len(report.Validate()) > 0 { |
| 50 | + return errors.New("report is invalid") |
| 51 | + } |
| 52 | + encoder := json.NewEncoder(w) |
| 53 | + if pretty { |
| 54 | + encoder.SetIndent("", " ") |
| 55 | + } |
| 56 | + err := encoder.Encode(report) |
| 57 | + if err != nil { |
| 58 | + return fmt.Errorf("error writing ctrf json report: %v", err) |
| 59 | + } |
| 60 | + return nil |
| 61 | +} |
| 62 | + |
| 63 | +func (report *Report) WriteFile(filePath string) error { |
| 64 | + file, err := os.Create(filePath) |
| 65 | + if err != nil { |
| 66 | + return fmt.Errorf("error writing ctrf json report: %v", err) |
| 67 | + } |
| 68 | + defer func(file *os.File) { |
| 69 | + _ = file.Close() |
| 70 | + }(file) |
| 71 | + |
| 72 | + return report.Write(file, true) |
| 73 | +} |
| 74 | + |
| 75 | +func (report *Report) Validate() []error { |
| 76 | + results := report.Results |
| 77 | + if results == nil { |
| 78 | + return singleError("missing property 'results'") |
| 79 | + } |
| 80 | + |
| 81 | + var errs []error |
| 82 | + if results.Tool == nil { |
| 83 | + errs = append(errs, errors.New("missing property 'results.tool'")) |
| 84 | + } else { |
| 85 | + errs = append(errs, results.Tool.Validate()...) |
| 86 | + } |
| 87 | + if results.Summary == nil { |
| 88 | + errs = append(errs, errors.New("missing property 'results.summary'")) |
| 89 | + } else { |
| 90 | + errs = append(errs, results.Summary.Validate()...) |
| 91 | + } |
| 92 | + if results.Tests == nil { |
| 93 | + errs = append(errs, errors.New("missing property 'results.tests'")) |
| 94 | + } |
| 95 | + return errs |
| 96 | +} |
| 97 | + |
| 98 | +func singleError(errorMessage string) []error { |
| 99 | + return []error{errors.New(errorMessage)} |
| 100 | +} |
| 101 | + |
| 102 | +type Results struct { |
| 103 | + Tool *Tool `json:"tool"` |
| 104 | + Summary *Summary `json:"summary"` |
| 105 | + Tests []*TestResult `json:"tests"` |
| 106 | + Environment *Environment `json:"environment,omitempty"` |
| 107 | + Extra interface{} `json:"extra,omitempty"` |
| 108 | +} |
| 109 | + |
| 110 | +type Tool struct { |
| 111 | + Name string `json:"name"` |
| 112 | + Version string `json:"version,omitempty"` |
| 113 | + Extra interface{} `json:"extra,omitempty"` |
| 114 | +} |
| 115 | + |
| 116 | +func (tool *Tool) Validate() []error { |
| 117 | + if tool.Name == "" { |
| 118 | + return singleError("missing property 'results.tool.name'") |
| 119 | + } |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +type Summary struct { |
| 124 | + Tests int `json:"tests"` |
| 125 | + Passed int `json:"passed"` |
| 126 | + Failed int `json:"failed"` |
| 127 | + Pending int `json:"pending"` |
| 128 | + Skipped int `json:"skipped"` |
| 129 | + Other int `json:"other"` |
| 130 | + Suites int `json:"suites,omitempty"` |
| 131 | + Start int64 `json:"start"` |
| 132 | + Stop int64 `json:"stop"` |
| 133 | + Extra interface{} `json:"extra,omitempty"` |
| 134 | +} |
| 135 | + |
| 136 | +func (summary *Summary) Validate() []error { |
| 137 | + var errs []error |
| 138 | + if summary.Tests < 0 { |
| 139 | + errs = append(errs, errors.New("invalid property 'results.summary.tests'")) |
| 140 | + } |
| 141 | + if summary.Passed < 0 { |
| 142 | + errs = append(errs, errors.New("invalid property 'results.summary.passed'")) |
| 143 | + } |
| 144 | + if summary.Failed < 0 { |
| 145 | + errs = append(errs, errors.New("invalid property 'results.summary.failed'")) |
| 146 | + } |
| 147 | + if summary.Pending < 0 { |
| 148 | + errs = append(errs, errors.New("invalid property 'results.summary.pending'")) |
| 149 | + } |
| 150 | + if summary.Skipped < 0 { |
| 151 | + errs = append(errs, errors.New("invalid property 'results.summary.skipped'")) |
| 152 | + } |
| 153 | + if summary.Other < 0 { |
| 154 | + errs = append(errs, errors.New("invalid property 'results.summary.other'")) |
| 155 | + } |
| 156 | + if summary.Start < 0 { |
| 157 | + errs = append(errs, errors.New("invalid property 'results.summary.start'")) |
| 158 | + } |
| 159 | + if summary.Stop < 0 { |
| 160 | + errs = append(errs, errors.New("invalid property 'results.summary.stop'")) |
| 161 | + } |
| 162 | + if summary.Suites < 0 { |
| 163 | + errs = append(errs, errors.New("invalid property 'results.summary.suites'")) |
| 164 | + } |
| 165 | + if summary.Start > summary.Stop { |
| 166 | + errs = append(errs, errors.New("invalid summary timestamps: start can't be greater than stop")) |
| 167 | + } |
| 168 | + testsSum := summary.Passed + summary.Failed + summary.Pending + summary.Skipped + summary.Other |
| 169 | + if summary.Tests != testsSum { |
| 170 | + errs = append(errs, fmt.Errorf("invalid summary counts: tests (%d) must be the sum of passed, failed, pending, skipped, and other (%d)", summary.Tests, testsSum)) |
| 171 | + |
| 172 | + } |
| 173 | + return errs |
| 174 | +} |
| 175 | + |
| 176 | +type TestStatus string |
| 177 | + |
| 178 | +const ( |
| 179 | + TestPassed TestStatus = "passed" |
| 180 | + TestFailed TestStatus = "failed" |
| 181 | + TestSkipped TestStatus = "skipped" |
| 182 | + TestPending TestStatus = "pending" |
| 183 | + TestOther TestStatus = "other" |
| 184 | +) |
| 185 | + |
| 186 | +type TestResult struct { |
| 187 | + Name string `json:"name"` |
| 188 | + Status TestStatus `json:"status"` |
| 189 | + Duration float64 `json:"duration"` |
| 190 | + Start int64 `json:"start,omitempty"` |
| 191 | + Stop int64 `json:"stop,omitempty"` |
| 192 | + Suite string `json:"suite,omitempty"` |
| 193 | + Message string `json:"message,omitempty"` |
| 194 | + Trace string `json:"trace,omitempty"` |
| 195 | + RawStatus string `json:"rawStatus,omitempty"` |
| 196 | + Tags []string `json:"tags,omitempty"` |
| 197 | + Type string `json:"type,omitempty"` |
| 198 | + Filepath string `json:"filepath,omitempty"` |
| 199 | + Retry int `json:"retry,omitempty"` |
| 200 | + Flake bool `json:"flake,omitempty"` |
| 201 | + Browser string `json:"browser,omitempty"` |
| 202 | + Device string `json:"device,omitempty"` |
| 203 | + Screenshot string `json:"screenshot,omitempty"` |
| 204 | + Parameters interface{} `json:"parameters,omitempty"` |
| 205 | + Steps []interface{} `json:"steps,omitempty"` |
| 206 | + Extra interface{} `json:"extra,omitempty"` |
| 207 | +} |
| 208 | + |
| 209 | +type Environment struct { |
| 210 | + AppName string `json:"appName,omitempty"` |
| 211 | + AppVersion string `json:"appVersion,omitempty"` |
| 212 | + OSPlatform string `json:"osPlatform,omitempty"` |
| 213 | + OSRelease string `json:"osRelease,omitempty"` |
| 214 | + OSVersion string `json:"osVersion,omitempty"` |
| 215 | + BuildName string `json:"buildName,omitempty"` |
| 216 | + BuildNumber string `json:"buildNumber,omitempty"` |
| 217 | + Extra interface{} `json:"extra,omitempty"` |
| 218 | +} |
0 commit comments