Skip to content

Commit

Permalink
fix(find-all): prevent errors for inaccessible or deleted directories (
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikg committed Apr 4, 2022
1 parent dc47d96 commit e1e9b28
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 10 deletions.
20 changes: 14 additions & 6 deletions src/find-all.ts
Expand Up @@ -22,13 +22,21 @@ async function* findTSConfig(
visited: Set<string> = new Set<string>()
): AsyncGenerator<string> {
if (!visited.has(dir)) {
const dirents = await fs.readdir(dir, { withFileTypes: true });
for (const dirent of dirents) {
if (dirent.isDirectory() && (!options?.skip || !options.skip(dirent.name))) {
yield* findTSConfig(path.resolve(dir, dirent.name), options, visited);
} else if (dirent.isFile() && dirent.name === 'tsconfig.json') {
yield path.resolve(dir, dirent.name);
visited.add(dir);
try {
const dirents = await fs.readdir(dir, { withFileTypes: true });
for (const dirent of dirents) {
if (dirent.isDirectory() && (!options?.skip || !options.skip(dirent.name))) {
yield* findTSConfig(path.resolve(dir, dirent.name), options, visited);
} else if (dirent.isFile() && dirent.name === 'tsconfig.json') {
yield path.resolve(dir, dirent.name);
}
}
} catch (e) {
if (e.code === 'EACCES' || e.code === 'ENOENT') {
return; // directory inaccessible or deleted
}
throw e;
}
}
}
Expand Down
34 changes: 30 additions & 4 deletions tests/find-all.ts
Expand Up @@ -2,6 +2,7 @@ import { suite } from 'uvu';
import * as assert from 'uvu/assert';
import path from 'path';
import { findAll } from '../src/find-all.js';
import * as fs from 'fs';
import glob from 'tiny-glob';
const test = suite('findAll');

Expand Down Expand Up @@ -52,13 +53,14 @@ test('should find tsconfig in child directory', async () => {
});

test('should find multiple tsconfig in child directories', async () => {
const expected = (await glob('tests/fixtures/**/tsconfig.json')).map((file) =>
const expected = (await glob('tests/fixtures/find-all/multiple/**/tsconfig.json')).map((file) =>
path.resolve(file)
);
expected.sort();
const found = await findAll(path.join('tests', 'fixtures'));

const found = await findAll(path.join('tests', 'fixtures', 'find-all', 'multiple'));
found.sort();
assert.equal(found, expected, 'found all tsconfig in test/fixtures');
assert.equal(found, expected, 'found all tsconfig in test/fixtures/find-all/multiple');
});

test('should handle directories with recursive symlinks', async () => {
Expand All @@ -71,7 +73,6 @@ test('should handle directories with recursive symlinks', async () => {
found.sort();
assert.equal(found, expected, 'found all tsconfig in test/fixtures/find-all/recursive-symlink');
});
test.run();

test('should exclude skipped directories', async () => {
const expected = [
Expand All @@ -88,4 +89,29 @@ test('should exclude skipped directories', async () => {
'found filtered tsconfig in test/fixtures/find-all/recursive-symlink'
);
});

test('should handle directories with inaccessible children', async () => {
const inaccessible = path.resolve(
'tests',
'fixtures',
'find-all',
'inaccessible-dir',
'_inaccessible'
);
try {
if (fs.existsSync(inaccessible)) {
fs.chmodSync(inaccessible, 0o000);
}
} catch (e) {
assert.unreachable(`failed to set inaccessible-child permissions: ${e}`);
}
const expected = [
path.resolve('tests', 'fixtures', 'find-all', 'inaccessible-dir', 'tsconfig.json')
];
expected.sort();
const found = await findAll(path.join('tests', 'fixtures', 'find-all', 'inaccessible-dir'));
found.sort();
assert.equal(found, expected, 'found all tsconfig in test/fixtures/find-all/inaccessible-dir');
});

test.run();
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.

0 comments on commit e1e9b28

Please sign in to comment.