Skip to content

Commit

Permalink
Omit empty <properties> tag when there are no properties.
Browse files Browse the repository at this point in the history
Go doesn't omit empty parent tags for empty values[1], so we'll work
around this for now by creating a pointer to the property slice.

[1]: golang/go#7233.
  • Loading branch information
jstemmer committed Mar 20, 2022
1 parent 43c784a commit 458fe89
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
29 changes: 20 additions & 9 deletions go-junit-report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
16 changes: 11 additions & 5 deletions pkg/junit/junit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 12 additions & 1 deletion pkg/junit/junit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
}

0 comments on commit 458fe89

Please sign in to comment.