Skip to content

Commit

Permalink
junit: Remove ANSI Codes
Browse files Browse the repository at this point in the history
to avoid invalid XML characters
  • Loading branch information
martin-schulze-vireso committed Mar 17, 2024
1 parent db381c7 commit bb4a720
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ The format is based on [Keep a Changelog][kac] and this project adheres to
* fix `--gather-test-outputs-in` fails on tests with multiple `/` (#789)
* install does not create unused `/usr/share/bats` anymore (#857)
* ensure IFS is unchanged in `{setup,teardown}{_suite,_file,}`, `@test` and free code (#879)
* junit formatter: remove ANSI Codes to avoid invalid XML character (#886)

### Changed

Expand Down
6 changes: 5 additions & 1 deletion libexec/bats-core/bats-format-junit
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,12 @@ xml_escape() {
output=${output//>/\>}
output=${output//'"'/\"}
output=${output//\'/\'}
# remove ANSI Color codes
local CONTROL_CHAR=$'\033'
output=${output//$CONTROL_CHAR/\}
local REGEX="$CONTROL_CHAR\[[0-9;]+m"
while [[ "$output" =~ $REGEX ]]; do
output=${output//${BASH_REMATCH[0]}/}
done
printf "%s" "$output"
}

Expand Down
10 changes: 5 additions & 5 deletions test/junit-formatter.bats
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ TESTSUITES_REGEX="<testsuites time=\"$FLOAT_REGEX\">"

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

@test "junit formatter: test suites" {
Expand Down

0 comments on commit bb4a720

Please sign in to comment.