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

go junitxml: compute per package start times rather than using per execution #406

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
14 changes: 13 additions & 1 deletion internal/junitxml/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,23 @@ func generate(exec *testjson.Execution, cfg Config) JUnitTestSuites {
if cfg.customElapsed != "" {
suites.Time = cfg.customElapsed
}

overallEarliestTimestamp := exec.EarliestTime()
if overallEarliestTimestamp.IsZero() {
overallEarliestTimestamp = exec.Started()
}

for _, pkgname := range exec.Packages() {
pkg := exec.Package(pkgname)
if cfg.HideEmptyPackages && pkg.IsEmpty() {
continue
}

earliestTimestamp := pkg.EarliestTime()
if earliestTimestamp.IsZero() {
earliestTimestamp = overallEarliestTimestamp
}

junitpkg := JUnitTestSuite{
Name: cfg.FormatTestSuiteName(pkgname),
Tests: pkg.Total,
Expand All @@ -119,7 +131,7 @@ func generate(exec *testjson.Execution, cfg Config) JUnitTestSuites {
Timestamp: cfg.customTimestamp,
}
if cfg.customTimestamp == "" {
junitpkg.Timestamp = exec.Started().Format(time.RFC3339)
junitpkg.Timestamp = earliestTimestamp.Format(time.RFC3339)
}
suites.Suites = append(suites.Suites, junitpkg)
}
Expand Down
35 changes: 35 additions & 0 deletions testjson/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,21 @@ func (p *Package) TestCases() []TestCase {
return tc
}

// EarliestTime returns the earliest start time found within a child test case.
func (p *Package) EarliestTime() time.Time {
found := false
earliest := time.Time{}

for _, tc := range p.TestCases() {
if !found || tc.Time.Before(earliest) {
earliest = tc.Time
found = true
}
}

return earliest
}

// LastFailedByName returns the most recent test with name in the list of Failed
// tests. If no TestCase is found with that name, an empty TestCase is returned.
//
Expand Down Expand Up @@ -652,6 +667,26 @@ func (e *Execution) Started() time.Time {
return e.started
}

// EarliestTime returns the earliest runtime found in any of the packages within this execution.
func (e *Execution) EarliestTime() time.Time {
found := false
earliest := time.Time{}

for _, p := range e.packages {
candidate := p.EarliestTime()
if candidate.IsZero() {
continue
}

if !found || candidate.Before(earliest) {
earliest = candidate
found = true
}
}

return earliest
}

// newExecution returns a new Execution and records the current time as the
// time the test execution started.
func newExecution() *Execution {
Expand Down