Skip to content

Commit

Permalink
Fix error when reading inaccessible directories with `gitignore: true…
Browse files Browse the repository at this point in the history
…` and `suppressErrors: true` (#246)
  • Loading branch information
fwouts authored Apr 11, 2023
1 parent 55a3c64 commit e95da57
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"get-stream": "^6.0.1",
"glob-stream": "^7.0.0",
"rimraf": "^3.0.2",
"tempy": "^3.0.0",
"tsd": "^0.19.1",
"typescript": "^4.5.5",
"xo": "^0.47.0"
Expand Down
14 changes: 14 additions & 0 deletions tests/globby.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from 'node:path';
import util from 'node:util';
import test from 'ava';
import getStream from 'get-stream';
import {temporaryDirectory} from 'tempy';
import {
globby,
globbySync,
Expand Down Expand Up @@ -261,6 +262,19 @@ test('gitignore option and objectMode option', async t => {
t.truthy(result[0].path);
});

test('gitignore option and suppressErrors option', async t => {
const temporary = temporaryDirectory();
fs.mkdirSync(path.join(temporary, 'foo'));
fs.writeFileSync(path.join(temporary, '.gitignore'), 'baz', 'utf8');
fs.writeFileSync(path.join(temporary, 'bar'), '', 'utf8');
fs.writeFileSync(path.join(temporary, 'baz'), '', 'utf8');
// Block access to "foo", which should be silently ignored.
fs.chmodSync(path.join(temporary, 'foo'), 0o000);
const result = await runGlobby(t, '**/*', {cwd: temporary, gitignore: true, suppressErrors: true});
t.is(result.length, 1);
t.truthy(result.includes('bar'));
});

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

0 comments on commit e95da57

Please sign in to comment.