Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
Add junit output for files which are successfully linted. (#4566)
Browse files Browse the repository at this point in the history
* Add junit output for files which are successfully linted.

* Fix linting issues

* Optimised JUnit formatter.

* Added test which confirms that JUnit formatter correctly handles a mixture of files which lint successfully and files which contain errors.

* Refactored JUnit formatter.
  • Loading branch information
darrena092 authored and Josh Goldberg committed Mar 10, 2019
1 parent 521e5a3 commit c5da14a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 7 deletions.
15 changes: 14 additions & 1 deletion src/formatters/junitFormatter.ts
Expand Up @@ -41,9 +41,11 @@ export class Formatter extends AbstractFormatter {
};
/* tslint:enable:object-literal-sort-keys */

public format(failures: RuleFailure[]): string {
public format(failures: RuleFailure[], _fixes?: RuleFailure[], fileNames?: string[]): string {
let output = '<?xml version="1.0" encoding="utf-8"?><testsuites package="tslint">';

const failureFileNames: Set<string> = new Set([...failures.map(f => f.getFileName())]);

if (failures.length !== 0) {
const failuresSorted = failures.sort((a, b) =>
a.getFileName().localeCompare(b.getFileName()),
Expand Down Expand Up @@ -76,6 +78,17 @@ export class Formatter extends AbstractFormatter {
}
}

if (fileNames !== undefined && fileNames.length !== 0) {
// Filter out files which have had a failure associated with them.
const filteredFileNames = fileNames.filter(fileName => !failureFileNames.has(fileName));

for (const fileName of filteredFileNames) {
output += `<testsuite name="${this.escapeXml(fileName)}" errors="0">`;
output += `<testcase name="${this.escapeXml(fileName)}" />`;
output += `</testsuite>`;
}
}

output += "</testsuites>";
return output;
}
Expand Down
73 changes: 67 additions & 6 deletions test/formatters/junitFormatterTests.ts
Expand Up @@ -106,14 +106,75 @@ describe("JUnit Formatter", () => {
</testsuite>
</testsuites>`.replace(/>\s+/g, ">"); // Remove whitespace between tags;

assert.equal(formatter.format(failures), expectedResult);
assert.equal(formatter.format(failures, [], [TEST_FILE_1, TEST_FILE_2]), expectedResult);
});

it("handles no failures", () => {
const result = formatter.format([]);
assert.deepEqual(
result,
'<?xml version="1.0" encoding="utf-8"?><testsuites package="tslint"></testsuites>',
);
const result = formatter.format([], [], ["test1.ts", "test2.ts", "test3.ts"]);
const expectedResult = `<?xml version="1.0" encoding="utf-8"?>
<testsuites package="tslint">
<testsuite name="test1.ts" errors="0">
<testcase name="test1.ts" />
</testsuite>
<testsuite name="test2.ts" errors="0">
<testcase name="test2.ts" />
</testsuite>
<testsuite name="test3.ts" errors="0">
<testcase name="test3.ts" />
</testsuite>
</testsuites>`.replace(/>\s+/g, ">");

assert.equal(result, expectedResult);
});

it("handles a mixture of failures and successes", () => {
const maxPosition1 = sourceFile1.getFullWidth();

const failures = [
createFailure(sourceFile1, 0, 1, "first failure", "first-name", undefined, "error"),
createFailure(
sourceFile1,
2,
3,
"&<>'\" should be escaped",
"escape",
undefined,
"error",
),
createFailure(
sourceFile1,
maxPosition1 - 1,
maxPosition1,
"last failure",
"last-name",
undefined,
"error",
),
];

const expectedResult = `<?xml version="1.0" encoding="utf-8"?>
<testsuites package="tslint">
<testsuite name="formatters/jsonFormatter.test.ts">
<testcase name="first-name" classname="formatters/jsonFormatter.test.ts">
<failure type="error">first failure Line 1, Column 1</failure>
</testcase>
<testcase name="escape" classname="formatters/jsonFormatter.test.ts">
<failure type="error">&amp;&lt;&gt;&#39;&quot; should be escaped Line 1, Column 3</failure>
</testcase>
<testcase name="last-name" classname="formatters/jsonFormatter.test.ts">
<failure type="error">last failure Line 6, Column 3</failure>
</testcase>
</testsuite>
<testsuite name="test1.ts" errors="0">
<testcase name="test1.ts" />
</testsuite>
<testsuite name="test2.ts" errors="0">
<testcase name="test2.ts" />
</testsuite>
</testsuites>`.replace(/>\s+/g, ">");

const result = formatter.format(failures, [], [TEST_FILE_1, "test1.ts", "test2.ts"]);

assert.equal(result, expectedResult);
});
});

0 comments on commit c5da14a

Please sign in to comment.