-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.test.ts
832 lines (790 loc) · 32.9 KB
/
events.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
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
import {mkdir, open, rename, rm, rmdir} from "node:fs/promises";
import * as os from "node:os";
import {basename, resolve, sep} from "node:path";
import type {WatchEventType} from "node:fs";
import {watch} from "node:fs";
import test from "ava";
import {temporaryDirectoryTask, temporaryFileTask} from "tempy";
import {EMPTY, firstValueFrom, from, of, Subject} from "rxjs";
import {delay, map, mergeMap, mergeWith, scan, switchMap, takeWhile, toArray} from "rxjs/operators";
import {
all,
first,
fold,
initial,
last,
partition,
partitionWhile,
tail,
take
} from "@softwareventures/array";
import type {ChangeEvent, FileEvent, RenameEvent} from "./events.js";
import {observeFileEvents} from "./events.js";
const platformTest = os.platform() === "win32" || os.platform() === "linux" ? test : test.serial;
// Test our assumption about fs.watch, that it will emit events for any changes
// that happen to watched files immediately after the call to fs.watch returns.
//
// This behaviour is not documented, but it is implied by the absence of any
// "ready" event.
platformTest("fs.watch: watch file, events start emitting immediately after return", async t => {
await temporaryFileTask(async path => {
const file = await open(path, "w");
await file.close();
const abortController = new AbortController();
const events: Array<
| {readonly event: "Error"; readonly error: Error}
| {readonly event: WatchEventType; readonly path: string | null}
> = [];
const watcher = watch(
path,
{signal: abortController.signal},
(event, path) => void events.push({event, path})
);
watcher.addListener("error", error => void events.push({event: "Error", error}));
// On macOS, fs.watch does not start emitting events until after an
// arbitrary delay.
// See https://github.com/nodejs/node/issues/52601
if (os.platform() !== "win32" && os.platform() !== "linux") {
await new Promise(resolve => {
setTimeout(resolve, 200);
});
}
const file2 = await open(path, "w");
await file2.close();
abortController.abort();
t.deepEqual(events, [{event: "change", path: basename(path)}]);
});
});
// Test our assumption about fs.watch, that it will emit events for any changes
// that happen to watched files immediately after the call to fs.watch returns.
//
// This behaviour is not documented, but it is implied by the absence of any
// "ready" event.
platformTest(
"fs.watch: watch directory, events start emitting immediately after return",
async t => {
await temporaryDirectoryTask(async path => {
const abortController = new AbortController();
const events: Array<
| {readonly event: "Error"; readonly error: Error}
| {readonly event: WatchEventType; readonly path: string | null}
> = [];
const watcher = watch(
path,
{signal: abortController.signal},
(event, path) => void events.push({event, path})
);
watcher.addListener("error", error => void events.push({event: "Error", error}));
// On macOS, fs.watch does not start emitting events until after an
// arbitrary delay.
// See https://github.com/nodejs/node/issues/52601
if (os.platform() !== "win32" && os.platform() !== "linux") {
await new Promise(resolve => {
setTimeout(resolve, 200);
});
}
const file = await open(resolve(path, "test"), "w");
await file.close();
// On macOS, there is an arbitrary delay before each event.
if (os.platform() !== "win32" && os.platform() !== "linux") {
await new Promise(resolve => {
setTimeout(resolve, 200);
});
}
abortController.abort();
t.deepEqual(events, [{event: "rename", path: "test"}]);
});
}
);
// Recursive directory watch is unavailable on Linux
if (os.platform() !== "linux") {
// Test our assumption about fs.watch, that it will emit events for any changes
// that happen to watched files immediately after the call to fs.watch returns.
//
// This behaviour is not documented, but it is implied by the absence of any
// "ready" event.
platformTest(
"fs.watch: watch directory recursively, events start emitting immediately after return",
async t => {
await temporaryDirectoryTask(async path => {
const abortController = new AbortController();
const events: Array<
| {readonly event: "Error"; readonly error: Error}
| {readonly event: WatchEventType; readonly path: string | null}
> = [];
const watcher = watch(
path,
{signal: abortController.signal, recursive: true},
(event, path) => void events.push({event, path})
);
watcher.addListener("error", error => void events.push({event: "Error", error}));
// On macOS, fs.watch does not start emitting events until after an
// arbitrary delay.
// See https://github.com/nodejs/node/issues/52601
if (os.platform() !== "win32") {
await new Promise(resolve => {
setTimeout(resolve, 200);
});
}
await mkdir(resolve(path, "a"));
const file = await open(resolve(path, "a", "b"), "w");
await file.close();
// On macOS, there is an arbitrary delay before each event.
if (os.platform() !== "win32" && os.platform() !== "linux") {
await new Promise(resolve => {
setTimeout(resolve, 200);
});
}
abortController.abort();
t.deepEqual(take(events, 2), [
{event: "rename", path: "a"},
{event: "rename", path: `a${sep}b`}
]);
});
}
);
}
platformTest("observeFileEvents: file no actions", async t => {
const {events} = await testFileEvents(async () => {});
t.deepEqual(events, []);
});
platformTest("observeFileEvents: open file for append and close", async t => {
const {events} = await testFileEvents(async path => {
const file = await open(path, "a");
await file.close();
});
t.deepEqual(events, []);
});
platformTest("observeFileEvents: open file for write and close", async t => {
const {testFilename, events} = await testFileEvents(async path => {
const file = await open(path, "w");
await file.close();
});
t.deepEqual(events, [{event: "Change", path: testFilename}]);
});
platformTest("observeFileEvents: open file for write, write text and close", async t => {
const {testFilename, events} = await testFileEvents(async path => {
const file = await open(path, "w");
await file.write("test");
await file.close();
});
if (os.platform() === "win32" || os.platform() === "linux") {
t.deepEqual(omitDuplicateChange(events), [{event: "Change", path: testFilename}]);
} else {
t.deepEqual(omitDuplicateChange(omitRenameAfterChange(events)), [
{event: "Change", path: testFilename}
]);
}
});
platformTest(
"observeFileEvents: open file for write, write text, close, open same file for append, write text, and close",
async t => {
const {testFilename, events} = await testFileEvents(async path => {
const file = await open(path, "w");
await file.write("test");
await file.close();
const file2 = await open(path, "a");
await file2.write("test2");
await file2.close();
});
// Expect at least two "Change" events, but possibly more.
// When a file is opened and then written, this generates at least one
// change event, but sometimes more than one.
// Since we open and write the file twice, there should be at least
// two "Change" events.
t.deepEqual(
fold(
os.platform() === "win32" || os.platform() === "linux"
? events
: omitRenameAfterChange(events),
(accumulator: Array<RenameEvent | ChangeEvent>, event) =>
event.event === "Change" &&
last(accumulator)?.event === "Change" &&
last(accumulator)?.path === event.path &&
last(initial(accumulator))?.event === "Change" &&
last(initial(accumulator))?.path === event.path
? accumulator
: [...accumulator, event],
[]
),
[
{event: "Change", path: testFilename},
{event: "Change", path: testFilename}
]
);
}
);
platformTest("observeFileEvents: rename file", async t => {
const {testFilename, events} = await testFileEvents(async path => {
await rename(path, `${path}_renamed`);
});
t.deepEqual(events, [
{
event: "Rename",
path:
os.platform() === "win32" || os.platform() === "linux"
? testFilename
: `${testFilename}_renamed`
}
]);
});
platformTest("observeFileEvents: delete file", async t => {
const {testFilename, events} = await testFileEvents(async path => {
await rm(path);
});
t.deepEqual(
events,
os.platform() === "linux"
? [
{event: "Change", path: testFilename},
{event: "Rename", path: testFilename},
{event: "Rename", path: testFilename}
]
: [{event: "Rename", path: testFilename}]
);
});
platformTest("observeFileEvents: observe directory non-recursive, no actions", async t => {
const {events} = await testDirectoryEvents(async () => {});
t.deepEqual(events, []);
});
platformTest(
"observeFileEvents: observe directory non-recursive, open file for write and close",
async t => {
const {events} = await testDirectoryEvents(async path => {
const file = await open(resolve(path, "a"), "w");
await file.close();
});
t.deepEqual(events, [{event: "Rename", path: "a"}]);
}
);
platformTest(
"observeFileEvents: observe directory non-recursive, open file for write, write text, and close",
async t => {
const {events} = await testDirectoryEvents(async path => {
const file = await open(resolve(path, "a"), "w");
await file.write("test");
await file.close();
});
if (os.platform() === "win32") {
t.deepEqual(omitDuplicateChange(events), [
{event: "Rename", path: "a"},
{event: "Change", path: "a"}
]);
} else if (os.platform() === "linux") {
t.deepEqual(omitChangeAfterRename(events), [{event: "Rename", path: "a"}]);
} else {
t.deepEqual(omitChangeAfterRename(omitRenameAfterChange(omitDuplicateRename(events))), [
{event: "Rename", path: "a"}
]);
}
}
);
platformTest(
"observeFileEvents: observe directory non-recursive, open file for write, write text, close, open another file for write, write text, and close",
async t => {
const {events} = await testDirectoryEvents(async path => {
const file = await open(resolve(path, "a"), "w");
await file.write("test");
await file.close();
const file2 = await open(resolve(path, "b"), "w");
await file2.write("test2");
await file2.close();
});
if (os.platform() === "win32") {
t.deepEqual(omitDuplicateChange(events), [
{event: "Rename", path: "a"},
{event: "Change", path: "a"},
{event: "Rename", path: "b"},
{event: "Change", path: "b"}
]);
} else if (os.platform() === "linux") {
t.deepEqual(omitChangeAfterRename(events), [
{event: "Rename", path: "a"},
{event: "Rename", path: "b"}
]);
} else {
t.deepEqual(omitChangeAfterRename(omitRenameAfterChange(omitDuplicateRename(events))), [
{event: "Rename", path: "a"},
{event: "Rename", path: "b"}
]);
}
}
);
platformTest(
"observeFileEvents: observe directory non-recursive, open file for write, close, open another file for write, close, rename first file",
async t => {
const {events} = await testDirectoryEvents(async path => {
const pathA = resolve(path, "a");
const pathB = resolve(path, "b");
const pathC = resolve(path, "c");
const file = await open(pathA, "w");
await file.close();
const file2 = await open(pathB, "w");
await file2.close();
await rename(pathA, pathC);
await rm(pathB);
});
if (os.platform() === "win32" || os.platform() === "linux") {
t.deepEqual(events, [
{event: "Rename", path: "a"},
{event: "Rename", path: "b"},
{event: "Rename", path: "a"},
{event: "Rename", path: "c"},
{event: "Rename", path: "b"}
]);
} else {
// When a file is moved, renamed, or deleted, sometimes the events
// pertaining to the file before it was moved, renamed, or deleted
// are omitted.
const [aEvents, otherEvents] = partition(events, event => event.path === "a");
const [bEvents, otherEvents2] = partition(otherEvents, event => event.path === "b");
const [cEvents, unexpectedEvents] = partition(
otherEvents2,
event => event.path === "c"
);
t.deepEqual(first(aEvents), {event: "Rename", path: "a"});
t.true(all(tail(aEvents), event => event.event === "Rename"));
t.deepEqual(first(bEvents), {event: "Rename", path: "b"});
t.true(all(tail(bEvents), event => event.event === "Rename"));
t.deepEqual(cEvents, [{event: "Rename", path: "c"}]);
t.deepEqual(unexpectedEvents, []);
}
}
);
platformTest(
"observeFileEvents: observe directory non-recursive, make inner directory, open file for write in inner directory, close, move file to observed directory",
async t => {
const {events} = await testDirectoryEvents(async path => {
const pathA = resolve(path, "a");
const pathB = resolve(pathA, "b");
const pathC = resolve(path, "c");
await mkdir(pathA);
const file = await open(pathB, "w");
await file.close();
await rename(pathB, pathC);
await rmdir(pathA);
});
if (os.platform() === "win32") {
// Creating a file in the watched directory sometimes, but not always,
// generates a "Change" event for the watched directory.
const [aEvents, otherEvents] = partitionWhile(events, event => event.path === "a");
t.deepEqual(first(aEvents), {event: "Rename", path: "a"});
t.true(all(tail(aEvents), event => event.event === "Change"));
t.deepEqual(otherEvents, [
{event: "Rename", path: "c"},
{event: "Rename", path: "a"}
]);
} else if (os.platform() === "linux") {
t.deepEqual(events, [
{event: "Rename", path: "a"},
{event: "Rename", path: "c"},
{event: "Rename", path: "a"}
]);
} else {
// When a file is moved, renamed, or deleted, sometimes the events
// pertaining to the file before it was moved, renamed, or deleted
// are omitted.
const [aEvents, otherEvents] = partition(events, event => event.path === "a");
const [cEvents, unexpectedEvents] = partition(otherEvents, event => event.path === "c");
t.deepEqual(first(aEvents), {event: "Rename", path: "a"});
t.true(all(tail(aEvents), event => event.event === "Rename"));
t.deepEqual(cEvents, [{event: "Rename", path: "c"}]);
t.deepEqual(unexpectedEvents, []);
}
}
);
platformTest(
"observeFileEvents: observe directory non-recursive, delete observed directory",
async t => {
const result = testDirectoryEvents(async path => {
await rmdir(path);
});
if (os.platform() === "win32") {
await t.throwsAsync(result, {code: "EPERM"});
} else if (os.platform() === "linux") {
// The "path" field in both events will be the name of the directory we are watching.
const {testFilename, events} = await result;
t.deepEqual(events, [
{event: "Rename", path: testFilename},
{event: "Rename", path: testFilename}
]);
} else {
const {events} = await result;
t.deepEqual(events, []);
}
}
);
// Recursive directory observation is unavailable on Linux
if (os.platform() !== "linux") {
platformTest("observeFileEvents: observe directory recursive, no actions", async t => {
const {events} = await testDirectoryEvents(async () => {}, true);
t.deepEqual(events, []);
});
platformTest(
"observeFileEvents: observe directory recursive, open file for write and close",
async t => {
const {events} = await testDirectoryEvents(async path => {
const file = await open(resolve(path, "a"), "w");
await file.close();
}, true);
t.deepEqual(events, [{event: "Rename", path: "a"}]);
}
);
platformTest(
"observeFileEvents: observe directory recursive, open file for write, write text, and close",
async t => {
const {events} = await testDirectoryEvents(async path => {
const file = await open(resolve(path, "a"), "w");
await file.write("test");
await file.close();
}, true);
if (os.platform() === "win32") {
t.deepEqual(omitDuplicateChange(events), [
{event: "Rename", path: "a"},
{event: "Change", path: "a"}
]);
} else {
t.deepEqual(
omitChangeAfterRename(omitRenameAfterChange(omitDuplicateRename(events))),
[{event: "Rename", path: "a"}]
);
}
}
);
platformTest(
"observeFileEvents: observe directory recursive, open file for write, write text, close, open another file for write, write text, close",
async t => {
const {events} = await testDirectoryEvents(async path => {
const file = await open(resolve(path, "a"), "w");
await file.write("test");
await file.close();
const file2 = await open(resolve(path, "b"), "w");
await file2.write("test2");
await file2.close();
}, true);
if (os.platform() === "win32") {
t.deepEqual(omitDuplicateChange(events), [
{event: "Rename", path: "a"},
{event: "Change", path: "a"},
{event: "Rename", path: "b"},
{event: "Change", path: "b"}
]);
} else {
t.deepEqual(
omitChangeAfterRename(omitRenameAfterChange(omitDuplicateRename(events))),
[
{event: "Rename", path: "a"},
{event: "Rename", path: "b"}
]
);
}
}
);
platformTest(
"observeFileEvents: observe directory recursive, open file for write, close, open another file for write, close, rename first file",
async t => {
const {events} = await testDirectoryEvents(async path => {
const pathA = resolve(path, "a");
const pathB = resolve(path, "b");
const pathC = resolve(path, "c");
const file = await open(pathA, "w");
await file.close();
const file2 = await open(pathB, "w");
await file2.close();
await rename(pathA, pathC);
await rm(pathB);
}, true);
if (os.platform() === "win32") {
t.deepEqual(events, [
{event: "Rename", path: "a"},
{event: "Rename", path: "b"},
{event: "Rename", path: "a"},
{event: "Rename", path: "c"},
{event: "Rename", path: "b"}
]);
} else {
// When a file is moved, renamed, or deleted, sometimes the events
// pertaining to the file before it was moved, renamed, or deleted
// are omitted.
const [aEvents, otherEvents] = partition(events, event => event.path === "a");
const [bEvents, otherEvents2] = partition(otherEvents, event => event.path === "b");
const [cEvents, unexpectedEvents] = partition(
otherEvents2,
event => event.path === "c"
);
t.deepEqual(first(aEvents), {event: "Rename", path: "a"});
t.true(all(tail(aEvents), event => event.event === "Rename"));
t.deepEqual(first(bEvents), {event: "Rename", path: "b"});
t.deepEqual(cEvents, [{event: "Rename", path: "c"}]);
t.deepEqual(unexpectedEvents, []);
}
}
);
platformTest(
"observeFileEvents: observe directory recursive, make inner directory, open file for write in inner directory, close, move file to observed directory, delete inner directory",
async t => {
const {events} = await testDirectoryEvents(async path => {
const pathA = resolve(path, "a");
const pathB = resolve(pathA, "b");
const pathC = resolve(path, "c");
await mkdir(pathA);
const file = await open(pathB, "w");
await file.close();
await rename(pathB, pathC);
await rmdir(pathA);
}, true);
if (os.platform() === "win32") {
// Creating or deleting a file in the watched directory sometimes,
// but not always, generates a "Change" event for the watched directory.
const [events1, afterEvents1] = partitionWhile(events, event => event.path === "a");
const [events2, afterEvents2] = partitionWhile(
afterEvents1,
event => event.path === `a${sep}b`
);
const [events3, afterEvents3] = partitionWhile(
afterEvents2,
event => event.path === "a"
);
const [events4, afterEvents4] = partitionWhile(
afterEvents3,
event => event.path === `a${sep}b`
);
const [events5, afterEvents5] = partitionWhile(
afterEvents4,
event => event.path === "a"
);
const [events6, otherEvents] = partitionWhile(
afterEvents5,
event => event.path === "c"
);
t.deepEqual(events1, [{event: "Rename", path: "a"}]);
t.deepEqual(events2, [{event: "Rename", path: `a${sep}b`}]);
t.true(events3.length <= 1);
t.true(all(events3, event => event.event === "Change"));
t.deepEqual(events4, [{event: "Rename", path: `a${sep}b`}]);
t.true(events5.length <= 1);
t.true(all(events5, event => event.event === "Change"));
t.deepEqual(events6, [{event: "Rename", path: "c"}]);
if (otherEvents.length === 1) {
t.deepEqual(otherEvents, [{event: "Rename", path: "a"}]);
} else {
t.deepEqual(otherEvents, [
{event: "Change", path: "a"},
{event: "Rename", path: "a"}
]);
}
} else {
// When a file is moved, renamed, or deleted, sometimes the events
// pertaining to the file before it was moved, renamed, or deleted
// are omitted.
const [aEvents, otherEvents] = partition(events, event => event.path === "a");
const [bEvents, otherEvents2] = partition(
otherEvents,
event => event.path === `a${sep}b`
);
const [cEvents, unexpectedEvents] = partition(
otherEvents2,
event => event.path === "c"
);
t.deepEqual(first(aEvents), {event: "Rename", path: "a"});
t.true(all(tail(aEvents), event => event.event === "Rename"));
t.deepEqual(first(bEvents), {event: "Rename", path: `a${sep}b`});
t.true(all(tail(bEvents), event => event.event === "Rename"));
t.deepEqual(cEvents, [{event: "Rename", path: "c"}]);
t.deepEqual(unexpectedEvents, []);
}
}
);
platformTest(
"observeFileEvents: observe directory recursive, delete observed directory",
async t => {
const result = testDirectoryEvents(async path => {
await rmdir(path);
}, true);
if (os.platform() === "win32") {
await t.throwsAsync(result, {code: "EPERM"});
} else {
const {events} = await result;
t.deepEqual(events, []);
}
}
);
}
platformTest("observeFileEvents: first event is Ready", async t => {
await temporaryFileTask(async path => {
const writeFile = async (): Promise<void> => {
const file = await open(path, "w");
await file.close();
};
await writeFile();
const interval = setInterval(() => void writeFile(), 10);
const events = await firstValueFrom(
observeFileEvents(path).pipe(
mergeMap(event => {
clearInterval(interval);
return of(event).pipe(
mergeWith(
from(writeFile()).pipe(
map(() => ({event: "Done"}) as const),
delay(
os.platform() === "win32" || os.platform() === "linux"
? 20
: 200
)
)
)
);
}),
takeWhile(({event}) => event !== "Done"),
toArray()
)
);
t.true(events.length > 1);
t.deepEqual(events[0], {event: "Ready", path, recursive: false});
t.true(all(initial(tail(events)), ({event}) => event !== "Ready" && event !== "Done"));
});
});
interface TestEventsResult {
readonly testFilename: string;
readonly events: ReadonlyArray<RenameEvent | ChangeEvent>;
}
async function testFileEvents(actions: (path: string) => Promise<void>): Promise<TestEventsResult> {
return temporaryFileTask(async path => {
const file = await open(path, "w");
await file.close();
const events = await testEventsInternal({path, actions});
return {testFilename: basename(path), events};
});
}
async function testDirectoryEvents(
actions: (path: string) => Promise<void>,
recursive = false
): Promise<TestEventsResult> {
return temporaryDirectoryTask(async path => {
const events = await testEventsInternal({path, actions, recursive});
return {testFilename: basename(path), events};
});
}
interface TestEventsInternalOptions {
readonly path: string;
readonly actions: (path: string) => Promise<void>;
readonly recursive?: boolean | undefined;
}
async function testEventsInternal({
path,
actions,
recursive = false
}: TestEventsInternalOptions): Promise<Array<RenameEvent | ChangeEvent>> {
const doneEvents = new Subject<"Done">();
return firstValueFrom(
observeFileEvents({path, recursive}).pipe(
mergeWith(doneEvents),
scan(
(
{
state
}: {readonly event: FileEvent | "Done"; readonly state: "Active" | "Done"},
event
) => ({
event,
state: event === "Done" ? "Done" : state
}),
{event: {event: "Ready", path, recursive}, state: "Active"}
),
switchMap(({event, state}) =>
(event === "Done" ? EMPTY : of(event)).pipe(
mergeWith(
state === "Done"
? of({event: "Done"} as const).pipe(
delay(
os.platform() === "win32" || os.platform() === "linux"
? 20
: 200
)
)
: EMPTY
)
)
),
takeWhile(({event}) => event !== "Done"),
mergeMap(event => {
if (event.event === "Ready") {
return of(actions(path).then(() => void doneEvents.next("Done"))).pipe(
mergeMap(() => EMPTY)
);
} else if (event.event === "Done") {
return EMPTY;
} else {
return of(event);
}
}),
toArray()
)
);
}
function omitChangeAfterRename(
events: ReadonlyArray<RenameEvent | ChangeEvent>
): Array<RenameEvent | ChangeEvent> {
// On Linux and macOS, when observing a directory, when a file is created
// and then written, the "Rename" event is sometimes, but not always,
// followed by a "Change" event.
return fold(
events,
(accumulator: Array<RenameEvent | ChangeEvent>, event) =>
event.event === "Change" &&
last(accumulator)?.event === "Rename" &&
last(accumulator)?.path === event.path
? accumulator
: [...accumulator, event],
[]
);
}
function omitDuplicateChange(
events: ReadonlyArray<RenameEvent | ChangeEvent>
): Array<RenameEvent | ChangeEvent> {
// When observing a file, when the file is opened and then written, this
// generates at least one "Change" event, but sometimes more than one.
return fold(
events,
(accumulator: Array<RenameEvent | ChangeEvent>, event) =>
event.event === "Change" &&
last(accumulator)?.event === "Change" &&
last(accumulator)?.path === event.path
? accumulator
: [...accumulator, event],
[]
);
}
function omitDuplicateRename(
events: ReadonlyArray<RenameEvent | ChangeEvent>
): Array<RenameEvent | ChangeEvent> {
// On macOS, when observing a directory, when a new file is opened and
// then written, this generates at least one "Rename" event, but sometimes
// more than one.
return fold(
events,
(accumulator: Array<RenameEvent | ChangeEvent>, event) =>
event.event === "Rename" &&
last(accumulator)?.event === "Rename" &&
last(accumulator)?.path === event.path
? accumulator
: [...accumulator, event],
[]
);
}
function omitRenameAfterChange(
events: ReadonlyArray<RenameEvent | ChangeEvent>
): Array<RenameEvent | ChangeEvent> {
// On macOS, when an existing file is opened and written, this sometimes
// generates a "Rename" event after the "Change" events.
return fold(
events,
(accumulator: Array<RenameEvent | ChangeEvent>, event) =>
event.event === "Rename" &&
last(accumulator)?.event === "Change" &&
last(accumulator)?.path === event.path
? accumulator
: [...accumulator, event],
[]
);
}