From cabd2a9a7e81057c3006558add071fd80662827c Mon Sep 17 00:00:00 2001 From: Florian Greinacher Date: Thu, 20 Jan 2022 18:09:32 +0100 Subject: [PATCH] fix(cli): correctly handle special characters in JUnit failure details (#2028) --- .../errors-with-special-xml-strings.json | 46 ++++++++++++++++ .../src/formatters/__tests__/junit.test.ts | 55 ++++++++++++++++++- packages/cli/src/formatters/junit.ts | 11 +++- .../formats/results-format-junit.scenario | 2 +- 4 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 packages/cli/src/formatters/__tests__/__fixtures__/errors-with-special-xml-strings.json diff --git a/packages/cli/src/formatters/__tests__/__fixtures__/errors-with-special-xml-strings.json b/packages/cli/src/formatters/__tests__/__fixtures__/errors-with-special-xml-strings.json new file mode 100644 index 000000000..93aeda09f --- /dev/null +++ b/packages/cli/src/formatters/__tests__/__fixtures__/errors-with-special-xml-strings.json @@ -0,0 +1,46 @@ +[ + { + "code": "special-xml-strings", + "message": "start ' \" < > end", + "path": [ + "root", + "'", + "\"", + "leaf" + ], + "severity": 0, + "source": "", + "range": { + "start": { + "line": 0, + "character": 0 + }, + "end": { + "line": 0, + "character": 0 + } + } + }, + { + "code": "special-cdata-strings", + "message": "start ", + " { let parse: Parser['parseStringPromise']; @@ -69,7 +70,7 @@ describe('JUnit formatter', () => { $: { message: "should have required property '$ref'", }, - _: 'line 36, col 22, should have required property '$ref' (oas3-schema) at path #/paths/~1pets/get/responses/200/headers/header-1', + _: "line 36, col 22, should have required property '$ref' (oas3-schema) at path #/paths/~1pets/get/responses/200/headers/header-1", }, ], }, @@ -168,4 +169,56 @@ describe('JUnit formatter', () => { }, }); }); + + test('handles special XML strings properly', async () => { + const result = await parse(junit(specialXmlStrings, { failSeverity: DiagnosticSeverity.Error })); + expect(result).toEqual({ + testsuites: { + testsuite: [ + { + $: { + errors: '0', + failures: '2', + name: '', + package: 'org.spectral', + tests: '2', + time: '0', + }, + testcase: [ + { + $: { + classname: '', + name: 'org.spectral.special-xml-strings(#/root/\'/"/leaf)', + time: '0', + }, + failure: [ + { + $: { + message: 'start \' " < > end', + }, + _: 'line 1, col 1, start \' " < > end (special-xml-strings) at path #/root/\'/"/leaf', + }, + ], + }, + { + $: { + classname: '', + name: 'org.spectral.special-cdata-strings(#/root/]]>/ /', ']]]]>'); +} + export const junit: Formatter = (results, { failSeverity }) => { let output = ''; @@ -53,8 +60,8 @@ export const junit: Formatter = (results, { failSeverity }) => { output += ``; output += ''; output += ``; output += '\n'; diff --git a/test-harness/scenarios/formats/results-format-junit.scenario b/test-harness/scenarios/formats/results-format-junit.scenario index 9d218b7de..4f34b4c2a 100644 --- a/test-harness/scenarios/formats/results-format-junit.scenario +++ b/test-harness/scenarios/formats/results-format-junit.scenario @@ -56,6 +56,6 @@ info: - +