feat(junit reporter): add option to force usage of CDATA sections for content in XML elements (#12744)

Co-authored-by: Sergio Freire <sergio.freire@xpand-it.com>
This commit is contained in:
Sergio Freire 2022-06-10 09:25:52 +01:00 committed by GitHub
parent 531bdb2493
commit d193bd64c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 4 deletions

View File

@ -201,10 +201,14 @@ const discouragedXMLCharacters = /[\u0001-\u0008\u000b-\u000c\u000e-\u001f\u007f
function escape(text: string, stripANSIControlSequences: boolean, isCharacterData: boolean): string {
if (stripANSIControlSequences)
text = stripAnsiEscapes(text);
const escapeRe = isCharacterData ? /[&<]/g : /[&"<>]/g;
text = text.replace(escapeRe, c => ({ '&': '&amp;', '"': '&quot;', '<': '&lt;', '>': '&gt;' }[c]!));
if (isCharacterData)
text = text.replace(/]]>/g, ']]&gt;');
if (isCharacterData) {
text = '<![CDATA[' + text.replace(/]]>/g, ']]&gt;') + ']]>';
} else {
const escapeRe = /[&"'<>]/g;
text = text.replace(escapeRe, c => ({ '&': '&amp;', '"': '&quot;', "'": '&apos;', '<': '&lt;', '>': '&gt;' }[c]!));
}
text = text.replace(discouragedXMLCharacters, '');
return text;
}

View File

@ -141,6 +141,28 @@ test('should render stdout without ansi escapes', async ({ runInlineTest }) => {
expect(result.exitCode).toBe(0);
});
test('should render, by default, character data as CDATA sections', async ({ runInlineTest }) => {
const result = await runInlineTest({
'playwright.config.ts': `
module.exports = {
reporter: [ ['junit'] ],
};
`,
'a.test.ts': `
const { test } = pwt;
test('one', async ({}) => {
process.stdout.write('Hello world &"\\'<>]]>');
});
`,
}, { reporter: '' });
const xml = parseXML(result.output);
const testcase = xml['testsuites']['testsuite'][0]['testcase'][0];
expect(testcase['system-out'].length).toBe(1);
expect(testcase['system-out'][0].trim()).toBe('Hello world &"\'<>]]&gt;');
expect(result.output).toContain(`<system-out>\n<![CDATA[Hello world &"\'<>]]&gt;]]>\n</system-out>`);
expect(result.exitCode).toBe(0);
});
test('should render skipped', async ({ runInlineTest }) => {
const result = await runInlineTest({
'a.test.js': `