Skip to content

Commit 76a76d7

Browse files
committed
Use xml.EscapeText
1 parent bc70670 commit 76a76d7

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

junit/junit.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package junit
44

55
import (
6+
"bytes"
67
"encoding/xml"
78
"fmt"
89
"strings"
@@ -235,6 +236,16 @@ func formatDuration(d time.Duration) string {
235236
}
236237

237238
// formatOutput combines the lines from the given output into a single string.
238-
func formatOutput(output []string, indent int) string {
239-
return strings.Join(output, "\n")
239+
func formatOutput(output []string, _ int) string {
240+
buf := bytes.NewBufferString("")
241+
for i, o := range output {
242+
err := xml.EscapeText(buf, []byte(o))
243+
if err != nil {
244+
return "formatOutput: " + err.Error()
245+
}
246+
if i < len(output)-1 {
247+
buf.WriteString("\n")
248+
}
249+
}
250+
return buf.String()
240251
}

junit/junit_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ func TestCreateFromReport(t *testing.T) {
2626
Result: gtr.Pass,
2727
Output: []string{"ok"},
2828
},
29+
{
30+
Name: "TestEscapeOutput",
31+
Result: gtr.Pass,
32+
Output: []string{"\x00\v\f \t\\"},
33+
},
2934
{
3035
Name: "TestFail",
3136
Result: gtr.Fail,
@@ -47,14 +52,14 @@ func TestCreateFromReport(t *testing.T) {
4752
}
4853

4954
want := Testsuites{
50-
Tests: 6,
55+
Tests: 7,
5156
Errors: 3,
5257
Failures: 1,
5358
Skipped: 1,
5459
Suites: []Testsuite{
5560
{
5661
Name: "package/name",
57-
Tests: 6,
62+
Tests: 7,
5863
Errors: 3,
5964
ID: 0,
6065
Failures: 1,
@@ -72,6 +77,12 @@ func TestCreateFromReport(t *testing.T) {
7277
Time: "0.000",
7378
SystemOut: &Output{Data: "ok"},
7479
},
80+
{
81+
Name: "TestEscapeOutput",
82+
Classname: "package/name",
83+
Time: "0.000",
84+
SystemOut: &Output{Data: `��� &#x9;\`},
85+
},
7586
{
7687
Name: "TestFail",
7788
Classname: "package/name",

0 commit comments

Comments
 (0)