Files
node/test/parallel/test-vfs-fs-statSync.js
T
Matteo Collina 569369f927 vfs: dispatch fs/promises to mounted VFS instances
Add mount/unmount lifecycle on `VirtualFileSystem`, a handler registry
that fs.js and fs/promises.js consult via `vfsState.handlers`, and a
router that maps absolute paths to the VFS that owns them. When a VFS
is mounted, the public `fs.*` and `fs/promises` APIs (including
streams, `fs.watch`, and `opendir`) dispatch to the provider for paths
under the mount point, and fall through to the real filesystem
otherwise. Includes per-method dispatch tests, error-path coverage,
multi-mount routing tests, and router unit tests.

Ref: https://github.com/nodejs/node/pull/63115

Signed-off-by: Matteo Collina <hello@matteocollina.com>
PR-URL: https://github.com/nodejs/node/pull/63537
Refs: https://github.com/nodejs/node/pull/63115
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
2026-05-29 17:57:34 +00:00

62 lines
1.7 KiB
JavaScript

// Flags: --experimental-vfs
'use strict';
// fs.statSync / fs.lstatSync / fs.statfsSync dispatch through the VFS layer,
// including the `throwIfNoEntry: false` option.
require('../common');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const vfs = require('node:vfs');
const mountPoint = path.resolve('/tmp/vfs-statSync-' + process.pid);
const myVfs = vfs.create();
myVfs.mkdirSync('/src', { recursive: true });
myVfs.writeFileSync('/src/hello.txt', 'hello world');
myVfs.mount(mountPoint);
// statSync on a regular file
{
const s = fs.statSync(path.join(mountPoint, 'src/hello.txt'));
assert.strictEqual(s.isFile(), true);
assert.strictEqual(s.size, 11);
}
// statSync with throwIfNoEntry:false on missing path
assert.strictEqual(
fs.statSync(path.join(mountPoint, 'missing'), { throwIfNoEntry: false }),
undefined,
);
// statSync on missing path throws ENOENT by default
assert.throws(() => fs.statSync(path.join(mountPoint, 'missing')),
{ code: 'ENOENT' });
// lstatSync on a regular file
{
const s = fs.lstatSync(path.join(mountPoint, 'src/hello.txt'));
assert.strictEqual(s.isFile(), true);
}
// lstatSync with throwIfNoEntry:false on missing path
assert.strictEqual(
fs.lstatSync(path.join(mountPoint, 'missing'), { throwIfNoEntry: false }),
undefined,
);
// statfsSync returns number-typed values by default
{
const s = fs.statfsSync(path.join(mountPoint, 'src/hello.txt'));
assert.strictEqual(typeof s.bsize, 'number');
}
// statfsSync with bigint:true returns BigInt fields
{
const s = fs.statfsSync(path.join(mountPoint, 'src/hello.txt'),
{ bigint: true });
assert.strictEqual(typeof s.bsize, 'bigint');
}
myVfs.unmount();