Skip to content

Commit 7471a75

Browse files
committed
test: add missing callback tests for VFS promises test file
Add callback-based tests for writeFile, readlink, and fd operations (open, read, fstat, close) to match the sync coverage in test-vfs-basic.
1 parent e1c0f69 commit 7471a75

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

test/parallel/test-vfs-promises.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,81 @@ const vfs = require('node:vfs');
156156
}));
157157
}
158158

159+
// Test callback-based writeFile
160+
{
161+
const myVfs = vfs.create();
162+
163+
myVfs.writeFile('/cb-write.txt', 'callback written', common.mustCall((err) => {
164+
assert.strictEqual(err, null);
165+
assert.strictEqual(myVfs.readFileSync('/cb-write.txt', 'utf8'), 'callback written');
166+
}));
167+
168+
// Overwrite existing
169+
myVfs.writeFileSync('/cb-overwrite.txt', 'old');
170+
myVfs.writeFile('/cb-overwrite.txt', 'new', common.mustCall((err) => {
171+
assert.strictEqual(err, null);
172+
assert.strictEqual(myVfs.readFileSync('/cb-overwrite.txt', 'utf8'), 'new');
173+
}));
174+
175+
// Write with Buffer
176+
myVfs.writeFile('/cb-buf.txt', Buffer.from('buf data'), common.mustCall((err) => {
177+
assert.strictEqual(err, null);
178+
assert.strictEqual(myVfs.readFileSync('/cb-buf.txt', 'utf8'), 'buf data');
179+
}));
180+
}
181+
182+
// Test callback-based readlink
183+
{
184+
const myVfs = vfs.create();
185+
myVfs.writeFileSync('/link-target.txt', 'content');
186+
myVfs.symlinkSync('/link-target.txt', '/my-link.txt');
187+
188+
myVfs.readlink('/my-link.txt', common.mustCall((err, target) => {
189+
assert.strictEqual(err, null);
190+
assert.strictEqual(target, '/link-target.txt');
191+
}));
192+
193+
myVfs.readlink('/link-target.txt', common.mustCall((err) => {
194+
assert.strictEqual(err.code, 'EINVAL');
195+
}));
196+
}
197+
198+
// Test callback-based open, read, fstat, close
199+
{
200+
const myVfs = vfs.create();
201+
myVfs.writeFileSync('/fd-test.txt', 'fd content');
202+
203+
myVfs.open('/fd-test.txt', 'r', common.mustCall((err, fd) => {
204+
assert.strictEqual(err, null);
205+
assert.strictEqual(typeof fd, 'number');
206+
207+
// fstat
208+
myVfs.fstat(fd, common.mustCall((err, stats) => {
209+
assert.strictEqual(err, null);
210+
assert.strictEqual(stats.isFile(), true);
211+
assert.strictEqual(stats.size, 10);
212+
}));
213+
214+
// read
215+
const buf = Buffer.alloc(10);
216+
myVfs.read(fd, buf, 0, 10, 0, common.mustCall((err, bytesRead, buffer) => {
217+
assert.strictEqual(err, null);
218+
assert.strictEqual(bytesRead, 10);
219+
assert.strictEqual(buffer.toString(), 'fd content');
220+
}));
221+
222+
// close
223+
myVfs.close(fd, common.mustCall((err) => {
224+
assert.strictEqual(err, null);
225+
}));
226+
}));
227+
228+
// open non-existent
229+
myVfs.open('/nonexistent.txt', 'r', common.mustCall((err) => {
230+
assert.strictEqual(err.code, 'ENOENT');
231+
}));
232+
}
233+
159234
// ==================== Promise API Tests ====================
160235

161236
// Test promises.readFile

0 commit comments

Comments
 (0)