Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error when reading inaccessible directories with gitignore: true and suppressErrors: true #246

Merged
merged 7 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ node_modules
yarn.lock
bench
*.tmp
tmp
tmp/*
!tmp/notignored
9 changes: 5 additions & 4 deletions ignore.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ const getIsIgnoredPredicate = (files, cwd) => {

const normalizeOptions = (options = {}) => ({
cwd: toPath(options.cwd) || process.cwd(),
suppressErrors: Boolean(options.suppressErrors),
});

export const isIgnoredByIgnoreFiles = async (patterns, options) => {
const {cwd} = normalizeOptions(options);
const {cwd, suppressErrors} = normalizeOptions(options);

const paths = await fastGlob(patterns, {cwd, ...ignoreFilesGlobOptions});
const paths = await fastGlob(patterns, {cwd, suppressErrors, ...ignoreFilesGlobOptions});

const files = await Promise.all(
paths.map(async filePath => ({
Expand All @@ -76,9 +77,9 @@ export const isIgnoredByIgnoreFiles = async (patterns, options) => {
};

export const isIgnoredByIgnoreFilesSync = (patterns, options) => {
const {cwd} = normalizeOptions(options);
const {cwd, suppressErrors} = normalizeOptions(options);

const paths = fastGlob.sync(patterns, {cwd, ...ignoreFilesGlobOptions});
const paths = fastGlob.sync(patterns, {cwd, suppressErrors, ...ignoreFilesGlobOptions});

const files = paths.map(filePath => ({
filePath,
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ const getIgnoreFilesPatterns = options => {
const getFilter = async options => {
const ignoreFilesPatterns = getIgnoreFilesPatterns(options);
return createFilterFunction(
ignoreFilesPatterns.length > 0 && await isIgnoredByIgnoreFiles(ignoreFilesPatterns, {cwd: options.cwd}),
ignoreFilesPatterns.length > 0 && await isIgnoredByIgnoreFiles(ignoreFilesPatterns, options),
);
};

const getFilterSync = options => {
const ignoreFilesPatterns = getIgnoreFilesPatterns(options);
return createFilterFunction(
ignoreFilesPatterns.length > 0 && isIgnoredByIgnoreFilesSync(ignoreFilesPatterns, {cwd: options.cwd}),
ignoreFilesPatterns.length > 0 && isIgnoredByIgnoreFilesSync(ignoreFilesPatterns, options),
);
};

Expand Down
23 changes: 20 additions & 3 deletions tests/globby.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ test('expose isDynamicPattern', t => {
}
});

test('expandDirectories option', async t => {
test.serial('expandDirectories option', async t => {
t.deepEqual(await runGlobby(t, temporary), ['tmp/a.tmp', 'tmp/b.tmp', 'tmp/c.tmp', 'tmp/d.tmp', 'tmp/e.tmp']);
for (const temporaryDirectory of getPathValues(temporary)) {
// eslint-disable-next-line no-await-in-loop
Expand All @@ -168,11 +168,11 @@ test('expandDirectories option', async t => {
}), ['tmp/a.tmp']);
});

test('expandDirectories:true and onlyFiles:true option', async t => {
test.serial('expandDirectories:true and onlyFiles:true option', async t => {
t.deepEqual(await runGlobby(t, temporary, {onlyFiles: true}), ['tmp/a.tmp', 'tmp/b.tmp', 'tmp/c.tmp', 'tmp/d.tmp', 'tmp/e.tmp']);
});

test.failing('expandDirectories:true and onlyFiles:false option', async t => {
test.serial.failing('expandDirectories:true and onlyFiles:false option', async t => {
// Node-glob('tmp/**') => ['tmp', 'tmp/a.tmp', 'tmp/b.tmp', 'tmp/c.tmp', 'tmp/d.tmp', 'tmp/e.tmp']
// Fast-glob('tmp/**') => ['tmp/a.tmp', 'tmp/b.tmp', 'tmp/c.tmp', 'tmp/d.tmp', 'tmp/e.tmp']
// See https://github.com/mrmlnc/fast-glob/issues/47
Expand Down Expand Up @@ -261,6 +261,23 @@ test('gitignore option and objectMode option', async t => {
t.truthy(result[0].path);
});

test.serial('gitignore option and suppressErrors option', async t => {
const fooPath = path.join(temporary, 'foo.tmp');
const notignoredPath = path.join(temporary, 'notignored');
try {
fs.mkdirSync(fooPath);
fs.chmodSync(fooPath, 0o000);
fs.writeFileSync(notignoredPath, 'notignored', 'utf8');
const result = await runGlobby(t, temporary, {gitignore: true, suppressErrors: true});
t.is(result.length, 1);
t.truthy(result.includes('tmp/notignored'));
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
} finally {
fs.chmodSync(fooPath, 0o777);
fs.rmdirSync(fooPath);
fs.unlinkSync(notignoredPath);
}
});

test('respects ignoreFiles string option', async t => {
const actual = await runGlobby(t, '*', {gitignore: false, ignoreFiles: '.gitignore', onlyFiles: false});
t.false(actual.includes('node_modules'));
Expand Down