From 59f4b48e92445d530bbf43a39842314b3dd6e42e Mon Sep 17 00:00:00 2001 From: Medusalix <8124898+medusalix@users.noreply.github.com> Date: Sun, 3 Mar 2019 14:15:58 +0100 Subject: [PATCH] Add check to ensure `cwd` is a directory (#110) Fixes #105 --- index.js | 8 ++++++++ test.js | 24 ++++++++++++++++-------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 8cf5cf8..08c3150 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ 'use strict'; +const fs = require('fs'); const arrayUnion = require('array-union'); const glob = require('glob'); const fastGlob = require('fast-glob'); @@ -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 = []; diff --git a/test.js b/test.js index 8c0a718..53acc35 100644 --- a/test.js +++ b/test.js @@ -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'); });