-
Notifications
You must be signed in to change notification settings - Fork 51
/
test.js
322 lines (261 loc) · 7.66 KB
/
test.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* eslint-env mocha */
'use strict';
var path = require('path');
var assert = require('assert');
var Vinyl = require('vinyl');
var streamAssert = require('stream-assert');
var tempWrite = require('temp-write');
var jscs = require('./');
var stdoutWrite = process.stdout.write;
var stdoutStub;
function stubStdout() {
stdoutStub = '';
process.stdout.write = function (str) {
stdoutStub += str;
};
}
function teardown() {
process.stdout.write = stdoutWrite;
}
// in case test fails due to timeout
afterEach(teardown);
it('should check code style of JS files', function (cb) {
var stream = jscs();
stream
.pipe(streamAssert.first(function (file) {
var errors = file.jscs.errors;
assert(/Multiple var declaration/.test(errors.explainError(errors.getErrorList()[0], false)));
}))
.pipe(streamAssert.second(function (file) {
var errors = file.jscs.errors;
assert(/Illegal space before/.test(errors.explainError(errors.getErrorList()[1], false)));
}))
.pipe(streamAssert.end(cb));
stream.write(new Vinyl({
base: __dirname,
path: path.join(__dirname, 'fixture.js'),
contents: new Buffer('var x = 1,y = 2;')
}));
stream.write(new Vinyl({
base: __dirname,
path: path.join(__dirname, 'fixture2.js'),
contents: new Buffer('var x = { a: 1 };')
}));
stream.end();
});
it('should check code style of JS files using a preset', function (cb) {
var stream = jscs({
configPath: tempWrite.sync(JSON.stringify({preset: 'google'}))
});
stream
.pipe(streamAssert.first(function (file) {
var errors = file.jscs.errors;
assert(/Missing line feed at file end/.test(errors.explainError(errors.getErrorList()[1], false)));
}))
.pipe(streamAssert.end(cb));
stream.write(new Vinyl({
base: __dirname,
path: path.join(__dirname, 'fixture.js'),
contents: new Buffer('var x = 1,y = 2;')
}));
stream.end();
});
it('should pass valid files', function (cb) {
var stream = jscs();
stream.pipe(jscs.reporter('fail')).on('error', function (err) {
assert(false, err);
}).on('end', cb).resume();
stream.write(new Vinyl({
path: path.join(__dirname, 'fixture.js'),
contents: new Buffer('var x = 1; var y = 2;')
}));
stream.end();
});
it('should respect "excludeFiles" from config', function (cb) {
var stream = jscs();
stream.pipe(jscs.reporter('fail')).on('error', function (err) {
assert(false, err);
}).on('end', cb).resume();
stream.write(new Vinyl({
base: __dirname,
path: path.join(__dirname, 'excluded.js'),
contents: new Buffer('var x = { a: 1 };')
}));
stream.end();
});
it('should accept configPath options', function (cb) {
var stream = jscs({
configPath: '.jscsrc'
});
stream
.pipe(streamAssert.first(function (file) {
var errors = file.jscs.errors;
var errorList = errors.getErrorList();
assert(errorList.length === 1 && /Multiple var declaration/.test(errors.explainError(errorList[0], false)));
}))
.pipe(streamAssert.end(cb));
stream.write(new Vinyl({
base: __dirname,
path: path.join(__dirname, 'fixture.js'),
contents: new Buffer('import x from \'x\'; var x = 1, y = 2;')
}));
stream.end();
});
it('should accept the fix option', function (cb) {
var stream = jscs({
fix: true
});
stream.on('data', function (file) {
assert.equal(file.contents.toString(), 'var x = {a: 1, b: 2}');
});
stream.on('end', cb);
stream.write(new Vinyl({
base: __dirname,
path: path.join(__dirname, 'fixture.js'),
contents: new Buffer('var x = { a: 1, b: 2 }')
}));
stream.end();
});
it('should run autofix over as many errors as possible', function (done) {
var config = {
maxErrors: 1,
requireSpaceBeforeBinaryOperators: ['=']
};
var validJS = 'var foo =1;\nvar bar =2;';
var invalidJS = 'var foo=1;\nvar bar=2;';
var stream = jscs({
fix: true,
configPath: tempWrite.sync(JSON.stringify(config))
});
stream
.pipe(streamAssert.first(function (file) {
assert.equal(file.contents.toString(), validJS);
}))
.pipe(streamAssert.second(function (file) {
assert.equal(file.contents.toString(), validJS);
}))
.pipe(streamAssert.end(done));
stream.write(new Vinyl({
base: __dirname,
path: path.join(__dirname, 'fixture.js'),
contents: new Buffer(invalidJS)
}));
stream.write(new Vinyl({
base: __dirname,
path: path.join(__dirname, 'fixture2.js'),
contents: new Buffer(invalidJS)
}));
stream.end();
});
it('should not mutate the options object passed as argument', function () {
var options = {foo: true};
jscs(options);
assert.equal(options.foo, true);
});
describe('Reporter', function () {
it('`.reporter()` called with no arguments should use the default reporter', function (cb) {
stubStdout();
var stream = jscs();
stream.pipe(jscs.reporter()).on('end', function () {
assert(/Multiple var declaration[^]*---\^/.test(stdoutStub));
teardown();
cb();
}).resume();
stream.write(new Vinyl({
base: __dirname,
path: path.join(__dirname, 'fixture.js'),
contents: new Buffer('var x = 1,y = 2;')
}));
stream.end();
});
it('`.reporter()` called with a non-function argument should delegate reporter loading to JSCS', function (cb) {
stubStdout();
var stream = jscs();
stream.pipe(jscs.reporter('inlinesingle')).on('end', function () {
assert(/line 1, col 8, disallowMultipleVarDecl: Multiple var declaration/.test(stdoutStub));
teardown();
cb();
}).resume();
stream.write(new Vinyl({
base: __dirname,
path: path.join(__dirname, 'fixture.js'),
contents: new Buffer('var x = 1,y = 2;')
}));
stream.end();
});
it('`.reporter()` should accept a function', function (cb) {
function reporterFn(errors) {
assert(/Multiple var declaration/.test(errors[0].explainError(errors[0].getErrorList()[0], false)));
cb();
}
var stream = jscs();
stream.pipe(jscs.reporter(reporterFn)).resume();
stream.write(new Vinyl({
base: __dirname,
path: path.join(__dirname, 'fixture.js'),
contents: new Buffer('var x = 1,y = 2;')
}));
stream.end();
});
it('`fail` reporter should emit an error at the end of the stream', function (cb) {
var stream = jscs();
var passedErrorAssertion = false;
stream
.pipe(jscs.reporter('fail'))
.on('error', function (err) {
assert(err instanceof Error && /JSCS/.test(err.message));
passedErrorAssertion = true;
})
.pipe(streamAssert.length(2))
.pipe(streamAssert.first(function (file) {
var errors = file.jscs.errors;
assert(/Multiple var declaration/.test(errors.explainError(errors.getErrorList()[0], false)));
}))
.pipe(streamAssert.second(function (file) {
assert(file.jscs.success);
}))
.pipe(streamAssert.end(function (err) {
if (err) {
cb(err);
return;
}
assert(passedErrorAssertion, 'Did not emit an error');
cb();
}));
stream.write(new Vinyl({
base: __dirname,
path: path.join(__dirname, 'fixture.js'),
contents: new Buffer('var x = 1,y = 2;')
}));
stream.write(new Vinyl({
base: __dirname,
path: path.join(__dirname, 'passing.js'),
contents: new Buffer('var x = 1; var y = 2;')
}));
stream.end();
});
it('`failImmediately` reporter should emit an error immediately', function (cb) {
var stream = jscs();
stream
.pipe(jscs.reporter('failImmediately'))
.on('error', function (err) {
assert(err instanceof Error && /JSCS/.test(err.message));
cb();
})
.pipe(streamAssert.second(function () {
cb(new Error('Did not emit an error immediately'));
}))
.pipe(streamAssert.end());
stream.write(new Vinyl({
base: __dirname,
path: path.join(__dirname, 'fixture.js'),
contents: new Buffer('var x = 1,y = 2;')
}));
stream.write(new Vinyl({
base: __dirname,
path: path.join(__dirname, 'passing.js'),
contents: new Buffer('var x = 1; var y = 2;')
}));
stream.end();
});
});