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
  • Loading branch information
fgreinacher committed Jan 20, 2022
1 parent 062ae0c commit cabd2a9
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 4 deletions.
@@ -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 <![CDATA[ ]]> <![CDATA[ end",
"path": [
"root",
"]]>",
"<![CDATA[",
"leaf"
],
"severity": 0,
"source": "",
"range": {
"start": {
"line": 0,
"character": 0
},
"end": {
"line": 0,
"character": 0
}
}
}
]
55 changes: 54 additions & 1 deletion packages/cli/src/formatters/__tests__/junit.test.ts
Expand Up @@ -4,6 +4,7 @@ import { DiagnosticSeverity } from '@stoplight/types';

const oas3SchemaErrors = require('./__fixtures__/oas3-schema-errors.json');
const mixedErrors = require('./__fixtures__/mixed-errors.json');
const specialXmlStrings = require('./__fixtures__/errors-with-special-xml-strings.json');

describe('JUnit formatter', () => {
let parse: Parser['parseStringPromise'];
Expand Down Expand Up @@ -69,7 +70,7 @@ describe('JUnit formatter', () => {
$: {
message: "should have required property '$ref'",
},
_: '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' (oas3-schema) at path #/paths/~1pets/get/responses/200/headers/header-1",
},
],
},
Expand Down Expand Up @@ -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/]]>/<![CDATA[/leaf)',
time: '0',
},
failure: [
{
$: {
message: 'start <![CDATA[ ]]> <![CDATA[ end',
},
_: 'line 1, col 1, start <![CDATA[ ]]> <![CDATA[ end (special-cdata-strings) at path #/root/]]>/<![CDATA[/leaf',
},
],
},
],
},
],
},
});
});
});
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 @@ -53,8 +60,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(path)}`;
output += `${prepareForCdata(result.message)} (${result.code}) `;
output += `at path ${prepareForCdata(path)}`;
output += ']]>';
output += `</failure>`;
output += '</testcase>\n';
Expand Down
Expand Up @@ -56,6 +56,6 @@ info:
<?xml version="1.0" encoding="utf-8"?>
<testsuites>
<testsuite package="org.spectral" time="0" tests="1" errors="0" failures="1" name="{document}">
<testcase time="0" name="org.spectral.api-servers(#)" classname="{document}"><failure message="&quot;servers&quot; must be present and non-empty array."><![CDATA[line 1, col 1, &quot;servers&quot; must be present and non-empty array. (api-servers) at path #]]></failure></testcase>
<testcase time="0" name="org.spectral.api-servers(#)" classname="{document}"><failure message="&quot;servers&quot; must be present and non-empty array."><![CDATA[line 1, col 1, "servers" must be present and non-empty array. (api-servers) at path #]]></failure></testcase>
</testsuite>
</testsuites>

0 comments on commit cabd2a9

Please sign in to comment.