Skip to content

Commit 7535e6d

Browse files
committed
Improve error message for invalid cwd option
Fixes #151
1 parent c10f600 commit 7535e6d

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

index.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,16 @@ const checkCwdOption = (cwd, fsImplementation = fs) => {
128128
return;
129129
}
130130

131+
let stats;
131132
try {
132-
if (!fsImplementation.statSync(cwd).isDirectory()) {
133-
throw new Error('The `cwd` option must be a path to a directory');
134-
}
135-
} catch (error) {
136-
if (error.message === 'The `cwd` option must be a path to a directory') {
137-
throw error;
138-
}
133+
stats = fsImplementation.statSync(cwd);
134+
} catch {
135+
// If stat fails (e.g., path doesn't exist), let fast-glob handle it
136+
return;
137+
}
138+
139+
if (!stats.isDirectory()) {
140+
throw new Error(`The \`cwd\` option must be a path to a directory, got: ${cwd}`);
139141
}
140142
};
141143

tests/generate-glob-tasks.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {
1414
isUnique,
1515
} from './utilities.js';
1616

17+
const cwdDirectoryError = {message: /The `cwd` option must be a path to a directory, got:/};
18+
1719
const runGenerateGlobTasks = async (t, patterns, options) => {
1820
const promiseResult = await generateGlobTasks(patterns, options);
1921
const syncResult = generateGlobTasksSync(patterns, options);
@@ -54,12 +56,10 @@ for (const value of invalidPatterns) {
5456
}
5557

5658
test('throws when specifying a file as cwd', async t => {
57-
const error = {message: 'The `cwd` option must be a path to a directory'};
58-
5959
for (const file of getPathValues(path.resolve('fixtures/gitignore/bar.js'))) {
6060
// eslint-disable-next-line no-await-in-loop
61-
await t.throwsAsync(generateGlobTasks('*', {cwd: file}), error);
62-
t.throws(() => generateGlobTasksSync('*', {cwd: file}), error);
61+
await t.throwsAsync(generateGlobTasks('*', {cwd: file}), cwdDirectoryError);
62+
t.throws(() => generateGlobTasksSync('*', {cwd: file}), cwdDirectoryError);
6363
}
6464
});
6565

tests/globby.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import {
2323
const cwd = process.cwd();
2424
const temporary = 'tmp';
2525

26+
const cwdDirectoryError = {message: /The `cwd` option must be a path to a directory, got:/};
27+
2628
const fixture = [
2729
'a.tmp',
2830
'b.tmp',
@@ -704,29 +706,27 @@ test('`{extension: false}` and `expandDirectories.extensions` option', async t =
704706
});
705707

706708
test('throws when specifying a file as cwd', async t => {
707-
const error = {message: 'The `cwd` option must be a path to a directory'};
708-
709709
for (const file of getPathValues(path.resolve('fixtures/gitignore/bar.js'))) {
710710
// eslint-disable-next-line no-await-in-loop
711-
await t.throwsAsync(globby('.', {cwd: file}), error);
711+
await t.throwsAsync(globby('.', {cwd: file}), cwdDirectoryError);
712712
// eslint-disable-next-line no-await-in-loop
713-
await t.throwsAsync(globby('*', {cwd: file}), error);
714-
t.throws(() => globbySync('.', {cwd: file}), error);
715-
t.throws(() => globbySync('*', {cwd: file}), error);
716-
t.throws(() => globbyStream('.', {cwd: file}), error);
717-
t.throws(() => globbyStream('*', {cwd: file}), error);
713+
await t.throwsAsync(globby('*', {cwd: file}), cwdDirectoryError);
714+
t.throws(() => globbySync('.', {cwd: file}), cwdDirectoryError);
715+
t.throws(() => globbySync('*', {cwd: file}), cwdDirectoryError);
716+
t.throws(() => globbyStream('.', {cwd: file}), cwdDirectoryError);
717+
t.throws(() => globbyStream('*', {cwd: file}), cwdDirectoryError);
718718
}
719719
});
720720

721721
test('throws when specifying a file as cwd - isDynamicPattern', t => {
722722
for (const file of getPathValues(path.resolve('fixtures/gitignore/bar.js'))) {
723723
t.throws(() => {
724724
isDynamicPattern('.', {cwd: file});
725-
}, {message: 'The `cwd` option must be a path to a directory'});
725+
}, cwdDirectoryError);
726726

727727
t.throws(() => {
728728
isDynamicPattern('*', {cwd: file});
729-
}, {message: 'The `cwd` option must be a path to a directory'});
729+
}, cwdDirectoryError);
730730
}
731731
});
732732

0 commit comments

Comments
 (0)