|
| 1 | +// Flags: --expose-internals |
1 | 2 | 'use strict'; |
2 | 3 |
|
3 | 4 | const common = require('../common'); |
@@ -268,3 +269,122 @@ const vfs = require('node:vfs'); |
268 | 269 | { code: 'ENOENT' } |
269 | 270 | ); |
270 | 271 | })().then(common.mustCall()); |
| 272 | + |
| 273 | +// Test promises.writeFile |
| 274 | +(async () => { |
| 275 | + const myVfs = vfs.create(); |
| 276 | + |
| 277 | + await myVfs.promises.writeFile('/write-test.txt', 'async written'); |
| 278 | + assert.strictEqual(myVfs.readFileSync('/write-test.txt', 'utf8'), 'async written'); |
| 279 | + |
| 280 | + // Overwrite existing file |
| 281 | + await myVfs.promises.writeFile('/write-test.txt', 'overwritten'); |
| 282 | + assert.strictEqual(myVfs.readFileSync('/write-test.txt', 'utf8'), 'overwritten'); |
| 283 | + |
| 284 | + // Write with Buffer |
| 285 | + await myVfs.promises.writeFile('/buffer-write.txt', Buffer.from('buffer data')); |
| 286 | + assert.strictEqual(myVfs.readFileSync('/buffer-write.txt', 'utf8'), 'buffer data'); |
| 287 | +})().then(common.mustCall()); |
| 288 | + |
| 289 | +// Test promises.appendFile |
| 290 | +(async () => { |
| 291 | + const myVfs = vfs.create(); |
| 292 | + myVfs.writeFileSync('/append-test.txt', 'start'); |
| 293 | + |
| 294 | + await myVfs.promises.appendFile('/append-test.txt', '-end'); |
| 295 | + assert.strictEqual(myVfs.readFileSync('/append-test.txt', 'utf8'), 'start-end'); |
| 296 | + |
| 297 | + // Append to non-existent file creates it |
| 298 | + await myVfs.promises.appendFile('/new-append.txt', 'new content'); |
| 299 | + assert.strictEqual(myVfs.readFileSync('/new-append.txt', 'utf8'), 'new content'); |
| 300 | +})().then(common.mustCall()); |
| 301 | + |
| 302 | +// Test promises.mkdir |
| 303 | +(async () => { |
| 304 | + const myVfs = vfs.create(); |
| 305 | + |
| 306 | + await myVfs.promises.mkdir('/async-dir'); |
| 307 | + const stat = myVfs.statSync('/async-dir'); |
| 308 | + assert.strictEqual(stat.isDirectory(), true); |
| 309 | + |
| 310 | + // Recursive mkdir |
| 311 | + await myVfs.promises.mkdir('/async-dir/nested/deep', { recursive: true }); |
| 312 | + assert.strictEqual(myVfs.statSync('/async-dir/nested/deep').isDirectory(), true); |
| 313 | + |
| 314 | + // Mkdir on existing directory throws without recursive |
| 315 | + await assert.rejects( |
| 316 | + myVfs.promises.mkdir('/async-dir'), |
| 317 | + { code: 'EEXIST' } |
| 318 | + ); |
| 319 | +})().then(common.mustCall()); |
| 320 | + |
| 321 | +// Test promises.unlink |
| 322 | +(async () => { |
| 323 | + const myVfs = vfs.create(); |
| 324 | + myVfs.writeFileSync('/unlink-test.txt', 'to delete'); |
| 325 | + |
| 326 | + await myVfs.promises.unlink('/unlink-test.txt'); |
| 327 | + assert.strictEqual(myVfs.existsSync('/unlink-test.txt'), false); |
| 328 | + |
| 329 | + await assert.rejects( |
| 330 | + myVfs.promises.unlink('/nonexistent.txt'), |
| 331 | + { code: 'ENOENT' } |
| 332 | + ); |
| 333 | +})().then(common.mustCall()); |
| 334 | + |
| 335 | +// Test promises.rmdir |
| 336 | +(async () => { |
| 337 | + const myVfs = vfs.create(); |
| 338 | + myVfs.mkdirSync('/rmdir-test'); |
| 339 | + |
| 340 | + await myVfs.promises.rmdir('/rmdir-test'); |
| 341 | + assert.strictEqual(myVfs.existsSync('/rmdir-test'), false); |
| 342 | + |
| 343 | + // Rmdir on non-empty directory throws |
| 344 | + myVfs.mkdirSync('/nonempty'); |
| 345 | + myVfs.writeFileSync('/nonempty/file.txt', 'content'); |
| 346 | + await assert.rejects( |
| 347 | + myVfs.promises.rmdir('/nonempty'), |
| 348 | + { code: 'ENOTEMPTY' } |
| 349 | + ); |
| 350 | +})().then(common.mustCall()); |
| 351 | + |
| 352 | +// Test promises.rename |
| 353 | +(async () => { |
| 354 | + const myVfs = vfs.create(); |
| 355 | + myVfs.writeFileSync('/rename-src.txt', 'rename me'); |
| 356 | + |
| 357 | + await myVfs.promises.rename('/rename-src.txt', '/rename-dest.txt'); |
| 358 | + assert.strictEqual(myVfs.existsSync('/rename-src.txt'), false); |
| 359 | + assert.strictEqual(myVfs.readFileSync('/rename-dest.txt', 'utf8'), 'rename me'); |
| 360 | +})().then(common.mustCall()); |
| 361 | + |
| 362 | +// Test promises.copyFile |
| 363 | +(async () => { |
| 364 | + const myVfs = vfs.create(); |
| 365 | + myVfs.writeFileSync('/copy-src.txt', 'copy me'); |
| 366 | + |
| 367 | + await myVfs.promises.copyFile('/copy-src.txt', '/copy-dest.txt'); |
| 368 | + assert.strictEqual(myVfs.readFileSync('/copy-dest.txt', 'utf8'), 'copy me'); |
| 369 | + // Source still exists |
| 370 | + assert.strictEqual(myVfs.existsSync('/copy-src.txt'), true); |
| 371 | + |
| 372 | + await assert.rejects( |
| 373 | + myVfs.promises.copyFile('/nonexistent.txt', '/fail.txt'), |
| 374 | + { code: 'ENOENT' } |
| 375 | + ); |
| 376 | +})().then(common.mustCall()); |
| 377 | + |
| 378 | +// Test async truncate (via file handle) |
| 379 | +(async () => { |
| 380 | + const myVfs = vfs.create(); |
| 381 | + myVfs.writeFileSync('/truncate-test.txt', 'async content'); |
| 382 | + |
| 383 | + const fd = myVfs.openSync('/truncate-test.txt', 'r+'); |
| 384 | + const { getVirtualFd } = require('internal/vfs/fd'); |
| 385 | + const handle = getVirtualFd(fd); |
| 386 | + |
| 387 | + await handle.entry.truncate(5); |
| 388 | + myVfs.closeSync(fd); |
| 389 | + assert.strictEqual(myVfs.readFileSync('/truncate-test.txt', 'utf8'), 'async'); |
| 390 | +})().then(common.mustCall()); |
0 commit comments