Skip to content

Commit 59f4b48

Browse files
medusalixsindresorhus
authored andcommitted
Add check to ensure cwd is a directory (#110)
Fixes #105
1 parent 9a9cf1e commit 59f4b48

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

index.js

+8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
const fs = require('fs');
23
const arrayUnion = require('array-union');
34
const glob = require('glob');
45
const fastGlob = require('fast-glob');
@@ -15,9 +16,16 @@ const assertPatternsInput = patterns => {
1516
}
1617
};
1718

19+
const checkCwdOption = options => {
20+
if (options && options.cwd && !fs.statSync(options.cwd).isDirectory()) {
21+
throw new Error('The `cwd` option must be a path to a directory');
22+
}
23+
};
24+
1825
const generateGlobTasks = (patterns, taskOptions) => {
1926
patterns = arrayUnion([].concat(patterns));
2027
assertPatternsInput(patterns);
28+
checkCwdOption(taskOptions);
2129

2230
const globTasks = [];
2331

test.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -243,20 +243,28 @@ test.failing('`{extension: false}` and `expandDirectories.extensions` option', t
243243
);
244244
});
245245

246-
// https://github.com/sindresorhus/globby/issues/105
247-
test.failing('throws ENOTDIR when specifying a file as cwd - async', async t => {
246+
test('throws when specifying a file as cwd - async', async t => {
248247
const isFile = path.resolve('fixtures/gitignore/bar.js');
249-
await t.throwsAsync(globby('.', {cwd: isFile}), {code: 'ENOTDIR'});
250-
await t.throwsAsync(globby('*', {cwd: isFile}), {code: 'ENOTDIR'});
248+
249+
await t.throwsAsync(
250+
globby('.', {cwd: isFile}),
251+
'The `cwd` option must be a path to a directory'
252+
);
253+
254+
await t.throwsAsync(
255+
globby('*', {cwd: isFile}),
256+
'The `cwd` option must be a path to a directory'
257+
);
251258
});
252259

253-
// https://github.com/sindresorhus/globby/issues/105
254-
test.failing('throws ENOTDIR when specifying a file as cwd - sync', t => {
260+
test('throws when specifying a file as cwd - sync', t => {
255261
const isFile = path.resolve('fixtures/gitignore/bar.js');
262+
256263
t.throws(() => {
257264
globby.sync('.', {cwd: isFile});
258-
}, {code: 'ENOTDIR'});
265+
}, 'The `cwd` option must be a path to a directory');
266+
259267
t.throws(() => {
260268
globby.sync('*', {cwd: isFile});
261-
}, {code: 'ENOTDIR'});
269+
}, 'The `cwd` option must be a path to a directory');
262270
});

0 commit comments

Comments
 (0)