Skip to content

Commit

Permalink
fs: introduce dirent.parentPath
Browse files Browse the repository at this point in the history
The goal is to replace `dirent.path` using a name that's less likely to
create confusion.
`dirent.path` value has not been stable, moving it to a different
property name should avoid breaking some upgrading user expectations.
  • Loading branch information
aduh95 committed Nov 30, 2023
1 parent a70d9e5 commit d1af6f2
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
15 changes: 14 additions & 1 deletion doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -6638,6 +6638,19 @@ The file name that this {fs.Dirent} object refers to. The type of this
value is determined by the `options.encoding` passed to [`fs.readdir()`][] or
[`fs.readdirSync()`][].
#### `dirent.parentPath`
<!-- YAML
added:
- REPLACEME
-->
> Stability: 1 – Experimental
* {string}
The path to the parent directory of the file this {fs.Dirent} object refers to.
#### `dirent.path`
<!-- YAML
Expand All @@ -6648,7 +6661,7 @@ added:
* {string}
The base path that this {fs.Dirent} object refers to.
Alias for `dirent.parentPath`.
### Class: `fs.FSWatcher`
Expand Down
2 changes: 1 addition & 1 deletion lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1426,7 +1426,7 @@ function readdirSyncRecursive(basePath, options) {
const dirent = getDirent(path, readdirResult[0][i], readdirResult[1][i]);
ArrayPrototypePush(readdirResults, dirent);
if (dirent.isDirectory()) {
ArrayPrototypePush(pathsQueue, pathModule.join(dirent.path, dirent.name));
ArrayPrototypePush(pathsQueue, pathModule.join(dirent.parentPath, dirent.name));
}
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/fs/dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class Dir {
}

readSyncRecursive(dirent) {
const path = pathModule.join(dirent.path, dirent.name);
const path = pathModule.join(dirent.parentPath, dirent.name);
const ctx = { path };
const handle = dirBinding.opendir(
pathModule.toNamespacedPath(path),
Expand Down
1 change: 1 addition & 0 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ function assertEncoding(encoding) {
class Dirent {
constructor(name, type, path) {
this.name = name;
this.parentPath = path;
this.path = path;
this[kType] = type;
}
Expand Down
8 changes: 5 additions & 3 deletions test/parallel/test-fs-opendir.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ const invalidCallbackObj = {
const entries = files.map(() => {
const dirent = dir.readSync();
assertDirent(dirent);
return dirent.name;
});
assert.deepStrictEqual(files, entries.sort());
return { name: dirent.name, path: dirent.path, dirname: dirent.parentPath, toString() { return dirent.name; } };
}).sort();
assert.deepStrictEqual(entries.map((d) => d.name), files);
assert.deepStrictEqual(entries.map((d) => d.path), Array(entries.length).fill(testDir));
assert.deepStrictEqual(entries.map((d) => d.parentPath), Array(entries.length).fill(testDir));

Check failure on line 55 in test/parallel/test-fs-opendir.js

View workflow job for this annotation

GitHub Actions / test-macOS

--- stderr --- node:assert:126 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal: + actual - expected [ + undefined, + undefined, + undefined, + undefined, + undefined - '/Users/runner/work/node/node/test/.tmp.838', - '/Users/runner/work/node/node/test/.tmp.838', - '/Users/runner/work/node/node/test/.tmp.838', - '/Users/runner/work/node/node/test/.tmp.838', - '/Users/runner/work/node/node/test/.tmp.838' ] at Object.<anonymous> (/Users/runner/work/node/node/test/parallel/test-fs-opendir.js:55:10) at Module._compile (node:internal/modules/cjs/loader:1375:14) at Module._extensions..js (node:internal/modules/cjs/loader:1434:10) at Module.load (node:internal/modules/cjs/loader:1206:32) at Module._load (node:internal/modules/cjs/loader:1022:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12) at node:internal/main/run_main_module:28:49 { generatedMessage: true, code: 'ERR_ASSERTION', actual: [ undefined, undefined, undefined, undefined, undefined ], expected: [ '/Users/runner/work/node/node/test/.tmp.838', '/Users/runner/work/node/node/test/.tmp.838', '/Users/runner/work/node/node/test/.tmp.838', '/Users/runner/work/node/node/test/.tmp.838', '/Users/runner/work/node/node/test/.tmp.838' ], operator: 'deepStrictEqual' } Node.js v22.0.0-pre Command: out/Release/node --test-reporter=spec --test-reporter-destination=stdout --test-reporter=./tools/github_reporter/index.js --test-reporter-destination=stdout /Users/runner/work/node/node/test/parallel/test-fs-opendir.js

// dir.read should return null when no more entries exist
assert.strictEqual(dir.readSync(), null);
Expand Down

0 comments on commit d1af6f2

Please sign in to comment.