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 1 commit
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
Next Next commit
Add check to ensure cwd is a directory
  • Loading branch information
medusalix committed Feb 25, 2019
commit db1eaf621b5b184396d1806203f50df9c6864cc1
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('Option `cwd` must be a path to a directory');
}
};

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

const globTasks = [];

Expand Down
20 changes: 12 additions & 8 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,20 +243,24 @@ 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}),
'Option `cwd` must be a path to a directory'
);
await t.throwsAsync(
globby('*', {cwd: isFile}),
'Option `cwd` 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'});
}, 'Option `cwd` must be a path to a directory');
t.throws(() => {
globby.sync('*', {cwd: isFile});
}, {code: 'ENOTDIR'});
}, 'Option `cwd` must be a path to a directory');
});