From 4b87b03e55501df0ffe0d6ca0a6b2cdcc838bf52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Stemmer?= Date: Tue, 15 Mar 2022 19:44:26 +0000 Subject: [PATCH] junit: Use the timestamp from the gtr.Report when creating Testsuites --- go-junit-report.go | 3 +-- go-junit-report_test.go | 10 +++++++--- pkg/junit/junit.go | 13 +++++++------ 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/go-junit-report.go b/go-junit-report.go index 10bb660..923c5d7 100644 --- a/go-junit-report.go +++ b/go-junit-report.go @@ -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 != "" { diff --git a/go-junit-report_test.go b/go-junit-report_test.go index 137f745..3e7df18 100644 --- a/go-junit-report_test.go +++ b/go-junit-report_test.go @@ -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 { diff --git a/pkg/junit/junit.go b/pkg/junit/junit.go index 60c2dc9..e140180 100644 --- a/pkg/junit/junit.go +++ b/pkg/junit/junit.go @@ -127,16 +127,17 @@ 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, + Name: pkg.Name, + Hostname: hostname, + } + + if !pkg.Timestamp.IsZero() { + suite.SetTimestamp(pkg.Timestamp) } for k, v := range pkg.Properties {