forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulp-util-tests.ts
597 lines (519 loc) · 17 KB
/
gulp-util-tests.ts
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/// <reference path="../mocha/mocha.d.ts" />
/// <reference path="../should/should.d.ts" />
/// <reference path='../gulp/gulp.d.ts' />
/// <reference path='../through2/through2.d.ts' />
/// <reference path='gulp-util.d.ts' />
import gulp = require('gulp');
import util = require('gulp-util');
import path = require('path');
import should = require('should');
import Stream = require('stream');
import through = require('through2');
var es = require('event-stream');
// ReSharper disable WrongExpressionStatement
describe('File()', () => {
it('should return a valid file', done => {
var fname = path.join(__dirname, './fixtures/test.coffee');
var base = path.join(__dirname, './fixtures/');
var file = new util.File({
base: base,
cwd: __dirname,
path: fname
});
should.exist(file, 'root');
should.exist(file.relative, 'relative');
should.exist(file.path, 'path');
should.exist(file.cwd, 'cwd');
should.exist(file.base, 'base');
file.path.should.equal(fname);
file.cwd.should.equal(__dirname);
file.base.should.equal(base);
file.relative.should.equal('test.coffee');
done();
});
it('should return a valid file 2', done => {
var fname = path.join(__dirname, './fixtures/test.coffee');
var base = __dirname;
var file = new util.File({
base: base,
cwd: __dirname,
path: fname
});
should.exist(file, 'root');
should.exist(file.relative, 'relative');
should.exist(file.path, 'path');
should.exist(file.cwd, 'cwd');
should.exist(file.base, 'base');
file.path.should.equal(fname);
file.cwd.should.equal(__dirname);
file.base.should.equal(base);
file.relative.should.equal(path.normalize('fixtures/test.coffee'));
done();
});
});
describe('replaceExtension()', () => {
it('should return a valid replaced extension on nested', done => {
var fname = path.join(__dirname, './fixtures/test.coffee');
var expected = path.join(__dirname, './fixtures/test.js');
var nu = util.replaceExtension(fname, '.js');
should.exist(nu);
nu.should.equal(expected);
done();
});
it('should return a valid replaced extension on flat', done => {
var fname = 'test.coffee';
var expected = 'test.js';
var nu = util.replaceExtension(fname, '.js');
should.exist(nu);
nu.should.equal(expected);
done();
});
it('should not return a valid replaced extension on empty string', done => {
var fname = '';
var expected = '';
var nu = util.replaceExtension(fname, '.js');
should.exist(nu);
nu.should.equal(expected);
done();
});
});
describe('colors', () => {
it('should be a chalk instance', done => {
util.colors.should.equal(require('chalk'));
done();
});
});
util.log('stuff happened', 'Really it did', util.colors.cyan('123'));
describe('date', () => {
it('should be a date format instance', done => {
util.date.should.equal(require('dateformat'));
done();
});
it('should return today\'s date', done => {
var time = new Date();
var dateutil = util.date('HH:MM:ss');
dateutil.should.equal(('0' + time.getHours()).slice(-2) + ':' + ('0' + time.getMinutes()).slice(-2) + ':' + ('0' + time.getSeconds()).slice(-2));
done();
});
});
var now = new Date();
// Basic usage
util.date(now, 'dddd, mmmm dS, yyyy, h:MM:ss TT');
// Saturday, June 9th, 2007, 5:46:21 PM
// You can use one of several named masks
util.date(now, 'isoDateTime');
// 2007-06-09T17:46:21
// ...Or add your own
util.date.masks.hammerTime = 'HH:MM! "Can\'t touch this!"';
util.date(now, 'hammerTime');
// 17:46! Can't touch this!
// When using the standalone dateFormat function,
// you can also provide the date as a string
util.date('Jun 9 2007', 'fullDate');
// Saturday, June 9, 2007
// Note that if you don't include the mask argument,
// dateFormat.masks.default is used
util.date(now);
// Sat Jun 09 2007 17:46:21
// And if you don't include the date argument,
// the current date and time is used
util.date();
// Sat Jun 09 2007 17:46:22
// You can also skip the date argument (as long as your mask doesn't
// contain any numbers), in which case the current date/time is used
util.date('longTime');
// 5:46:22 PM EST
// And finally, you can convert local time to UTC time. Simply pass in
// true as an additional argument (no argument skipping allowed in this case):
util.date(now, 'longTime', true);
// 10:46:21 PM UTC
// ...Or add the prefix 'UTC:' or 'GMT:' to your mask.
util.date(now, 'UTC:h:MM:ss TT Z');
// 10:46:21 PM UTC
// You can also get the ISO 8601 week of the year:
util.date(now, 'W');
// 42
// and also get the ISO 8601 numeric representation of the day of the week:
util.date(now, 'N');
// 6
describe('log()', () => {
it('should work i guess', done => {
var writtenValue = '';
util.log(1, 2, 3, 4, 'five');
var time = util.date(new Date(), 'HH:MM:ss');
writtenValue.should.eql('[' + util.colors.gray(time) + '] 1 2 3 4 five\n');
done();
});
});
describe('template()', () => {
it('should work with just a template', done => {
var opt = {
name: 'todd',
file: {
path: 'hi.js'
}
};
var expected = 'test todd hi.js';
var tmpl = util.template('test <%= name %> <%= file.path %>');
should.exist(tmpl);
'function'.should.equal(typeof (tmpl));
// eval it now
var etmpl = tmpl(opt);
'string'.should.equal(typeof (etmpl));
etmpl.should.equal(expected);
done();
});
it('should work with a template and data', done => {
var opt = {
name: 'todd',
file: {
path: 'hi.js'
}
};
var expected = 'test todd hi.js';
var tmpl = util.template('test <%= name %> <%= file.path %>', opt);
should.exist(tmpl);
'string'.should.equal(typeof (tmpl));
tmpl.should.equal(expected);
done();
});
//it('should throw an error when no file object is passed', done => {
// var opt = {
// name: 'todd'
// };
// try {
// var tmpl = util.template('test <%= name %> <%= file.path %>', opt);
// } catch (err) {
// should.exist(err);
// done();
// }
//});
it('should ignore modified templateSettings', done => {
var templateSettings = require('lodash.templatesettings');
templateSettings.interpolate = /\{\{([\s\S]+?)\}\}/g;
var opt = {
name: 'todd',
file: {
path: 'hi.js'
}
};
var expected = 'test {{name}} hi.js';
var tmpl = util.template('test {{name}} <%= file.path %>');
should.exist(tmpl);
'function'.should.equal(typeof (tmpl));
// eval it now
var etmpl = tmpl(opt);
'string'.should.equal(typeof (etmpl));
etmpl.should.equal(expected);
done();
});
it('should allow ES6 delimiters', done => {
var opt = {
name: 'todd',
file: {
path: 'hi.js'
}
};
var expected = 'test todd hi.js';
var tmpl = util.template('test ${name} ${file.path}');
should.exist(tmpl);
'function'.should.equal(typeof (tmpl));
// eval it now
var etmpl = tmpl(opt);
'string'.should.equal(typeof (etmpl));
etmpl.should.equal(expected);
done();
});
});
describe('env', () => {
it('should exist', done => {
should.exist(util.env);
should.exist(util.env._);
done();
});
});
describe('noop()', () => {
it('should return a stream', done => {
util.isStream(util.noop()).should.equal(true);
done();
});
it('should return a stream that passes through all data', done => {
var inp = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var stream = util.noop();
es.readArray(inp)
.pipe(stream)
.pipe(es.writeArray((err: Error, arr: any[]) => {
should.not.exist(err);
should.exist(arr);
arr.should.eql(inp);
done();
}));
});
});
describe('beep()', () => {
it('should send the right code to stdout', done => {
var writtenValue = '';
util.beep();
writtenValue.should.equal('\x07');
done();
});
});
describe('isStream()', () => {
it('should work on a stream', done => {
util.isStream(through()).should.equal(true);
done();
});
it('should not work on a buffer', done => {
util.isStream(new Buffer('huh')).should.equal(false);
done();
});
});
describe('isBuffer()', () => {
it('should work on a buffer', done => {
util.isBuffer(new Buffer('huh')).should.equal(true);
done();
});
it('should not work on a stream', done => {
util.isBuffer(through()).should.equal(false);
done();
});
});
describe('isNull()', () => {
it('should work on a null', done => {
util.isNull(null).should.equal(true);
done();
});
it('should not work on a stream', done => {
util.isNull(through()).should.equal(false);
done();
});
it('should not work on undefined', done => {
util.isNull(undefined).should.equal(false);
done();
});
});
// linefeed
declare var lines: string[];
lines.join(util.linefeed);
describe('combine()', () => {
it('should return a function', done => {
var src = [1, 2, 3];
var inp = es.readArray(src);
var factory = util.combine(inp);
factory.should.be.type('function');
done();
});
it('should return a function that returns a stream combination', done => {
var src = [1, 2, 3];
var inp = es.readArray(src);
var inp2 = es.writeArray((err: Error, data: any[]) => {
should.not.exist(err);
data.should.eql(src);
done();
});
var factory = util.combine(inp, inp2);
factory().should.be.instanceof(Stream.Readable);
});
it('should return a function that returns a stream combination when given an array', done => {
var src = [1, 2, 3];
var inp = es.readArray(src);
var inp2 = es.writeArray((err: Error, data: any[]) => {
should.not.exist(err);
data.should.eql(src);
done();
});
var factory = util.combine([inp, inp2]);
factory().should.be.instanceof(Stream.Readable);
});
});
describe('buffer()', () => {
it('should buffer stuff and return an array into the callback', done => {
var src = [1, 2, 3];
var inp = es.readArray(src);
inp.pipe(util.buffer((err: Error, data: any[]) => {
should.not.exist(err);
should.exist(data);
data.should.eql(src);
done();
}));
});
it('should buffer stuff and emit it as a data event', done => {
var src = [1, 2, 3];
var inp = es.readArray(src);
inp.pipe(util.buffer()).on('data', (data: any[]) => {
should.exist(data);
data.should.eql(src);
done();
});
});
it('should buffer stuff and return a stream with the buffered data', done => {
var src = [1, 2, 3];
var inp = es.readArray(src);
inp.pipe(util.buffer()).pipe(es.through((data: any[]) => {
should.exist(data);
data.should.eql(src);
done();
}));
});
});
describe('PluginError()', () => {
it('should default name to Error', () => {
var err = new util.PluginError('test', 'something broke');
err.name.should.equal('Error');
});
it('should default name to Error, even when wrapped error has no name', () => {
var realErr = { message: 'something broke' };
var err = new util.PluginError('test', realErr);
err.name.should.equal('Error');
});
it('should print the plugin name in toString', () => {
var err = new util.PluginError('test', 'something broke');
err.toString().indexOf('test').should.not.equal(-1);
});
it('should not include the stack by default in toString', () => {
var err = new util.PluginError('test', 'something broke');
// just check that there are no 'at' lines
err.toString().indexOf('at').should.equal(-1);
});
it('should include the stack when specified in toString', () => {
var err = new util.PluginError('test', 'something broke', { stack: "at huh", showStack: true });
// just check that there are 'at' lines
err.toString().indexOf('at').should.not.equal(-1);
});
it('should take arguments as one object', () => {
var err = new util.PluginError({
plugin: 'test',
message: 'something broke'
});
err.plugin.should.equal('test');
err.message.should.equal('something broke');
});
it('should take arguments as plugin name and one object', () => {
var err = new util.PluginError('test', {
message: 'something broke'
});
err.plugin.should.equal('test');
err.message.should.equal('something broke');
});
it('should take arguments as plugin name and message', () => {
var err = new util.PluginError('test', 'something broke');
err.plugin.should.equal('test');
err.message.should.equal('something broke');
});
it('should take arguments as plugin name, message, and one object', () => {
var err = new util.PluginError('test', 'something broke', { showStack: true });
err.plugin.should.equal('test');
err.message.should.equal('something broke');
err.showStack.should.equal(true);
});
it('should take arguments as plugin name, error, and one object', () => {
var realErr = new Error('something broke');
(<any>realErr).fileName = 'original.js';
var err = new util.PluginError('test', realErr, { showStack: true, fileName: 'override.js' });
err.plugin.should.equal('test');
err.message.should.equal('something broke');
err.fileName.should.equal('override.js');
err.showStack.should.equal(true);
});
it('should take properties from error', () => {
var realErr = new Error('something broke');
(<any>realErr).abstractProperty = 'abstract';
var err = new util.PluginError('test', realErr);
err.plugin.should.equal('test');
err.message.should.equal('something broke');
(<any>err).abstractProperty.should.equal('abstract');
});
it('should be configured to show properties by default', () => {
var err = new util.PluginError('test', 'something broke');
err.showProperties.should.be.true;
});
it('should not be configured to take option to show properties', () => {
var err = new util.PluginError('test', 'something broke', { showProperties: false });
err.showProperties.should.be.false;
});
it('should show properties', () => {
var err = new util.PluginError('test', 'it broke', { showProperties: true });
err.fileName = 'original.js';
err.lineNumber = 35;
err.toString().indexOf('it broke').should.not.equal(-1);
err.toString().indexOf('message:').should.equal(-1);
err.toString().indexOf('fileName:').should.not.equal(-1);
});
it('should show properties', () => {
var realErr = new Error('something broke');
(<any>realErr).fileName = 'original.js';
(<any>realErr).lineNumber = 35;
var err = new util.PluginError('test', realErr, { showProperties: true });
err.toString().indexOf('message:').should.equal(-1);
err.toString().indexOf('fileName:').should.not.equal(-1);
});
it('should not show properties', () => {
var realErr = new Error('something broke');
(<any>realErr).fileName = 'original.js';
(<any>realErr).lineNumber = 35;
var err = new util.PluginError('test', realErr, { showProperties: false });
err.toString().indexOf('message:').should.equal(-1);
err.toString().indexOf('fileName:').should.equal(-1);
});
it('should not show properties, but should show stack', () => {
var err = new util.PluginError('test', 'it broke', { stack: 'test stack', showStack: true, showProperties: false });
err.fileName = 'original.js';
err.lineNumber = 35;
err.toString().indexOf('message:').should.equal(-1);
err.toString().indexOf('fileName:').should.equal(-1);
err.toString().indexOf('test stack').should.not.equal(-1);
});
it('should not show properties, but should show stack for real error', () => {
var realErr = new Error('something broke');
(<any>realErr).fileName = 'original.js';
(<any>realErr).lineNumber = 35;
(<any>realErr).stack = 'test stack';
var err = new util.PluginError('test', realErr, { showStack: true, showProperties: false });
err.toString().indexOf('message:').should.equal(-1);
err.toString().indexOf('fileName:').should.equal(-1);
err.toString().indexOf('test stack').should.not.equal(-1);
});
it('should not show properties, but should show stack for _stack', () => {
var realErr = new Error('something broke');
(<any>realErr).fileName = 'original.js';
(<any>realErr).lineNumber = 35;
(<any>realErr)._stack = 'test stack';
var err = new util.PluginError('test', realErr, { showStack: true, showProperties: false });
err.toString().indexOf('message:').should.equal(-1);
err.toString().indexOf('fileName:').should.equal(-1);
err.toString().indexOf('test stack').should.not.equal(-1);
});
it('should show properties and stack', () => {
var realErr = new Error('something broke');
(<any>realErr).fileName = 'original.js';
(<any>realErr).stack = 'test stack';
var err = new util.PluginError('test', realErr, { showStack: true });
err.toString().indexOf('message:').should.equal(-1);
err.toString().indexOf('fileName:').should.not.equal(-1);
err.toString().indexOf('test stack').should.not.equal(-1);
});
it('should show properties added after the error is created', () => {
var realErr = new Error('something broke');
var err = new util.PluginError('test', realErr);
err.fileName = 'original.js';
err.toString().indexOf('message:').should.equal(-1);
err.toString().indexOf('fileName:').should.not.equal(-1);
});
it('should toString quickly', function(done) {
this.timeout(100);
var err = new util.PluginError('test', 'it broke', { showStack: true });
var str = err.toString();
done();
});
it('should toString quickly with original error', function(done) {
this.timeout(100);
var realErr = new Error('it broke');
var err = new util.PluginError('test', realErr, { showStack: true });
var str = err.toString();
done();
});
it('should not show "Details:" if there are no properties to show', () => {
var err = new util.PluginError('plugin', 'message');
err.toString().indexOf('Details:').should.equal(-1);
});
});