Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set start & stop times so durtion can be properly calculated #6

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 53 additions & 31 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,38 +43,52 @@ 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{
Suite: event.Package,
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{
Suite: event.Package,
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{
Suite: event.Package,
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{
Suite: event.Package,
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{
Suite: event.Package,
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{
Suite: event.Package,
Name: event.Test,
Status: ctrf.TestSkipped,
Duration: duration,
})
}

}
return report, nil
}

Expand All @@ -82,11 +97,18 @@ func WriteReportToFile(filename string, report *ctrf.Report) error {
if err != nil {
return err
}

fmt.Println("go-ctrf-json-reporter: successfully written ctrf json to", filename)
return nil
}

func secondsToMillis(seconds float64) int64 {
return int64(seconds * 1000)
}

func parseTimeString(timeString string) (int64, error) {
t, err := time.Parse(time.RFC3339Nano, timeString)
if err != nil {
return 0, err
}
return t.UnixNano() / int64(time.Millisecond), nil
}