Skip to content

Commit

Permalink
fix: readDir will check if passed path is a directory (#464)
Browse files Browse the repository at this point in the history
Closes: #462
  • Loading branch information
piotr-oles committed Jun 24, 2020
1 parent 5b59691 commit ad0eac9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/typescript-reporter/file-system/PassiveFileSystem.ts
Expand Up @@ -24,15 +24,19 @@ function createPassiveFileSystem(caseSensitive = false, realFileSystem: FileSyst
}

function memReadFile(path: string, encoding?: string): string | undefined {
if (memExists(path)) {
const stats = memReadStats(path);

if (stats && stats.isFile()) {
return mem
.readFileSync(normalizePath(path), { encoding: encoding as BufferEncoding })
.toString();
}
}

function memReadDir(path: string): Dirent[] {
if (memExists(path)) {
const stats = memReadStats(path);

if (stats && stats.isDirectory()) {
return mem.readdirSync(normalizePath(path), { withFileTypes: true }) as Dirent[];
}

Expand Down
8 changes: 6 additions & 2 deletions src/typescript-reporter/file-system/RealFileSystem.ts
Expand Up @@ -48,7 +48,9 @@ function createRealFileSystem(caseSensitive = false): FileSystem {
const normalizedPath = normalizePath(path);

if (!readFileCache.has(normalizedPath)) {
if (exists(normalizedPath)) {
const stats = readStats(normalizedPath);

if (stats && stats.isFile()) {
readFileCache.set(normalizedPath, fs.readFileSync(normalizedPath, { encoding }).toString());
} else {
readFileCache.set(normalizedPath, undefined);
Expand All @@ -62,7 +64,9 @@ function createRealFileSystem(caseSensitive = false): FileSystem {
const normalizedPath = normalizePath(path);

if (!readDirCache.has(normalizedPath)) {
if (exists(normalizedPath)) {
const stats = readStats(normalizedPath);

if (stats && stats.isDirectory()) {
readDirCache.set(normalizedPath, fs.readdirSync(normalizedPath, { withFileTypes: true }));
} else {
readDirCache.set(normalizedPath, []);
Expand Down

0 comments on commit ad0eac9

Please sign in to comment.