-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
junit.js
95 lines (75 loc) · 2.62 KB
/
junit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* @fileoverview jUnit Reporter
* @author Jamund Ferguson
*/
/*jshint node:true*/
//------------------------------------------------------------------------------
// Helper Functions
//------------------------------------------------------------------------------
function getMessageType(message, rules) {
if (message.fatal || rules[message.ruleId] === 2) {
return "Error";
} else {
return "Warning";
}
}
/**
* Replace special characters before write to output.
*
* Rules:
* - single quotes is the escape sequence for double-quotes
* - < is the escape sequence for <
* - > is the escape sequence for >
* - " is the escape sequence for "
* - ' is the escape sequence for '
* - & is the escape sequence for &
*
* @param {String} message to escape
* @return escaped message as {String}
*/
function escapeSpecialCharacters(str) {
str = str || "";
var pairs = {
"&": "&",
"\"": """,
"'": "'",
"<": "<",
">": ">"
};
for (var r in pairs) {
str = str.replace(new RegExp(r, "g"), pairs[r]);
}
return str || "";
}
//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------
module.exports = function(results, config) {
var output = "",
rules = config.rules || {};
output += "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
output += "<testsuites>\n";
results.forEach(function(result) {
var messages = result.messages;
if (messages.length) {
output += "<testsuite package=\"org.eslint\" time=\"0\" tests=\"" + messages.length + "\" errors=\"" + messages.length + "\" name=\"" + result.filePath + "\">\n";
}
messages.forEach(function(message) {
var type = message.fatal ? "error" : "failure";
output += "<testcase time=\"0\" name=\"org.eslint." + (message.ruleId || "unknown") + "\">";
output += "<" + type + " message=\"" + escapeSpecialCharacters(message.message) + "\">";
output += "<![CDATA[";
output += "line " + message.line + ", col ";
output += message.column + ", " + getMessageType(message, rules);
output += " - " + escapeSpecialCharacters(message.message);
output += "]]>";
output += "</" + type + ">";
output += "</testcase>\n";
});
if (messages.length) {
output += "</testsuite>\n";
}
});
output += "</testsuites>\n";
return output;
};