Skip to content

Commit 2b95b97

Browse files
committed
vfs: address review comments on cp-sync and test docs
Use optional chaining for vfsState.handlers checks in cp-sync.js and reuse stat result to avoid double statSync call. Add YAML metadata to MockFSContext class documentation.
1 parent 8728a45 commit 2b95b97

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

doc/api/test.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2416,6 +2416,10 @@ The mock file system is automatically cleaned up when the test completes.
24162416

24172417
## Class: `MockFSContext`
24182418

2419+
<!-- YAML
2420+
added: REPLACEME
2421+
-->
2422+
24192423
The `MockFSContext` object is returned by `mock.fs()` and provides the
24202424
following methods and properties:
24212425

lib/internal/fs/cp/cp-sync.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function cpSyncFn(src, dest, opts) {
5656
// Skip C++ path checks for VFS paths (C++ lstat can't see VFS files).
5757
// The JS-level getStats() calls lstatSync which is VFS-intercepted.
5858
const { vfsState } = require('internal/fs/utils');
59-
if (vfsState.handlers === null || !vfsState.handlers.statSync(src)) {
59+
if (!vfsState.handlers?.statSync(src)) {
6060
fsBinding.cpSyncCheckPaths(src, dest, opts.dereference, opts.recursive);
6161
} else {
6262
// Minimal JS-level validation that mirrors what C++ does
@@ -103,7 +103,7 @@ function onFile(srcStat, destStat, src, dest, opts) {
103103
if (opts.force) {
104104
// Skip C++ fast path for VFS paths (C++ can't see VFS files).
105105
const { vfsState } = require('internal/fs/utils');
106-
if (vfsState.handlers === null || !vfsState.handlers.statSync(src)) {
106+
if (!vfsState.handlers?.statSync(src)) {
107107
return fsBinding.cpSyncOverrideFile(src, dest, opts.mode, opts.preserveTimestamps);
108108
}
109109
unlinkSync(dest);
@@ -167,7 +167,7 @@ function copyDir(src, dest, opts, mkDir, srcMode) {
167167
// we can run the whole function faster in C++.
168168
// Skip C++ fast path for VFS paths (C++ can't see VFS files).
169169
const { vfsState } = require('internal/fs/utils');
170-
if (vfsState.handlers === null || !vfsState.handlers.statSync(src)) {
170+
if (!vfsState.handlers?.statSync(src)) {
171171
// TODO(dario-piotrowicz): look into making cpSyncCopyDir also accept the potential filter function
172172
return fsBinding.cpSyncCopyDir(src, dest,
173173
opts.force,
@@ -240,8 +240,9 @@ function onLink(destStat, src, dest, verbatimSymlinks) {
240240
// internalModuleStat is a C++ call that can't see VFS files.
241241
const { vfsState } = require('internal/fs/utils');
242242
let srcIsDir;
243-
if (vfsState.handlers !== null && vfsState.handlers.statSync(src)) {
244-
srcIsDir = statSync(src).isDirectory();
243+
const vfsStat = vfsState.handlers?.statSync(src);
244+
if (vfsStat) {
245+
srcIsDir = vfsStat.isDirectory();
245246
} else {
246247
srcIsDir = fsBinding.internalModuleStat(src) === 1;
247248
}

0 commit comments

Comments
 (0)