From ec68e6127caa011ed6985175b6e2ef6e6c6985a4 Mon Sep 17 00:00:00 2001 From: ZexDC Date: Fri, 19 May 2023 14:29:59 +0300 Subject: [PATCH 1/3] Remove ANSI escape sequences --- junit/junit.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/junit/junit.go b/junit/junit.go index e87b6ed0..ea551cb7 100644 --- a/junit/junit.go +++ b/junit/junit.go @@ -8,6 +8,7 @@ import ( "io" "strings" "time" + "regexp" "github.com/jstemmer/go-junit-report/v2/gtr" ) @@ -251,7 +252,8 @@ func formatDuration(d time.Duration) string { // formatOutput combines the lines from the given output into a single string. func formatOutput(output []string) string { - return escapeIllegalChars(strings.Join(output, "\n")) + cleanString := removeEscapeSequences(strings.Join(output, "\n")) + return escapeIllegalChars(strings.Join(cleanString, "\n")) } func escapeIllegalChars(str string) string { @@ -263,6 +265,14 @@ func escapeIllegalChars(str string) string { }, str) } +func removeEscapeSequences(s string) string { + // Define a regular expression to match ANSI escape sequences + ansiEscapeRegex := regexp.MustCompile(`\x1b\[[0-9;]*[a-zA-Z]`) + // Remove ANSI escape sequences from the input string + cleanString := ansiEscapeRegex.ReplaceAllString(s, "") + return cleanString +} + // 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. From 26add432d17955bb53a0c0cbd21c9b7a5844146a Mon Sep 17 00:00:00 2001 From: ZexDC Date: Fri, 19 May 2023 11:47:44 +0000 Subject: [PATCH 2/3] Add ANSI format unit test --- junit/junit.go | 4 ++-- junit/junit_test.go | 19 +++++++++++++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/junit/junit.go b/junit/junit.go index ea551cb7..367f45be 100644 --- a/junit/junit.go +++ b/junit/junit.go @@ -6,9 +6,9 @@ import ( "encoding/xml" "fmt" "io" + "regexp" "strings" "time" - "regexp" "github.com/jstemmer/go-junit-report/v2/gtr" ) @@ -253,7 +253,7 @@ func formatDuration(d time.Duration) string { // formatOutput combines the lines from the given output into a single string. func formatOutput(output []string) string { cleanString := removeEscapeSequences(strings.Join(output, "\n")) - return escapeIllegalChars(strings.Join(cleanString, "\n")) + return escapeIllegalChars(cleanString) } func escapeIllegalChars(str string) string { diff --git a/junit/junit_test.go b/junit/junit_test.go index b1fd4e57..3a1407c9 100644 --- a/junit/junit_test.go +++ b/junit/junit_test.go @@ -32,6 +32,11 @@ func TestCreateFromReport(t *testing.T) { Result: gtr.Pass, Output: []string{"\x00\v\f \t\\"}, }, + { + Name: "TestRemoveOutputANSI", + Result: gtr.Pass, + Output: []string{"This contains some", "\x1b[1mANSI\x1b[0m", "sequence"}, + }, { Name: "TestFail", Result: gtr.Fail, @@ -53,14 +58,14 @@ func TestCreateFromReport(t *testing.T) { } want := Testsuites{ - Tests: 7, + Tests: 8, Errors: 3, Failures: 1, Skipped: 1, Suites: []Testsuite{ { Name: "package/name", - Tests: 7, + Tests: 8, Errors: 3, ID: 0, Failures: 1, @@ -84,6 +89,12 @@ func TestCreateFromReport(t *testing.T) { Time: "0.000", SystemOut: &Output{Data: `��� \`}, }, + { + Name: "TestRemoveOutputANSI", + Classname: "package/name", + Time: "0.000", + SystemOut: &Output{Data: "This contains some\nANSI\nsequence"}, + }, { Name: "TestFail", Classname: "package/name", @@ -193,8 +204,8 @@ func TestWriteXML(t *testing.T) { var suites Testsuites - ts := Testsuite{Name:"Example"} - ts.AddTestcase(Testcase{Name: "Test", }) + ts := Testsuite{Name: "Example"} + ts.AddTestcase(Testcase{Name: "Test"}) suites.AddSuite(ts) var buf bytes.Buffer From 1f7a0ac58034bce64453e5279509b34dddf85375 Mon Sep 17 00:00:00 2001 From: ZexDC Date: Fri, 16 Jun 2023 15:48:45 +0000 Subject: [PATCH 3/3] Use stripansi library --- go.mod | 5 ++++- go.sum | 2 ++ junit/junit.go | 15 ++++----------- junit/junit_test.go | 2 +- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index 40de11d1..30082839 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/jstemmer/go-junit-report/v2 go 1.13 -require github.com/google/go-cmp v0.5.8 +require ( + github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect + github.com/google/go-cmp v0.5.8 +) diff --git a/go.sum b/go.sum index e9b099ce..9541a1ca 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ +github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8= +github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo= github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= diff --git a/junit/junit.go b/junit/junit.go index 367f45be..f5ba111e 100644 --- a/junit/junit.go +++ b/junit/junit.go @@ -6,10 +6,11 @@ import ( "encoding/xml" "fmt" "io" - "regexp" "strings" "time" + "github.com/acarl005/stripansi" + "github.com/jstemmer/go-junit-report/v2/gtr" ) @@ -252,11 +253,11 @@ func formatDuration(d time.Duration) string { // formatOutput combines the lines from the given output into a single string. func formatOutput(output []string) string { - cleanString := removeEscapeSequences(strings.Join(output, "\n")) - return escapeIllegalChars(cleanString) + return escapeIllegalChars(strings.Join(output, "\n")) } func escapeIllegalChars(str string) string { + str = stripansi.Strip(str) return strings.Map(func(r rune) rune { if isInCharacterRange(r) { return r @@ -265,14 +266,6 @@ func escapeIllegalChars(str string) string { }, str) } -func removeEscapeSequences(s string) string { - // Define a regular expression to match ANSI escape sequences - ansiEscapeRegex := regexp.MustCompile(`\x1b\[[0-9;]*[a-zA-Z]`) - // Remove ANSI escape sequences from the input string - cleanString := ansiEscapeRegex.ReplaceAllString(s, "") - return cleanString -} - // 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. diff --git a/junit/junit_test.go b/junit/junit_test.go index 3a1407c9..cc4a72d5 100644 --- a/junit/junit_test.go +++ b/junit/junit_test.go @@ -35,7 +35,7 @@ func TestCreateFromReport(t *testing.T) { { Name: "TestRemoveOutputANSI", Result: gtr.Pass, - Output: []string{"This contains some", "\x1b[1mANSI\x1b[0m", "sequence"}, + Output: []string{"This contains some", "\x1b[38;5;140mANSI\x1b[0m", "sequence"}, }, { Name: "TestFail",