From 3a28601f4c2ee0cb1cea6859c0ff8c6c878a5032 Mon Sep 17 00:00:00 2001 From: Nicolas Goudry Date: Sun, 11 Feb 2024 10:46:11 +0100 Subject: [PATCH] Fix read permission error on ignore files search (#259) --- fixtures/bad-permissions/noread/unreadable.js | 0 ignore.js | 21 ++++++++++--------- tests/ignore.js | 19 +++++++++++++++++ 3 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 fixtures/bad-permissions/noread/unreadable.js diff --git a/fixtures/bad-permissions/noread/unreadable.js b/fixtures/bad-permissions/noread/unreadable.js new file mode 100644 index 0000000..e69de29 diff --git a/ignore.js b/ignore.js index 5d9c5e2..6d54c64 100644 --- a/ignore.js +++ b/ignore.js @@ -8,13 +8,13 @@ import slash from 'slash'; import {toPath} from 'unicorn-magic'; import {isNegativePattern} from './utilities.js'; +const defaultIgnoredDirectories = [ + '**/node_modules', + '**/flow-typed', + '**/coverage', + '**/.git', +]; const ignoreFilesGlobOptions = { - ignore: [ - '**/node_modules', - '**/flow-typed', - '**/coverage', - '**/.git', - ], absolute: true, dot: true, }; @@ -62,12 +62,13 @@ const normalizeOptions = (options = {}) => ({ cwd: toPath(options.cwd) ?? process.cwd(), suppressErrors: Boolean(options.suppressErrors), deep: typeof options.deep === 'number' ? options.deep : Number.POSITIVE_INFINITY, + ignore: [...options.ignore ?? [], ...defaultIgnoredDirectories], }); export const isIgnoredByIgnoreFiles = async (patterns, options) => { - const {cwd, suppressErrors, deep} = normalizeOptions(options); + const {cwd, suppressErrors, deep, ignore} = normalizeOptions(options); - const paths = await fastGlob(patterns, {cwd, suppressErrors, deep, ...ignoreFilesGlobOptions}); + const paths = await fastGlob(patterns, {cwd, suppressErrors, deep, ignore, ...ignoreFilesGlobOptions}); const files = await Promise.all( paths.map(async filePath => ({ @@ -80,9 +81,9 @@ export const isIgnoredByIgnoreFiles = async (patterns, options) => { }; export const isIgnoredByIgnoreFilesSync = (patterns, options) => { - const {cwd, suppressErrors, deep} = normalizeOptions(options); + const {cwd, suppressErrors, deep, ignore} = normalizeOptions(options); - const paths = fastGlob.sync(patterns, {cwd, suppressErrors, deep, ...ignoreFilesGlobOptions}); + const paths = fastGlob.sync(patterns, {cwd, suppressErrors, deep, ignore, ...ignoreFilesGlobOptions}); const files = paths.map(filePath => ({ filePath, diff --git a/tests/ignore.js b/tests/ignore.js index a8546eb..798bab2 100644 --- a/tests/ignore.js +++ b/tests/ignore.js @@ -1,3 +1,4 @@ +import {chmod} from 'node:fs/promises'; import path from 'node:path'; import test from 'ava'; import slash from 'slash'; @@ -186,3 +187,21 @@ test('custom ignore files', async t => { ], ); }); + +test.serial('bad permissions', async t => { + const cwd = path.join(PROJECT_ROOT, 'fixtures/bad-permissions'); + const noReadDirectory = path.join(cwd, 'noread'); + + await chmod(noReadDirectory, 0o000); + + await t.notThrowsAsync( + runIsIgnoredByIgnoreFiles( + t, + '**/*', + {cwd, ignore: ['noread']}, + () => {}, + ), + ); + + t.teardown(() => chmod(noReadDirectory, 0o755)); +});