Skip to content

Commit f5495cd

Browse files
committed
chore: Fix some linter errors
1 parent e7d058d commit f5495cd

3 files changed

Lines changed: 81 additions & 52 deletions

File tree

ctrf/ctrf.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ func (summary *Summary) Validate() []error {
192192
testsSum := summary.Passed + summary.Failed + summary.Pending + summary.Skipped + summary.Other + summary.Flaky
193193
if summary.Tests != testsSum {
194194
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))
195-
196195
}
197196
return errs
198197
}

reporter/reporter.go

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ type TestEvent struct {
2323
Output string
2424
}
2525

26+
const (
27+
ActionBuildOutput = "build-output"
28+
ActionBuildFail = "build-fail"
29+
ActionOutput = "output"
30+
ActionRun = "run"
31+
ActionPass = "pass"
32+
ActionFail = "fail"
33+
ActionSkip = "skip"
34+
)
35+
2636
var buildOutput []string
2737

2838
func ParseTestResults(r io.Reader, verbose bool, env *ctrf.Environment) (*ctrf.Report, error) {
@@ -44,15 +54,15 @@ func ParseTestResults(r io.Reader, verbose bool, env *ctrf.Environment) (*ctrf.R
4454
testEvents = append(testEvents, event)
4555

4656
if verbose {
47-
if event.Action == "build-output" || event.Action == "output" {
57+
if event.Action == ActionBuildOutput || event.Action == ActionOutput {
4858
fmt.Print(event.Output)
4959
}
5060
}
5161
}
5262

5363
for i, event := range testEvents {
5464

55-
if event.Action == "build-output" || event.Action == "build-fail" || event.Action == "fail" {
65+
if event.Action == ActionBuildOutput || event.Action == ActionBuildFail || event.Action == ActionFail {
5666
if report.Results.Extra == nil {
5767
report.Results.Extra = make(map[string]any)
5868
}
@@ -61,13 +71,13 @@ func ParseTestResults(r io.Reader, verbose bool, env *ctrf.Environment) (*ctrf.R
6171
return nil, fmt.Errorf("expected a map, but got %T instead", report.Results.Extra)
6272
}
6373

64-
if event.Action == "fail" {
74+
if event.Action == ActionFail {
6575
if _, ok := extraMap["FailedBuild"]; !ok {
6676
extraMap["FailedBuild"] = true
6777
}
6878
}
6979

70-
if event.Action == "build-output" {
80+
if event.Action == ActionBuildOutput {
7181
if _, ok := extraMap["buildOutput"]; !ok {
7282
extraMap["buildOutput"] = []TestEvent{}
7383
}
@@ -77,7 +87,7 @@ func ParseTestResults(r io.Reader, verbose bool, env *ctrf.Environment) (*ctrf.R
7787
continue
7888
}
7989

80-
if event.Action == "build-fail" {
90+
if event.Action == ActionBuildFail {
8191
if _, ok := extraMap["buildFail"]; !ok {
8292
extraMap["buildFail"] = []TestEvent{}
8393
}
@@ -87,7 +97,7 @@ func ParseTestResults(r io.Reader, verbose bool, env *ctrf.Environment) (*ctrf.R
8797
}
8898
}
8999

90-
if event.Action == "output" {
100+
if event.Action == ActionOutput {
91101
buildOutput = append(buildOutput, event.Output)
92102
}
93103

@@ -107,14 +117,14 @@ func ParseTestResults(r io.Reader, verbose bool, env *ctrf.Environment) (*ctrf.R
107117

108118
// If this is a "run" event, record the start time of the test. We'll look this up later when
109119
// we process the "pass"/"fail"/"skip" event for the test to create the TestResult
110-
if event.Action == "run" {
120+
if event.Action == ActionRun {
111121
testStartTimes[testNameKey(event.Package, event.Test)] = eventTime
112122
}
113123
}
114124

115125
// From this point on, we only deal with pass, fail, and skip events, which indicate that the
116126
// test has completed, and we can create/update a TestResult for it.
117-
if event.Action == "pass" || event.Action == "fail" || event.Action == "skip" {
127+
if event.Action == ActionPass || event.Action == ActionFail || event.Action == ActionSkip {
118128
// Look up the start time, and use this event's time as the endTime, to mark the start/stop times
119129
// for the test result. Duration we get from the event.Elapsed field, which better takes into
120130
// account parallel tests, setup/teardown time, etc...
@@ -127,7 +137,7 @@ func ParseTestResults(r io.Reader, verbose bool, env *ctrf.Environment) (*ctrf.R
127137
// Determine the message for this test result. We only include messages on failures though,
128138
// per the CTRF spec, so if this is not a failure, we pass an empty string for the message.
129139
message := ""
130-
if event.Action == "fail" {
140+
if event.Action == ActionFail {
131141
message = getMessagesForTest(testEvents, i, event.Package, event.Test, startTime)
132142
}
133143

@@ -158,7 +168,7 @@ func ParseTestResults(r io.Reader, verbose bool, env *ctrf.Environment) (*ctrf.R
158168
return report, nil
159169
}
160170

161-
// addResult adds a new test result to the report, filling out all the relevant details
171+
// addResult adds a new test result to the report, filling out all the relevant details.
162172
func addResult(report *ctrf.Report, result *ctrf.TestResult) {
163173
// Update the overall test count in the Summary
164174
report.Results.Summary.Tests++
@@ -171,59 +181,63 @@ func addResult(report *ctrf.Report, result *ctrf.TestResult) {
171181
report.Results.Summary.Failed++
172182
case ctrf.TestSkipped:
173183
report.Results.Summary.Skipped++
184+
case ctrf.TestPending:
185+
report.Results.Summary.Pending++
186+
default:
187+
report.Results.Summary.Other++
174188
}
175189

176190
// Append the result to the report's results
177191
report.Results.Tests = append(report.Results.Tests, result)
178192
}
179193

180-
func updateResult(report *ctrf.Report, existing, new *ctrf.TestResult) {
194+
func updateResult(report *ctrf.Report, oldResult, newResult *ctrf.TestResult) {
181195
// If the existing result does not have a retries field, initialize it, and move the
182196
// results to the first RetryAttempts object
183-
if existing.RetryAttempts == nil {
184-
existing.Retries = 1
185-
existing.RetryAttempts = append(existing.RetryAttempts, ctrf.RetryAttempt{
197+
if oldResult.RetryAttempts == nil {
198+
oldResult.Retries = 1
199+
oldResult.RetryAttempts = append(oldResult.RetryAttempts, ctrf.RetryAttempt{
186200
Attempt: 1,
187-
Status: existing.Status,
188-
Message: existing.Message,
189-
Duration: existing.Duration,
190-
Start: existing.Start,
191-
Stop: existing.Stop,
201+
Status: oldResult.Status,
202+
Message: oldResult.Message,
203+
Duration: oldResult.Duration,
204+
Start: oldResult.Start,
205+
Stop: oldResult.Stop,
192206
})
193207
}
194208

195209
// If this is a pass after a failure, mark the test as flaky, not failed,
196210
// and update the summary counts accordingly
197-
if existing.Status == ctrf.TestFailed && new.Status == ctrf.TestPassed {
198-
existing.Flaky = true
211+
if oldResult.Status == ctrf.TestFailed && newResult.Status == ctrf.TestPassed {
212+
oldResult.Flaky = true
199213
report.Results.Summary.Flaky++
200214
report.Results.Summary.Failed--
201215
}
202216

203217
// Update the overall test status to match that of the new result
204-
existing.Status = new.Status
218+
oldResult.Status = newResult.Status
205219

206220
// Clear out the top-level message on the overall result, since the messages are in the retries
207-
existing.Message = ""
221+
oldResult.Message = ""
208222

209223
// Update the times of the overall test result
210-
existing.Duration += new.Duration
211-
if new.Stop > existing.Stop {
212-
existing.Stop = new.Stop
224+
oldResult.Duration += newResult.Duration
225+
if newResult.Stop > oldResult.Stop {
226+
oldResult.Stop = newResult.Stop
213227
}
214-
if new.Start < existing.Start {
215-
existing.Start = new.Start
228+
if newResult.Start < oldResult.Start {
229+
oldResult.Start = newResult.Start
216230
}
217231

218232
// Now add the new attempt to the retries
219-
existing.Retries++
220-
existing.RetryAttempts = append(existing.RetryAttempts, ctrf.RetryAttempt{
221-
Attempt: existing.Retries,
222-
Status: new.Status,
223-
Message: new.Message,
224-
Duration: new.Duration,
225-
Start: new.Start,
226-
Stop: new.Stop,
233+
oldResult.Retries++
234+
oldResult.RetryAttempts = append(oldResult.RetryAttempts, ctrf.RetryAttempt{
235+
Attempt: oldResult.Retries,
236+
Status: newResult.Status,
237+
Message: newResult.Message,
238+
Duration: newResult.Duration,
239+
Start: newResult.Start,
240+
Stop: newResult.Stop,
227241
})
228242
}
229243

@@ -234,11 +248,11 @@ func testNameKey(suite, name string) string {
234248

235249
func actionToTestResult(action string) ctrf.TestStatus {
236250
switch action {
237-
case "pass":
251+
case ActionPass:
238252
return ctrf.TestPassed
239-
case "fail":
253+
case ActionFail:
240254
return ctrf.TestFailed
241-
case "skip":
255+
case ActionSkip:
242256
return ctrf.TestSkipped
243257
default:
244258
return ctrf.TestOther
@@ -310,7 +324,7 @@ func getMessagesForTest(testEvents []TestEvent, index int, packageName, testName
310324
}
311325
}
312326

313-
if testEvents[i].Action == "output" {
327+
if testEvents[i].Action == ActionOutput {
314328
messages = append(messages, testEvents[i].Output)
315329
}
316330
}

reporter/reporter_test.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ func Test_Enrich_Reporter(t *testing.T) {
2121
Stop: 1740874081832,
2222
},
2323
}}}
24+
25+
//nolint:lll // The test inputs are raw strings taken from real test runs
2426
input := `{"Time":"2025-03-02T01:08:01.832222033+01:00","Action":"start","Package":"github.com/ctrf-io/go-ctrf-json-reporter/reporter"}
2527
{"Time":"2025-03-02T01:08:01.832309292+01:00","Action":"run","Package":"github.com/ctrf-io/go-ctrf-json-reporter/reporter","Test":"Test_Enrich_Reporter"}
2628
{"Time":"2025-03-02T01:08:01.832321979+01:00","Action":"output","Package":"github.com/ctrf-io/go-ctrf-json-reporter/reporter","Test":"Test_Enrich_Reporter","Output":"=== RUN Test_Enrich_Reporter\n"}
@@ -105,6 +107,8 @@ func Test_Enrich_ReporterWithUnorderedMessages(t *testing.T) {
105107
Stop: 1760718477126,
106108
},
107109
}}}
110+
111+
//nolint:lll // The test inputs are raw strings taken from real test runs
108112
input := `{"Time":"2025-10-17T12:27:57.126761-04:00","Action":"run","Package":"github.com/ctrf-io/go-ctrf-json-reporter/reporter","Test":"Test_Enrich_Reporter"}
109113
{"Time":"2025-10-17T12:27:57.126764-04:00","Action":"output","Package":"github.com/ctrf-io/go-ctrf-json-reporter/reporter","Test":"Test_Enrich_Reporter","Output":"=== RUN Test_Enrich_Reporter\n"}
110114
{"Time":"2025-10-17T12:27:57.126769-04:00","Action":"run","Package":"github.com/ctrf-io/go-ctrf-json-reporter/reporter","Test":"Test_Enrich_Reporter/Test1"}
@@ -177,12 +181,18 @@ func TestDetectFlakyTests(t *testing.T) {
177181
Duration: 150,
178182
Message: "",
179183
RetryAttempts: []ctrf.RetryAttempt{
180-
{Attempt: 1, Status: ctrf.TestFailed, Start: 1775245677863, Stop: 1775245677914, Duration: 50,
181-
Message: "=== RUN Test_Flaky_Fail\n flaky_test.go:21: This test is designed to fail.\n--- FAIL: Test_Flaky_Fail (0.05s)\n"},
182-
{Attempt: 2, Status: ctrf.TestFailed, Start: 1775245678350, Stop: 1775245678401, Duration: 50,
183-
Message: "=== RUN Test_Flaky_Fail\n flaky_test.go:21: This test is designed to fail.\n--- FAIL: Test_Flaky_Fail (0.05s)\n"},
184-
{Attempt: 3, Status: ctrf.TestFailed, Start: 1775245679196, Stop: 1775245679247, Duration: 50,
185-
Message: "=== RUN Test_Flaky_Fail\n flaky_test.go:21: This test is designed to fail.\n--- FAIL: Test_Flaky_Fail (0.05s)\n"},
184+
{
185+
Attempt: 1, Status: ctrf.TestFailed, Start: 1775245677863, Stop: 1775245677914, Duration: 50,
186+
Message: "=== RUN Test_Flaky_Fail\n flaky_test.go:21: This test is designed to fail.\n--- FAIL: Test_Flaky_Fail (0.05s)\n",
187+
},
188+
{
189+
Attempt: 2, Status: ctrf.TestFailed, Start: 1775245678350, Stop: 1775245678401, Duration: 50,
190+
Message: "=== RUN Test_Flaky_Fail\n flaky_test.go:21: This test is designed to fail.\n--- FAIL: Test_Flaky_Fail (0.05s)\n",
191+
},
192+
{
193+
Attempt: 3, Status: ctrf.TestFailed, Start: 1775245679196, Stop: 1775245679247, Duration: 50,
194+
Message: "=== RUN Test_Flaky_Fail\n flaky_test.go:21: This test is designed to fail.\n--- FAIL: Test_Flaky_Fail (0.05s)\n",
195+
},
186196
},
187197
},
188198
{
@@ -204,15 +214,21 @@ func TestDetectFlakyTests(t *testing.T) {
204214
Start: 1775245677914,
205215
Stop: 1775245679646,
206216
RetryAttempts: []ctrf.RetryAttempt{
207-
{Attempt: 1, Status: ctrf.TestFailed, Duration: 50, Start: 1775245677914, Stop: 1775245677967,
208-
Message: "=== RUN Test_Flaky_Flaky\n flaky_test.go:37: Flaky Failure (attempt 1)\n--- FAIL: Test_Flaky_Flaky (0.05s)\n"},
209-
{Attempt: 2, Status: ctrf.TestFailed, Duration: 50, Start: 1775245678784, Stop: 1775245678837,
210-
Message: "=== RUN Test_Flaky_Flaky\n flaky_test.go:54: Flaky Failure (attempt 2)\n--- FAIL: Test_Flaky_Flaky (0.05s)\n"},
217+
{
218+
Attempt: 1, Status: ctrf.TestFailed, Duration: 50, Start: 1775245677914, Stop: 1775245677967,
219+
Message: "=== RUN Test_Flaky_Flaky\n flaky_test.go:37: Flaky Failure (attempt 1)\n--- FAIL: Test_Flaky_Flaky (0.05s)\n",
220+
},
221+
{
222+
Attempt: 2, Status: ctrf.TestFailed, Duration: 50, Start: 1775245678784, Stop: 1775245678837,
223+
Message: "=== RUN Test_Flaky_Flaky\n flaky_test.go:54: Flaky Failure (attempt 2)\n--- FAIL: Test_Flaky_Flaky (0.05s)\n",
224+
},
211225
{Attempt: 3, Status: ctrf.TestPassed, Duration: 50, Start: 1775245679595, Stop: 1775245679646},
212226
},
213227
},
214-
}}}
228+
},
229+
}}
215230

231+
//nolint:lll // The test inputs are raw strings taken from real test runs
216232
input := `{"Time":"2026-04-03T13:47:57.561046-06:00","Action":"start","Package":"github.com/ctrf-io/go-ctrf-json-reporter/examples/flaky"}
217233
{"Time":"2026-04-03T13:47:57.812415-06:00","Action":"run","Package":"github.com/ctrf-io/go-ctrf-json-reporter/examples/flaky","Test":"Test_Flaky_Pass"}
218234
{"Time":"2026-04-03T13:47:57.812541-06:00","Action":"output","Package":"github.com/ctrf-io/go-ctrf-json-reporter/examples/flaky","Test":"Test_Flaky_Pass","Output":"=== RUN Test_Flaky_Pass\n"}

0 commit comments

Comments
 (0)