Skip to content

Commit 458fe89

Browse files
committedMar 20, 2022
Omit empty <properties> tag when there are no properties.
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.
1 parent 43c784a commit 458fe89

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed
 

‎go-junit-report_test.go

+20-9
Original file line numberDiff line numberDiff line change
@@ -223,21 +223,26 @@ func testReport(input, reportFile, packageName string, t *testing.T) {
223223
func modifyForBackwardsCompat(testsuites junit.Testsuites) junit.Testsuites {
224224
testsuites.XMLName.Local = ""
225225
for i, suite := range testsuites.Suites {
226-
if covIdx, covProp := getProperty("coverage.statements.pct", suite.Properties); covIdx > -1 {
227-
pct, _ := strconv.ParseFloat(covProp.Value, 64)
228-
testsuites.Suites[i].Properties[covIdx].Value = fmt.Sprintf("%.2f", pct)
229-
}
230-
testsuites.Suites[i].Properties = dropProperty("go.version", suite.Properties)
231-
232226
for j := range suite.Testcases {
233227
testsuites.Suites[i].Testcases[j].Classname = suite.Name
234228
}
229+
230+
if suite.Properties != nil {
231+
if covIdx, covProp := getProperty("coverage.statements.pct", *suite.Properties); covIdx > -1 {
232+
pct, _ := strconv.ParseFloat(covProp.Value, 64)
233+
(*testsuites.Suites[i].Properties)[covIdx].Value = fmt.Sprintf("%.2f", pct)
234+
}
235+
testsuites.Suites[i].Properties = dropProperty("go.version", suite.Properties)
236+
}
235237
}
236238
return testsuites
237239
}
238240

239241
func dropNewProperties(testsuites junit.Testsuites) junit.Testsuites {
240242
for i, suite := range testsuites.Suites {
243+
if suite.Properties == nil {
244+
continue
245+
}
241246
ps := suite.Properties
242247
ps = dropProperty("goos", ps)
243248
ps = dropProperty("goarch", ps)
@@ -247,14 +252,20 @@ func dropNewProperties(testsuites junit.Testsuites) junit.Testsuites {
247252
return testsuites
248253
}
249254

250-
func dropProperty(name string, properties []junit.Property) []junit.Property {
255+
func dropProperty(name string, properties *[]junit.Property) *[]junit.Property {
256+
if properties == nil {
257+
return nil
258+
}
251259
var props []junit.Property
252-
for _, prop := range properties {
260+
for _, prop := range *properties {
253261
if prop.Name != name {
254262
props = append(props, prop)
255263
}
256264
}
257-
return props
265+
if len(props) == 0 {
266+
return nil
267+
}
268+
return &props
258269
}
259270

260271
func getProperty(name string, properties []junit.Property) (int, junit.Property) {

‎pkg/junit/junit.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,21 @@ type Testsuite struct {
4848
Time string `xml:"time,attr"` // duration in seconds
4949
Timestamp string `xml:"timestamp,attr,omitempty"` // date and time in ISO8601
5050

51-
Properties []Property `xml:"properties>property,omitempty"`
52-
Testcases []Testcase `xml:"testcase,omitempty"`
53-
SystemOut *Output `xml:"system-out,omitempty"`
54-
SystemErr *Output `xml:"system-err,omitempty"`
51+
Properties *[]Property `xml:"properties>property,omitempty"`
52+
Testcases []Testcase `xml:"testcase,omitempty"`
53+
SystemOut *Output `xml:"system-out,omitempty"`
54+
SystemErr *Output `xml:"system-err,omitempty"`
5555
}
5656

5757
// AddProperty adds a property with the given name and value to this Testsuite.
5858
func (t *Testsuite) AddProperty(name, value string) {
59-
t.Properties = append(t.Properties, Property{Name: name, Value: value})
59+
prop := Property{Name: name, Value: value}
60+
if t.Properties == nil {
61+
t.Properties = &[]Property{prop}
62+
return
63+
}
64+
props := append(*t.Properties, prop)
65+
t.Properties = &props
6066
}
6167

6268
// AddTestcase adds Testcase tc to this Testsuite.

‎pkg/junit/junit_test.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestMarshalUnmarshal(t *testing.T) {
2727
Skipped: 1,
2828
Time: "12.345",
2929
Timestamp: "2012-03-09T14:38:06+01:00",
30-
Properties: []Property{{"key", "value"}},
30+
Properties: properties("key", "value"),
3131
Testcases: []Testcase{
3232
{
3333
Name: "test1",
@@ -62,3 +62,14 @@ func TestMarshalUnmarshal(t *testing.T) {
6262
t.Errorf("Unmarshal result incorrect, diff (-want +got):\n%s\n", diff)
6363
}
6464
}
65+
66+
func properties(keyvals ...string) *[]Property {
67+
if len(keyvals)%2 != 0 {
68+
panic("invalid keyvals specified")
69+
}
70+
var props []Property
71+
for i := 0; i < len(keyvals); i += 2 {
72+
props = append(props, Property{keyvals[i], keyvals[i+1]})
73+
}
74+
return &props
75+
}

0 commit comments

Comments
 (0)
Please sign in to comment.