junit: Use the timestamp from the gtr.Report when creating Testsuites

This commit is contained in:
Joël Stemmer 2022-03-15 19:44:26 +00:00
parent b0a9864d1e
commit 4b87b03e55
3 changed files with 15 additions and 11 deletions

View File

@ -7,7 +7,6 @@ import (
"io"
"os"
"strings"
"time"
"github.com/jstemmer/go-junit-report/v2/pkg/junit"
"github.com/jstemmer/go-junit-report/v2/pkg/parser/gotest"
@ -94,7 +93,7 @@ func main() {
}
hostname, _ := os.Hostname() // ignore error
testsuites := junit.CreateFromReport(report, hostname, time.Now())
testsuites := junit.CreateFromReport(report, hostname)
var out io.Writer = os.Stdout
if *output != "" {

View File

@ -191,7 +191,12 @@ func testReport(input, reportFile, packageName string, t *testing.T) {
}
defer file.Close()
parser := gotest.New(gotest.PackageName(packageName))
parser := gotest.New(
gotest.PackageName(packageName),
gotest.TimestampFunc(func() time.Time {
return time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC)
}),
)
report, err := parser.Parse(file)
if err != nil {
@ -204,8 +209,7 @@ func testReport(input, reportFile, packageName string, t *testing.T) {
}
}
testTime := time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC)
actual := junit.CreateFromReport(report, "hostname", testTime)
actual := junit.CreateFromReport(report, "hostname")
expectedXML, err := loadTestReport(reportFile, "")
if err != nil {

View File

@ -127,18 +127,19 @@ type Output struct {
}
// CreateFromReport creates a JUnit representation of the given gtr.Report.
func CreateFromReport(report gtr.Report, hostname string, timestamp time.Time) Testsuites {
ts := timestamp.Format(time.RFC3339)
func CreateFromReport(report gtr.Report, hostname string) Testsuites {
var suites Testsuites
for _, pkg := range report.Packages {
var duration time.Duration
suite := Testsuite{
Name: pkg.Name,
Timestamp: ts,
Hostname: hostname,
}
if !pkg.Timestamp.IsZero() {
suite.SetTimestamp(pkg.Timestamp)
}
for k, v := range pkg.Properties {
suite.AddProperty(k, v)
}