-
-
Notifications
You must be signed in to change notification settings - Fork 582
/
test.mjs
2135 lines (1960 loc) · 85.1 KB
/
test.mjs
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
import fs from 'node:fs';
import sysPath from 'node:path';
import {describe, it, before, after, beforeEach, afterEach} from 'node:test';
import {fileURLToPath, pathToFileURL} from 'node:url';
import {promisify} from 'node:util';
import childProcess from 'node:child_process';
import chai from 'chai';
import {rimraf} from 'rimraf';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import upath from 'upath';
import chokidar from './esm/index.js';
import { EVENTS as EV, isWindows, isMacos, isIBMi } from './esm/handler.js';
import { URL } from 'url'; // in Browser, the URL in native accessible on window
const __filename = fileURLToPath(new URL('', import.meta.url));
// Will contain trailing slash
const __dirname = fileURLToPath(new URL('.', import.meta.url));
const {expect} = chai;
chai.use(sinonChai);
chai.should();
const exec = promisify(childProcess.exec);
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 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
const TEST_TIMEOUT = 8000;
let subdirId = 0;
let currentDir;
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 === EV.ALL ?
(event, path) => spy(event, path) :
(path) => spy(path)) :
spy;
const timeout = setTimeout(() => {
reject(new Error('timeout'));
}, TEST_TIMEOUT);
watcher.on(EV.ERROR, (...args) => {
clearTimeout(timeout);
reject(...args);
});
watcher.on(EV.READY, () => {
clearTimeout(timeout);
resolve(spy);
});
watcher.on(eventName, handler);
});
};
const waitForWatcher = (watcher) => {
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(new Error('timeout'));
}, TEST_TIMEOUT);
watcher.on(EV.ERROR, (...args) => {
clearTimeout(timeout);
reject(...args);
});
watcher.on(EV.READY, (...args) => {
clearTimeout(timeout);
resolve(...args);
});
});
};
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) => {
const wt = chokidar.watch(path, opts);
allWatchers.push(wt);
return wt;
};
const waitFor = (spies) => {
if (spies.length === 0) throw new TypeError('SPies zero');
return new Promise((resolve, reject) => {
let checkTimer;
const timeout = setTimeout(() => {
clearTimeout(checkTimer);
reject(new Error('timeout'));
}, TEST_TIMEOUT);
const isSpyReady = (spy) => {
if (Array.isArray(spy)) {
return spy[0].callCount >= spy[1];
}
return spy.callCount >= 1;
};
const checkSpiesReady = () => {
clearTimeout(checkTimer);
if (spies.every(isSpyReady)) {
clearTimeout(timeout);
resolve();
} else {
checkTimer = setTimeout(checkSpiesReady, 20);
}
};
checkSpiesReady();
});
};
const waitForEvents = (watcher, count) => {
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(new Error('timeout'));
}, TEST_TIMEOUT);
const events = [];
const handler = (event, path) => {
events.push(`[ALL] ${event}: ${path}`)
if (events.length === count) {
watcher.off('all', handler);
clearTimeout(timeout);
resolve(events);
}
};
watcher.on('all', handler);
});
};
const dateNow = () => Date.now().toString();
const runTests = (baseopts) => {
let macosFswatch;
let win32Polling;
let options;
baseopts.persistent = true;
before(() => {
// flags for bypassing special-case test failures on CI
macosFswatch = isMacos && !baseopts.usePolling;
win32Polling = isWindows && baseopts.usePolling;
slowerDelay = macosFswatch ? 100 : undefined;
});
beforeEach(function clean() {
options = {};
Object.keys(baseopts).forEach((key) => {
options[key] = baseopts[key];
});
});
describe('watch a directory', () => {
let readySpy, rawSpy, watcher, watcher2;
beforeEach(async () => {
options.ignoreInitial = true;
options.alwaysStat = true;
readySpy = sinon.spy(function readySpy(){});
rawSpy = sinon.spy(function rawSpy(){});
watcher = chokidar_watch(currentDir, options).on(EV.READY, readySpy).on(EV.RAW, rawSpy);
await waitForWatcher(watcher);
});
afterEach(async () => {
await waitFor([readySpy]);
await watcher.close();
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 = sinon.spy(function addSpy(){});
watcher.on(EV.ADD, spy);
await delay();
await write(testPath, dateNow());
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(EV.ADD, (path) => {
spy(path);
});
write(paths[0], dateNow());
write(paths[1], dateNow());
write(paths[2], dateNow());
write(paths[3], dateNow());
write(paths[4], dateNow());
await delay(100);
write(paths[5], dateNow());
write(paths[6], dateNow());
await delay(150);
write(paths[7], dateNow());
write(paths[8], dateNow());
await waitFor([[spy, 4]]);
await delay(1000);
await waitFor([[spy, 9]]);
paths.forEach(path => {
spy.should.have.been.calledWith(path);
});
});
it('should emit thirtythree `add` events when thirtythree files were added in nine directories', async () => {
await 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);
await delay();
readySpy.resetHistory();
watcher2 = chokidar_watch(currentDir, options).on(EV.READY, readySpy).on(EV.RAW, rawSpy);
const spy = await aspy(watcher2, EV.ADD, null, true);
const filesToWrite = [
test1Path,
test2Path,
test3Path,
test4Path,
test5Path,
test6Path,
test7Path,
test8Path,
test9Path,
testb1Path,
testb2Path,
testb3Path,
testb4Path,
testb5Path,
testb6Path,
testb7Path,
testb8Path,
testb9Path,
testc1Path,
testc2Path,
testc3Path,
testc4Path,
testc5Path,
testc6Path,
testc7Path,
testc8Path,
testc9Path,
testd1Path,
teste1Path,
testf1Path,
testg1Path,
testh1Path,
testi1Path
];
let currentCallCount = 0;
for (const fileToWrite of filesToWrite) {
await write(fileToWrite, dateNow());
await waitFor([[spy, ++currentCallCount]]);
}
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 = sinon.spy(function addDirSpy(){});
watcher.on(EV.ADD_DIR, spy);
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 = sinon.spy(function changeSpy(){});
watcher.on(EV.CHANGE, spy);
spy.should.not.have.been.called;
await write(testPath, dateNow());
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 = sinon.spy(function unlinkSpy(){});
watcher.on(EV.UNLINK, spy);
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');
const spy = sinon.spy(function unlinkDirSpy(){});
await fs_mkdir(testDir, PERM_ARR);
await delay(300);
watcher.on(EV.UNLINK_DIR, spy);
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');
const spy = sinon.spy(function unlinkDirSpy(){});
await fs_mkdir(testDir, PERM_ARR);
await fs_mkdir(testDir2, PERM_ARR);
await fs_mkdir(testDir3, PERM_ARR);
await delay(300);
watcher.on(EV.UNLINK_DIR, spy);
await rimraf(testDir2);
await waitFor([[spy, 2]]);
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(EV.UNLINK, unlinkSpy).on(EV.ADD, addSpy);
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 () => {
if (isWindows) {
console.warn('test skipped')
return true;
}
const unlinkSpy = sinon.spy(function unlink(){});
const addSpy = sinon.spy(function add(){});
const changeSpy = sinon.spy(function change(){});
const testPath = getFixturePath('add.txt');
watcher
.on(EV.UNLINK, unlinkSpy)
.on(EV.ADD, addSpy)
.on(EV.CHANGE, changeSpy);
await write(testPath, 'hello');
await waitFor([[addSpy.withArgs(testPath), 1]]);
unlinkSpy.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(100);
await write(testPath, dateNow());
await waitFor([[addSpy.withArgs(testPath), 2]]);
addSpy.should.have.been.calledWith(testPath);
changeSpy.should.not.have.been.called;
expect(addSpy.callCount).to.equal(2);
});
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');
watcher.on(EV.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');
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 = sinon.spy(function addSpy(){});
watcher.on(EV.ADD, spy);
spy.should.not.have.been.called;
await fs_mkdir(testDir, PERM_ARR);
await write(testPath, dateNow());
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(EV.UNLINK_DIR, unlinkSpy).on(EV.ADD_DIR, addSpy);
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);
});
it('should emit `unlinkDir` and `add` when dir is replaced by file', async () => {
options.ignoreInitial = true;
const unlinkSpy = sinon.spy(function unlinkSpy(){});
const addSpy = sinon.spy(function addSpy(){});
const testPath = getFixturePath('dirFile');
await fs_mkdir(testPath, PERM_ARR);
await delay(300);
watcher.on(EV.UNLINK_DIR, unlinkSpy).on(EV.ADD, addSpy);
await fs_rmdir(testPath);
await waitFor([unlinkSpy]);
await write(testPath, 'file content');
await waitFor([addSpy]);
unlinkSpy.should.have.been.calledWith(testPath);
addSpy.should.have.been.calledWith(testPath);
});
it('should emit `unlink` and `addDir` when file is replaced by dir', async () => {
options.ignoreInitial = true;
const unlinkSpy = sinon.spy(function unlinkSpy(){});
const addSpy = sinon.spy(function addSpy(){});
const testPath = getFixturePath('fileDir');
await write(testPath, 'file content');
watcher.on(EV.UNLINK, unlinkSpy).on(EV.ADD_DIR, addSpy);
await delay(300);
await fs_unlink(testPath);
await delay(300);
await fs_mkdir(testPath, PERM_ARR);
await waitFor([addSpy, unlinkSpy]);
unlinkSpy.should.have.been.calledWith(testPath);
addSpy.should.have.been.calledWith(testPath);
});
});
describe('watch individual files', () => {
it('should emit `ready` when three files were added', async () => {
const readySpy = sinon.spy(function readySpy(){});
const watcher = chokidar_watch(currentDir, options).on(EV.READY, readySpy);
const path1 = getFixturePath('add1.txt');
const path2 = getFixturePath('add2.txt');
const path3 = getFixturePath('add3.txt');
watcher.add(path1);
watcher.add(path2);
watcher.add(path3);
await waitForWatcher(watcher);
// callCount is 1 on macOS, 4 on Ubuntu
readySpy.callCount.should.be.greaterThanOrEqual(1);
});
it('should detect changes', async () => {
const testPath = getFixturePath('change.txt');
const watcher = chokidar_watch(testPath, options);
const spy = await aspy(watcher, EV.CHANGE);
await write(testPath, dateNow());
await waitFor([spy]);
spy.should.have.always.been.calledWith(testPath);
});
it('should detect unlinks', async () => {
const testPath = getFixturePath('unlink.txt');
const watcher = chokidar_watch(testPath, options);
const spy = await aspy(watcher, EV.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');
const watcher = chokidar_watch([testPath], options)
.on(EV.UNLINK, unlinkSpy)
.on(EV.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');
const watcher = chokidar_watch(testPath, options);
const spy = await aspy(watcher, EV.ALL);
await delay();
await write(siblingPath, dateNow());
await write(testPath, dateNow());
await waitFor([spy]);
spy.should.have.always.been.calledWith(EV.ADD, testPath);
});
it('should detect safe-edit', async () => {
const testPath = getFixturePath('change.txt');
const safePath = getFixturePath('tmp.txt');
await write(testPath, dateNow());
const watcher = chokidar_watch(testPath, options);
const spy = await aspy(watcher, EV.ALL);
await delay();
await write(safePath, dateNow());
await fs_rename(safePath, testPath);
await delay(300);
await write(safePath, dateNow());
await fs_rename(safePath, testPath);
await delay(300);
await write(safePath, dateNow());
await fs_rename(safePath, testPath);
await delay(300);
await waitFor([spy]);
spy.withArgs(EV.CHANGE, testPath).should.have.been.calledThrice;
});
// 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);
const watcher = chokidar_watch([testPath, otherPath], options);
// intentionally for this test don't write fs.writeFileSync(otherPath, 'other');
const spy = await aspy(watcher, EV.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');
const watcher = chokidar_watch([testPath, otherPath], options)
.on(EV.UNLINK, unlinkSpy)
.on(EV.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');
const watcher = chokidar_watch([testPath, otherPath], options)
.on(EV.UNLINK, unlinkSpy)
.on(EV.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');
const watcher = chokidar_watch([testPath, otherPath], options)
.on(EV.UNLINK, unlinkSpy)
.on(EV.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');
const watcher = chokidar_watch([testPath, otherPath], options)
.on(EV.UNLINK, unlinkSpy)
.on(EV.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');
const watcher = chokidar_watch([testPath, otherPath, other2Path], options)
.on(EV.UNLINK, unlinkSpy)
.on(EV.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, dateNow());
const watcher = chokidar_watch(currentDir, options);
const spy = await aspy(watcher, EV.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');
const watcher = chokidar_watch(testPath, options);
const spy = await aspy(watcher, EV.ADD);
await delay();
await write(testPath, dateNow());
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');
const watcher = chokidar_watch(testDir, options);
const spy = await aspy(watcher, EV.ALL);
spy.should.not.have.been.called;
await delay();
await fs_mkdir(testDir, PERM_ARR);
await waitFor([spy.withArgs(EV.ADD_DIR)]);
await write(testPath, 'hello');
await waitFor([spy.withArgs(EV.ADD)]);
spy.should.have.been.calledWith(EV.ADD_DIR, testDir);
spy.should.have.been.calledWith(EV.ADD, testPath);
});
});
describe('not watch glob patterns', () => {
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(currentDir, options), EV.ALL);
spy.should.have.been.calledWith(EV.ADD, filePath);
await delay();
await write(filePath, dateNow());
await waitFor([spy.withArgs(EV.CHANGE, filePath)]);
spy.should.have.been.calledWith(EV.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');
const watcher = chokidar_watch(watchPath, options);
const spy = await aspy(watcher, EV.ALL);
spy.should.have.been.calledWith(EV.ADD, filePath);
spy.should.not.have.been.calledWith(EV.ADD_DIR, matchingDir);
spy.should.not.have.been.calledWith(EV.ADD, matchingFile);
spy.should.not.have.been.calledWith(EV.ADD, matchingFile2);
await delay();
await write(filePath, dateNow());
await waitFor([spy.withArgs(EV.CHANGE, filePath)]);
spy.should.have.been.calledWith(EV.CHANGE, filePath);
});
it('should treat glob-like filenames as literal filenames', 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');
const watcher = chokidar_watch(watchPath, options);
const spy = await aspy(watcher, EV.ALL);
spy.should.have.been.calledWith(EV.ADD, filePath);
spy.should.not.have.been.calledWith(EV.ADD_DIR, matchingDir);
spy.should.not.have.been.calledWith(EV.ADD, matchingFile);
spy.should.not.have.been.calledWith(EV.ADD, matchingFile2);
await delay();
await write(filePath, dateNow());
await waitFor([spy.withArgs(EV.CHANGE, filePath)]);
spy.should.have.been.calledWith(EV.CHANGE, filePath);
});
});
describe('watch symlinks', () => {
if (isWindows) return true;
let linkedDir;
beforeEach(async () => {
linkedDir = sysPath.resolve(currentDir, '..', `${subdirId}-link`);
await fs_symlink(currentDir, linkedDir, isWindows ? 'dir' : null);
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(){});
const watcher = chokidar_watch(linkedDir, options)
.on(EV.ADD_DIR, dirSpy)
.on(EV.ADD, addSpy);
await waitForWatcher(watcher);
dirSpy.should.have.been.calledWith(linkedDir);
addSpy.should.have.been.calledWith(sysPath.join(linkedDir, 'change.txt'));
addSpy.should.have.been.calledWith(sysPath.join(linkedDir, 'unlink.txt'));
});
it('should watch symlinked files', async () => {
const changePath = getFixturePath('change.txt');
const linkPath = getFixturePath('link.txt');
fs.symlinkSync(changePath, linkPath);
const watcher = chokidar_watch(linkPath, options);
const spy = await aspy(watcher, EV.ALL);
await write(changePath, dateNow());
await waitFor([spy.withArgs(EV.CHANGE)]);
spy.should.have.been.calledWith(EV.ADD, linkPath);
spy.should.have.been.calledWith(EV.CHANGE, linkPath);
});
it('should follow symlinked files within a normal dir', async () => {
const changePath = getFixturePath('change.txt');
const linkPath = getFixturePath('subdir/link.txt');
fs.symlinkSync(changePath, linkPath);
const watcher = chokidar_watch(getFixturePath('subdir'), options);
const spy = await aspy(watcher, EV.ALL);
await write(changePath, dateNow());
await waitFor([spy.withArgs(EV.CHANGE, linkPath)]);
spy.should.have.been.calledWith(EV.ADD, linkPath);
spy.should.have.been.calledWith(EV.CHANGE, linkPath);
});
it('should watch paths with a symlinked parent', async () => {
const testDir = sysPath.join(linkedDir, 'subdir');
const testFile = sysPath.join(testDir, 'add.txt');
const watcher = chokidar_watch(testDir, options);
const spy = await aspy(watcher, EV.ALL);
spy.should.have.been.calledWith(EV.ADD_DIR, testDir);
spy.should.have.been.calledWith(EV.ADD, testFile);
await write(getFixturePath('subdir/add.txt'), dateNow());
await waitFor([spy.withArgs(EV.CHANGE)]);
spy.should.have.been.calledWith(EV.CHANGE, testFile);
});
it('should not recurse indefinitely on circular symlinks', async () => {
await fs_symlink(currentDir, getFixturePath('subdir/circular'), isWindows ? 'dir' : null);
return new Promise((resolve, reject) => {