|
| 1 | +// Flags: --expose-internals |
1 | 2 | 'use strict'; |
2 | 3 |
|
3 | | -require('../common'); |
| 4 | +const common = require('../common'); |
4 | 5 | const assert = require('assert'); |
5 | 6 | const vfs = require('node:vfs'); |
6 | 7 |
|
@@ -187,3 +188,112 @@ const vfs = require('node:vfs'); |
187 | 188 | myVfs.mkdirSync('/existing', { recursive: true }); |
188 | 189 | assert.strictEqual(myVfs.existsSync('/existing'), true); |
189 | 190 | } |
| 191 | + |
| 192 | +// Test mounting at root '/' (exercises joinMountPath with '/' relativePath) |
| 193 | +{ |
| 194 | + const myVfs = vfs.create(); |
| 195 | + myVfs.mkdirSync('/app/data', { recursive: true }); |
| 196 | + myVfs.writeFileSync('/app/data/config.json', '{}'); |
| 197 | + |
| 198 | + // Mount at root |
| 199 | + myVfs.mount('/'); |
| 200 | + assert.strictEqual(myVfs.mountPoint, '/'); |
| 201 | + |
| 202 | + // Access the mount point root itself (relativePath = '/') |
| 203 | + assert.strictEqual(myVfs.shouldHandle('/'), true); |
| 204 | + assert.strictEqual(myVfs.shouldHandle('/app'), true); |
| 205 | + assert.strictEqual(myVfs.shouldHandle('/app/data/config.json'), true); |
| 206 | + |
| 207 | + myVfs.unmount(); |
| 208 | +} |
| 209 | + |
| 210 | +// Test deeply nested paths (exercises getParentPath and getBaseName internally) |
| 211 | +{ |
| 212 | + const myVfs = vfs.create(); |
| 213 | + myVfs.mkdirSync('/a/b/c/d/e', { recursive: true }); |
| 214 | + myVfs.writeFileSync('/a/b/c/d/e/file.txt', 'deep'); |
| 215 | + |
| 216 | + // Read the deeply nested file |
| 217 | + assert.strictEqual(myVfs.readFileSync('/a/b/c/d/e/file.txt', 'utf8'), 'deep'); |
| 218 | + |
| 219 | + // Readdir at various levels |
| 220 | + assert.ok(myVfs.readdirSync('/a').includes('b')); |
| 221 | + assert.ok(myVfs.readdirSync('/a/b').includes('c')); |
| 222 | + assert.ok(myVfs.readdirSync('/a/b/c/d/e').includes('file.txt')); |
| 223 | + |
| 224 | + // Stat files at root level |
| 225 | + myVfs.writeFileSync('/root-file.txt', 'root'); |
| 226 | + const stat = myVfs.statSync('/root-file.txt'); |
| 227 | + assert.strictEqual(stat.isFile(), true); |
| 228 | +} |
| 229 | + |
| 230 | +// Test truncateSync on file handle |
| 231 | +{ |
| 232 | + const myVfs = vfs.create(); |
| 233 | + myVfs.writeFileSync('/truncate.txt', 'hello world'); |
| 234 | + |
| 235 | + // Open file and truncate |
| 236 | + const fd = myVfs.openSync('/truncate.txt', 'r+'); |
| 237 | + const handle = require('internal/vfs/fd').getVirtualFd(fd); |
| 238 | + |
| 239 | + // Truncate to smaller size |
| 240 | + handle.entry.truncateSync(5); |
| 241 | + myVfs.closeSync(fd); |
| 242 | + |
| 243 | + assert.strictEqual(myVfs.readFileSync('/truncate.txt', 'utf8'), 'hello'); |
| 244 | +} |
| 245 | + |
| 246 | +// Test truncateSync extending file |
| 247 | +{ |
| 248 | + const myVfs = vfs.create(); |
| 249 | + myVfs.writeFileSync('/extend.txt', 'hi'); |
| 250 | + |
| 251 | + const fd = myVfs.openSync('/extend.txt', 'r+'); |
| 252 | + const handle = require('internal/vfs/fd').getVirtualFd(fd); |
| 253 | + |
| 254 | + // Truncate to larger size (extends with null bytes) |
| 255 | + handle.entry.truncateSync(10); |
| 256 | + myVfs.closeSync(fd); |
| 257 | + |
| 258 | + const content = myVfs.readFileSync('/extend.txt'); |
| 259 | + assert.strictEqual(content.length, 10); |
| 260 | + assert.strictEqual(content.slice(0, 2).toString(), 'hi'); |
| 261 | +} |
| 262 | + |
| 263 | +// Test async truncate |
| 264 | +{ |
| 265 | + const myVfs = vfs.create(); |
| 266 | + myVfs.writeFileSync('/async-truncate.txt', 'async content'); |
| 267 | + |
| 268 | + const fd = myVfs.openSync('/async-truncate.txt', 'r+'); |
| 269 | + const handle = require('internal/vfs/fd').getVirtualFd(fd); |
| 270 | + |
| 271 | + handle.entry.truncate(5).then(common.mustCall(() => { |
| 272 | + myVfs.closeSync(fd); |
| 273 | + assert.strictEqual(myVfs.readFileSync('/async-truncate.txt', 'utf8'), 'async'); |
| 274 | + })); |
| 275 | +} |
| 276 | + |
| 277 | +// Test rename operation |
| 278 | +{ |
| 279 | + const myVfs = vfs.create(); |
| 280 | + myVfs.writeFileSync('/old-name.txt', 'content'); |
| 281 | + |
| 282 | + myVfs.renameSync('/old-name.txt', '/new-name.txt'); |
| 283 | + |
| 284 | + assert.strictEqual(myVfs.existsSync('/old-name.txt'), false); |
| 285 | + assert.strictEqual(myVfs.existsSync('/new-name.txt'), true); |
| 286 | + assert.strictEqual(myVfs.readFileSync('/new-name.txt', 'utf8'), 'content'); |
| 287 | +} |
| 288 | + |
| 289 | +// Test copyFile operation |
| 290 | +{ |
| 291 | + const myVfs = vfs.create(); |
| 292 | + myVfs.writeFileSync('/source.txt', 'copy me'); |
| 293 | + |
| 294 | + myVfs.copyFileSync('/source.txt', '/dest.txt'); |
| 295 | + |
| 296 | + assert.strictEqual(myVfs.existsSync('/source.txt'), true); |
| 297 | + assert.strictEqual(myVfs.existsSync('/dest.txt'), true); |
| 298 | + assert.strictEqual(myVfs.readFileSync('/dest.txt', 'utf8'), 'copy me'); |
| 299 | +} |
0 commit comments