Skip to content

Commit 7309f18

Browse files
committed
Allow whitespaces
1 parent 517787c commit 7309f18

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
lines changed

junit/junit.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
package junit
44

55
import (
6-
"bytes"
76
"encoding/xml"
87
"fmt"
98
"strings"
109
"time"
10+
"unicode"
1111

1212
"github.com/jstemmer/go-junit-report/v2/gtr"
1313
)
@@ -236,16 +236,31 @@ func formatDuration(d time.Duration) string {
236236
}
237237

238238
// formatOutput combines the lines from the given output into a single string.
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()
239+
func formatOutput(output []string) string {
240+
return escapeIllegalChars(strings.Join(output, "\n"))
241+
}
242+
243+
func escapeIllegalChars(str string) string {
244+
return strings.Map(func(r rune) rune {
245+
if unicode.IsSpace(r) {
246+
return r
245247
}
246-
if i < len(output)-1 {
247-
buf.WriteString("\n")
248+
if isInCharacterRange(r) {
249+
return r
248250
}
249-
}
250-
return buf.String()
251+
return '�'
252+
}, str)
253+
}
254+
255+
// Decide whether the given rune is in the XML Character Range, per
256+
// the Char production of https://www.xml.com/axml/testaxml.htm,
257+
// Section 2.2 Characters.
258+
// Form: encoding/xml/xml.go
259+
func isInCharacterRange(r rune) (inrange bool) {
260+
return r == 0x09 ||
261+
r == 0x0A ||
262+
r == 0x0D ||
263+
r >= 0x20 && r <= 0xD7FF ||
264+
r >= 0xE000 && r <= 0xFFFD ||
265+
r >= 0x10000 && r <= 0x10FFFF
251266
}

junit/junit_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func TestCreateFromReport(t *testing.T) {
8181
Name: "TestEscapeOutput",
8282
Classname: "package/name",
8383
Time: "0.000",
84-
SystemOut: &Output{Data: `��� &#x9;\`},
84+
SystemOut: &Output{Data: "�\v\f \t\\"},
8585
},
8686
{
8787
Name: "TestFail",

parser/gotest/internal/collector/collector_test.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,3 @@ func TestActiveID(t *testing.T) {
115115
}
116116

117117
}
118-
119-
func TestSafeText(t *testing.T) {
120-
l := line{text: "\tx\x00y\x1bz\n"}
121-
got := l.SafeText()
122-
want := "\txyz\n"
123-
if diff := cmp.Diff(want, got); diff != "" {
124-
t.Errorf("SafeText() for %q incorrect (-want +got):\n%s", l.text, diff)
125-
}
126-
}

0 commit comments

Comments
 (0)