Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check to ensure cwd is a directory #110

Merged
merged 3 commits into from
Mar 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});