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 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/formatters/junitFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,20 @@ 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<string>();
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()),
);
let previousFilename: string | null = null;
for (const failure of failuresSorted) {
if (!failureFileNames.has(failure.getFileName())) {
failureFileNames.add(failure.getFileName());
}
const lineAndCharacter = failure.getStartPosition().getLineAndCharacter();
const message = this.escapeXml(failure.getFailure());
const rule = this.escapeXml(failure.getRuleName());
Expand All @@ -76,6 +81,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
Original file line number Diff line number Diff line change
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);
});
});