From 7309f185c283eb9826925f6d7dd911fc96c3d7ad Mon Sep 17 00:00:00 2001 From: Tomasz Janiszewski Date: Thu, 15 Sep 2022 10:56:56 +0200 Subject: [PATCH] Allow whitespaces --- junit/junit.go | 37 +++++++++++++------ junit/junit_test.go | 2 +- .../internal/collector/collector_test.go | 9 ----- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/junit/junit.go b/junit/junit.go index 57f83046..80c68b3e 100644 --- a/junit/junit.go +++ b/junit/junit.go @@ -3,11 +3,11 @@ package junit import ( - "bytes" "encoding/xml" "fmt" "strings" "time" + "unicode" "github.com/jstemmer/go-junit-report/v2/gtr" ) @@ -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 } diff --git a/junit/junit_test.go b/junit/junit_test.go index 4e7f71b7..ab9e53a0 100644 --- a/junit/junit_test.go +++ b/junit/junit_test.go @@ -81,7 +81,7 @@ func TestCreateFromReport(t *testing.T) { Name: "TestEscapeOutput", Classname: "package/name", Time: "0.000", - SystemOut: &Output{Data: `��� \`}, + SystemOut: &Output{Data: "�\v\f \t\\"}, }, { Name: "TestFail", diff --git a/parser/gotest/internal/collector/collector_test.go b/parser/gotest/internal/collector/collector_test.go index 95ee9274..bfad6f93 100644 --- a/parser/gotest/internal/collector/collector_test.go +++ b/parser/gotest/internal/collector/collector_test.go @@ -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) - } -}