Files
node/test/parallel/test-vfs-fs-open-callback.js
T
Matteo CollinaandJames M Snell 4d9cb71fa2 vfs: integrate with CJS and ESM module loaders
Co-authored-by: James M Snell <jasnell@gmail.com>
Signed-off-by: Matteo Collina <hello@matteocollina.com>
PR-URL: https://github.com/nodejs/node/pull/63653
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: James M Snell <jasnell@gmail.com>
2026-08-31 10:31:16 +02:00

75 lines
2.4 KiB
JavaScript

// Flags: --experimental-vfs
'use strict';
// fs.open / fs.fstat / fs.read / fs.write / fs.close / fs.ftruncate 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 };
}
// Open + fstat + read + close
{
const { myVfs, mountPoint } = mounted();
fs.open(path.join(mountPoint, 'src/hello.txt'), 'r',
common.mustSucceed((fd) => {
assert.notStrictEqual(fd & 0x40000000, 0);
fs.fstat(fd, common.mustSucceed((stats) => {
assert.strictEqual(stats.isFile(), true);
const buf = Buffer.alloc(11);
fs.read(fd, buf, 0, 11, 0,
common.mustSucceed((n, buffer) => {
assert.strictEqual(n, 11);
assert.strictEqual(buffer.toString(), 'hello world');
fs.close(fd,
common.mustSucceed(() => myVfs.unmount()));
}));
}));
}));
}
// Open + write + close
{
const { myVfs, mountPoint } = mounted();
fs.open(path.join(mountPoint, 'src/aw.txt'), 'w',
common.mustSucceed((fd) => {
const data = Buffer.from('async-fd');
fs.write(fd, data, 0, data.length, 0,
common.mustSucceed((n) => {
assert.strictEqual(n, data.length);
fs.close(fd, common.mustSucceed(() => {
assert.strictEqual(
fs.readFileSync(
path.join(mountPoint, 'src/aw.txt'), 'utf8'),
'async-fd',
);
myVfs.unmount();
}));
}));
}));
}
// ftruncate (cb)
{
const { myVfs, mountPoint } = mounted();
const fd = fs.openSync(path.join(mountPoint, 'src/hello.txt'), 'r+');
fs.ftruncate(fd, 5, common.mustSucceed(() => {
fs.closeSync(fd);
assert.strictEqual(
fs.readFileSync(path.join(mountPoint, 'src/hello.txt'), 'utf8'),
'hello',
);
myVfs.unmount();
}));
}