diff --git a/lib/Supervisor.js b/lib/Supervisor.js index bc889142..22ddbf2f 100644 --- a/lib/Supervisor.js +++ b/lib/Supervisor.js @@ -201,7 +201,27 @@ function run( xml.testsuite.testcase = xml.testsuite.testcase.concat(values); - console.log(XmlBuilder.create(xml).end()); + // The XmlBuilder by default does not remove characters that are + // invalid in XML, like backspaces. However, we can pass it an + // `invalidCharReplacement` option to tell it how to handle + // those characters, rather than crashing. In an attempt to + // retain useful information in the output, we try and output a + // hex-encoded unicode codepoint for the invalid character. For + // example, the start of a terminal escape (`\u{001B}` in Elm) will be output as a + // literal `\u{001B}`. + var invalidCharReplacement = function (char) { + return ( + '\\u{' + + char.codePointAt(0).toString(16).padStart(4, '0') + + '}' + ); + }; + + console.log( + XmlBuilder.create(xml, { + invalidCharReplacement: invalidCharReplacement, + }).end() + ); } } diff --git a/tests/ci.js b/tests/ci.js index 4cd7fa9f..3a16acbe 100755 --- a/tests/ci.js +++ b/tests/ci.js @@ -292,6 +292,12 @@ describe('Testing elm-test on single Elm files', () => { } } + it(`Should not crash the junit reporter on invalid characters`, () => { + const itsPath = path.join('tests', 'InvalidXMLCharacter', 'Test.elm'); + const runResult = execElmTest([itsPath, '--report', 'junit'], cwd); + assertTestSuccess(runResult); + }); + it(`Should run every file in tests/CompileError`, () => { const filesFound = readdir(path.join(cwd, 'tests', 'CompileError')); assert.deepStrictEqual( diff --git a/tests/fixtures/tests/InvalidXMLCharacter/Test.elm b/tests/fixtures/tests/InvalidXMLCharacter/Test.elm new file mode 100644 index 00000000..2da526e6 --- /dev/null +++ b/tests/fixtures/tests/InvalidXMLCharacter/Test.elm @@ -0,0 +1,14 @@ +module InvalidXMLCharacter.Test exposing (invalidCharacter) + +import Expect +import Test exposing (..) + + +invalidCharacter : Test +invalidCharacter = + describe "The junit reporter should not crash due to invalid (for XML) characters in the output" + [ test "backspace: \u{0008}" <| + \() -> Expect.pass + , test "escape: \u{001B}" <| + \() -> Expect.pass + ]