Skip to content

Commit

Permalink
fix(cli): correctly handle special characters in JUnit failure details
Browse files Browse the repository at this point in the history
Those details are contained in XML CDATA section and should therefore not be XML-escaped.
  • Loading branch information
fgreinacher committed Jan 19, 2022
1 parent f1b2c2c commit 44da16a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
Expand Up @@ -51,7 +51,7 @@
},
{
"code": "oas3-schema",
"message": "should have required property '$ref'",
"message": "should have required property '$ref' and handle special strings like \", <, > or ]]> well",
"path": [
"paths",
"/pets",
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/formatters/__tests__/junit.test.ts
Expand Up @@ -67,9 +67,10 @@ describe('JUnit formatter', () => {
failure: [
{
$: {
message: "should have required property '$ref'",
message:
"should have required property '$ref' and handle special strings like \", <, > or ]]> well",
},
_: 'line 36, col 22, should have required property &apos;$ref&apos; (oas3-schema) at path #/paths/~1pets/get/responses/200/headers/header-1',
_: "line 36, col 22, should have required property '$ref' and handle special strings like \", <, > or ]]> well (oas3-schema) at path #/paths/~1pets/get/responses/200/headers/header-1",
},
],
},
Expand Down
11 changes: 9 additions & 2 deletions packages/cli/src/formatters/junit.ts
Expand Up @@ -29,6 +29,13 @@ import { printPath, PrintStyle } from '@stoplight/spectral-runtime';
import { Formatter } from './types';
import { groupBySource, xmlEscape } from './utils';

/**
* Prepares the given text for inclusion in an XML CDATA section.
*/
function prepareForCdata(text: string): string {
return text.replace(']]>', ']]]]><![CDATA[>');
}

export const junit: Formatter = (results, { failSeverity }) => {
let output = '';

Expand All @@ -50,8 +57,8 @@ export const junit: Formatter = (results, { failSeverity }) => {
output += `<failure message="${xmlEscape(result.message)}">`;
output += '<![CDATA[';
output += `line ${result.range.start.line + 1}, col ${result.range.start.character + 1}, `;
output += `${xmlEscape(result.message)} (${result.code}) `;
output += `at path ${xmlEscape(printPath(result.path, PrintStyle.EscapedPointer))}`;
output += `${prepareForCdata(result.message)} (${result.code}) `;
output += `at path ${prepareForCdata(printPath(result.path, PrintStyle.EscapedPointer))}`;
output += ']]>';
output += `</failure>`;
output += '</testcase>\n';
Expand Down

0 comments on commit 44da16a

Please sign in to comment.