Skip to content

Commit ad0eac9

Browse files
authored
fix: readDir will check if passed path is a directory (#464)
Closes: #462
1 parent 5b59691 commit ad0eac9

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/typescript-reporter/file-system/PassiveFileSystem.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,19 @@ function createPassiveFileSystem(caseSensitive = false, realFileSystem: FileSyst
2424
}
2525

2626
function memReadFile(path: string, encoding?: string): string | undefined {
27-
if (memExists(path)) {
27+
const stats = memReadStats(path);
28+
29+
if (stats && stats.isFile()) {
2830
return mem
2931
.readFileSync(normalizePath(path), { encoding: encoding as BufferEncoding })
3032
.toString();
3133
}
3234
}
3335

3436
function memReadDir(path: string): Dirent[] {
35-
if (memExists(path)) {
37+
const stats = memReadStats(path);
38+
39+
if (stats && stats.isDirectory()) {
3640
return mem.readdirSync(normalizePath(path), { withFileTypes: true }) as Dirent[];
3741
}
3842

src/typescript-reporter/file-system/RealFileSystem.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ function createRealFileSystem(caseSensitive = false): FileSystem {
4848
const normalizedPath = normalizePath(path);
4949

5050
if (!readFileCache.has(normalizedPath)) {
51-
if (exists(normalizedPath)) {
51+
const stats = readStats(normalizedPath);
52+
53+
if (stats && stats.isFile()) {
5254
readFileCache.set(normalizedPath, fs.readFileSync(normalizedPath, { encoding }).toString());
5355
} else {
5456
readFileCache.set(normalizedPath, undefined);
@@ -62,7 +64,9 @@ function createRealFileSystem(caseSensitive = false): FileSystem {
6264
const normalizedPath = normalizePath(path);
6365

6466
if (!readDirCache.has(normalizedPath)) {
65-
if (exists(normalizedPath)) {
67+
const stats = readStats(normalizedPath);
68+
69+
if (stats && stats.isDirectory()) {
6670
readDirCache.set(normalizedPath, fs.readdirSync(normalizedPath, { withFileTypes: true }));
6771
} else {
6872
readDirCache.set(normalizedPath, []);

0 commit comments

Comments
 (0)