|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | + |
| 5 | +if (!common.isWindows) { |
| 6 | + common.skip('Windows-specific test'); |
| 7 | +} |
| 8 | + |
| 9 | +const assert = require('assert'); |
| 10 | +const fs = require('fs'); |
| 11 | +const path = require('path'); |
| 12 | +const vfs = require('node:vfs'); |
| 13 | + |
| 14 | +// Test mounting with Windows drive letter path |
| 15 | +{ |
| 16 | + const myVfs = vfs.create(); |
| 17 | + myVfs.writeFileSync('/data.txt', 'Hello from VFS'); |
| 18 | + myVfs.mkdirSync('/subdir'); |
| 19 | + myVfs.writeFileSync('/subdir/nested.txt', 'Nested content'); |
| 20 | + |
| 21 | + // Mount at a Windows path with drive letter |
| 22 | + const mountPath = path.join(process.env.TEMP || 'C:\\temp', 'vfs-test-mount'); |
| 23 | + myVfs.mount(mountPath); |
| 24 | + |
| 25 | + assert.strictEqual(myVfs.mounted, true); |
| 26 | + assert.strictEqual(myVfs.mountPoint, mountPath); |
| 27 | + |
| 28 | + // Read file through fs module using Windows path |
| 29 | + const content = fs.readFileSync(path.join(mountPath, 'data.txt'), 'utf8'); |
| 30 | + assert.strictEqual(content, 'Hello from VFS'); |
| 31 | + |
| 32 | + // Read nested file |
| 33 | + const nestedContent = fs.readFileSync(path.join(mountPath, 'subdir', 'nested.txt'), 'utf8'); |
| 34 | + assert.strictEqual(nestedContent, 'Nested content'); |
| 35 | + |
| 36 | + // Stat operations |
| 37 | + const stat = fs.statSync(path.join(mountPath, 'data.txt')); |
| 38 | + assert.strictEqual(stat.isFile(), true); |
| 39 | + |
| 40 | + const dirStat = fs.statSync(path.join(mountPath, 'subdir')); |
| 41 | + assert.strictEqual(dirStat.isDirectory(), true); |
| 42 | + |
| 43 | + // Readdir |
| 44 | + const entries = fs.readdirSync(mountPath); |
| 45 | + assert.deepStrictEqual(entries.sort(), ['data.txt', 'subdir']); |
| 46 | + |
| 47 | + myVfs.unmount(); |
| 48 | + assert.strictEqual(myVfs.mounted, false); |
| 49 | +} |
| 50 | + |
| 51 | +// Test mounting at drive root (e.g., C:\virtual) |
| 52 | +{ |
| 53 | + const myVfs = vfs.create(); |
| 54 | + myVfs.writeFileSync('/test.txt', 'Drive root test'); |
| 55 | + |
| 56 | + // Use current drive letter |
| 57 | + const driveLetter = process.cwd().slice(0, 2); // e.g., "C:" |
| 58 | + const mountPath = driveLetter + '\\vfs-test-root'; |
| 59 | + myVfs.mount(mountPath); |
| 60 | + |
| 61 | + assert.strictEqual(myVfs.mounted, true); |
| 62 | + |
| 63 | + const content = fs.readFileSync(mountPath + '\\test.txt', 'utf8'); |
| 64 | + assert.strictEqual(content, 'Drive root test'); |
| 65 | + |
| 66 | + myVfs.unmount(); |
| 67 | +} |
| 68 | + |
| 69 | +// Test that mountPoint returns Windows-style absolute path |
| 70 | +{ |
| 71 | + const myVfs = vfs.create(); |
| 72 | + myVfs.writeFileSync('/file.txt', 'content'); |
| 73 | + |
| 74 | + const mountPath = 'C:\\virtual'; |
| 75 | + myVfs.mount(mountPath); |
| 76 | + |
| 77 | + // mountPoint should be an absolute Windows path |
| 78 | + assert.ok(myVfs.mountPoint.includes(':'), 'mountPoint should contain drive letter'); |
| 79 | + assert.ok(path.isAbsolute(myVfs.mountPoint), 'mountPoint should be absolute'); |
| 80 | + |
| 81 | + myVfs.unmount(); |
| 82 | +} |
| 83 | + |
| 84 | +// Test require from Windows VFS path |
| 85 | +{ |
| 86 | + const myVfs = vfs.create(); |
| 87 | + myVfs.writeFileSync('/module.js', 'module.exports = { value: 42 };'); |
| 88 | + |
| 89 | + const mountPath = path.join(process.env.TEMP || 'C:\\temp', 'vfs-require-test'); |
| 90 | + myVfs.mount(mountPath); |
| 91 | + |
| 92 | + const modulePath = path.join(mountPath, 'module.js'); |
| 93 | + const mod = require(modulePath); |
| 94 | + assert.strictEqual(mod.value, 42); |
| 95 | + |
| 96 | + myVfs.unmount(); |
| 97 | + |
| 98 | + // Clean up require cache |
| 99 | + delete require.cache[modulePath]; |
| 100 | +} |
0 commit comments