forked from eslint/eslint
/
junit.js
218 lines (180 loc) · 10.3 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* @fileoverview Tests for jUnit Formatter.
* @author Jamund Ferguson
*/
/* jshint node:true */
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const assert = require("chai").assert,
formatter = require("../../../../lib/cli-engine/formatters/junit"),
process = require("process");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const suppliedFilePath = (process.platform === "win32") ? "C:\\path\\to\\foo.js" : "/path/to/foo.js";
const expectedClassName = (process.platform === "win32") ? "C:\\path\\to\\foo" : "/path/to/foo";
describe("formatter:junit", () => {
describe("when there are no problems", () => {
const code = [];
it("should not complain about anything", () => {
const result = formatter(code);
assert.strictEqual(result.replace(/\n/gu, ""), "<?xml version=\"1.0\" encoding=\"utf-8\"?><testsuites></testsuites>");
});
});
describe("when passed a single message", () => {
const code = [{
filePath: suppliedFilePath,
messages: [{
message: "Unexpected foo.",
severity: 2,
line: 5,
column: 10,
ruleId: "foo"
}]
}];
it("should return a single <testcase> with a message and the line and col number in the body (error)", () => {
const result = formatter(code);
assert.strictEqual(result.replace(/\n/gu, ""), `<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite package="org.eslint" time="0" tests="1" errors="1" name="${suppliedFilePath}"><testcase time="0" name="org.eslint.foo" classname="${expectedClassName}"><failure message="Unexpected foo."><![CDATA[line 5, col 10, Error - Unexpected foo. (foo)]]></failure></testcase></testsuite></testsuites>`);
});
it("should return a single <testcase> with a message and the line and col number in the body (warning)", () => {
code[0].messages[0].severity = 1;
const result = formatter(code);
assert.strictEqual(result.replace(/\n/gu, ""), `<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite package="org.eslint" time="0" tests="1" errors="1" name="${suppliedFilePath}"><testcase time="0" name="org.eslint.foo" classname="${expectedClassName}"><failure message="Unexpected foo."><![CDATA[line 5, col 10, Warning - Unexpected foo. (foo)]]></failure></testcase></testsuite></testsuites>`);
});
});
describe("when passed a fatal error message", () => {
const code = [{
filePath: suppliedFilePath,
messages: [{
fatal: true,
message: "Unexpected foo.",
line: 5,
column: 10,
ruleId: "foo"
}]
}];
it("should return a single <testcase> and an <error>", () => {
const result = formatter(code);
assert.strictEqual(result.replace(/\n/gu, ""), `<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite package="org.eslint" time="0" tests="1" errors="1" name="${suppliedFilePath}"><testcase time="0" name="org.eslint.foo" classname="${expectedClassName}"><error message="Unexpected foo."><![CDATA[line 5, col 10, Error - Unexpected foo. (foo)]]></error></testcase></testsuite></testsuites>`);
});
});
describe("when passed a fatal error message with no line or column", () => {
const code = [{
filePath: suppliedFilePath,
messages: [{
fatal: true,
message: "Unexpected foo."
}]
}];
it("should return a single <testcase> and an <error>", () => {
const result = formatter(code);
assert.strictEqual(result.replace(/\n/gu, ""), `<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite package="org.eslint" time="0" tests="1" errors="1" name="${suppliedFilePath}"><testcase time="0" name="org.eslint.unknown" classname="${expectedClassName}"><error message="Unexpected foo."><![CDATA[line 0, col 0, Error - Unexpected foo.]]></error></testcase></testsuite></testsuites>`);
});
});
describe("when passed a fatal error message with no line, column, or message text", () => {
const code = [{
filePath: suppliedFilePath,
messages: [{
fatal: true
}]
}];
it("should return a single <testcase> and an <error>", () => {
const result = formatter(code);
assert.strictEqual(result.replace(/\n/gu, ""), `<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite package="org.eslint" time="0" tests="1" errors="1" name="${suppliedFilePath}"><testcase time="0" name="org.eslint.unknown" classname="${expectedClassName}"><error message=""><![CDATA[line 0, col 0, Error - ]]></error></testcase></testsuite></testsuites>`);
});
});
describe("when passed multiple messages", () => {
const code = [{
filePath: suppliedFilePath,
messages: [{
message: "Unexpected foo.",
severity: 2,
line: 5,
column: 10,
ruleId: "foo"
}, {
message: "Unexpected bar.",
severity: 1,
line: 6,
column: 11,
ruleId: "bar"
}]
}];
it("should return a multiple <testcase>'s", () => {
const result = formatter(code);
assert.strictEqual(result.replace(/\n/gu, ""), `<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite package="org.eslint" time="0" tests="2" errors="2" name="${suppliedFilePath}"><testcase time="0" name="org.eslint.foo" classname="${expectedClassName}"><failure message="Unexpected foo."><![CDATA[line 5, col 10, Error - Unexpected foo. (foo)]]></failure></testcase><testcase time="0" name="org.eslint.bar" classname="${expectedClassName}"><failure message="Unexpected bar."><![CDATA[line 6, col 11, Warning - Unexpected bar. (bar)]]></failure></testcase></testsuite></testsuites>`);
});
});
describe("when passed special characters", () => {
const code = [{
filePath: suppliedFilePath,
messages: [{
message: "Unexpected <foo></foo>\b\t\n\f\r牛逼.",
severity: 1,
line: 5,
column: 10,
ruleId: "foo"
}]
}];
it("should make them go away", () => {
const result = formatter(code);
assert.strictEqual(result.replace(/\n/gu, ""), `<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite package="org.eslint" time="0" tests="1" errors="1" name="${suppliedFilePath}"><testcase time="0" name="org.eslint.foo" classname="${expectedClassName}"><failure message="Unexpected <foo></foo>	  牛逼."><![CDATA[line 5, col 10, Warning - Unexpected <foo></foo>	  牛逼. (foo)]]></failure></testcase></testsuite></testsuites>`);
});
});
describe("when passed multiple files with 1 message each", () => {
const code = [{
filePath: suppliedFilePath,
messages: [{
message: "Unexpected foo.",
severity: 1,
line: 5,
column: 10,
ruleId: "foo"
}]
}, {
filePath: "bar.js",
messages: [{
message: "Unexpected bar.",
severity: 2,
line: 6,
column: 11,
ruleId: "bar"
}]
}];
it("should return 2 <testsuite>'s", () => {
const result = formatter(code);
assert.strictEqual(result.replace(/\n/gu, ""), `<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite package="org.eslint" time="0" tests="1" errors="1" name="${suppliedFilePath}"><testcase time="0" name="org.eslint.foo" classname="${expectedClassName}"><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="1" name="bar.js"><testcase time="0" name="org.eslint.bar" classname="bar"><failure message="Unexpected bar."><![CDATA[line 6, col 11, Error - Unexpected bar. (bar)]]></failure></testcase></testsuite></testsuites>`);
});
});
describe("when passed multiple files should print even if no errors", () => {
const code = [{
filePath: suppliedFilePath,
messages: [{
message: "Unexpected foo.",
severity: 1,
line: 5,
column: 10,
ruleId: "foo"
}]
}, {
filePath: "bar.js",
messages: []
}];
it("should return 2 <testsuite>", () => {
const result = formatter(code);
assert.strictEqual(result.replace(/\n/gu, ""), `<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite package="org.eslint" time="0" tests="1" errors="1" name="${suppliedFilePath}"><testcase time="0" name="org.eslint.foo" classname="${expectedClassName}"><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" classname="bar" /></testsuite></testsuites>`);
});
});
describe("when passed a file with no errors", () => {
const code = [{
filePath: suppliedFilePath,
messages: []
}];
it("should print a passing <testcase>", () => {
const result = formatter(code);
assert.strictEqual(result.replace(/\n/gu, ""), `<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite package="org.eslint" time="0" tests="1" errors="0" name="${suppliedFilePath}"><testcase time="0" name="${suppliedFilePath}" classname="${expectedClassName}" /></testsuite></testsuites>`);
});
});
});