Repository navigation
Expand file tree
/
Copy pathnode-parity.test.ts
More file actions
1544 lines (1405 loc) · 63.9 KB
/
Copy pathnode-parity.test.ts
File metadata and controls
1544 lines (1405 loc) · 63.9 KB
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
/**
* Differential parity against real `node:fs`.
*
* Every other test in this suite asserts behaviour someone *believed* Node has. These run the
* same operation twice — once through the full library stack (method layer → wire encoding →
* `VFSEngine`) and once through `node:fs` on a real temp directory — and compare the outcomes.
* A divergence is a compatibility bug by construction, with no room for a wrong expectation.
*
* Only semantics both filesystems can agree on are compared: contents, entry lists, sizes,
* permission bits, and the `code` of any thrown error. Timestamps, inode numbers and device
* ids are inherently different and are excluded.
*/
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { mkdtempSync, rmSync } from 'node:fs';
import * as nodefs from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { createHarness, createFsHarness as createFsHarnessForCp, type Harness } from './helpers/engine-transport.js';
import { OP } from '../src/protocol/opcodes.js';
import { DISPATCHED_OPS } from '../src/protocol/dispatch.js';
import { readFileSync } from '../src/methods/readFile.js';
import { writeFileSync } from '../src/methods/writeFile.js';
import { appendFileSync } from '../src/methods/appendFile.js';
import { mkdirSync } from '../src/methods/mkdir.js';
import { readdirSync } from '../src/methods/readdir.js';
import { statSync, lstatSync } from '../src/methods/stat.js';
import { unlinkSync } from '../src/methods/unlink.js';
import { rmdirSync } from '../src/methods/rmdir.js';
import { renameSync } from '../src/methods/rename.js';
import { copyFileSync } from '../src/methods/copyFile.js';
import { truncateSync } from '../src/methods/truncate.js';
import { symlinkSync, readlinkSync } from '../src/methods/symlink.js';
import { existsSync } from '../src/methods/exists.js';
import { chmodSync } from '../src/methods/chmod.js';
import { rmSync as vfsRmSync } from '../src/methods/rm.js';
import { linkSync } from '../src/methods/link.js';
import { accessSync } from '../src/methods/access.js';
import { realpathSync } from '../src/methods/realpath.js';
import { utimesSync } from '../src/methods/utimes.js';
import { mkdtempSync as vfsMkdtempSync } from '../src/methods/mkdtemp.js';
import { globSync as vfsGlobSync } from '../src/methods/glob.js';
import { statfsSync } from '../src/methods/statfs.js';
import { openSync, closeSync, writeSyncFd, readSync, ftruncateSync } from '../src/methods/open.js';
let harness: Harness;
let root: string;
beforeEach(() => {
harness = createHarness();
root = mkdtempSync(join(tmpdir(), 'parity-'));
});
afterEach(() => {
rmSync(root, { recursive: true, force: true });
});
/** Map a library path onto the real temp directory. */
const real = (p: string) => join(root, p);
/** Run `ours` and `theirs`, returning either the value or the thrown error's `code`. */
function compare<T>(ours: () => T, theirs: () => T): { ours: T | string; theirs: T | string } {
const run = <R>(fn: () => R): R | string => {
try {
return fn();
} catch (e) {
return `ERR:${(e as NodeJS.ErrnoException).code ?? (e as Error).name}`;
}
};
return { ours: run(ours), theirs: run(theirs) };
}
/** Assert both implementations produce the same outcome. */
function same<T>(ours: () => T, theirs: () => T, label?: string) {
const r = compare(ours, theirs);
expect(r.ours, label).toEqual(r.theirs);
return r.ours;
}
const text = (v: unknown) => (typeof v === 'string' ? v : new TextDecoder().decode(v as Uint8Array));
describe('parity: files', () => {
it('write then read round-trips identically', () => {
same(
() => { writeFileSync(harness.request, '/a.txt', 'hello'); return text(readFileSync(harness.request, '/a.txt', 'utf8')); },
() => { nodefs.writeFileSync(real('a.txt'), 'hello'); return nodefs.readFileSync(real('a.txt'), 'utf8'); }
);
});
it('reading a missing file gives the same error code', () => {
same(
() => readFileSync(harness.request, '/nope.txt', 'utf8'),
() => nodefs.readFileSync(real('nope.txt'), 'utf8')
);
});
it('reading a directory gives the same error code', () => {
mkdirSync(harness.request, '/d');
nodefs.mkdirSync(real('d'));
same(
() => readFileSync(harness.request, '/d', 'utf8'),
() => nodefs.readFileSync(real('d'), 'utf8')
);
});
it('writing over a directory gives the same error code', () => {
mkdirSync(harness.request, '/d');
nodefs.mkdirSync(real('d'));
same(
() => writeFileSync(harness.request, '/d', 'x'),
() => nodefs.writeFileSync(real('d'), 'x')
);
});
it('writing into a missing parent gives the same error code', () => {
same(
() => writeFileSync(harness.request, '/missing/a.txt', 'x'),
() => nodefs.writeFileSync(real('missing/a.txt'), 'x')
);
});
it('append creates then extends, identically', () => {
same(
() => { appendFileSync(harness.request, '/log', 'aa'); appendFileSync(harness.request, '/log', 'bb'); return text(readFileSync(harness.request, '/log', 'utf8')); },
() => { nodefs.appendFileSync(real('log'), 'aa'); nodefs.appendFileSync(real('log'), 'bb'); return nodefs.readFileSync(real('log'), 'utf8'); }
);
});
it('empty files behave the same', () => {
same(
() => { writeFileSync(harness.request, '/e', ''); return [text(readFileSync(harness.request, '/e', 'utf8')), (statSync(harness.request, '/e') as { size: number }).size]; },
() => { nodefs.writeFileSync(real('e'), ''); return [nodefs.readFileSync(real('e'), 'utf8'), nodefs.statSync(real('e')).size]; }
);
});
it('overwriting truncates rather than merging', () => {
same(
() => { writeFileSync(harness.request, '/o', 'longer-content'); writeFileSync(harness.request, '/o', 'ab'); return text(readFileSync(harness.request, '/o', 'utf8')); },
() => { nodefs.writeFileSync(real('o'), 'longer-content'); nodefs.writeFileSync(real('o'), 'ab'); return nodefs.readFileSync(real('o'), 'utf8'); }
);
});
it('unicode content round-trips byte-identically', () => {
const content = 'héllo 😀 世界';
same(
() => { writeFileSync(harness.request, '/u', content); return text(readFileSync(harness.request, '/u', 'utf8')); },
() => { nodefs.writeFileSync(real('u'), content); return nodefs.readFileSync(real('u'), 'utf8'); }
);
});
it('truncate grows with zeros and shrinks, the same way', () => {
same(
() => {
writeFileSync(harness.request, '/t', 'abcdef');
truncateSync(harness.request, '/t', 3);
const short = text(readFileSync(harness.request, '/t', 'utf8'));
truncateSync(harness.request, '/t', 6);
const grown = Array.from(readFileSync(harness.request, '/t') as Uint8Array);
return [short, grown];
},
() => {
nodefs.writeFileSync(real('t'), 'abcdef');
nodefs.truncateSync(real('t'), 3);
const short = nodefs.readFileSync(real('t'), 'utf8');
nodefs.truncateSync(real('t'), 6);
const grown = Array.from(nodefs.readFileSync(real('t')));
return [short, grown];
}
);
});
});
describe('parity: directories', () => {
it('mkdir on an existing path gives the same error code', () => {
same(
() => { mkdirSync(harness.request, '/d'); mkdirSync(harness.request, '/d'); },
() => { nodefs.mkdirSync(real('d')); nodefs.mkdirSync(real('d')); }
);
});
it('recursive mkdir over an existing tree is a no-op for both', () => {
same(
() => { mkdirSync(harness.request, '/a/b/c', { recursive: true }); return mkdirSync(harness.request, '/a/b/c', { recursive: true }); },
() => { nodefs.mkdirSync(real('a/b/c'), { recursive: true }); return nodefs.mkdirSync(real('a/b/c'), { recursive: true }); }
);
});
it('recursive mkdir returns the first created directory', () => {
same(
() => mkdirSync(harness.request, '/x/y/z', { recursive: true })?.replace(/^\//, ''),
() => nodefs.mkdirSync(real('x/y/z'), { recursive: true })?.slice(root.length + 1)
);
});
it('mkdir without a parent gives the same error code', () => {
same(
() => mkdirSync(harness.request, '/no/parent'),
() => nodefs.mkdirSync(real('no/parent'))
);
});
it('readdir lists the same sorted entries', () => {
same(
() => {
mkdirSync(harness.request, '/d');
writeFileSync(harness.request, '/d/b.txt', '');
writeFileSync(harness.request, '/d/a.txt', '');
mkdirSync(harness.request, '/d/sub');
return (readdirSync(harness.request, '/d') as string[]).slice().sort();
},
() => {
nodefs.mkdirSync(real('d'));
nodefs.writeFileSync(real('d/b.txt'), '');
nodefs.writeFileSync(real('d/a.txt'), '');
nodefs.mkdirSync(real('d/sub'));
return nodefs.readdirSync(real('d')).slice().sort();
}
);
});
it('readdir on a file gives the same error code', () => {
writeFileSync(harness.request, '/f', 'x');
nodefs.writeFileSync(real('f'), 'x');
same(
() => readdirSync(harness.request, '/f'),
() => nodefs.readdirSync(real('f'))
);
});
it('readdir on a missing directory gives the same error code', () => {
same(
() => readdirSync(harness.request, '/gone'),
() => nodefs.readdirSync(real('gone'))
);
});
it('rmdir on a non-empty directory gives the same error code', () => {
same(
() => { mkdirSync(harness.request, '/d'); writeFileSync(harness.request, '/d/f', 'x'); rmdirSync(harness.request, '/d'); },
() => { nodefs.mkdirSync(real('d')); nodefs.writeFileSync(real('d/f'), 'x'); nodefs.rmdirSync(real('d')); }
);
});
it('rmdir on a file gives the same error code', () => {
writeFileSync(harness.request, '/f', 'x');
nodefs.writeFileSync(real('f'), 'x');
same(
() => rmdirSync(harness.request, '/f'),
() => nodefs.rmdirSync(real('f'))
);
});
it('unlink on a directory fails for both', () => {
// errno here is platform-dependent — EISDIR on Linux, EPERM on macOS/BSD — and Node reports
// whatever the host kernel returns. We always report the Linux spelling, so compare only
// that both refuse, not which errno they pick.
mkdirSync(harness.request, '/d');
nodefs.mkdirSync(real('d'));
const r = compare(() => unlinkSync(harness.request, '/d'), () => nodefs.unlinkSync(real('d')));
expect(String(r.ours)).toMatch(/^ERR:/);
expect(String(r.theirs)).toMatch(/^ERR:/);
});
it('recursive readdir lists the same tree', () => {
same(
() => {
mkdirSync(harness.request, '/r/a/b', { recursive: true });
writeFileSync(harness.request, '/r/top.txt', '');
writeFileSync(harness.request, '/r/a/mid.txt', '');
writeFileSync(harness.request, '/r/a/b/deep.txt', '');
return (readdirSync(harness.request, '/r', { recursive: true }) as string[]).slice().sort();
},
() => {
nodefs.mkdirSync(real('r/a/b'), { recursive: true });
nodefs.writeFileSync(real('r/top.txt'), '');
nodefs.writeFileSync(real('r/a/mid.txt'), '');
nodefs.writeFileSync(real('r/a/b/deep.txt'), '');
return nodefs.readdirSync(real('r'), { recursive: true }).slice().sort();
}
);
});
});
describe('parity: stat', () => {
it('reports the same type, size and permission bits for a file', () => {
same(
() => {
writeFileSync(harness.request, '/f', 'abcdef');
const s = statSync(harness.request, '/f') as nodefs.Stats;
return [s.isFile(), s.isDirectory(), s.isSymbolicLink(), s.size, s.mode & 0o777];
},
() => {
nodefs.writeFileSync(real('f'), 'abcdef');
const s = nodefs.statSync(real('f'));
return [s.isFile(), s.isDirectory(), s.isSymbolicLink(), s.size, s.mode & 0o777];
}
);
});
it('reports the same type and permission bits for a directory', () => {
same(
() => {
mkdirSync(harness.request, '/d');
const s = statSync(harness.request, '/d') as nodefs.Stats;
return [s.isFile(), s.isDirectory(), s.mode & 0o777];
},
() => {
nodefs.mkdirSync(real('d'));
const s = nodefs.statSync(real('d'));
return [s.isFile(), s.isDirectory(), s.mode & 0o777];
}
);
});
it('stat of a missing path gives the same error code', () => {
same(
() => statSync(harness.request, '/nope'),
() => nodefs.statSync(real('nope'))
);
});
it('chmod is reflected in stat identically', () => {
same(
() => { writeFileSync(harness.request, '/f', 'x'); chmodSync(harness.request, '/f', 0o640); return (statSync(harness.request, '/f') as nodefs.Stats).mode & 0o777; },
() => { nodefs.writeFileSync(real('f'), 'x'); nodefs.chmodSync(real('f'), 0o640); return nodefs.statSync(real('f')).mode & 0o777; }
);
});
it('exists agrees for files, directories and missing paths', () => {
writeFileSync(harness.request, '/f', 'x');
mkdirSync(harness.request, '/d');
nodefs.writeFileSync(real('f'), 'x');
nodefs.mkdirSync(real('d'));
for (const p of ['f', 'd', 'nope']) {
expect(existsSync(harness.request, '/' + p), p).toBe(nodefs.existsSync(real(p)));
}
});
});
describe('parity: rename, copy, link', () => {
it('rename moves content and removes the source', () => {
same(
() => {
writeFileSync(harness.request, '/a', 'data');
renameSync(harness.request, '/a', '/b');
return [existsSync(harness.request, '/a'), text(readFileSync(harness.request, '/b', 'utf8'))];
},
() => {
nodefs.writeFileSync(real('a'), 'data');
nodefs.renameSync(real('a'), real('b'));
return [nodefs.existsSync(real('a')), nodefs.readFileSync(real('b'), 'utf8')];
}
);
});
it('rename over an existing file overwrites it', () => {
same(
() => {
writeFileSync(harness.request, '/a', 'new');
writeFileSync(harness.request, '/b', 'old');
renameSync(harness.request, '/a', '/b');
return text(readFileSync(harness.request, '/b', 'utf8'));
},
() => {
nodefs.writeFileSync(real('a'), 'new');
nodefs.writeFileSync(real('b'), 'old');
nodefs.renameSync(real('a'), real('b'));
return nodefs.readFileSync(real('b'), 'utf8');
}
);
});
it('renaming a missing source gives the same error code', () => {
same(
() => renameSync(harness.request, '/gone', '/b'),
() => nodefs.renameSync(real('gone'), real('b'))
);
});
it('copyFile duplicates content and leaves the source', () => {
same(
() => {
writeFileSync(harness.request, '/a', 'data');
copyFileSync(harness.request, '/a', '/b');
return [text(readFileSync(harness.request, '/a', 'utf8')), text(readFileSync(harness.request, '/b', 'utf8'))];
},
() => {
nodefs.writeFileSync(real('a'), 'data');
nodefs.copyFileSync(real('a'), real('b'));
return [nodefs.readFileSync(real('a'), 'utf8'), nodefs.readFileSync(real('b'), 'utf8')];
}
);
});
it('copyFile with COPYFILE_EXCL onto an existing target gives the same error code', () => {
same(
() => {
writeFileSync(harness.request, '/a', 'x');
writeFileSync(harness.request, '/b', 'y');
copyFileSync(harness.request, '/a', '/b', nodefs.constants.COPYFILE_EXCL);
},
() => {
nodefs.writeFileSync(real('a'), 'x');
nodefs.writeFileSync(real('b'), 'y');
nodefs.copyFileSync(real('a'), real('b'), nodefs.constants.COPYFILE_EXCL);
}
);
});
it('symlink target round-trips through readlink verbatim', () => {
// readlink returns the target exactly as given — it is not resolved. Both sides use the
// same relative target so the comparison is apples to apples.
same(
() => { writeFileSync(harness.request, '/t', 'x'); symlinkSync(harness.request, 't', '/l'); return readlinkSync(harness.request, '/l'); },
() => { nodefs.writeFileSync(real('t'), 'x'); nodefs.symlinkSync('t', real('l')); return nodefs.readlinkSync(real('l')); }
);
});
it('lstat sees the link where stat follows it', () => {
same(
() => {
writeFileSync(harness.request, '/t', 'abc');
symlinkSync(harness.request, '/t', '/l');
const l = lstatSync(harness.request, '/l') as nodefs.Stats;
const s = statSync(harness.request, '/l') as nodefs.Stats;
return [l.isSymbolicLink(), l.isFile(), s.isSymbolicLink(), s.isFile(), s.size];
},
() => {
nodefs.writeFileSync(real('t'), 'abc');
nodefs.symlinkSync(real('t'), real('l'));
const l = nodefs.lstatSync(real('l'));
const s = nodefs.statSync(real('l'));
return [l.isSymbolicLink(), l.isFile(), s.isSymbolicLink(), s.isFile(), s.size];
}
);
});
it('readlink on a regular file gives the same error code', () => {
writeFileSync(harness.request, '/f', 'x');
nodefs.writeFileSync(real('f'), 'x');
same(
() => readlinkSync(harness.request, '/f'),
() => nodefs.readlinkSync(real('f'))
);
});
it('reading through a dangling symlink gives the same error code', () => {
same(
() => { symlinkSync(harness.request, '/missing', '/l'); return readFileSync(harness.request, '/l', 'utf8'); },
() => { nodefs.symlinkSync(real('missing'), real('l')); return nodefs.readFileSync(real('l'), 'utf8'); }
);
});
});
describe('parity: file descriptors', () => {
it('open/write/read/close round-trips the same bytes', () => {
same(
() => {
const fd = openSync(harness.request, '/fd.txt', 'w');
writeSyncFd(harness.request, fd, new TextEncoder().encode('hello fd'), 0, 8, 0);
closeSync(harness.request, fd);
return text(readFileSync(harness.request, '/fd.txt', 'utf8'));
},
() => {
const fd = nodefs.openSync(real('fd.txt'), 'w');
nodefs.writeSync(fd, Buffer.from('hello fd'), 0, 8, 0);
nodefs.closeSync(fd);
return nodefs.readFileSync(real('fd.txt'), 'utf8');
}
);
});
it('reads at an explicit position identically', () => {
same(
() => {
writeFileSync(harness.request, '/p.txt', 'abcdefgh');
const fd = openSync(harness.request, '/p.txt', 'r');
const buf = new Uint8Array(4);
const n = readSync(harness.request, fd, buf, 0, 4, 2);
closeSync(harness.request, fd);
return [n, text(buf)];
},
() => {
nodefs.writeFileSync(real('p.txt'), 'abcdefgh');
const fd = nodefs.openSync(real('p.txt'), 'r');
const buf = Buffer.alloc(4);
const n = nodefs.readSync(fd, buf, 0, 4, 2);
nodefs.closeSync(fd);
return [n, buf.toString()];
}
);
});
it('reading past EOF returns zero bytes for both', () => {
same(
() => {
writeFileSync(harness.request, '/s.txt', 'ab');
const fd = openSync(harness.request, '/s.txt', 'r');
const buf = new Uint8Array(8);
const n = readSync(harness.request, fd, buf, 0, 8, 100);
closeSync(harness.request, fd);
return n;
},
() => {
nodefs.writeFileSync(real('s.txt'), 'ab');
const fd = nodefs.openSync(real('s.txt'), 'r');
const buf = Buffer.alloc(8);
const n = nodefs.readSync(fd, buf, 0, 8, 100);
nodefs.closeSync(fd);
return n;
}
);
});
it("opening a missing file with 'r' gives the same error code", () => {
same(
() => openSync(harness.request, '/gone', 'r'),
() => nodefs.openSync(real('gone'), 'r')
);
});
it("'wx' onto an existing file gives the same error code", () => {
writeFileSync(harness.request, '/x', 'a');
nodefs.writeFileSync(real('x'), 'a');
same(
() => openSync(harness.request, '/x', 'wx'),
() => nodefs.openSync(real('x'), 'wx')
);
});
it('ftruncate shortens the file identically', () => {
same(
() => {
writeFileSync(harness.request, '/ft', 'abcdef');
const fd = openSync(harness.request, '/ft', 'r+');
ftruncateSync(harness.request, fd, 2);
closeSync(harness.request, fd);
return text(readFileSync(harness.request, '/ft', 'utf8'));
},
() => {
nodefs.writeFileSync(real('ft'), 'abcdef');
const fd = nodefs.openSync(real('ft'), 'r+');
nodefs.ftruncateSync(fd, 2);
nodefs.closeSync(fd);
return nodefs.readFileSync(real('ft'), 'utf8');
}
);
});
it('operating on a closed fd gives the same error code', () => {
same(
() => {
const fd = openSync(harness.request, '/c', 'w');
closeSync(harness.request, fd);
return readSync(harness.request, fd, new Uint8Array(4), 0, 4, 0);
},
() => {
const fd = nodefs.openSync(real('c'), 'w');
nodefs.closeSync(fd);
return nodefs.readSync(fd, Buffer.alloc(4), 0, 4, 0);
}
);
});
});
describe('parity: payload layouts (regression guards)', () => {
// Each of these encodes through the real method layer and decodes through the real shared
// dispatch. A layout disagreement between the two shows up here as wrong data, which is how
// the uint32/float64 truncate bug went unnoticed: the old tests encoded and decoded
// themselves, so they agreed with each other and not with the shipped worker.
it('truncate honours the requested length instead of emptying the file', () => {
writeFileSync(harness.request, '/t', 'abcdef');
truncateSync(harness.request, '/t', 4);
// Regression: the length was decoded as uint32 from a float64 field, whose low four bytes
// are zero for every small integer — so this used to produce ''.
expect(text(readFileSync(harness.request, '/t', 'utf8'))).toBe('abcd');
});
it('ftruncate honours the requested length', () => {
writeFileSync(harness.request, '/ft', 'abcdef');
const fd = openSync(harness.request, '/ft', 'r+');
ftruncateSync(harness.request, fd, 4);
closeSync(harness.request, fd);
expect(text(readFileSync(harness.request, '/ft', 'utf8'))).toBe('abcd');
});
// The 4GB case proves a length above 2^32 survives the payload encoding rather than being
// silently cut to 32 bits. It used to cost about a second and 4GB of resident memory — growing
// a file wrote its extension out as zeros — and tripped the 5s default once, under load, during
// a release. It is ~35ms now that the engine skips the part of an extension the volume's own
// growth already zeroed, so it needs no special timeout; see vfs-engine's "truncate that grows
// a file", which guards that directly.
it.each([0, 1, 3, 1024, 4096, 0xffffffff + 1])('truncates to length %d exactly', (len) => {
writeFileSync(harness.request, '/big', 'x'.repeat(16));
truncateSync(harness.request, '/big', len);
expect((statSync(harness.request, '/big') as nodefs.Stats).size).toBe(len);
});
it('every opcode the method layer can emit has a decoder', () => {
// Guards the failure mode this whole file exists for: an op added to the protocol without a
// matching case falls through to EINVAL, which is easy to miss until something breaks.
const known = new Set(Object.values(OP));
for (const op of known) {
expect(DISPATCHED_OPS.has(op as number), `OP ${op} has no dispatch case`).toBe(true);
}
});
});
describe('parity: append growth (block-boundary regression)', () => {
// `append` now writes in place when the file's existing 4 KB block run has room, instead of
// relocating and copying the whole file every time. These pin the boundary behaviour that
// fast path could plausibly get wrong.
it('many small appends build the same file as Node', () => {
same(
() => {
for (let i = 0; i < 500; i++) appendFileSync(harness.request, '/log', `line ${i}\n`);
return text(readFileSync(harness.request, '/log', 'utf8'));
},
() => {
for (let i = 0; i < 500; i++) nodefs.appendFileSync(real('log'), `line ${i}\n`);
return nodefs.readFileSync(real('log'), 'utf8');
}
);
});
it('appends spanning a block boundary keep every byte', () => {
// 4 KB blocks: start just under one, then cross it, then cross several at once.
same(
() => {
writeFileSync(harness.request, '/b', 'a'.repeat(4090));
appendFileSync(harness.request, '/b', 'b'.repeat(12)); // crosses 4096
appendFileSync(harness.request, '/b', 'c'.repeat(9000)); // crosses several
appendFileSync(harness.request, '/b', 'd'); // back to in-place
const out = text(readFileSync(harness.request, '/b', 'utf8'));
return [out.length, out.slice(4085, 4105), out.slice(-1)];
},
() => {
nodefs.writeFileSync(real('b'), 'a'.repeat(4090));
nodefs.appendFileSync(real('b'), 'b'.repeat(12));
nodefs.appendFileSync(real('b'), 'c'.repeat(9000));
nodefs.appendFileSync(real('b'), 'd');
const out = nodefs.readFileSync(real('b'), 'utf8');
return [out.length, out.slice(4085, 4105), out.slice(-1)];
}
);
});
it('appending after a shrink does not resurrect the discarded bytes', () => {
// The in-place path writes at `size`, inside a run that still holds the old tail. If it
// used the run's capacity instead of the logical size, that stale data would reappear.
same(
() => {
writeFileSync(harness.request, '/s', 'x'.repeat(3000));
truncateSync(harness.request, '/s', 10);
appendFileSync(harness.request, '/s', 'END');
return text(readFileSync(harness.request, '/s', 'utf8'));
},
() => {
nodefs.writeFileSync(real('s'), 'x'.repeat(3000));
nodefs.truncateSync(real('s'), 10);
nodefs.appendFileSync(real('s'), 'END');
return nodefs.readFileSync(real('s'), 'utf8');
}
);
});
it('appending to an empty and to a missing file both work', () => {
same(
() => {
writeFileSync(harness.request, '/empty', '');
appendFileSync(harness.request, '/empty', 'first');
appendFileSync(harness.request, '/fresh', 'new');
return [text(readFileSync(harness.request, '/empty', 'utf8')), text(readFileSync(harness.request, '/fresh', 'utf8'))];
},
() => {
nodefs.writeFileSync(real('empty'), '');
nodefs.appendFileSync(real('empty'), 'first');
nodefs.appendFileSync(real('fresh'), 'new');
return [nodefs.readFileSync(real('empty'), 'utf8'), nodefs.readFileSync(real('fresh'), 'utf8')];
}
);
});
it('size and content stay consistent across a remount', () => {
// In-place appends mutate the inode without relocating; make sure the inode that lands on
// disk still describes the data.
for (let i = 0; i < 50; i++) appendFileSync(harness.request, '/p', 'chunk');
const before = text(readFileSync(harness.request, '/p', 'utf8'));
expect((statSync(harness.request, '/p') as nodefs.Stats).size).toBe(before.length);
expect(before).toBe('chunk'.repeat(50));
});
});
describe('parity: rm, link, access, realpath, utimes, glob', () => {
it('rm recursive removes a whole tree', () => {
same(
() => {
mkdirSync(harness.request, '/t/a/b', { recursive: true });
writeFileSync(harness.request, '/t/f', 'x');
writeFileSync(harness.request, '/t/a/g', 'y');
vfsRmSync(harness.request, '/t', { recursive: true });
return existsSync(harness.request, '/t');
},
() => {
nodefs.mkdirSync(real('t/a/b'), { recursive: true });
nodefs.writeFileSync(real('t/f'), 'x');
nodefs.writeFileSync(real('t/a/g'), 'y');
nodefs.rmSync(real('t'), { recursive: true });
return nodefs.existsSync(real('t'));
}
);
});
it('rm on a directory without recursive gives the same error code', () => {
mkdirSync(harness.request, '/d');
nodefs.mkdirSync(real('d'));
same(
() => vfsRmSync(harness.request, '/d'),
() => nodefs.rmSync(real('d'))
);
});
it('rm on a missing path errors, and force silences it for both', () => {
same(() => vfsRmSync(harness.request, '/gone'), () => nodefs.rmSync(real('gone')));
same(() => vfsRmSync(harness.request, '/gone', { force: true }), () => nodefs.rmSync(real('gone'), { force: true }));
});
it('hard links share content and nlink', () => {
same(
() => {
writeFileSync(harness.request, '/a', 'shared');
linkSync(harness.request, '/a', '/b');
const s = statSync(harness.request, '/b') as nodefs.Stats;
return [text(readFileSync(harness.request, '/b', 'utf8')), s.nlink];
},
() => {
nodefs.writeFileSync(real('a'), 'shared');
nodefs.linkSync(real('a'), real('b'));
const s = nodefs.statSync(real('b'));
return [nodefs.readFileSync(real('b'), 'utf8'), s.nlink];
}
);
});
it('unlinking one hard link leaves the other readable', () => {
same(
() => {
writeFileSync(harness.request, '/a', 'keep');
linkSync(harness.request, '/a', '/b');
unlinkSync(harness.request, '/a');
return [existsSync(harness.request, '/a'), text(readFileSync(harness.request, '/b', 'utf8'))];
},
() => {
nodefs.writeFileSync(real('a'), 'keep');
nodefs.linkSync(real('a'), real('b'));
nodefs.unlinkSync(real('a'));
return [nodefs.existsSync(real('a')), nodefs.readFileSync(real('b'), 'utf8')];
}
);
});
it('a write through one hard link is visible through the other', () => {
// The defining property of a hard link, and the one `link()` did not have while it
// copied the file: the two names were separate inodes, so this returned the original
// bytes here and the new bytes on node.
same(
() => {
writeFileSync(harness.request, '/a', 'first');
linkSync(harness.request, '/a', '/b');
writeFileSync(harness.request, '/b', 'second, rather longer');
const s = statSync(harness.request, '/a') as nodefs.Stats;
return [text(readFileSync(harness.request, '/a', 'utf8')), s.size, s.nlink];
},
() => {
nodefs.writeFileSync(real('a'), 'first');
nodefs.linkSync(real('a'), real('b'));
nodefs.writeFileSync(real('b'), 'second, rather longer');
const s = nodefs.statSync(real('a'));
return [nodefs.readFileSync(real('a'), 'utf8'), s.size, s.nlink];
}
);
});
it('nlink falls back as names are removed', () => {
same(
() => {
writeFileSync(harness.request, '/a', 'x');
linkSync(harness.request, '/a', '/b');
linkSync(harness.request, '/a', '/c');
const three = (statSync(harness.request, '/c') as nodefs.Stats).nlink;
unlinkSync(harness.request, '/a');
return [three, (statSync(harness.request, '/b') as nodefs.Stats).nlink];
},
() => {
nodefs.writeFileSync(real('a'), 'x');
nodefs.linkSync(real('a'), real('b'));
nodefs.linkSync(real('a'), real('c'));
const three = nodefs.statSync(real('c')).nlink;
nodefs.unlinkSync(real('a'));
return [three, nodefs.statSync(real('b')).nlink];
}
);
});
it('two names for one file report one inode number', () => {
// Inode numbers themselves are not comparable across the two filesystems, but whether
// two names share one is — it is what `[ a -ef b ]` and `find -samefile` test.
same(
() => {
writeFileSync(harness.request, '/a', 'x');
writeFileSync(harness.request, '/other', 'x');
linkSync(harness.request, '/a', '/b');
const [a, b, o] = ['/a', '/b', '/other'].map(p => (statSync(harness.request, p) as nodefs.Stats).ino);
return [a === b, a === o];
},
() => {
nodefs.writeFileSync(real('a'), 'x');
nodefs.writeFileSync(real('other'), 'x');
nodefs.linkSync(real('a'), real('b'));
const [a, b, o] = ['a', 'b', 'other'].map(p => nodefs.statSync(real(p)).ino);
return [a === b, a === o];
}
);
});
it('a directory cannot be renamed into or over itself', () => {
// Both of these used to report success and destroy the tree.
same(
() => {
mkdirSync(harness.request, '/d/sub', { recursive: true });
writeFileSync(harness.request, '/d/f', 'x');
const into = compare(() => renameSync(harness.request, '/d', '/d/sub'), () => {});
const over = compare(() => renameSync(harness.request, '/d/sub', '/d'), () => {});
return [
into.ours, over.ours,
text(readFileSync(harness.request, '/d/f', 'utf8')),
readdirSync(harness.request, '/d'),
];
},
() => {
nodefs.mkdirSync(join(real('d'), 'sub'), { recursive: true });
nodefs.writeFileSync(join(real('d'), 'f'), 'x');
const into = compare(() => nodefs.renameSync(real('d'), join(real('d'), 'sub')), () => {});
const over = compare(() => nodefs.renameSync(join(real('d'), 'sub'), real('d')), () => {});
return [
into.ours, over.ours,
nodefs.readFileSync(join(real('d'), 'f'), 'utf8'),
nodefs.readdirSync(real('d')),
];
}
);
});
it('link onto an existing path gives the same error code', () => {
writeFileSync(harness.request, '/a', 'x');
writeFileSync(harness.request, '/b', 'y');
nodefs.writeFileSync(real('a'), 'x');
nodefs.writeFileSync(real('b'), 'y');
same(() => linkSync(harness.request, '/a', '/b'), () => nodefs.linkSync(real('a'), real('b')));
});
it('access agrees on existence checks', () => {
writeFileSync(harness.request, '/f', 'x');
nodefs.writeFileSync(real('f'), 'x');
same(() => accessSync(harness.request, '/f'), () => nodefs.accessSync(real('f')));
same(() => accessSync(harness.request, '/gone'), () => nodefs.accessSync(real('gone')));
});
it('realpath resolves a symlink chain to the same relative target', () => {
same(
() => {
mkdirSync(harness.request, '/real');
writeFileSync(harness.request, '/real/f', 'x');
symlinkSync(harness.request, '/real', '/link');
return realpathSync(harness.request, '/link/f');
},
() => {
nodefs.mkdirSync(real('real'));
nodefs.writeFileSync(real('real/f'), 'x');
nodefs.symlinkSync(real('real'), real('link'));
return nodefs.realpathSync(real('link/f')).slice(nodefs.realpathSync(root).length);
}
);
});
it('realpath of a missing path gives the same error code', () => {
same(() => realpathSync(harness.request, '/nope'), () => nodefs.realpathSync(real('nope')));
});
it('utimes takes seconds, like Node', () => {
// This test used to divide by 1000 for the Node side, which hid a real 1000× divergence:
// we read a bare number as milliseconds where Node reads it as seconds, so
// `utimesSync(p, 1600000000)` stamped 1970 instead of 2020. Both sides now pass the same
// value, which is the whole point.
const atime = 1_600_000_000;
const mtime = 1_600_000_123;
same(
() => {
writeFileSync(harness.request, '/u', 'x');
utimesSync(harness.request, '/u', atime, mtime);
const s = statSync(harness.request, '/u') as nodefs.Stats;
return [s.atimeMs, s.mtimeMs];
},
() => {
nodefs.writeFileSync(real('u'), 'x');
nodefs.utimesSync(real('u'), atime, mtime);
const s = nodefs.statSync(real('u'));
return [s.atimeMs, s.mtimeMs];
}
);
});
it('utimes accepts Dates and numeric strings, and rejects what Node rejects', () => {
writeFileSync(harness.request, '/u', 'x');
nodefs.writeFileSync(real('u'), 'x');
same(
() => { utimesSync(harness.request, '/u', new Date(1e12), new Date(1e12)); return (statSync(harness.request, '/u') as nodefs.Stats).mtimeMs; },
() => { nodefs.utimesSync(real('u'), new Date(1e12), new Date(1e12)); return nodefs.statSync(real('u')).mtimeMs; }
);
same(
() => { utimesSync(harness.request, '/u', '1600000000' as never, '1600000000' as never); return (statSync(harness.request, '/u') as nodefs.Stats).mtimeMs; },
() => { nodefs.utimesSync(real('u'), '1600000000' as never, '1600000000' as never); return nodefs.statSync(real('u')).mtimeMs; }
);
for (const bad of [NaN, Infinity, 'abc']) {
expect(() => utimesSync(harness.request, '/u', bad as never, bad as never), String(bad))
.toThrow(expect.objectContaining({ code: 'ERR_INVALID_ARG_TYPE' }));
}
});
it('mkdtemp creates a unique directory from the prefix', () => {
const oursPath = vfsMkdtempSync(harness.request, '/tmp-');
const theirsPath = nodefs.mkdtempSync(join(root, 'tmp-'));
// Same shape: prefix preserved, six random characters appended, and it is a real directory.
expect(oursPath.startsWith('/tmp-')).toBe(true);
expect(oursPath.length).toBe('/tmp-'.length + 6);
expect(theirsPath.slice(root.length + 1).length).toBe('tmp-'.length + 6);
expect((statSync(harness.request, oursPath) as nodefs.Stats).isDirectory()).toBe(true);
expect(nodefs.statSync(theirsPath).isDirectory()).toBe(true);
// Two calls never collide.
expect(vfsMkdtempSync(harness.request, '/tmp-')).not.toBe(oursPath);
});
it('glob matches the same files', () => {
same(
() => {
mkdirSync(harness.request, '/g/sub', { recursive: true });
writeFileSync(harness.request, '/g/a.ts', '');
writeFileSync(harness.request, '/g/b.js', '');
writeFileSync(harness.request, '/g/sub/c.ts', '');
return (vfsGlobSync(harness.request, '/g/**/*.ts') as string[]).slice().sort();
},
() => {
nodefs.mkdirSync(real('g/sub'), { recursive: true });
nodefs.writeFileSync(real('g/a.ts'), '');
nodefs.writeFileSync(real('g/b.js'), '');
nodefs.writeFileSync(real('g/sub/c.ts'), '');
return nodefs.globSync(real('g/**/*.ts')).map((p) => p.slice(root.length)).sort();
}
);
});
it('readdir withFileTypes reports the same kinds', () => {
same(
() => {
mkdirSync(harness.request, '/wt/sub', { recursive: true });
writeFileSync(harness.request, '/wt/f.txt', '');
return (readdirSync(harness.request, '/wt', { withFileTypes: true }) as nodefs.Dirent[])
.map((d) => [d.name, d.isDirectory(), d.isFile()])
.sort((a, b) => String(a[0]).localeCompare(String(b[0])));
},
() => {
nodefs.mkdirSync(real('wt/sub'), { recursive: true });
nodefs.writeFileSync(real('wt/f.txt'), '');
return nodefs.readdirSync(real('wt'), { withFileTypes: true })
.map((d) => [d.name, d.isDirectory(), d.isFile()])
.sort((a, b) => String(a[0]).localeCompare(String(b[0])));
}
);
});
});
describe('parity: readdir encoding (fast-path regression)', () => {
// The names-only path writes UTF-8 straight into an over-allocated buffer via encodeInto and
// trims it, instead of building one Uint8Array per name. These pin what that could get wrong:
// the capacity bound for multi-byte names, the sort order, and the empty case.
it('multi-byte names survive the sized-by-estimate buffer', () => {
const names = ['plain.txt', 'héllo.txt', '世界.txt', '😀-emoji.txt', 'ünïcödé', 'а-cyrillic'];
same(
() => {
mkdirSync(harness.request, '/u');
for (const n of names) writeFileSync(harness.request, `/u/${n}`, '');
return (readdirSync(harness.request, '/u') as string[]).slice().sort();
},
() => {
nodefs.mkdirSync(real('u'));
for (const n of names) nodefs.writeFileSync(real(`u/${n}`), '');