Skip to content

Commit

Permalink
Add check to ensure cwd is a directory (#110)
Browse files Browse the repository at this point in the history
Fixes #105
  • Loading branch information
medusalix authored and sindresorhus committed Mar 3, 2019
1 parent 9a9cf1e commit 59f4b48
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
const fs = require('fs');
const arrayUnion = require('array-union');
const glob = require('glob');
const fastGlob = require('fast-glob');
Expand All @@ -15,9 +16,16 @@ const assertPatternsInput = patterns => {
}
};

const checkCwdOption = options => {
if (options && options.cwd && !fs.statSync(options.cwd).isDirectory()) {
throw new Error('The `cwd` option must be a path to a directory');
}
};

const generateGlobTasks = (patterns, taskOptions) => {
patterns = arrayUnion([].concat(patterns));
assertPatternsInput(patterns);
checkCwdOption(taskOptions);

const globTasks = [];

Expand Down
24 changes: 16 additions & 8 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,20 +243,28 @@ test.failing('`{extension: false}` and `expandDirectories.extensions` option', t
);
});

// https://github.com/sindresorhus/globby/issues/105
test.failing('throws ENOTDIR when specifying a file as cwd - async', async t => {
test('throws when specifying a file as cwd - async', async t => {
const isFile = path.resolve('fixtures/gitignore/bar.js');
await t.throwsAsync(globby('.', {cwd: isFile}), {code: 'ENOTDIR'});
await t.throwsAsync(globby('*', {cwd: isFile}), {code: 'ENOTDIR'});

await t.throwsAsync(
globby('.', {cwd: isFile}),
'The `cwd` option must be a path to a directory'
);

await t.throwsAsync(
globby('*', {cwd: isFile}),
'The `cwd` option must be a path to a directory'
);
});

// https://github.com/sindresorhus/globby/issues/105
test.failing('throws ENOTDIR when specifying a file as cwd - sync', t => {
test('throws when specifying a file as cwd - sync', t => {
const isFile = path.resolve('fixtures/gitignore/bar.js');

t.throws(() => {
globby.sync('.', {cwd: isFile});
}, {code: 'ENOTDIR'});
}, 'The `cwd` option must be a path to a directory');

t.throws(() => {
globby.sync('*', {cwd: isFile});
}, {code: 'ENOTDIR'});
}, 'The `cwd` option must be a path to a directory');
});

0 comments on commit 59f4b48

Please sign in to comment.