diff --git a/go-junit-report_test.go b/go-junit-report_test.go index 2b718e39..7909444a 100644 --- a/go-junit-report_test.go +++ b/go-junit-report_test.go @@ -223,21 +223,26 @@ func testReport(input, reportFile, packageName string, t *testing.T) { func modifyForBackwardsCompat(testsuites junit.Testsuites) junit.Testsuites { testsuites.XMLName.Local = "" for i, suite := range testsuites.Suites { - if covIdx, covProp := getProperty("coverage.statements.pct", suite.Properties); covIdx > -1 { - pct, _ := strconv.ParseFloat(covProp.Value, 64) - testsuites.Suites[i].Properties[covIdx].Value = fmt.Sprintf("%.2f", pct) - } - testsuites.Suites[i].Properties = dropProperty("go.version", suite.Properties) - for j := range suite.Testcases { testsuites.Suites[i].Testcases[j].Classname = suite.Name } + + if suite.Properties != nil { + if covIdx, covProp := getProperty("coverage.statements.pct", *suite.Properties); covIdx > -1 { + pct, _ := strconv.ParseFloat(covProp.Value, 64) + (*testsuites.Suites[i].Properties)[covIdx].Value = fmt.Sprintf("%.2f", pct) + } + testsuites.Suites[i].Properties = dropProperty("go.version", suite.Properties) + } } return testsuites } func dropNewProperties(testsuites junit.Testsuites) junit.Testsuites { for i, suite := range testsuites.Suites { + if suite.Properties == nil { + continue + } ps := suite.Properties ps = dropProperty("goos", ps) ps = dropProperty("goarch", ps) @@ -247,14 +252,20 @@ func dropNewProperties(testsuites junit.Testsuites) junit.Testsuites { return testsuites } -func dropProperty(name string, properties []junit.Property) []junit.Property { +func dropProperty(name string, properties *[]junit.Property) *[]junit.Property { + if properties == nil { + return nil + } var props []junit.Property - for _, prop := range properties { + for _, prop := range *properties { if prop.Name != name { props = append(props, prop) } } - return props + if len(props) == 0 { + return nil + } + return &props } func getProperty(name string, properties []junit.Property) (int, junit.Property) { diff --git a/pkg/junit/junit.go b/pkg/junit/junit.go index e17d93df..d8aa46f7 100644 --- a/pkg/junit/junit.go +++ b/pkg/junit/junit.go @@ -48,15 +48,21 @@ type Testsuite struct { Time string `xml:"time,attr"` // duration in seconds Timestamp string `xml:"timestamp,attr,omitempty"` // date and time in ISO8601 - Properties []Property `xml:"properties>property,omitempty"` - Testcases []Testcase `xml:"testcase,omitempty"` - SystemOut *Output `xml:"system-out,omitempty"` - SystemErr *Output `xml:"system-err,omitempty"` + Properties *[]Property `xml:"properties>property,omitempty"` + Testcases []Testcase `xml:"testcase,omitempty"` + SystemOut *Output `xml:"system-out,omitempty"` + SystemErr *Output `xml:"system-err,omitempty"` } // AddProperty adds a property with the given name and value to this Testsuite. func (t *Testsuite) AddProperty(name, value string) { - t.Properties = append(t.Properties, Property{Name: name, Value: value}) + prop := Property{Name: name, Value: value} + if t.Properties == nil { + t.Properties = &[]Property{prop} + return + } + props := append(*t.Properties, prop) + t.Properties = &props } // AddTestcase adds Testcase tc to this Testsuite. diff --git a/pkg/junit/junit_test.go b/pkg/junit/junit_test.go index 429e7314..7f32f771 100644 --- a/pkg/junit/junit_test.go +++ b/pkg/junit/junit_test.go @@ -27,7 +27,7 @@ func TestMarshalUnmarshal(t *testing.T) { Skipped: 1, Time: "12.345", Timestamp: "2012-03-09T14:38:06+01:00", - Properties: []Property{{"key", "value"}}, + Properties: properties("key", "value"), Testcases: []Testcase{ { Name: "test1", @@ -62,3 +62,14 @@ func TestMarshalUnmarshal(t *testing.T) { t.Errorf("Unmarshal result incorrect, diff (-want +got):\n%s\n", diff) } } + +func properties(keyvals ...string) *[]Property { + if len(keyvals)%2 != 0 { + panic("invalid keyvals specified") + } + var props []Property + for i := 0; i < len(keyvals); i += 2 { + props = append(props, Property{keyvals[i], keyvals[i+1]}) + } + return &props +}