diff --git a/cmd/go2junit/escape.go b/cmd/go2junit/escape.go new file mode 100644 index 0000000..50d7aff --- /dev/null +++ b/cmd/go2junit/escape.go @@ -0,0 +1,25 @@ +package main + +import "strings" + +// copied from go-junit-report for fixing issues with chars that +// are out of range. + +// from encoding/xml/xml.go, replace chars by unknown char +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 +} + +func escapeIllegalChars(str string) string { + return strings.Map(func(r rune) rune { + if isInCharacterRange(r) { + return r + } + return '\uFFFD' + }, str) +} diff --git a/cmd/go2junit/go2junit.go b/cmd/go2junit/go2junit.go index ce28a13..c98044a 100644 --- a/cmd/go2junit/go2junit.go +++ b/cmd/go2junit/go2junit.go @@ -50,57 +50,6 @@ func (t *Test) MarshalXML(e *xml.Encoder, start xml.StartElement) error { } func main() { - /* - testsuites := Testsuites{ - Time: 1.23, - TImestamp: time.Now(), - Suites: []*Testsuite{ - { - Name: "hello", - Tests: 0, - Failures: 0, - Errors: 0, - Disabled: 0, - Package: "", - Skipped: 0, - Time: "", - Timestamp: time.Now(), - Testsuites: []*Testsuite{ - { - Name: "abc", - Tests: 0, - Failures: 0, - Errors: 0, - Disabled: 0, - Package: "", - Skipped: 0, - Time: "", - Timestamp: time.Time{}, - Testsuites: nil, - Testcases: []*Testcase{ - { - Name: "test", - Classname: "", - Time: "", - Skipped: nil, - Error: &Result{ - Message: "error", - }, - Failure: nil, - SystemOut: "", - }, - }, - SystemOut: "ddd", - }, - }, - Testcases: nil, - SystemOut: "hello", - }, - }, - } - - */ - testsuites := Testsuites{} if len(os.Args) != 2 && len(os.Args) != 3 { @@ -143,7 +92,7 @@ func main() { fmt.Println() testsuites.Test(item.Time, pkg, item.Test) case "output": - testsuites.Output(item.Time, pkg, item.Test, item.Output) + testsuites.Output(item.Time, pkg, item.Test, escapeIllegalChars(item.Output)) fmt.Printf("%s", item.Output) case "pause": testsuites.Output(item.Time, pkg, item.Test, "PAUSED") @@ -152,7 +101,7 @@ func main() { case "pass": testsuites.Pass(item.Time, pkg, item.Test) case "bench": - testsuites.Bench(item.Time, pkg, item.Test, item.Output) + testsuites.Bench(item.Time, pkg, item.Test, escapeIllegalChars(item.Output)) case "fail": testsuites.Fail(item.Time, pkg, item.Test) case "skip":