-
Notifications
You must be signed in to change notification settings - Fork 44
/
test.js
836 lines (744 loc) · 22.7 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
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
var assert = require('assert');
var Tree = require('./utils/builder');
var watch = require('../lib/watch');
var is = require('../lib/is');
var hasNativeRecursive = require('../lib/has-native-recursive');
var tree = Tree();
var watcher;
beforeEach(function() {
tree = Tree();
});
afterEach(function(done) {
if (watcher && !watcher.isClosed()) {
watcher.on('close', done);
watcher.close();
} else {
done();
}
});
after(function() {
if (tree) {
tree.cleanup();
}
});
function wait(fn, timeout) {
try {
fn();
} catch (error) {
timeout -= 30;
if (timeout >= 0) {
setTimeout(function() {
wait(fn, timeout);
}, 30);
} else {
throw error;
}
}
}
describe('process events', function() {
it('should emit `close` event', function(done) {
var file = 'home/a/file1';
var fpath = tree.getPath(file);
watcher = watch(fpath, function() {});
watcher.on('close', function() {
done();
});
watcher.close();
});
it('should emit `ready` event when watching a file', function(done) {
var file = 'home/a/file1';
var fpath = tree.getPath(file);
watcher = watch(fpath);
watcher.on('ready', function() {
done();
});
});
it('should emit `ready` event when watching a directory recursively', function(done) {
var dir = tree.getPath('home');
watcher = watch(dir, { recursive: true });
watcher.on('ready', function() {
done();
});
});
it('should emit `ready` properly in a composed watcher', function(done) {
var dir1 = tree.getPath('home/a');
var dir2 = tree.getPath('home/b');
var file = tree.getPath('home/b/file1');
watcher = watch([dir1, dir2, file], { recursive: true });
watcher.on('ready', function() {
done();
});
});
});
describe('watch for files', function() {
it('should watch a single file and keep watching', function(done) {
var times = 1;
var file = 'home/a/file1';
var fpath = tree.getPath(file);
watcher = watch(fpath, { delay: 0 }, function(evt, name) {
assert.equal(fpath, name)
if (times++ >= 3) {
done();
}
});
watcher.on('ready', function() {
tree.modify(file);
tree.modify(file, 100);
tree.modify(file, 200);
});
});
it('should watch files inside a directory', function(done) {
var fpath = tree.getPath('home/a');
var stack = [
tree.getPath('home/a/file1'),
tree.getPath('home/a/file2')
];
watcher = watch(fpath, { delay: 0 }, function(evt, name) {
stack.splice(stack.indexOf(name), 1);
if (!stack.length) done();
});
watcher.on('ready', function() {
tree.modify('home/a/file1');
tree.modify('home/a/file2', 100);
});
});
it('should ignore duplicate changes', function(done) {
var file = 'home/a/file2';
var fpath = tree.getPath(file);
var times = 0;
watcher = watch(fpath, { delay: 200 }, function(evt, name) {
if (fpath === name) times++;
});
watcher.on('ready', function() {
tree.modify(file);
tree.modify(file, 100);
tree.modify(file, 150);
wait(function() {
assert.equal(times, 1)
done();
}, 250);
});
});
it('should listen to new created files', function(done) {
var home = tree.getPath('home');
var newfile1 = 'home/a/newfile' + Math.random();
var newfile2 = 'home/a/newfile' + Math.random();
var changes = [];
watcher = watch(home, { delay: 0, recursive: true }, function(evt, name) {
changes.push(name);
});
watcher.on('ready', function() {
tree.newFile(newfile1);
tree.newFile(newfile2);
wait(function() {
// On windows it will report its parent directory along with the filename
// https://github.com/yuanchuan/node-watch/issues/79
if (is.windows()) {
// Make sure new files are deteced
assert.ok(
changes.includes(tree.getPath(newfile1)) &&
changes.includes(tree.getPath(newfile2))
);
// It should only include new files and its parent directory
// if there are more than 2 events
if (changes.length > 2) {
let accepts = [
tree.getPath(newfile1),
tree.getPath(newfile2),
tree.getPath('home/a')
];
changes.forEach(function(name) {
assert.ok(accepts.includes(name), name + " should not be included");
});
}
} else {
assert.deepStrictEqual(
changes,
[tree.getPath(newfile1), tree.getPath(newfile2)]
);
}
done();
}, 100);
});
});
it('should error when parent gets deleted before calling fs.watch', function(done) {
var fpath = tree.getPath('home/a/file1');
watcher = watch(fpath, Object.defineProperty({}, 'test', {
enumerable: true,
get: function() {
tree.remove('home/a');
return 'test';
}
}));
watcher.on('error', function() {
done();
});
});
});
describe('watch for directories', function() {
it('should watch directories inside a directory', function(done) {
var home = tree.getPath('home');
var dir = tree.getPath('home/c');
var events = [];
watcher = watch(home, { delay: 0, recursive: true }, function(evt, name) {
if (name === dir) {
events.push(evt);
}
});
watcher.on('ready', function() {
tree.remove('home/c');
wait(function () {
assert.deepStrictEqual(
events,
[ 'remove' ]
);
done();
}, 400);
});
});
it('should watch new created directories', function(done) {
var home = tree.getPath('home');
watcher = watch(home, { delay: 0, recursive: true }, function(evt, name) {
if (name === tree.getPath('home/new/file1')) {
done();
}
});
watcher.on('ready', function() {
// newFile() will create the 'new/' directory and the 'new/file1' file,
// but, only the creation of the directory is observed.
// Because of that, there will only be one event for file1, when it
// is modified, not when it is created.
tree.newFile('home/new/file1');
tree.modify('home/new/file1', 100);
});
});
it('should not watch new created directories which are being skipped in the filter', function(done) {
var home = tree.getPath('home');
var options = {
delay: 0,
recursive: true,
filter: function(filePath, skip) {
if (/ignored/.test(filePath)) return skip;
return true;
}
}
watcher = watch(home, options, function(evt, name) {
assert.fail("event detect", name);
});
watcher.on('ready', function() {
tree.newFile('home/ignored/file');
tree.modify('home/ignored/file', 100);
wait(done, 150);
});
});
it('should keep watching after removal of sub directory', function(done) {
var home = tree.getPath('home');
var file1 = tree.getPath('home/e/file1');
var file2 = tree.getPath('home/e/file2');
var dir = tree.getPath('home/e/sub');
var events = [];
watcher = watch(home, { delay: 0, recursive: true }, function(evt, name) {
if (name === dir || name === file1 || name === file2) {
events.push(name);
}
});
watcher.on('ready', function() {
tree.remove('home/e/sub', 50);
tree.modify('home/e/file1', 100);
tree.modify('home/e/file2', 200);
wait(function() {
assert.deepStrictEqual(events, [dir, file1, file2]);
done();
}, 300);
});
});
it('should watch new directories without delay', function(done) {
var home = tree.getPath('home');
var events = [];
watcher = watch(home, { delay: 200, recursive: true }, function(evt, name) {
if (name === tree.getPath('home/new/file1')) {
events.push(evt);
}
});
watcher.on('ready', function() {
tree.newFile('home/new/file1');
tree.modify('home/new/file1', 50);
tree.modify('home/new/file1', 100);
wait(function() {
assert.deepStrictEqual(events, ['update']);
done();
}, 350);
});
});
it('should error when directory gets deleted before calling fs.watch', function(done) {
var dir = 'home/c';
var fpath = tree.getPath(dir);
watcher = watch(fpath, Object.defineProperty({}, 'test', {
enumerable: true,
get: function() {
tree.remove(dir);
return 'test';
}
}));
watcher.on('error', function() {
done();
});
});
});
describe('file events', function() {
it('should identify `remove` event', function(done) {
var file = 'home/a/file1';
var fpath = tree.getPath(file);
watcher = watch(fpath, function(evt, name) {
if (evt === 'remove' && name === fpath) done();
});
watcher.on('ready', function() {
tree.remove(file);
});
});
it('should identify `remove` event on directory', function(done) {
var dir = 'home/a';
var home = tree.getPath('home');
var fpath = tree.getPath(dir);
watcher = watch(home, function(evt, name) {
if (evt === 'remove' && name === fpath) done();
});
watcher.on('ready', function() {
tree.remove(dir);
});
});
it('should be able to handle many events on deleting', function(done) {
var dir = 'home/a';
var fpath = tree.getPath(dir);
var names = tree.newRandomFiles(dir, 300);
var count = 0;
watcher = watch(fpath, function(evt, name) {
count += 1;
if (count == names.length) done();
});
watcher.on('ready', function() {
names.forEach(tree.remove.bind(tree));
});
});
it('should identify `update` event', function(done) {
var file = 'home/a/file1';
var fpath = tree.getPath(file);
watcher = watch(fpath, function(evt, name) {
if (evt === 'update' && name === fpath) done();
});
watcher.on('ready', function() {
tree.modify(file);
});
});
it('should report `update` on new files', function(done) {
var dir = tree.getPath('home/a');
var file = 'home/a/newfile' + Date.now();
var fpath = tree.getPath(file);
watcher = watch(dir, function(evt, name) {
if (evt === 'update' && name === fpath) done();
});
watcher.on('ready', function() {
tree.newFile(file);
});
});
});
describe('options', function() {
describe('recursive', function() {
it('should watch recursively with `recursive: true` option', function(done) {
var dir = tree.getPath('home');
var file = tree.getPath('home/bb/file1');
watcher = watch(dir, { recursive: true }, function(evt, name) {
if (file === name) {
done();
}
});
watcher.on('ready', function() {
tree.modify('home/bb/file1');
});
});
});
describe('encoding', function() {
it('should throw on invalid encoding', function(done) {
var dir = tree.getPath('home/a');
try {
watcher = watch(dir, 'unknown');
} catch (e) {
done();
}
});
it('should accept options as an encoding string', function(done) {
var dir = tree.getPath('home/a');
var file = 'home/a/file1';
var fpath = tree.getPath(file);
watcher = watch(dir, 'utf8', function(evt, name) {
assert.equal(name.toString(), fpath);
done();
});
watcher.on('ready', function() {
tree.modify(file);
});
});
it('should support buffer encoding', function(done) {
var dir = tree.getPath('home/a');
var file = 'home/a/file1';
var fpath = tree.getPath(file);
watcher = watch(dir, 'buffer', function(evt, name) {
assert(Buffer.isBuffer(name), 'not a Buffer')
assert.equal(name.toString(), fpath);
done();
});
watcher.on('ready', function() {
tree.modify(file);
});
});
it('should support base64 encoding', function(done) {
var dir = tree.getPath('home/a');
var file = 'home/a/file1';
var fpath = tree.getPath(file);
watcher = watch(dir, 'base64', function(evt, name) {
assert.equal(
name,
Buffer.from(fpath).toString('base64'),
'wrong base64 encoding'
);
done();
});
watcher.on('ready', function() {
tree.modify(file);
});
});
it('should support hex encoding', function(done) {
var dir = tree.getPath('home/a');
var file = 'home/a/file1';
var fpath = tree.getPath(file);
watcher = watch(dir, 'hex', function(evt, name) {
assert.equal(
name,
Buffer.from(fpath).toString('hex'),
'wrong hex encoding'
);
done();
});
watcher.on('ready', function() {
tree.modify(file);
});
});
});
describe('filter', function() {
it('should only watch filtered directories', function(done) {
var matchRegularDir = false;
var matchIgnoredDir = false;
var options = {
delay: 0,
recursive: true,
filter: function(name) {
return !/deep_node_modules/.test(name);
}
};
watcher = watch(tree.getPath('home'), options, function(evt, name) {
if (/deep_node_modules/.test(name)) {
matchIgnoredDir = true;
} else {
matchRegularDir = true;
}
});
watcher.on('ready', function() {
tree.modify('home/b/file1');
tree.modify('home/deep_node_modules/ma/file1');
wait(function() {
assert(matchRegularDir, 'watch failed to detect regular file');
assert(!matchIgnoredDir, 'fail to ignore path `deep_node_modules`');
done();
}, 100);
});
});
it('should only report filtered files', function(done) {
var dir = tree.getPath('home');
var file1 = 'home/bb/file1';
var file2 = 'home/bb/file2';
var options = {
delay: 0,
recursive: true,
filter: function(name) {
return /file2/.test(name);
}
}
var times = 0;
var matchIgnoredFile = false;
watcher = watch(dir, options, function(evt, name) {
times++;
if (name === tree.getPath(file1)) {
matchIgnoredFile = true;
}
});
watcher.on('ready', function() {
tree.modify(file1);
tree.modify(file2, 50);
wait(function() {
assert.equal(times, 1, 'should only report /home/bb/file2 once');
assert.equal(matchIgnoredFile, false, 'home/bb/file1 should be ignored');
done();
}, 100);
});
});
it('should be able to filter with regexp', function(done) {
var dir = tree.getPath('home');
var file1 = 'home/bb/file1';
var file2 = 'home/bb/file2';
var options = {
delay: 0,
recursive: true,
filter: /file2/
}
var times = 0;
var matchIgnoredFile = false;
watcher = watch(dir, options, function(evt, name) {
times++;
if (name === tree.getPath(file1)) {
matchIgnoredFile = true;
}
});
watcher.on('ready', function() {
tree.modify(file1);
tree.modify(file2, 50);
wait(function() {
assert(times, 1, 'report file2');
assert(!matchIgnoredFile, 'home/bb/file1 should be ignored');
done();
}, 100);
});
});
it('should be able to skip subdirectories with `skip` flag', function(done) {
var home = tree.getPath('home');
var options = {
delay: 0,
recursive: true,
filter: function(name, skip) {
if (/\/deep_node_modules/.test(name)) return skip;
}
};
watcher = watch(home, options);
watcher.getWatchedPaths(function(paths) {
hasNativeRecursive(function(supportRecursive) {
var watched = supportRecursive
// The skip flag has no effect to the platforms which support recursive option,
// so the home directory is the only one that's in the watching list.
? [home]
// The deep_node_modules and all its subdirectories should not be watched
// with skip flag specified in the filter.
: tree.getAllDirectories().filter(function(name) {
return !/\/deep_node_modules/.test(name);
});
assert.deepStrictEqual(
watched.sort(), paths.sort()
);
done();
});
});
});
});
describe('delay', function() {
it('should have delayed response', function(done) {
var dir = tree.getPath('home/a');
var file = 'home/a/file1';
var start;
watcher = watch(dir, { delay: 300 }, function(evt, name) {
assert(Date.now() - start >= 300, 'delay not working');
done();
});
watcher.on('ready', function() {
start = Date.now();
tree.modify(file);
});
});
});
});
describe('parameters', function() {
it('should throw error on non-existed file', function(done) {
var somedir = tree.getPath('home/somedir');
watcher = watch(somedir);
watcher.on('error', function(err) {
if (err.message.includes('does not exist')) {
done()
}
})
});
it('should accept filename as Buffer', function(done) {
var fpath = tree.getPath('home/a/file1');
watcher = watch(Buffer.from(fpath), { delay: 0 }, function(evt, name) {
assert.equal(name, fpath);
done();
});
watcher.on('ready', function() {
tree.modify('home/a/file1');
});
});
it('should compose array of files or directories', function(done) {
var file1 = 'home/a/file1';
var file2 = 'home/a/file2';
var fpaths = [
tree.getPath(file1),
tree.getPath(file2)
];
var times = 0;
watcher = watch(fpaths, { delay: 0 }, function(evt, name) {
if (fpaths.indexOf(name) !== -1) times++;
if (times === 2) done(); // calling done more than twice causes mocha test to fail
});
watcher.on('ready', function() {
tree.modify(file1);
tree.modify(file2, 50);
});
});
it('should filter duplicate events for composed watcher', function(done) {
var home = 'home';
var dir = 'home/a';
var file1 = 'home/a/file1';
var file2 = 'home/a/file2';
var fpaths = [
tree.getPath(home),
tree.getPath(dir),
tree.getPath(file1),
tree.getPath(file2)
];
var changes = [];
watcher = watch(fpaths, { delay: 100, recursive: true }, function(evt, name) {
changes.push(name);
});
watcher.on('ready', function() {
tree.modify(file1);
tree.modify(file2, 50);
wait(function() {
assert.deepStrictEqual(
changes,
[tree.getPath(file1), tree.getPath(file2)]
);
done();
}, 200);
});
});
});
describe('watcher object', function() {
it('should using watcher object to watch', function(done) {
var dir = tree.getPath('home/a');
var file = 'home/a/file1';
var fpath = tree.getPath(file);
watcher = watch(dir, { delay: 0 });
watcher.on('ready', function() {
watcher.on('change', function(evt, name) {
assert.equal(evt, 'update');
assert.equal(name, fpath);
done();
});
tree.modify(file);
});
});
describe('close()', function() {
it('should close a watcher using .close()', function(done) {
var dir = tree.getPath('home/a');
var file = 'home/a/file1';
var times = 0;
watcher = watch(dir, { delay: 0 });
watcher.on('change', function(evt, name) {
times++;
});
watcher.on('ready', function() {
watcher.close();
tree.modify(file);
tree.modify(file, 100);
wait(function() {
assert(watcher.isClosed(), 'watcher should be closed');
assert.equal(times, 0, 'failed to close the watcher');
done();
}, 150);
});
});
it('should not watch after .close() is called', function(done) {
var dir = tree.getPath('home');
watcher = watch(dir, { delay: 0, recursive: true });
watcher.close();
watcher.getWatchedPaths(function(dirs) {
assert(dirs.length === 0);
done();
});
});
it('Do not emit after close', function(done) {
var dir = tree.getPath('home/a');
var file = 'home/a/file1';
var times = 0;
watcher = watch(dir, { delay: 0 });
watcher.on('change', function(evt, name) {
times++;
});
watcher.on('ready', function() {
watcher.close();
var timer = setInterval(function() {
tree.modify(file);
});
wait(function() {
clearInterval(timer);
assert(watcher.isClosed(), 'watcher should be closed');
assert.equal(times, 0, 'failed to close the watcher');
done();
}, 100);
});
});
});
describe('getWatchedPaths()', function() {
it('should get all the watched paths', function(done) {
var home = tree.getPath('home');
watcher = watch(home, {
delay: 0,
recursive: true
});
watcher.getWatchedPaths(function(paths) {
hasNativeRecursive(function(supportRecursive) {
var watched = supportRecursive
// The home directory is the only one that's being watched
// if the recursive option is natively supported.
? [home]
// Otherwise it should include all its subdirectories.
: tree.getAllDirectories();
assert.deepStrictEqual(
watched.sort(), paths.sort()
);
done();
});
});
});
it('should get its parent path instead of the file itself', function(done) {
var file = tree.getPath('home/a/file1');
// The parent path is actually being watched instead.
var parent = tree.getPath('home/a');
watcher = watch(file, { delay: 0 });
watcher.getWatchedPaths(function(paths) {
assert.deepStrictEqual([parent], paths);
done();
});
});
it('should work correctly with composed watcher', function(done) {
var a = tree.getPath('home/a');
var b = tree.getPath('home/b');
var file = tree.getPath('home/b/file1');
var nested = tree.getPath('home/deep_node_modules');
var ma = tree.getPath('home/deep_node_modules/ma');
var mb = tree.getPath('home/deep_node_modules/mb');
var mc = tree.getPath('home/deep_node_modules/mc');
watcher = watch([a, file, nested], {
delay: 0,
recursive: true
});
watcher.getWatchedPaths(function(paths) {
hasNativeRecursive(function(supportRecursive) {
var watched = supportRecursive
? [a, b, nested]
: [a, b, nested, ma, mb, mc];
assert.deepStrictEqual(
watched.sort(), paths.sort()
);
done();
});
});
});
});
});