Skip to content

Commit

Permalink
fix: don't include filename in path when calling readdir with `wi…
Browse files Browse the repository at this point in the history
…thFileTypes: true` (#1024)

* fix: readdir withFileType path bug

* fix: readdir(recursive) normalize to unix filepath sep. in filenames

* fix: fullPath normalization inplace

* fix: formatting

* fix: remove debugging console statement
  • Loading branch information
jonsch318 committed Apr 14, 2024
1 parent 2612163 commit 711c4bd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/Dirent.ts
Expand Up @@ -15,7 +15,7 @@ export class Dirent implements IDirent {

dirent.name = strToEncoding(link.getName(), encoding);
dirent.mode = mode;
dirent.path = link.getPath();
dirent.path = link.getParentPath();

return dirent;
}
Expand Down
24 changes: 12 additions & 12 deletions src/__tests__/volume/readdirSync.test.ts
Expand Up @@ -67,9 +67,9 @@ describe('readdirSync()', () => {
return { ...dirent };
});
expect(mapped).toEqual([
{ mode: 33206, name: 'af', path: '/x/af' },
{ mode: 16895, name: 'b', path: '/x/b' },
{ mode: 16895, name: 'c', path: '/x/c' },
{ mode: 33206, name: 'af', path: '/x' },
{ mode: 16895, name: 'b', path: '/x' },
{ mode: 16895, name: 'c', path: '/x' },
]);
});

Expand Down Expand Up @@ -105,16 +105,16 @@ describe('readdirSync()', () => {
})
.sort((a, b) => a.path.localeCompare(b.path));
expect(mapped).toEqual([
{ mode: 33206, name: 'af1', path: '/z/af1' },
{ mode: 33206, name: 'af2', path: '/z/af2' },
{ mode: 16895, name: 'b', path: '/z/b' },
{ mode: 33206, name: 'bf1', path: '/z/b/bf1' },
{ mode: 33206, name: 'bf2', path: '/z/b/bf2' },
{ 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: 16895, name: 'c', path: '/z/c/c' },
{ mode: 33206, name: '.cf0', path: '/z/c/c/.cf0' },
{ mode: 33206, name: 'cf1', path: '/z/c/c/cf1' },
{ mode: 33206, name: 'cf2', path: '/z/c/c/cf2' },
{ mode: 33206, name: '.cf0', path: '/z/c/c' },
{ mode: 33206, name: 'cf1', path: '/z/c/c' },
{ mode: 33206, name: 'cf2', path: '/z/c/c' },
]);
});
});
4 changes: 4 additions & 0 deletions src/node.ts
Expand Up @@ -409,6 +409,10 @@ export class Link extends EventEmitter {
return this.steps.join(SEP);
}

getParentPath(): string {
return this.steps.slice(0, -1).join(SEP);
}

getName(): string {
return this.steps[this.steps.length - 1];
}
Expand Down
6 changes: 5 additions & 1 deletion src/volume.ts
Expand Up @@ -1496,7 +1496,11 @@ export class Volume implements FsCallbackApi, FsSynchronousApi {

return list.map(dirent => {
if (options.recursive) {
return dirent.path.replace(filename2 + pathModule.posix.sep, '');
let fullPath = pathModule.join(dirent.path, dirent.name.toString());
if (isWin) {
fullPath = fullPath.replace(/\\/g, '/');
}
return fullPath.replace(filename2 + pathModule.posix.sep, '');
}
return dirent.name;
});
Expand Down

0 comments on commit 711c4bd

Please sign in to comment.