From 1b08f3b68cec36cc4a6a8fdecaaf9caedd70af47 Mon Sep 17 00:00:00 2001 From: Louis <15342503+SoulKa@users.noreply.github.com> Date: Tue, 17 Sep 2024 22:19:45 +0200 Subject: [PATCH] fix: add `parentPath` to `Dirent` (#1058) * #735 - add parentPath property to Dirent * #735 - fix tests for parentPath property * #735 - use Dirent parentPath instead of path in Volume --- src/Dirent.ts | 2 ++ src/__tests__/volume/readdirSync.test.ts | 26 ++++++++++++------------ src/volume.ts | 2 +- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/Dirent.ts b/src/Dirent.ts index 32cafd514..237598f11 100644 --- a/src/Dirent.ts +++ b/src/Dirent.ts @@ -16,12 +16,14 @@ export class Dirent implements IDirent { dirent.name = strToEncoding(link.getName(), encoding); dirent.mode = mode; dirent.path = link.getParentPath(); + dirent.parentPath = dirent.path; return dirent; } name: TDataOut = ''; path = ''; + parentPath = ''; private mode: number = 0; private _checkModeProperty(property: number): boolean { diff --git a/src/__tests__/volume/readdirSync.test.ts b/src/__tests__/volume/readdirSync.test.ts index 1e38079e6..80d8f67a6 100644 --- a/src/__tests__/volume/readdirSync.test.ts +++ b/src/__tests__/volume/readdirSync.test.ts @@ -66,9 +66,9 @@ describe('readdirSync()', () => { return { ...dirent }; }); expect(mapped).toEqual([ - { mode: 33206, name: 'af', path: '/x' }, - { mode: 16895, name: 'b', path: '/x' }, - { mode: 16895, name: 'c', path: '/x' }, + { mode: 33206, name: 'af', path: '/x', parentPath: '/x' }, + { mode: 16895, name: 'b', path: '/x', parentPath: '/x' }, + { mode: 16895, name: 'c', path: '/x', parentPath: '/x' }, ]); }); @@ -104,16 +104,16 @@ describe('readdirSync()', () => { }) .sort((a, b) => a.path.localeCompare(b.path)); expect(mapped).toEqual([ - { mode: 33206, name: 'af1', path: '/z' }, - { mode: 33206, name: 'af2', path: '/z' }, - { mode: 16895, name: 'b', path: '/z' }, - { mode: 16895, name: 'c', path: '/z' }, - { mode: 33206, name: 'bf1', path: '/z/b' }, - { mode: 33206, name: 'bf2', path: '/z/b' }, - { mode: 16895, name: 'c', path: '/z/c' }, - { mode: 33206, name: '.cf0', path: '/z/c/c' }, - { mode: 33206, name: 'cf1', path: '/z/c/c' }, - { mode: 33206, name: 'cf2', path: '/z/c/c' }, + { mode: 33206, name: 'af1', path: '/z', parentPath: '/z' }, + { mode: 33206, name: 'af2', path: '/z', parentPath: '/z' }, + { mode: 16895, name: 'b', path: '/z', parentPath: '/z' }, + { mode: 16895, name: 'c', path: '/z', parentPath: '/z' }, + { mode: 33206, name: 'bf1', path: '/z/b', parentPath: '/z/b' }, + { mode: 33206, name: 'bf2', path: '/z/b', parentPath: '/z/b' }, + { mode: 16895, name: 'c', path: '/z/c', parentPath: '/z/c' }, + { mode: 33206, name: '.cf0', path: '/z/c/c', parentPath: '/z/c/c' }, + { mode: 33206, name: 'cf1', path: '/z/c/c', parentPath: '/z/c/c' }, + { mode: 33206, name: 'cf2', path: '/z/c/c', parentPath: '/z/c/c' }, ]); }); diff --git a/src/volume.ts b/src/volume.ts index 7aa272be5..b79fa1d13 100644 --- a/src/volume.ts +++ b/src/volume.ts @@ -1607,7 +1607,7 @@ export class Volume implements FsCallbackApi, FsSynchronousApi { return list.map(dirent => { if (options.recursive) { - let fullPath = pathModule.join(dirent.path, dirent.name.toString()); + let fullPath = pathModule.join(dirent.parentPath, dirent.name.toString()); if (isWin) { fullPath = fullPath.replace(/\\/g, '/'); }