Skip to content

Commit aa19193

Browse files
committed
vfs: remove addFile and addDirectory methods
Users should use the standard writeFileSync/mkdirSync methods instead. MockFSContext retains addFile/addDirectory as convenience methods that prepend the prefix automatically.
1 parent 3d48b95 commit aa19193

4 files changed

Lines changed: 25 additions & 41 deletions

File tree

lib/internal/test_runner/mock/mock.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const {
5151
const { MockTimers } = require('internal/test_runner/mock/mock_timers');
5252
const { Module } = require('internal/modules/cjs/loader');
5353
const { _load, _nodeModulePaths, _resolveFilename, isBuiltin } = Module;
54-
const { join } = require('path');
54+
const { dirname, join } = require('path');
5555

5656
// Lazy-load VirtualFileSystem to avoid loading VFS code if fs mocking is not used
5757
const lazyVirtualFileSystem = getLazy(
@@ -432,19 +432,25 @@ class MockFSContext {
432432

433433
/**
434434
* Adds a file to the mock file system.
435-
* @param {string} filePath - The path of the file.
435+
* @param {string} filePath - The path of the file (relative to prefix).
436436
* @param {string|Buffer} content - The file content.
437437
*/
438438
addFile(filePath, content) {
439-
this.vfs.addFile(filePath, content);
439+
const fullPath = join(this.prefix, filePath);
440+
const parentDir = dirname(fullPath);
441+
if (parentDir !== '/') {
442+
this.vfs.mkdirSync(parentDir, { __proto__: null, recursive: true });
443+
}
444+
this.vfs.writeFileSync(fullPath, content);
440445
}
441446

442447
/**
443448
* Adds a directory to the mock file system.
444-
* @param {string} dirPath - The path of the directory.
449+
* @param {string} dirPath - The path of the directory (relative to prefix).
445450
*/
446451
addDirectory(dirPath) {
447-
this.vfs.addDirectory(dirPath);
452+
const fullPath = join(this.prefix, dirPath);
453+
this.vfs.mkdirSync(fullPath, { __proto__: null, recursive: true });
448454
}
449455

450456
/**
@@ -812,7 +818,11 @@ class MockTracker {
812818
for (let i = 0; i < paths.length; i++) {
813819
const filePath = paths[i];
814820
const content = files[filePath];
815-
vfs.addFile(filePath, content);
821+
const parentDir = dirname(filePath);
822+
if (parentDir !== '/') {
823+
vfs.mkdirSync(parentDir, { __proto__: null, recursive: true });
824+
}
825+
vfs.writeFileSync(filePath, content);
816826
}
817827
}
818828

lib/internal/vfs/file_system.js

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -460,32 +460,6 @@ class VirtualFileSystem {
460460
return undefined;
461461
}
462462

463-
/**
464-
* Adds a file directly to the VFS provider.
465-
* This writes directly to the provider bypassing mount point logic,
466-
* making it useful for populating the VFS before or after mounting.
467-
* @param {string} filePath The file path (relative to VFS root)
468-
* @param {Buffer|string} content The file content
469-
*/
470-
addFile(filePath, content) {
471-
const { dirname } = require('path');
472-
const parentDir = dirname(filePath);
473-
if (parentDir !== '/') {
474-
this[kProvider].mkdirSync(parentDir, { __proto__: null, recursive: true });
475-
}
476-
this[kProvider].writeFileSync(filePath, content);
477-
}
478-
479-
/**
480-
* Adds a directory directly to the VFS provider.
481-
* This writes directly to the provider bypassing mount point logic,
482-
* making it useful for populating the VFS before or after mounting.
483-
* @param {string} dirPath The directory path (relative to VFS root)
484-
*/
485-
addDirectory(dirPath) {
486-
this[kProvider].mkdirSync(dirPath, { __proto__: null, recursive: true });
487-
}
488-
489463
/**
490464
* Removes a directory synchronously.
491465
* @param {string} dirPath The directory path

test/parallel/test-runner-mock-fs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ test('mock.fs() exposes vfs property', (t) => {
148148
// Access the underlying VFS
149149
const vfs = mockFs.vfs;
150150
assert.ok(vfs);
151-
assert.strictEqual(typeof vfs.addFile, 'function');
152151
assert.strictEqual(typeof vfs.readFileSync, 'function');
152+
assert.strictEqual(typeof vfs.writeFileSync, 'function');
153153

154154
// Use VFS directly (requires full path including mount point)
155155
const content = vfs.readFileSync('/vfs-prop-test/file.txt', 'utf8');

test/parallel/test-vfs-watch.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ const vfs = require('node:vfs');
143143
}));
144144

145145
setTimeout(() => {
146-
// Use addFile to write directly to provider, bypassing mount path logic
147-
myVfs.addFile('/data/file.txt', 'updated via fs.watch');
146+
// Write to mounted path to trigger watcher
147+
myVfs.writeFileSync('/virtual/data/file.txt', 'updated via fs.watch');
148148
}, 100);
149149
}
150150

@@ -164,8 +164,8 @@ const vfs = require('node:vfs');
164164
fs.watchFile('/virtual2/data/watchfile.txt', { interval: 50, persistent: false }, listener);
165165

166166
setTimeout(() => {
167-
// Use addFile to write directly to provider, bypassing mount path logic
168-
myVfs.addFile('/data/watchfile.txt', 'updated via fs.watchFile');
167+
// Write to mounted path to trigger watcher
168+
myVfs.writeFileSync('/virtual2/data/watchfile.txt', 'updated via fs.watchFile');
169169
}, 100);
170170
}
171171

@@ -185,8 +185,8 @@ const vfs = require('node:vfs');
185185
}));
186186

187187
setTimeout(() => {
188-
// Use addFile to write directly to provider, bypassing mount path logic
189-
myVfs.addFile('/file.txt', 'vfs updated');
188+
// Write to mounted path to trigger watcher
189+
myVfs.writeFileSync('/overlay-test/file.txt', 'vfs updated');
190190
}, 100);
191191
}
192192

@@ -282,7 +282,7 @@ const vfs = require('node:vfs');
282282

283283
// Schedule a change
284284
setTimeout(() => {
285-
myVfs.addFile('/promises-test.txt', 'updated');
285+
myVfs.writeFileSync('/virtual-promises/promises-test.txt', 'updated');
286286
}, 100);
287287

288288
// Schedule abort after getting one event
@@ -370,7 +370,7 @@ const vfs = require('node:vfs');
370370
}));
371371

372372
setTimeout(() => {
373-
myVfs.addFile('/data/subdir/nested.txt', 'updated nested');
373+
myVfs.writeFileSync('/virtual-recursive/data/subdir/nested.txt', 'updated nested');
374374
}, 100);
375375
}
376376

0 commit comments

Comments
 (0)