Skip to content

Commit accc490

Browse files
samlevilyavolodin
authored andcommittedNov 1, 2017
Fix: Files with no failures get "passing" testcase (#9547)
This fixes JUnit parsing errors which treat no testcases as a failure (e.g. Atlassian bamboo).
1 parent ab0f66d commit accc490

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed
 

‎lib/formatters/junit.js

+21-15
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,28 @@ module.exports = function(results) {
3939

4040
const messages = result.messages;
4141

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

46-
output += `<testcase time="0" name="org.eslint.${message.ruleId || "unknown"}">`;
47-
output += `<${type} message="${xmlEscape(message.message || "")}">`;
48-
output += "<![CDATA[";
49-
output += `line ${message.line || 0}, col `;
50-
output += `${message.column || 0}, ${getMessageType(message)}`;
51-
output += ` - ${xmlEscape(message.message || "")}`;
52-
output += (message.ruleId ? ` (${message.ruleId})` : "");
53-
output += "]]>";
54-
output += `</${type}>`;
55-
output += "</testcase>\n";
56-
});
57-
output += "</testsuite>\n";
47+
output += `<testcase time="0" name="org.eslint.${message.ruleId || "unknown"}">`;
48+
output += `<${type} message="${xmlEscape(message.message || "")}">`;
49+
output += "<![CDATA[";
50+
output += `line ${message.line || 0}, col `;
51+
output += `${message.column || 0}, ${getMessageType(message)}`;
52+
output += ` - ${xmlEscape(message.message || "")}`;
53+
output += (message.ruleId ? ` (${message.ruleId})` : "");
54+
output += "]]>";
55+
output += `</${type}>`;
56+
output += "</testcase>\n";
57+
});
58+
output += "</testsuite>\n";
59+
} else {
60+
output += `<testsuite package="org.eslint" time="0" tests="1" errors="0" name="${result.filePath}">\n`;
61+
output += `<testcase time="0" name="${result.filePath}" />\n`;
62+
output += "</testsuite>\n";
63+
}
5864

5965
});
6066

‎tests/lib/formatters/junit.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,20 @@ describe("formatter:junit", () => {
195195
it("should return 2 <testsuite>", () => {
196196
const result = formatter(code);
197197

198-
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>");
198+
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>");
199+
});
200+
});
201+
202+
describe("when passed a file with no errors", () => {
203+
const code = [{
204+
filePath: "foo.js",
205+
messages: []
206+
}];
207+
208+
it("should print a passing <testcase>", () => {
209+
const result = formatter(code);
210+
211+
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>");
199212
});
200213
});
201214
});

1 commit comments

Comments
 (1)

adhamu commented on Nov 9, 2017

@adhamu

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.