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

Add junit output for files which are successfully linted. #4566

Merged
merged 5 commits into from Mar 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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())]);
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved

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);
});
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved

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);
});
});