Skip to content

Commit

Permalink
Merge pull request #625 from NoRedInk/junit-strip-invalid-characters
Browse files Browse the repository at this point in the history
Junit replace invalid characters
  • Loading branch information
zwilias authored Feb 15, 2023
2 parents 9c4ffdc + b6d5d8d commit a9f85eb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lib/Supervisor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}

Expand Down
6 changes: 6 additions & 0 deletions tests/ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
14 changes: 14 additions & 0 deletions tests/fixtures/tests/InvalidXMLCharacter/Test.elm
Original file line number Diff line number Diff line change
@@ -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
]

0 comments on commit a9f85eb

Please sign in to comment.