Skip to content

Commit

Permalink
Fix: Files with no failures get "passing" testcase (#9547)
Browse files Browse the repository at this point in the history
This fixes JUnit parsing errors which treat no testcases as a failure (e.g. Atlassian bamboo).
  • Loading branch information
samlev authored and ilyavolodin committed Nov 1, 2017
1 parent ab0f66d commit accc490
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
36 changes: 21 additions & 15 deletions lib/formatters/junit.js
Expand Up @@ -39,22 +39,28 @@ module.exports = function(results) {

const messages = result.messages;

output += `<testsuite package="org.eslint" time="0" tests="${messages.length}" errors="${messages.length}" name="${result.filePath}">\n`;
messages.forEach(message => {
const type = message.fatal ? "error" : "failure";
if (messages.length > 0) {
output += `<testsuite package="org.eslint" time="0" tests="${messages.length}" errors="${messages.length}" name="${result.filePath}">\n`;
messages.forEach(message => {
const type = message.fatal ? "error" : "failure";

output += `<testcase time="0" name="org.eslint.${message.ruleId || "unknown"}">`;
output += `<${type} message="${xmlEscape(message.message || "")}">`;
output += "<![CDATA[";
output += `line ${message.line || 0}, col `;
output += `${message.column || 0}, ${getMessageType(message)}`;
output += ` - ${xmlEscape(message.message || "")}`;
output += (message.ruleId ? ` (${message.ruleId})` : "");
output += "]]>";
output += `</${type}>`;
output += "</testcase>\n";
});
output += "</testsuite>\n";
output += `<testcase time="0" name="org.eslint.${message.ruleId || "unknown"}">`;
output += `<${type} message="${xmlEscape(message.message || "")}">`;
output += "<![CDATA[";
output += `line ${message.line || 0}, col `;
output += `${message.column || 0}, ${getMessageType(message)}`;
output += ` - ${xmlEscape(message.message || "")}`;
output += (message.ruleId ? ` (${message.ruleId})` : "");
output += "]]>";
output += `</${type}>`;
output += "</testcase>\n";
});
output += "</testsuite>\n";
} else {
output += `<testsuite package="org.eslint" time="0" tests="1" errors="0" name="${result.filePath}">\n`;
output += `<testcase time="0" name="${result.filePath}" />\n`;
output += "</testsuite>\n";
}

});

Expand Down
15 changes: 14 additions & 1 deletion tests/lib/formatters/junit.js
Expand Up @@ -195,7 +195,20 @@ describe("formatter:junit", () => {
it("should return 2 <testsuite>", () => {
const result = formatter(code);

assert.strictEqual(result.replace(/\n/g, ""), "<?xml version=\"1.0\" encoding=\"utf-8\"?><testsuites><testsuite package=\"org.eslint\" time=\"0\" tests=\"1\" errors=\"1\" name=\"foo.js\"><testcase time=\"0\" name=\"org.eslint.foo\"><failure message=\"Unexpected foo.\"><![CDATA[line 5, col 10, Warning - Unexpected foo. (foo)]]></failure></testcase></testsuite><testsuite package=\"org.eslint\" time=\"0\" tests=\"0\" errors=\"0\" name=\"bar.js\"></testsuite></testsuites>");
assert.strictEqual(result.replace(/\n/g, ""), "<?xml version=\"1.0\" encoding=\"utf-8\"?><testsuites><testsuite package=\"org.eslint\" time=\"0\" tests=\"1\" errors=\"1\" name=\"foo.js\"><testcase time=\"0\" name=\"org.eslint.foo\"><failure message=\"Unexpected foo.\"><![CDATA[line 5, col 10, Warning - Unexpected foo. (foo)]]></failure></testcase></testsuite><testsuite package=\"org.eslint\" time=\"0\" tests=\"1\" errors=\"0\" name=\"bar.js\"><testcase time=\"0\" name=\"bar.js\" /></testsuite></testsuites>");
});
});

describe("when passed a file with no errors", () => {
const code = [{
filePath: "foo.js",
messages: []
}];

it("should print a passing <testcase>", () => {
const result = formatter(code);

assert.strictEqual(result.replace(/\n/g, ""), "<?xml version=\"1.0\" encoding=\"utf-8\"?><testsuites><testsuite package=\"org.eslint\" time=\"0\" tests=\"1\" errors=\"0\" name=\"foo.js\"><testcase time=\"0\" name=\"foo.js\" /></testsuite></testsuites>");
});
});
});

1 comment on commit accc490

@adhamu
Copy link

@adhamu adhamu commented on accc490 Nov 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to say that this is a very welcome commit and one that our team was head scratching for days about so I'm glad I found this and hope it makes it into the next tagged release soon.

Please sign in to comment.