Skip to content

Commit

Permalink
Allow whitespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
janisz committed Sep 15, 2022
1 parent 517787c commit 7309f18
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
37 changes: 26 additions & 11 deletions junit/junit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
package junit

import (
"bytes"
"encoding/xml"
"fmt"
"strings"
"time"
"unicode"

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

// formatOutput combines the lines from the given output into a single string.
func formatOutput(output []string, _ int) string {
buf := bytes.NewBufferString("")
for i, o := range output {
err := xml.EscapeText(buf, []byte(o))
if err != nil {
return "formatOutput: " + err.Error()
func formatOutput(output []string) string {
return escapeIllegalChars(strings.Join(output, "\n"))
}

func escapeIllegalChars(str string) string {
return strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return r
}
if i < len(output)-1 {
buf.WriteString("\n")
if isInCharacterRange(r) {
return r
}
}
return buf.String()
return '�'
}, str)
}

// Decide whether the given rune is in the XML Character Range, per
// the Char production of https://www.xml.com/axml/testaxml.htm,
// Section 2.2 Characters.
// Form: encoding/xml/xml.go
func isInCharacterRange(r rune) (inrange bool) {
return r == 0x09 ||
r == 0x0A ||
r == 0x0D ||
r >= 0x20 && r <= 0xD7FF ||
r >= 0xE000 && r <= 0xFFFD ||
r >= 0x10000 && r <= 0x10FFFF
}
2 changes: 1 addition & 1 deletion junit/junit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestCreateFromReport(t *testing.T) {
Name: "TestEscapeOutput",
Classname: "package/name",
Time: "0.000",
SystemOut: &Output{Data: `��� &#x9;\`},
SystemOut: &Output{Data: "�\v\f \t\\"},
},
{
Name: "TestFail",
Expand Down
9 changes: 0 additions & 9 deletions parser/gotest/internal/collector/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,3 @@ func TestActiveID(t *testing.T) {
}

}

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

0 comments on commit 7309f18

Please sign in to comment.