Skip to content

Commit

Permalink
Set start & stop times so durtion can be properly calculated
Browse files Browse the repository at this point in the history
  • Loading branch information
asherf committed Sep 8, 2024
1 parent c886029 commit cdd40cb
Showing 1 changed file with 56 additions and 27 deletions.
83 changes: 56 additions & 27 deletions reporter/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"time"

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

report := ctrf.NewReport("gotest", env)

report.Results.Summary.Start = time.Now().UnixNano() / int64(time.Millisecond)
for _, event := range testEvents {
if verbose {
jsonEvent, err := json.Marshal(event)
Expand All @@ -42,35 +43,49 @@ func ParseTestResults(r io.Reader, verbose bool, env *ctrf.Environment) (*ctrf.R
}
fmt.Println(string(jsonEvent))
}
if event.Test != "" {
if event.Action == "pass" {
report.Results.Summary.Tests++
report.Results.Summary.Passed++
report.Results.Tests = append(report.Results.Tests, &ctrf.TestResult{
Name: event.Test,
Status: ctrf.TestPassed,
Duration: secondsToMillis(event.Elapsed),
})
} else if event.Action == "fail" {
report.Results.Summary.Tests++
report.Results.Summary.Failed++
report.Results.Tests = append(report.Results.Tests, &ctrf.TestResult{
Name: event.Test,
Status: ctrf.TestFailed,
Duration: secondsToMillis(event.Elapsed),
})
} else if event.Action == "skip" {
report.Results.Summary.Tests++
report.Results.Summary.Skipped++
report.Results.Tests = append(report.Results.Tests, &ctrf.TestResult{
Name: event.Test,
Status: ctrf.TestSkipped,
Duration: secondsToMillis(event.Elapsed),
})
if event.Test == "" {
continue
}
startTime, err := parseTimeString(event.Time)
duration := secondsToMillis(event.Elapsed)
if err != nil {
fmt.Fprintf(os.Stderr, "error parsing test event start time '%s' : %v\n", event.Time, err)
} else {
if report.Results.Summary.Start > startTime {
report.Results.Summary.Start = startTime
}
endTime := startTime + duration
if report.Results.Summary.Stop < endTime {
report.Results.Summary.Stop = endTime
}
}
}

if event.Action == "pass" {
report.Results.Summary.Tests++
report.Results.Summary.Passed++
report.Results.Tests = append(report.Results.Tests, &ctrf.TestResult{
Name: event.Test,
Status: ctrf.TestPassed,
Duration: duration,
})
} else if event.Action == "fail" {
report.Results.Summary.Tests++
report.Results.Summary.Failed++
report.Results.Tests = append(report.Results.Tests, &ctrf.TestResult{
Name: event.Test,
Status: ctrf.TestFailed,
Duration: duration,
})
} else if event.Action == "skip" {
report.Results.Summary.Tests++
report.Results.Summary.Skipped++
report.Results.Tests = append(report.Results.Tests, &ctrf.TestResult{
Name: event.Test,
Status: ctrf.TestSkipped,
Duration: duration,
})
}
}
return report, nil
}

Expand All @@ -87,3 +102,17 @@ func WriteReportToFile(filename string, report *ctrf.Report) error {
func secondsToMillis(seconds float64) int64 {
return int64(seconds * 1000)
}
func parseTimeString(timeString string) (int64, error) {
// Define the layout for parsing the time string
layout := time.RFC3339Nano

// Parse the time string
t, err := time.Parse(layout, timeString)
if err != nil {
return 0, err
}

// Convert the time to Unix timestamp in milliseconds
timestamp := t.UnixNano() / int64(time.Millisecond)
return timestamp, nil
}

0 comments on commit cdd40cb

Please sign in to comment.