Skip to content

Commit af6eb00

Browse files
Merge pull request #886 from martin-schulze-vireso/fix/junit_formatter_remove_ansi_codes
junit: Remove ANSI Codes
2 parents db381c7 + bb4a720 commit af6eb00

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

docs/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ The format is based on [Keep a Changelog][kac] and this project adheres to
2727
* fix `--gather-test-outputs-in` fails on tests with multiple `/` (#789)
2828
* install does not create unused `/usr/share/bats` anymore (#857)
2929
* ensure IFS is unchanged in `{setup,teardown}{_suite,_file,}`, `@test` and free code (#879)
30+
* junit formatter: remove ANSI Codes to avoid invalid XML character (#886)
3031

3132
### Changed
3233

libexec/bats-core/bats-format-junit

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,12 @@ xml_escape() {
103103
output=${output//>/\>}
104104
output=${output//'"'/\"}
105105
output=${output//\'/\'}
106+
# remove ANSI Color codes
106107
local CONTROL_CHAR=$'\033'
107-
output=${output//$CONTROL_CHAR/\}
108+
local REGEX="$CONTROL_CHAR\[[0-9;]+m"
109+
while [[ "$output" =~ $REGEX ]]; do
110+
output=${output//${BASH_REMATCH[0]}/}
111+
done
108112
printf "%s" "$output"
109113
}
110114

test/junit-formatter.bats

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ TESTSUITES_REGEX="<testsuites time=\"$FLOAT_REGEX\">"
5454

5555
echo "$output"
5656
[[ "${lines[2]}" == "<testsuite name=\"$ESCAPED_TEST_FILE_NAME\" tests=\"3\" failures=\"1\" errors=\"0\" skipped=\"1\" time=\""*"\" timestamp=\""*"\" hostname=\""*"\">" ]]
57-
[[ "${lines[3]}" == " <testcase classname=\"$ESCAPED_TEST_FILE_NAME\" name=\"Successful test with escape characters: &quot;&#39;&lt;&gt;&amp;&#27;[0m (0x1b)\" time=\""*"\" />" ]]
58-
[[ "${lines[4]}" == " <testcase classname=\"$ESCAPED_TEST_FILE_NAME\" name=\"Failed test with escape characters: &quot;&#39;&lt;&gt;&amp;&#27;[0m (0x1b)\" "* ]]
57+
[[ "${lines[3]}" == " <testcase classname=\"$ESCAPED_TEST_FILE_NAME\" name=\"Successful test with escape characters: &quot;&#39;&lt;&gt;&amp; (0x1b)\" time=\""*"\" />" ]]
58+
[[ "${lines[4]}" == " <testcase classname=\"$ESCAPED_TEST_FILE_NAME\" name=\"Failed test with escape characters: &quot;&#39;&lt;&gt;&amp; (0x1b)\" "* ]]
5959
[[ "${lines[5]}" == ' <failure type="failure">(in test file '*"$ESCAPED_TEST_FILE_NAME, line 6)" ]]
60-
[[ "${lines[6]}" == ' `echo &quot;&lt;&gt;&#39;&amp;&#27;[0m&quot; &amp;&amp; false&#39; failed'* ]]
61-
[[ "${lines[9]}" == " <testcase classname=\"$ESCAPED_TEST_FILE_NAME\" name=\"Skipped test with escape characters: &quot;&#39;&lt;&gt;&amp;&#27;[0m (0x1b)\" time=\""*"\">" ]]
62-
[[ "${lines[10]}" == " <skipped>&quot;&#39;&lt;&gt;&amp;&#27;[0m</skipped>" ]]
60+
[[ "${lines[6]}" == ' `echo &quot;&lt;&gt;&#39;&amp;&quot; &amp;&amp; false&#39; failed'* ]]
61+
[[ "${lines[9]}" == " <testcase classname=\"$ESCAPED_TEST_FILE_NAME\" name=\"Skipped test with escape characters: &quot;&#39;&lt;&gt;&amp; (0x1b)\" time=\""*"\">" ]]
62+
[[ "${lines[10]}" == " <skipped>&quot;&#39;&lt;&gt;&amp;</skipped>" ]]
6363
}
6464

6565
@test "junit formatter: test suites" {

0 commit comments

Comments
 (0)