forked from dsktschy/chokidar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
2065 lines (1913 loc) · 84.5 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
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
const chokidar = require('./');
const promisify = require('util').promisify;
const chai = require('chai');
const expect = chai.expect;
const should = chai.should();
const sinon = require('sinon');
const rimraf = promisify(require('rimraf'));
const fs = require('fs');
const sysPath = require('path');
const upath = require("upath");
const exec = promisify(require('child_process').exec);
chai.use(require('sinon-chai'));
const os = process.platform;
const write = promisify(fs.writeFile);
const fs_symlink = promisify(fs.symlink);
const fs_rename = promisify(fs.rename);
const fs_mkdir = promisify(fs.mkdir);
const fs_rmdir = promisify(fs.rmdir);
const fs_unlink = promisify(fs.unlink);
const isTravisMac = process.env.TRAVIS && os === 'darwin';
const FIXTURES_PATH_REL = 'test-fixtures';
const FIXTURES_PATH = sysPath.join(__dirname, FIXTURES_PATH_REL);
const allWatchers = [];
const PERM_ARR = 0o755; // rwe, r+e, r+e
let subdirId = 0;
let options;
let currentDir;
let macosFswatch;
let win32Polling;
let slowerDelay;
// spyOnReady
const aspy = (watcher, eventName, spy=null, noStat=false) => {
if (typeof eventName !== 'string') {
throw new TypeError('aspy: eventName must be a String');
}
if (spy == null) spy = sinon.spy();
return new Promise((resolve, reject) => {
const handler = noStat ?
eventName === 'all' ?
(event, path) => spy(event, path) :
(path) => spy(path) :
spy;
watcher.on('error', reject);
watcher.on('ready', () => { resolve(spy); });
watcher.on(eventName, handler);
});
};
const waitForWatcher = (watcher) => {
return new Promise((resolve, reject) => {
watcher.on('error', reject);
watcher.on('ready', resolve);
});
};
const delay = async (time) => {
return new Promise((resolve) => {
const timer = time || slowerDelay || 20;
setTimeout(resolve, timer);
});
};
const getFixturePath = (subPath) => {
const subd = subdirId && subdirId.toString() || '';
return sysPath.join(FIXTURES_PATH, subd, subPath);
};
const getGlobPath = (subPath) => {
const subd = subdirId && subdirId.toString() || '';
return upath.join(FIXTURES_PATH, subd, subPath);
};
currentDir = getFixturePath('');
const chokidar_watch = (path=currentDir, opts=options) => {
const wt = chokidar.watch(path, opts);
allWatchers.push(wt);
return wt;
};
const runTests = function(baseopts) {
baseopts.persistent = true;
before(() => {
// flags for bypassing special-case test failures on CI
macosFswatch = os === 'darwin' && !baseopts.usePolling && !baseopts.useFsEvents;
win32Polling = os === 'win32' && baseopts.usePolling;
slowerDelay = macosFswatch ? 100 : undefined;
});
beforeEach(function clean() {
options = {};
Object.keys(baseopts).forEach(function(key) {
options[key] = baseopts[key];
});
});
const waitFor = async (spies) => {
if (spies.length === 0) throw new TypeError('SPies zero');
return new Promise((resolve, reject) => {
const isSpyReady = (spy) => {
if (Array.isArray(spy)) {
return spy[0].callCount >= spy[1];
} else {
return spy.callCount >= 1;
}
};
let intrvl, timeo;
function finish() {
clearInterval(intrvl);
clearTimeout(timeo);
resolve();
}
intrvl = setInterval(() => {
process.nextTick(() => {
if (spies.every(isSpyReady)) finish();
});
}, 20);
timeo = setTimeout(finish, 5000);
});
};
describe('watch a directory', () => {
let readySpy, rawSpy, watcher, watcher2;
beforeEach(() => {
options.ignoreInitial = true;
options.alwaysStat = true;
readySpy = sinon.spy(function readySpy(){});
rawSpy = sinon.spy(function rawSpy(){});
watcher = chokidar_watch().on('ready', readySpy).on('raw', rawSpy);
});
afterEach(async () => {
await waitFor([readySpy]);
readySpy.should.have.been.calledOnce;
readySpy = undefined;
rawSpy = undefined;
});
it('should produce an instance of chokidar.FSWatcher', () => {
watcher.should.be.an.instanceof(chokidar.FSWatcher);
});
it('should expose public API methods', () => {
watcher.on.should.be.a('function');
watcher.emit.should.be.a('function');
watcher.add.should.be.a('function');
watcher.close.should.be.a('function');
watcher.getWatched.should.be.a('function');
});
it('should emit `add` event when file was added', async () => {
const testPath = getFixturePath('add.txt');
const spy = await aspy(watcher, 'add');
await delay();
await write(testPath, Date.now());
await waitFor([spy]);
spy.should.have.been.calledOnce;
spy.should.have.been.calledWith(testPath);
expect(spy.args[0][1]).to.be.ok; // stats
rawSpy.should.have.been.called;
});
it('should emit nine `add` events when nine files were added in one directory', async () => {
const paths = [];
for (let i = 1; i <= 9; i++) {
paths.push(getFixturePath(`add${i}.txt`));
}
const spy = sinon.spy();
watcher.on('add', (path) => {
spy(path);
});
await waitForWatcher(watcher);
for (let path of paths.slice(0, 5)) {
await write(path, Date.now());
}
await delay(100);
for (let path of paths.slice(5)) {
await write(path, Date.now());
}
await waitFor([[spy, 4]]);
await waitFor([[spy, 9]]);
await delay(1000);
paths.forEach(path => {
spy.should.have.been.calledWith(path);
});
});
it('should emit thirtythree `add` events when thirtythree files were added in nine directories', async () => {
watcher.close();
const test1Path = getFixturePath('add1.txt');
const testb1Path = getFixturePath('b/add1.txt');
const testc1Path = getFixturePath('c/add1.txt');
const testd1Path = getFixturePath('d/add1.txt');
const teste1Path = getFixturePath('e/add1.txt');
const testf1Path = getFixturePath('f/add1.txt');
const testg1Path = getFixturePath('g/add1.txt');
const testh1Path = getFixturePath('h/add1.txt');
const testi1Path = getFixturePath('i/add1.txt');
const test2Path = getFixturePath('add2.txt');
const testb2Path = getFixturePath('b/add2.txt');
const testc2Path = getFixturePath('c/add2.txt');
const test3Path = getFixturePath('add3.txt');
const testb3Path = getFixturePath('b/add3.txt');
const testc3Path = getFixturePath('c/add3.txt');
const test4Path = getFixturePath('add4.txt');
const testb4Path = getFixturePath('b/add4.txt');
const testc4Path = getFixturePath('c/add4.txt');
const test5Path = getFixturePath('add5.txt');
const testb5Path = getFixturePath('b/add5.txt');
const testc5Path = getFixturePath('c/add5.txt');
const test6Path = getFixturePath('add6.txt');
const testb6Path = getFixturePath('b/add6.txt');
const testc6Path = getFixturePath('c/add6.txt');
const test7Path = getFixturePath('add7.txt');
const testb7Path = getFixturePath('b/add7.txt');
const testc7Path = getFixturePath('c/add7.txt');
const test8Path = getFixturePath('add8.txt');
const testb8Path = getFixturePath('b/add8.txt');
const testc8Path = getFixturePath('c/add8.txt');
const test9Path = getFixturePath('add9.txt');
const testb9Path = getFixturePath('b/add9.txt');
const testc9Path = getFixturePath('c/add9.txt');
fs.mkdirSync(getFixturePath('b'), PERM_ARR);
fs.mkdirSync(getFixturePath('c'), PERM_ARR);
fs.mkdirSync(getFixturePath('d'), PERM_ARR);
fs.mkdirSync(getFixturePath('e'), PERM_ARR);
fs.mkdirSync(getFixturePath('f'), PERM_ARR);
fs.mkdirSync(getFixturePath('g'), PERM_ARR);
fs.mkdirSync(getFixturePath('h'), PERM_ARR);
fs.mkdirSync(getFixturePath('i'), PERM_ARR);
watcher2 = chokidar_watch().on('ready', readySpy).on('raw', rawSpy);
const spy = await aspy(watcher2, 'add', null, true);
await write(test1Path, Date.now());
await write(test2Path, Date.now());
await write(test3Path, Date.now());
await write(test4Path, Date.now());
await write(test5Path, Date.now());
await delay(200);
await write(test6Path, Date.now());
await write(test7Path, Date.now());
await write(test8Path, Date.now());
await write(test9Path, Date.now());
await write(testb1Path, Date.now());
await write(testb2Path, Date.now());
await write(testb3Path, Date.now());
await write(testb4Path, Date.now());
await write(testb5Path, Date.now());
await delay(200);
await write(testb6Path, Date.now());
await write(testb7Path, Date.now());
await write(testb8Path, Date.now());
await write(testb9Path, Date.now());
await write(testc1Path, Date.now());
await write(testc2Path, Date.now());
await write(testc3Path, Date.now());
await write(testc4Path, Date.now());
await write(testc5Path, Date.now());
await delay(150);
await write(testc6Path, Date.now());
await write(testc7Path, Date.now());
await write(testc8Path, Date.now());
await write(testc9Path, Date.now());
await write(testd1Path, Date.now());
await write(teste1Path, Date.now());
await write(testf1Path, Date.now());
await delay(100);
await write(testg1Path, Date.now());
await write(testh1Path, Date.now());
await write(testi1Path, Date.now());
await waitFor([[spy, 11]]);
await waitFor([[spy, 22]]);
await waitFor([[spy, 33]]);
await delay(1000);
spy.should.have.been.calledWith(test1Path);
spy.should.have.been.calledWith(test2Path);
spy.should.have.been.calledWith(test3Path);
spy.should.have.been.calledWith(test4Path);
spy.should.have.been.calledWith(test5Path);
spy.should.have.been.calledWith(test6Path);
spy.should.have.been.calledWith(test7Path);
spy.should.have.been.calledWith(test8Path);
spy.should.have.been.calledWith(test9Path);
spy.should.have.been.calledWith(testb1Path);
spy.should.have.been.calledWith(testb2Path);
spy.should.have.been.calledWith(testb3Path);
spy.should.have.been.calledWith(testb4Path);
spy.should.have.been.calledWith(testb5Path);
spy.should.have.been.calledWith(testb6Path);
spy.should.have.been.calledWith(testb7Path);
spy.should.have.been.calledWith(testb8Path);
spy.should.have.been.calledWith(testb9Path);
spy.should.have.been.calledWith(testc1Path);
spy.should.have.been.calledWith(testc2Path);
spy.should.have.been.calledWith(testc3Path);
spy.should.have.been.calledWith(testc4Path);
spy.should.have.been.calledWith(testc5Path);
spy.should.have.been.calledWith(testc6Path);
spy.should.have.been.calledWith(testc7Path);
spy.should.have.been.calledWith(testc8Path);
spy.should.have.been.calledWith(testc9Path);
spy.should.have.been.calledWith(testd1Path);
spy.should.have.been.calledWith(teste1Path);
spy.should.have.been.calledWith(testf1Path);
spy.should.have.been.calledWith(testg1Path);
spy.should.have.been.calledWith(testh1Path);
spy.should.have.been.calledWith(testi1Path);
});
it('should emit `addDir` event when directory was added', async () => {
const testDir = getFixturePath('subdir');
const spy = await aspy(watcher, 'addDir');
spy.should.not.have.been.called;
await fs_mkdir(testDir, PERM_ARR);
await waitFor([spy]);
spy.should.have.been.calledOnce;
spy.should.have.been.calledWith(testDir);
expect(spy.args[0][1]).to.be.ok; // stats
rawSpy.should.have.been.called;
});
it('should emit `change` event when file was changed', async () => {
const testPath = getFixturePath('change.txt');
const spy = await aspy(watcher, 'change');
spy.should.not.have.been.called;
await write(testPath, Date.now());
await waitFor([spy]);
spy.should.have.been.calledWith(testPath);
expect(spy.args[0][1]).to.be.ok; // stats
rawSpy.should.have.been.called;
spy.should.have.been.calledOnce;
});
it('should emit `unlink` event when file was removed', async () => {
const testPath = getFixturePath('unlink.txt');
const spy = await aspy(watcher, 'unlink');
spy.should.not.have.been.called;
await fs_unlink(testPath);
await waitFor([spy]);
spy.should.have.been.calledWith(testPath);
expect(spy.args[0][1]).to.not.be.ok; // no stats
rawSpy.should.have.been.called;
spy.should.have.been.calledOnce;
});
it('should emit `unlinkDir` event when a directory was removed', async () => {
const testDir = getFixturePath('subdir');
fs.mkdirSync(testDir, PERM_ARR);
const spy = await aspy(watcher, 'unlinkDir');
await delay();
await fs_rmdir(testDir);
await waitFor([spy]);
spy.should.have.been.calledWith(testDir);
expect(spy.args[0][1]).to.not.be.ok; // no stats
rawSpy.should.have.been.called;
spy.should.have.been.calledOnce;
});
it('should emit two `unlinkDir` event when two nested directories were removed', async () => {
const testDir = getFixturePath('subdir');
const testDir2 = getFixturePath('subdir/subdir2');
const testDir3 = getFixturePath('subdir/subdir2/subdir3');
fs.mkdirSync(testDir, PERM_ARR);
fs.mkdirSync(testDir2, PERM_ARR);
fs.mkdirSync(testDir3, PERM_ARR);
const spy = await aspy(watcher, 'unlinkDir');
await waitFor([spy]);
await rimraf(testDir2); // test removing in one
await waitFor([spy]);
spy.should.have.been.calledWith(testDir2);
spy.should.have.been.calledWith(testDir3);
expect(spy.args[0][1]).to.not.be.ok; // no stats
rawSpy.should.have.been.called;
spy.should.have.been.calledTwice;
});
it('should emit `unlink` and `add` events when a file is renamed', async () => {
const unlinkSpy = sinon.spy(function unlink(){});
const addSpy = sinon.spy(function add(){});
const testPath = getFixturePath('change.txt');
const newPath = getFixturePath('moved.txt');
watcher.on('unlink', unlinkSpy).on('add', addSpy);
await waitForWatcher(watcher);
unlinkSpy.should.not.have.been.called;
addSpy.should.not.have.been.called;
await delay();
await fs_rename(testPath, newPath);
await waitFor([unlinkSpy, addSpy]);
unlinkSpy.should.have.been.calledWith(testPath);
expect(unlinkSpy.args[0][1]).to.not.be.ok; // no stats
addSpy.should.have.been.calledOnce;
addSpy.should.have.been.calledWith(newPath);
expect(addSpy.args[0][1]).to.be.ok; // stats
rawSpy.should.have.been.called;
if (!macosFswatch) unlinkSpy.should.have.been.calledOnce;
});
it('should emit `add`, not `change`, when previously deleted file is re-added', async () => {
const unlinkSpy = sinon.spy(function unlink(){});
const addSpy = sinon.spy(function add(){});
const changeSpy = sinon.spy(function change(){});
const testPath = getFixturePath('add.txt');
fs.writeFileSync(testPath, 'hello');
watcher
.on('unlink', unlinkSpy)
.on('add', addSpy)
.on('change', changeSpy);
await waitForWatcher(watcher);
unlinkSpy.should.not.have.been.called;
addSpy.should.not.have.been.called;
changeSpy.should.not.have.been.called;
await fs_unlink(testPath);
await waitFor([unlinkSpy.withArgs(testPath)]);
unlinkSpy.should.have.been.calledWith(testPath);
await delay();
await write(testPath, Date.now());
await waitFor([addSpy.withArgs(testPath)]);
addSpy.should.have.been.calledWith(testPath);
changeSpy.should.not.have.been.called;
});
it('should not emit `unlink` for previously moved files', async () => {
const unlinkSpy = sinon.spy(function unlink(){});
const testPath = getFixturePath('change.txt');
const newPath1 = getFixturePath('moved.txt');
const newPath2 = getFixturePath('moved-again.txt');
await aspy(watcher, 'unlink', unlinkSpy);
await fs_rename(testPath, newPath1);
await delay(300);
await fs_rename(newPath1, newPath2);
await waitFor([unlinkSpy.withArgs(newPath1)]);
unlinkSpy.withArgs(testPath).should.have.been.calledOnce;
unlinkSpy.withArgs(newPath1).should.have.been.calledOnce;
unlinkSpy.withArgs(newPath2).should.not.have.been.called;
});
it('should survive ENOENT for missing subdirectories', async () => {
const testDir = getFixturePath('notadir');
await waitForWatcher(watcher);
watcher.add(testDir);
});
it('should notice when a file appears in a new directory', async () => {
const testDir = getFixturePath('subdir');
const testPath = getFixturePath('subdir/add.txt');
const spy = await aspy(watcher, 'add');
spy.should.not.have.been.called;
await fs_mkdir(testDir, PERM_ARR);
await write(testPath, Date.now());
await waitFor([spy]);
spy.should.have.been.calledOnce;
spy.should.have.been.calledWith(testPath);
expect(spy.args[0][1]).to.be.ok; // stats
rawSpy.should.have.been.called;
});
it('should watch removed and re-added directories', async () => {
const unlinkSpy = sinon.spy(function unlinkSpy(){});
const addSpy = sinon.spy(function addSpy(){});
const parentPath = getFixturePath('subdir2');
const subPath = getFixturePath('subdir2/subsub');
watcher.on('unlinkDir', unlinkSpy).on('addDir', addSpy);
await waitForWatcher(watcher);
await fs_mkdir(parentPath, PERM_ARR);
await delay(win32Polling ? 900 : 300);
await fs_rmdir(parentPath);
await waitFor([unlinkSpy.withArgs(parentPath)]);
unlinkSpy.should.have.been.calledWith(parentPath);
await fs_mkdir(parentPath, PERM_ARR);
await delay(win32Polling ? 2200 : 1200);
await fs_mkdir(subPath, PERM_ARR);
await waitFor([[addSpy, 3]]);
addSpy.should.have.been.calledWith(parentPath);
addSpy.should.have.been.calledWith(subPath);
});
});
describe('watch individual files', () => {
it('should detect changes', async () => {
const testPath = getFixturePath('change.txt');
let watcher = chokidar_watch(testPath, options);
const spy = await aspy(watcher, 'change');
await write(testPath, Date.now());
await waitFor([spy]);
spy.should.have.always.been.calledWith(testPath);
});
it('should detect unlinks', async () => {
const testPath = getFixturePath('unlink.txt');
let watcher = chokidar_watch(testPath, options);
const spy = await aspy(watcher, 'unlink');
await delay();
await fs_unlink(testPath);
await waitFor([spy]);
spy.should.have.been.calledWith(testPath);
});
it('should detect unlink and re-add', async () => {
options.ignoreInitial = true;
const unlinkSpy = sinon.spy(function unlinkSpy(){});
const addSpy = sinon.spy(function addSpy(){});
const testPath = getFixturePath('unlink.txt');
let watcher = chokidar_watch([testPath], options)
.on('unlink', unlinkSpy)
.on('add', addSpy);
await waitForWatcher(watcher);
await delay();
await fs_unlink(testPath);
await waitFor([unlinkSpy]);
unlinkSpy.should.have.been.calledWith(testPath);
await delay();
await write(testPath, 're-added');
await waitFor([addSpy]);
addSpy.should.have.been.calledWith(testPath);
});
it('should ignore unwatched siblings', async () => {
const testPath = getFixturePath('add.txt');
const siblingPath = getFixturePath('change.txt');
let watcher = chokidar_watch(testPath, options);
const spy = await aspy(watcher, 'all');
await delay();
await write(siblingPath, Date.now());
await write(testPath, Date.now());
await waitFor([spy]);
spy.should.have.always.been.calledWith('add', testPath);
});
// PR 682 is failing.
describe.skip('Skipping gh-682: should detect unlink', () => {
it('should detect unlink while watching a non-existent second file in another directory', async () => {
const testPath = getFixturePath('unlink.txt');
const otherDirPath = getFixturePath('other-dir');
const otherPath = getFixturePath('other-dir/other.txt');
fs.mkdirSync(otherDirPath, PERM_ARR);
let watcher = chokidar_watch([testPath, otherPath], options);
// intentionally for this test don't write fs.writeFileSync(otherPath, 'other');
const spy = await aspy(watcher, 'unlink');
await delay();
await fs_unlink(testPath);
await waitFor([spy]);
spy.should.have.been.calledWith(testPath);
});
it('should detect unlink and re-add while watching a second file', async () => {
options.ignoreInitial = true;
const unlinkSpy = sinon.spy(function unlinkSpy(){});
const addSpy = sinon.spy(function addSpy(){});
const testPath = getFixturePath('unlink.txt');
const otherPath = getFixturePath('other.txt');
fs.writeFileSync(otherPath, 'other');
let watcher = chokidar_watch([testPath, otherPath], options)
.on('unlink', unlinkSpy)
.on('add', addSpy);
await waitForWatcher(watcher);
await delay();
await fs_unlink(testPath);
await waitFor([unlinkSpy]);
await delay();
unlinkSpy.should.have.been.calledWith(testPath);
await delay();
write(testPath, 're-added');
await waitFor([addSpy]);
addSpy.should.have.been.calledWith(testPath);
});
it('should detect unlink and re-add while watching a non-existent second file in another directory', async () => {
options.ignoreInitial = true;
const unlinkSpy = sinon.spy(function unlinkSpy(){});
const addSpy = sinon.spy(function addSpy(){});
const testPath = getFixturePath('unlink.txt');
const otherDirPath = getFixturePath('other-dir');
const otherPath = getFixturePath('other-dir/other.txt');
fs.mkdirSync(otherDirPath, PERM_ARR);
// intentionally for this test don't write fs.writeFileSync(otherPath, 'other');
let watcher = chokidar_watch([testPath, otherPath], options)
.on('unlink', unlinkSpy)
.on('add', addSpy);
await waitForWatcher(watcher);
await delay();
await fs_unlink(testPath);
await waitFor([unlinkSpy]);
await delay();
unlinkSpy.should.have.been.calledWith(testPath);
await delay();
await write(testPath, 're-added');
await waitFor([addSpy]);
addSpy.should.have.been.calledWith(testPath);
});
it('should detect unlink and re-add while watching a non-existent second file in the same directory', async () => {
options.ignoreInitial = true;
const unlinkSpy = sinon.spy(function unlinkSpy(){});
const addSpy = sinon.spy(function addSpy(){});
const testPath = getFixturePath('unlink.txt');
const otherPath = getFixturePath('other.txt');
// intentionally for this test don't write fs.writeFileSync(otherPath, 'other');
let watcher = chokidar_watch([testPath, otherPath], options)
.on('unlink', unlinkSpy)
.on('add', addSpy);
await waitForWatcher(watcher);
await delay();
await fs_unlink(testPath);
await waitFor([unlinkSpy]);
await delay();
unlinkSpy.should.have.been.calledWith(testPath);
await delay();
await write(testPath, 're-added');
await waitFor([addSpy]);
addSpy.should.have.been.calledWith(testPath);
});
it('should detect two unlinks and one re-add', async () => {
options.ignoreInitial = true;
const unlinkSpy = sinon.spy(function unlinkSpy(){});
const addSpy = sinon.spy(function addSpy(){});
const testPath = getFixturePath('unlink.txt');
const otherPath = getFixturePath('other.txt');
fs.writeFileSync(otherPath, 'other');
let watcher = chokidar_watch([testPath, otherPath], options)
.on('unlink', unlinkSpy)
.on('add', addSpy);
await waitForWatcher(watcher);
await delay();
await fs_unlink(otherPath);
await delay();
await fs_unlink(testPath);
await waitFor([[unlinkSpy, 2]]);
await delay();
unlinkSpy.should.have.been.calledWith(otherPath);
unlinkSpy.should.have.been.calledWith(testPath);
await delay();
await write(testPath, 're-added');
await waitFor([addSpy]);
addSpy.should.have.been.calledWith(testPath);
});
it('should detect unlink and re-add while watching a second file and a non-existent third file', async () => {
options.ignoreInitial = true;
const unlinkSpy = sinon.spy(function unlinkSpy(){});
const addSpy = sinon.spy(function addSpy(){});
const testPath = getFixturePath('unlink.txt');
const otherPath = getFixturePath('other.txt');
const other2Path = getFixturePath('other2.txt');
fs.writeFileSync(otherPath, 'other');
// intentionally for this test don't write fs.writeFileSync(other2Path, 'other2');
let watcher = chokidar_watch([testPath, otherPath, other2Path], options)
.on('unlink', unlinkSpy)
.on('add', addSpy);
await waitForWatcher(watcher);
await delay();
await fs_unlink(testPath);
await waitFor([unlinkSpy]);
await delay();
unlinkSpy.should.have.been.calledWith(testPath);
await delay();
await write(testPath, 're-added');
await waitFor([addSpy]);
addSpy.should.have.been.calledWith(testPath);
});
});
});
describe('renamed directory', () => {
it('should emit `add` for a file in a renamed directory', async () => {
options.ignoreInitial = true;
const testDir = getFixturePath('subdir');
const testPath = getFixturePath('subdir/add.txt');
const renamedDir = getFixturePath('subdir-renamed');
const expectedPath = sysPath.join(renamedDir, 'add.txt');
await fs_mkdir(testDir, PERM_ARR);
await write(testPath, Date.now());
let watcher = chokidar_watch(currentDir, options);
const spy = await aspy(watcher, 'add');
await delay(1000);
await fs_rename(testDir, renamedDir);
await waitFor([spy.withArgs(expectedPath)]);
spy.should.have.been.calledWith(expectedPath);
});
});
describe('watch non-existent paths', () => {
it('should watch non-existent file and detect add', async () => {
const testPath = getFixturePath('add.txt');
let watcher = chokidar_watch(testPath, options);
const spy = await aspy(watcher, 'add');
await delay();
await write(testPath, Date.now());
await waitFor([spy]);
spy.should.have.been.calledWith(testPath);
});
it('should watch non-existent dir and detect addDir/add', async () => {
const testDir = getFixturePath('subdir');
const testPath = getFixturePath('subdir/add.txt');
let watcher = chokidar_watch(testDir, options);
const spy = await aspy(watcher, 'all');
spy.should.not.have.been.called;
await delay();
await fs_mkdir(testDir, PERM_ARR);
await delay();
await write(testPath, 'hello');
await waitFor([spy.withArgs('add')]);
spy.should.have.been.calledWith('addDir', testDir);
spy.should.have.been.calledWith('add', testPath);
});
});
describe('watch glob patterns', () => {
it('should correctly watch and emit based on glob input', async () => {
const watchPath = getGlobPath('*a*.txt');
const addPath = getFixturePath('add.txt');
const changePath = getFixturePath('change.txt');
let watcher = chokidar_watch(watchPath, options);
const spy = await aspy(watcher, 'all');
spy.should.have.been.calledWith('add', changePath);
await write(addPath, Date.now());
await write(changePath, Date.now());
await delay();
await waitFor([[spy, 3], spy.withArgs('add', addPath)]);
spy.should.have.been.calledWith('add', addPath);
spy.should.have.been.calledWith('change', changePath);
spy.should.not.have.been.calledWith('add', getFixturePath('unlink.txt'));
spy.should.not.have.been.calledWith('addDir');
});
it('should respect negated glob patterns', async () => {
const watchPath = getGlobPath('*');
const negatedWatchPath = '!' + getGlobPath('*a*.txt');
const unlinkPath = getFixturePath('unlink.txt');
let watcher = chokidar_watch([watchPath, negatedWatchPath], options);
const spy = await aspy(watcher, 'all');
spy.should.have.been.calledOnce;
spy.should.have.been.calledWith('add', unlinkPath);
await delay();
await fs_unlink(unlinkPath);
await waitFor([[spy, 2], spy.withArgs('unlink')]);
spy.should.have.been.calledTwice;
spy.should.have.been.calledWith('unlink', unlinkPath);
});
it('should traverse subdirs to match globstar patterns', async () => {
const watchPath = getGlobPath('../../test-*/' + subdirId + '/**/a*.txt');
const addFile = getFixturePath('add.txt');
const subdir = getFixturePath('subdir');
const subsubdir = getFixturePath('subdir/subsub');
const aFile = getFixturePath('subdir/a.txt');
const bFile = getFixturePath('subdir/b.txt');
const subFile = getFixturePath('subdir/subsub/ab.txt');
fs.mkdirSync(subdir, PERM_ARR);
fs.mkdirSync(subsubdir, PERM_ARR);
fs.writeFileSync(aFile, 'b');
fs.writeFileSync(bFile, 'b');
fs.writeFileSync(subFile, 'b');
await delay();
let watcher = chokidar_watch(watchPath, options);
const spy = await aspy(watcher, 'all');
await Promise.all([
write(addFile, Date.now()),
write(subFile, Date.now()),
fs_unlink(aFile),
fs_unlink(bFile),
]);
await waitFor([spy.withArgs('change')]);
spy.withArgs('change').should.have.been.calledOnce;
spy.should.have.been.calledWith('change', subFile);
await waitFor([spy.withArgs('unlink')]);
spy.withArgs('unlink').should.have.been.calledOnce;
spy.should.have.been.calledWith('unlink', aFile);
await waitFor([[spy.withArgs('add'), 3]]);
spy.withArgs('add').should.have.been.calledThrice;
});
it('should resolve relative paths with glob patterns', async () => {
const id = subdirId.toString();
const watchPath = 'test-*/' + id + '/*a*.txt';
// getFixturePath() returns absolute paths, so use sysPath.join() instead
const addPath = sysPath.join(FIXTURES_PATH_REL, id, 'add.txt');
const changePath = sysPath.join(FIXTURES_PATH_REL, id, 'change.txt');
const unlinkPath = getFixturePath('unlink.txt');
let watcher = chokidar_watch(watchPath, options);
const spy = await aspy(watcher, 'all');
spy.should.have.been.calledWith('add');
await Promise.all([
write(addPath, Date.now()),
write(changePath, Date.now())
]);
await waitFor([[spy, 3], spy.withArgs('add', addPath)]);
spy.should.have.been.calledWith('add', addPath);
spy.should.have.been.calledWith('change', changePath);
spy.should.not.have.been.calledWith('add', unlinkPath);
spy.should.not.have.been.calledWith('addDir');
if (!macosFswatch) spy.should.have.been.calledThrice;
});
it('should correctly handle conflicting glob patterns', async () => {
const changePath = getFixturePath('change.txt');
const unlinkPath = getFixturePath('unlink.txt');
const addPath = getFixturePath('add.txt');
const watchPaths = [getGlobPath('change*'), getGlobPath('unlink*')];
let watcher = chokidar_watch(watchPaths, options);
const spy = await aspy(watcher, 'all');
spy.should.have.been.calledWith('add', changePath);
spy.should.have.been.calledWith('add', unlinkPath);
spy.should.have.been.calledTwice;
await delay();
await fs_unlink(unlinkPath);
await write(addPath, Date.now());
await write(changePath, Date.now());
await waitFor([[spy, 4], spy.withArgs('unlink', unlinkPath)]);
spy.should.have.been.calledWith('change', changePath);
spy.should.have.been.calledWith('unlink', unlinkPath);
spy.should.not.have.been.calledWith('add', addPath);
spy.callCount.should.equal(4);
});
it('should correctly handle intersecting glob patterns', async () => {
const changePath = getFixturePath('change.txt');
const watchPaths = [getGlobPath('cha*'), getGlobPath('*nge.*')];
let watcher = chokidar_watch(watchPaths, options);
const spy = await aspy(watcher, 'all');
spy.should.have.been.calledWith('add', changePath);
spy.should.have.been.calledOnce;
await write(changePath, Date.now());
await delay();
await waitFor([[spy, 2]]);
spy.should.have.been.calledWith('change', changePath);
spy.should.have.been.calledTwice;
});
it('should not confuse glob-like filenames with globs', async () => {
const filePath = getFixturePath('nota[glob].txt');
await write(filePath, 'b');
await delay();
const spy = await aspy(chokidar_watch(), 'all');
spy.should.have.been.calledWith('add', filePath);
await delay();
await write(filePath, Date.now());
await waitFor([spy.withArgs('change', filePath)]);
spy.should.have.been.calledWith('change', filePath);
});
it('should treat glob-like directory names as literal directory names when globbing is disabled', async () => {
options.disableGlobbing = true;
const filePath = getFixturePath('nota[glob]/a.txt');
const watchPath = getFixturePath('nota[glob]');
const testDir = getFixturePath('nota[glob]');
const matchingDir = getFixturePath('notag');
const matchingFile = getFixturePath('notag/b.txt');
const matchingFile2 = getFixturePath('notal');
fs.mkdirSync(testDir, PERM_ARR);
fs.writeFileSync(filePath, 'b');
fs.mkdirSync(matchingDir, PERM_ARR);
fs.writeFileSync(matchingFile, 'c');
fs.writeFileSync(matchingFile2, 'd');
let watcher = chokidar_watch(watchPath, options);
const spy = await aspy(watcher, 'all');
spy.should.have.been.calledWith('add', filePath);
spy.should.not.have.been.calledWith('addDir', matchingDir);
spy.should.not.have.been.calledWith('add', matchingFile);
spy.should.not.have.been.calledWith('add', matchingFile2);
await delay();
await write(filePath, Date.now());
await waitFor([spy.withArgs('change', filePath)]);
spy.should.have.been.calledWith('change', filePath);
});
it('should treat glob-like filenames as literal filenames when globbing is disabled', async () => {
options.disableGlobbing = true;
const filePath = getFixturePath('nota[glob]');
// This isn't using getGlobPath because it isn't treated as a glob
const watchPath = getFixturePath('nota[glob]');
const matchingDir = getFixturePath('notag');
const matchingFile = getFixturePath('notag/a.txt');
const matchingFile2 = getFixturePath('notal');
fs.writeFileSync(filePath, 'b');
fs.mkdirSync(matchingDir, PERM_ARR);
fs.writeFileSync(matchingFile, 'c');
fs.writeFileSync(matchingFile2, 'd');
let watcher = chokidar_watch(watchPath, options);
const spy = await aspy(watcher, 'all');
spy.should.have.been.calledWith('add', filePath);
spy.should.not.have.been.calledWith('addDir', matchingDir);
spy.should.not.have.been.calledWith('add', matchingFile);
spy.should.not.have.been.calledWith('add', matchingFile2);
await delay();
await write(filePath, Date.now());
await waitFor([spy.withArgs('change', filePath)]);
spy.should.have.been.calledWith('change', filePath);
});
it('should not prematurely filter dirs against complex globstar patterns', async () => {
const deepFile = getFixturePath('subdir/subsub/subsubsub/a.txt');
const watchPath = getGlobPath('../../test-*/' + subdirId + '/**/subsubsub/*.txt');
fs.mkdirSync(getFixturePath('subdir'), PERM_ARR);
fs.mkdirSync(getFixturePath('subdir/subsub'), PERM_ARR);
fs.mkdirSync(getFixturePath('subdir/subsub/subsubsub'), PERM_ARR);
fs.writeFileSync(deepFile, 'b');
let watcher = chokidar_watch(watchPath, options);
const spy = await aspy(watcher, 'all');
await delay();
await write(deepFile, Date.now());
await waitFor([[spy, 2]]);
spy.should.have.been.calledWith('add', deepFile);
spy.should.have.been.calledWith('change', deepFile);
});
it('should emit matching dir events', async () => {
// test with and without globstar matches
const watchPaths = [getGlobPath('*'), getGlobPath('subdir/subsub/**/*')];
const deepDir = getFixturePath('subdir/subsub/subsubsub');
const deepFile = sysPath.join(deepDir, 'a.txt');
fs.mkdirSync(getFixturePath('subdir'), PERM_ARR);
fs.mkdirSync(getFixturePath('subdir/subsub'), PERM_ARR);
let watcher = chokidar_watch(watchPaths, options);
const spy = await aspy(watcher, 'all');
spy.should.have.been.calledWith('addDir', getFixturePath('subdir'));
spy.withArgs('addDir').should.have.been.calledOnce;
fs.mkdirSync(deepDir, PERM_ARR);
fs.writeFileSync(deepFile, Date.now());
await waitFor([[spy.withArgs('addDir'), 2], spy.withArgs('add', deepFile)]);
if (win32Polling) return true;
spy.should.have.been.calledWith('addDir', deepDir);
fs.unlinkSync(deepFile);
fs.rmdirSync(deepDir);
await waitFor([spy.withArgs('unlinkDir')]);
spy.should.have.been.calledWith('unlinkDir', deepDir);
});
it('should correctly handle glob with braces', async () => {
const watchPath = upath.normalizeSafe(getGlobPath('{subdir/*,subdir1/subsub1}/subsubsub/*.txt'));
const deepFileA = getFixturePath('subdir/subsub/subsubsub/a.txt');
const deepFileB = getFixturePath('subdir1/subsub1/subsubsub/a.txt');
fs.mkdirSync(getFixturePath('subdir'), PERM_ARR);
fs.mkdirSync(getFixturePath('subdir/subsub'), PERM_ARR);
fs.mkdirSync(getFixturePath('subdir/subsub/subsubsub'), PERM_ARR);
fs.mkdirSync(getFixturePath('subdir1'), PERM_ARR);
fs.mkdirSync(getFixturePath('subdir1/subsub1'), PERM_ARR);
fs.mkdirSync(getFixturePath('subdir1/subsub1/subsubsub'), PERM_ARR);
fs.writeFileSync(deepFileA, Date.now());
fs.writeFileSync(deepFileB, Date.now());
let watcher = chokidar_watch(watchPath, options);
const spy = await aspy(watcher, 'all');
spy.should.have.been.calledWith('add', deepFileA);
spy.should.have.been.calledWith('add', deepFileB);
fs.appendFileSync(deepFileA, Date.now());
fs.appendFileSync(deepFileB, Date.now());
await waitFor([[spy, 4]]);
spy.should.have.been.calledWith('change', deepFileA);
spy.should.have.been.calledWith('change', deepFileB);
});
});
describe('watch symlinks', () => {
if (os === 'win32') return true;
let linkedDir;
beforeEach(async () => {
linkedDir = sysPath.resolve(currentDir, '..', subdirId + '-link');
await fs_symlink(currentDir, linkedDir);
await fs_mkdir(getFixturePath('subdir'), PERM_ARR);
await write(getFixturePath('subdir/add.txt'), 'b');
return true;
});
afterEach(async () => {
await fs_unlink(linkedDir);
return true;
});
it('should watch symlinked dirs', async () => {
const dirSpy = sinon.spy(function dirSpy(){});
const addSpy = sinon.spy(function addSpy(){});
let watcher = chokidar_watch(linkedDir, options)
.on('addDir', dirSpy)