Skip to content

Commit cdd40cb

Browse files
committed
Set start & stop times so durtion can be properly calculated
1 parent c886029 commit cdd40cb

File tree

1 file changed

+56
-27
lines changed

1 file changed

+56
-27
lines changed

reporter/reporter.go

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"os"
8+
"time"
89

910
"github.com/ctrf-io/go-ctrf-json-reporter/ctrf"
1011
)
@@ -33,7 +34,7 @@ func ParseTestResults(r io.Reader, verbose bool, env *ctrf.Environment) (*ctrf.R
3334
}
3435

3536
report := ctrf.NewReport("gotest", env)
36-
37+
report.Results.Summary.Start = time.Now().UnixNano() / int64(time.Millisecond)
3738
for _, event := range testEvents {
3839
if verbose {
3940
jsonEvent, err := json.Marshal(event)
@@ -42,35 +43,49 @@ func ParseTestResults(r io.Reader, verbose bool, env *ctrf.Environment) (*ctrf.R
4243
}
4344
fmt.Println(string(jsonEvent))
4445
}
45-
if event.Test != "" {
46-
if event.Action == "pass" {
47-
report.Results.Summary.Tests++
48-
report.Results.Summary.Passed++
49-
report.Results.Tests = append(report.Results.Tests, &ctrf.TestResult{
50-
Name: event.Test,
51-
Status: ctrf.TestPassed,
52-
Duration: secondsToMillis(event.Elapsed),
53-
})
54-
} else if event.Action == "fail" {
55-
report.Results.Summary.Tests++
56-
report.Results.Summary.Failed++
57-
report.Results.Tests = append(report.Results.Tests, &ctrf.TestResult{
58-
Name: event.Test,
59-
Status: ctrf.TestFailed,
60-
Duration: secondsToMillis(event.Elapsed),
61-
})
62-
} else if event.Action == "skip" {
63-
report.Results.Summary.Tests++
64-
report.Results.Summary.Skipped++
65-
report.Results.Tests = append(report.Results.Tests, &ctrf.TestResult{
66-
Name: event.Test,
67-
Status: ctrf.TestSkipped,
68-
Duration: secondsToMillis(event.Elapsed),
69-
})
46+
if event.Test == "" {
47+
continue
48+
}
49+
startTime, err := parseTimeString(event.Time)
50+
duration := secondsToMillis(event.Elapsed)
51+
if err != nil {
52+
fmt.Fprintf(os.Stderr, "error parsing test event start time '%s' : %v\n", event.Time, err)
53+
} else {
54+
if report.Results.Summary.Start > startTime {
55+
report.Results.Summary.Start = startTime
56+
}
57+
endTime := startTime + duration
58+
if report.Results.Summary.Stop < endTime {
59+
report.Results.Summary.Stop = endTime
7060
}
7161
}
72-
}
7362

63+
if event.Action == "pass" {
64+
report.Results.Summary.Tests++
65+
report.Results.Summary.Passed++
66+
report.Results.Tests = append(report.Results.Tests, &ctrf.TestResult{
67+
Name: event.Test,
68+
Status: ctrf.TestPassed,
69+
Duration: duration,
70+
})
71+
} else if event.Action == "fail" {
72+
report.Results.Summary.Tests++
73+
report.Results.Summary.Failed++
74+
report.Results.Tests = append(report.Results.Tests, &ctrf.TestResult{
75+
Name: event.Test,
76+
Status: ctrf.TestFailed,
77+
Duration: duration,
78+
})
79+
} else if event.Action == "skip" {
80+
report.Results.Summary.Tests++
81+
report.Results.Summary.Skipped++
82+
report.Results.Tests = append(report.Results.Tests, &ctrf.TestResult{
83+
Name: event.Test,
84+
Status: ctrf.TestSkipped,
85+
Duration: duration,
86+
})
87+
}
88+
}
7489
return report, nil
7590
}
7691

@@ -87,3 +102,17 @@ func WriteReportToFile(filename string, report *ctrf.Report) error {
87102
func secondsToMillis(seconds float64) int64 {
88103
return int64(seconds * 1000)
89104
}
105+
func parseTimeString(timeString string) (int64, error) {
106+
// Define the layout for parsing the time string
107+
layout := time.RFC3339Nano
108+
109+
// Parse the time string
110+
t, err := time.Parse(layout, timeString)
111+
if err != nil {
112+
return 0, err
113+
}
114+
115+
// Convert the time to Unix timestamp in milliseconds
116+
timestamp := t.UnixNano() / int64(time.Millisecond)
117+
return timestamp, nil
118+
}

0 commit comments

Comments
 (0)