Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v18.x backport] fs: introduce dirent.parentPath #51021

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fs: introduce dirent.parentPath
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.

PR-URL: #50976
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
  • Loading branch information
aduh95 committed Dec 2, 2023
commit a990e8fec8149d008efe5b8681873bb4983386e0
13 changes: 13 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -6437,6 +6437,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 Down
2 changes: 1 addition & 1 deletion lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1431,7 +1431,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
1 change: 1 addition & 0 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,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 entries = files.map(() => {
const dirent = dir.readSync();
assertDirent(dirent);
return dirent.name;
});
assert.deepStrictEqual(files, entries.sort());
return { name: dirent.name, path: dirent.path, parentPath: dirent.parentPath, toString() { return dirent.name; } };
}).sort();
assert.deepStrictEqual(entries.map((d) => d.name), files);
assert.deepStrictEqual(entries.map((d) => d.path), files.map((name) => path.join(testDir, name)));
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:125 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal: + actual - expected [ + '/Users/runner/work/node/node/test/.tmp.822/empty', + '/Users/runner/work/node/node/test/.tmp.822/files', + '/Users/runner/work/node/node/test/.tmp.822/for', + '/Users/runner/work/node/node/test/.tmp.822/just', + '/Users/runner/work/node/node/test/.tmp.822/testing' - '/Users/runner/work/node/node/test/.tmp.822', - '/Users/runner/work/node/node/test/.tmp.822', - '/Users/runner/work/node/node/test/.tmp.822', - '/Users/runner/work/node/node/test/.tmp.822', - '/Users/runner/work/node/node/test/.tmp.822' ] at Object.<anonymous> (/Users/runner/work/node/node/test/parallel/test-fs-opendir.js:55:10) at Module._compile (node:internal/modules/cjs/loader:1356:14) at Module._extensions..js (node:internal/modules/cjs/loader:1414:10) at Module.load (node:internal/modules/cjs/loader:1197:32) at Module._load (node:internal/modules/cjs/loader:1013:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:128:12) at node:internal/main/run_main_module:28:49 { generatedMessage: true, code: 'ERR_ASSERTION', actual: [ '/Users/runner/work/node/node/test/.tmp.822/empty', '/Users/runner/work/node/node/test/.tmp.822/files', '/Users/runner/work/node/node/test/.tmp.822/for', '/Users/runner/work/node/node/test/.tmp.822/just', '/Users/runner/work/node/node/test/.tmp.822/testing' ], expected: [ '/Users/runner/work/node/node/test/.tmp.822', '/Users/runner/work/node/node/test/.tmp.822', '/Users/runner/work/node/node/test/.tmp.822', '/Users/runner/work/node/node/test/.tmp.822', '/Users/runner/work/node/node/test/.tmp.822' ], operator: 'deepStrictEqual' } Node.js v18.19.1-pre Command: out/Release/node /Users/runner/work/node/node/test/parallel/test-fs-opendir.js

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

View workflow job for this annotation

GitHub Actions / test-linux

--- stderr --- node:assert:125 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal: + actual - expected [ + '/home/runner/work/node/node/test/.tmp.842/empty', + '/home/runner/work/node/node/test/.tmp.842/files', + '/home/runner/work/node/node/test/.tmp.842/for', + '/home/runner/work/node/node/test/.tmp.842/just', + '/home/runner/work/node/node/test/.tmp.842/testing' - '/home/runner/work/node/node/test/.tmp.842', - '/home/runner/work/node/node/test/.tmp.842', - '/home/runner/work/node/node/test/.tmp.842', - '/home/runner/work/node/node/test/.tmp.842', - '/home/runner/work/node/node/test/.tmp.842' ] at Object.<anonymous> (/home/runner/work/node/node/test/parallel/test-fs-opendir.js:55:10) at Module._compile (node:internal/modules/cjs/loader:1356:14) at Module._extensions..js (node:internal/modules/cjs/loader:1414:10) at Module.load (node:internal/modules/cjs/loader:1197:32) at Module._load (node:internal/modules/cjs/loader:1013:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:128:12) at node:internal/main/run_main_module:28:49 { generatedMessage: true, code: 'ERR_ASSERTION', actual: [ '/home/runner/work/node/node/test/.tmp.842/empty', '/home/runner/work/node/node/test/.tmp.842/files', '/home/runner/work/node/node/test/.tmp.842/for', '/home/runner/work/node/node/test/.tmp.842/just', '/home/runner/work/node/node/test/.tmp.842/testing' ], expected: [ '/home/runner/work/node/node/test/.tmp.842', '/home/runner/work/node/node/test/.tmp.842', '/home/runner/work/node/node/test/.tmp.842', '/home/runner/work/node/node/test/.tmp.842', '/home/runner/work/node/node/test/.tmp.842' ], operator: 'deepStrictEqual' } Node.js v18.19.1-pre Command: out/Release/node /home/runner/work/node/node/test/parallel/test-fs-opendir.js

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

View workflow job for this annotation

GitHub Actions / test-asan

--- stderr --- node:assert:125 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal: + actual - expected [ + '/home/runner/work/node/node/test/.tmp.858/empty', + '/home/runner/work/node/node/test/.tmp.858/files', + '/home/runner/work/node/node/test/.tmp.858/for', + '/home/runner/work/node/node/test/.tmp.858/just', + '/home/runner/work/node/node/test/.tmp.858/testing' - '/home/runner/work/node/node/test/.tmp.858', - '/home/runner/work/node/node/test/.tmp.858', - '/home/runner/work/node/node/test/.tmp.858', - '/home/runner/work/node/node/test/.tmp.858', - '/home/runner/work/node/node/test/.tmp.858' ] at Object.<anonymous> (/home/runner/work/node/node/test/parallel/test-fs-opendir.js:55:10) at Module._compile (node:internal/modules/cjs/loader:1356:14) at Module._extensions..js (node:internal/modules/cjs/loader:1414:10) at Module.load (node:internal/modules/cjs/loader:1197:32) at Module._load (node:internal/modules/cjs/loader:1013:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:128:12) at node:internal/main/run_main_module:28:49 { generatedMessage: true, code: 'ERR_ASSERTION', actual: [ '/home/runner/work/node/node/test/.tmp.858/empty', '/home/runner/work/node/node/test/.tmp.858/files', '/home/runner/work/node/node/test/.tmp.858/for', '/home/runner/work/node/node/test/.tmp.858/just', '/home/runner/work/node/node/test/.tmp.858/testing' ], expected: [ '/home/runner/work/node/node/test/.tmp.858', '/home/runner/work/node/node/test/.tmp.858', '/home/runner/work/node/node/test/.tmp.858', '/home/runner/work/node/node/test/.tmp.858', '/home/runner/work/node/node/test/.tmp.858' ], operator: 'deepStrictEqual' } Node.js v18.19.1-pre Command: out/Release/node /home/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
Loading