mirror of
https://github.com/nodejs/node.git
synced 2026-11-21 12:59:16 -05:00
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>
62 lines
1.8 KiB
JavaScript
62 lines
1.8 KiB
JavaScript
// Flags: --experimental-vfs
|
|
'use strict';
|
|
|
|
// The promise-based fs methods that accept `encoding: 'buffer'` must convert
|
|
// the (string) provider result into a Buffer before resolving.
|
|
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const fsp = require('fs/promises');
|
|
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');
|
|
const mountPoint = myVfs.mount();
|
|
return { myVfs, mountPoint };
|
|
}
|
|
|
|
(async () => {
|
|
// readdir
|
|
{
|
|
const { myVfs, mountPoint } = mounted();
|
|
const entries = await fsp.readdir(path.join(mountPoint, 'src'),
|
|
{ encoding: 'buffer' });
|
|
assert.ok(entries.every(Buffer.isBuffer));
|
|
assert.ok(entries.some((b) => b.toString() === 'hello.txt'));
|
|
myVfs.unmount();
|
|
}
|
|
|
|
// realpath
|
|
{
|
|
const { myVfs, mountPoint } = mounted();
|
|
const p = path.join(mountPoint, 'src/hello.txt');
|
|
const rp = await fsp.realpath(p, { encoding: 'buffer' });
|
|
assert.ok(Buffer.isBuffer(rp));
|
|
assert.strictEqual(rp.toString(), p);
|
|
myVfs.unmount();
|
|
}
|
|
|
|
// readlink
|
|
{
|
|
const { myVfs, mountPoint } = mounted();
|
|
await fsp.symlink('hello.txt', path.join(mountPoint, 'src/ln.txt'));
|
|
const target = await fsp.readlink(path.join(mountPoint, 'src/ln.txt'),
|
|
{ encoding: 'buffer' });
|
|
assert.ok(Buffer.isBuffer(target));
|
|
assert.strictEqual(target.toString(), 'hello.txt');
|
|
myVfs.unmount();
|
|
}
|
|
|
|
// mkdtemp
|
|
{
|
|
const { myVfs, mountPoint } = mounted();
|
|
const dir = await fsp.mkdtemp(path.join(mountPoint, 'src/td-'),
|
|
{ encoding: 'buffer' });
|
|
assert.ok(Buffer.isBuffer(dir));
|
|
myVfs.unmount();
|
|
}
|
|
})().then(common.mustCall());
|