Files
node/test/parallel/test-vfs-fs-readFile-callback.js
Matteo Collina 49ad95045a vfs: unflag
Enable node:vfs by default. --no-experimental-vfs disables node:vfs
exposure and --vfs-load.

Assisted-by: pi
Signed-off-by: Matteo Collina <hello@matteocollina.com>
PR-URL: https://github.com/nodejs/node/pull/66318
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
2026-10-15 07:53:03 +00:00

73 lines
2.0 KiB
JavaScript

// Flags: --disable-warning=ExperimentalWarning
'use strict';
// fs.readFile, fs.readdir, fs.realpath, fs.access, and fs.exists callbacks
// dispatch through VFS.
const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const vfs = require('node:vfs');
function mounted() {
const myVfs = vfs.create();
myVfs.mkdirSync('/src', { recursive: true });
myVfs.writeFileSync('/src/hello.txt', 'hello world');
const mountPoint = myVfs.mount();
return { myVfs, mountPoint };
}
// readFile (cb)
{
const { myVfs, mountPoint } = mounted();
fs.readFile(path.join(mountPoint, 'src/hello.txt'), 'utf8',
common.mustSucceed((data) => {
assert.strictEqual(data, 'hello world');
myVfs.unmount();
}));
}
// readdir (cb)
{
const { myVfs, mountPoint } = mounted();
fs.readdir(path.join(mountPoint, 'src'),
common.mustSucceed((entries) => {
assert.ok(entries.includes('hello.txt'));
myVfs.unmount();
}));
}
// realpath (cb)
{
const { myVfs, mountPoint } = mounted();
fs.realpath(path.join(mountPoint, 'src/hello.txt'),
common.mustSucceed((rp) => {
assert.strictEqual(rp, path.join(mountPoint, 'src/hello.txt'));
myVfs.unmount();
}));
}
// access (cb)
{
const { myVfs, mountPoint } = mounted();
fs.access(path.join(mountPoint, 'src/hello.txt'),
common.mustSucceed(() => {
myVfs.unmount();
}));
}
// exists (cb) - signature is (exists) not (err, exists), use mustCall
{
const { myVfs, mountPoint } = mounted();
fs.exists(path.join(mountPoint, 'src/hello.txt'),
common.mustCall((ok) => {
assert.strictEqual(ok, true);
fs.exists(path.join(mountPoint, 'missing'),
common.mustCall((ok2) => {
assert.strictEqual(ok2, false);
myVfs.unmount();
}));
}));
}