Skip to content

Commit 9521365

Browse files
Merge pull request #16 from ctrf-io/fix/log-output
reporter: add build failure handling and output collection
2 parents 172ae16 + f2ae0bc commit 9521365

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

cmd/go-ctrf-json-reporter/main.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@ import (
88
"os"
99
)
1010

11+
var buildFailed bool
12+
1113
func main() {
1214
var outputFile string
1315
var verbose bool
16+
var quiet bool
1417
flag.BoolVar(&verbose, "verbose", false, "Enable verbose output")
1518
flag.BoolVar(&verbose, "v", false, "Enable verbose output (shorthand)")
19+
flag.BoolVar(&quiet, "quiet", false, "Disable all log output")
20+
flag.BoolVar(&quiet, "q", false, "Disable all log output (shorthand)")
1621
flag.StringVar(&outputFile, "output", "ctrf-report.json", "The output file for the test results")
1722
flag.StringVar(&outputFile, "o", "ctrf-report.json", "The output file for the test results (shorthand)")
1823

@@ -57,15 +62,44 @@ func main() {
5762
}
5863
}
5964

60-
report, err := reporter.ParseTestResults(os.Stdin, verbose, env)
65+
effectiveVerbose := verbose && !quiet
66+
67+
report, err := reporter.ParseTestResults(os.Stdin, effectiveVerbose, env)
6168
if err != nil {
62-
_, _ = fmt.Fprintf(os.Stderr, "Error parsing test results: %v\n", err)
69+
if !quiet {
70+
_, _ = fmt.Fprintf(os.Stderr, "Error parsing test results: %v\n", err)
71+
}
6372
os.Exit(1)
6473
}
6574

6675
err = reporter.WriteReportToFile(outputFile, report)
6776
if err != nil {
68-
_, _ = fmt.Fprintf(os.Stderr, "Error writing the report to file: %v\n", err)
77+
if !quiet {
78+
_, _ = fmt.Fprintf(os.Stderr, "Error writing the report to file: %v\n", err)
79+
}
80+
os.Exit(1)
81+
}
82+
83+
if !verbose && !quiet {
84+
buildOutput := reporter.GetBuildOutput()
85+
fmt.Println(buildOutput)
86+
}
87+
88+
if report.Results.Extra != nil {
89+
extraMap := report.Results.Extra.(map[string]interface{})
90+
if _, ok := extraMap["buildFail"]; ok {
91+
buildFailed = true
92+
}
93+
if _, ok := extraMap["FailedBuild"]; ok {
94+
buildFailed = true
95+
}
96+
}
97+
98+
if report.Results.Summary.Failed > 0 {
99+
buildFailed = true
100+
}
101+
102+
if buildFailed {
69103
os.Exit(1)
70104
}
71105
}

reporter/reporter.go

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

26+
var buildOutput []string
27+
2628
func ParseTestResults(r io.Reader, verbose bool, env *ctrf.Environment) (*ctrf.Report, error) {
2729
var testEvents []TestEvent
2830
decoder := json.NewDecoder(r)
@@ -47,6 +49,43 @@ func ParseTestResults(r io.Reader, verbose bool, env *ctrf.Environment) (*ctrf.R
4749
}
4850
fmt.Println(string(jsonEvent))
4951
}
52+
53+
if event.Action == "build-output" || event.Action == "build-fail" || event.Action == "fail" {
54+
if report.Results.Extra == nil {
55+
report.Results.Extra = make(map[string]interface{})
56+
}
57+
extraMap := report.Results.Extra.(map[string]interface{})
58+
59+
if event.Action == "fail" {
60+
if _, ok := extraMap["FailedBuild"]; !ok {
61+
extraMap["FailedBuild"] = true
62+
}
63+
}
64+
65+
if event.Action == "build-output" {
66+
if _, ok := extraMap["buildOutput"]; !ok {
67+
extraMap["buildOutput"] = []TestEvent{}
68+
}
69+
buildOutputEvents := extraMap["buildOutput"].([]TestEvent)
70+
extraMap["buildOutput"] = append(buildOutputEvents, event)
71+
buildOutput = append(buildOutput, event.Output)
72+
continue
73+
}
74+
75+
if event.Action == "build-fail" {
76+
if _, ok := extraMap["buildFail"]; !ok {
77+
extraMap["buildFail"] = []TestEvent{}
78+
}
79+
buildFailEvents := extraMap["buildFail"].([]TestEvent)
80+
extraMap["buildFail"] = append(buildFailEvents, event)
81+
break
82+
}
83+
}
84+
85+
if event.Action == "output" {
86+
buildOutput = append(buildOutput, event.Output)
87+
}
88+
5089
if event.Test == "" {
5190
continue
5291
}
@@ -162,6 +201,10 @@ func WriteReportToFile(filename string, report *ctrf.Report) error {
162201
return nil
163202
}
164203

204+
func GetBuildOutput() string {
205+
return strings.Join(buildOutput, "")
206+
}
207+
165208
func secondsToMillis(seconds float64) int64 {
166209
return int64(seconds * 1000)
167210
}

0 commit comments

Comments
 (0)