Skip to content

Commit

Permalink
Raise error if cwd is not a directory
Browse files Browse the repository at this point in the history
Fix #235
  • Loading branch information
isaacs committed Feb 10, 2016
1 parent 3bd419c commit 0ffab1f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
4 changes: 2 additions & 2 deletions common.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ function setopts (self, pattern, options) {
if (!ownProp(options, "cwd"))
self.cwd = cwd
else {
self.cwd = options.cwd
self.changedCwd = path.resolve(options.cwd) !== cwd
self.cwd = path.resolve(options.cwd)
self.changedCwd = self.cwd !== cwd
}

self.root = options.root || path.resolve(self.cwd, "/")
Expand Down
7 changes: 7 additions & 0 deletions glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,13 @@ Glob.prototype._readdirError = function (f, er, cb) {
case 'ENOTSUP': // https://github.com/isaacs/node-glob/issues/205
case 'ENOTDIR': // totally normal. means it *does* exist.
this.cache[this._makeAbs(f)] = 'FILE'
if (f === this.cwd) {
var error = new Error(er.code + ' invalid cwd ' + f)
error.path = f
error.code = er.code
this.emit('error', error)
this.abort()
}
break

case 'ENOENT': // not terribly unusual
Expand Down
6 changes: 6 additions & 0 deletions sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,12 @@ GlobSync.prototype._readdirError = function (f, er) {
case 'ENOTSUP': // https://github.com/isaacs/node-glob/issues/205
case 'ENOTDIR': // totally normal. means it *does* exist.
this.cache[this._makeAbs(f)] = 'FILE'
if (f === this.cwd) {
var error = new Error(er.code + ' invalid cwd ' + f)
error.path = f
error.code = er.code
throw error
}
break

case 'ENOENT': // not terribly unusual
Expand Down
24 changes: 21 additions & 3 deletions test/cwd-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var origCwd = process.cwd()
process.chdir(__dirname + '/fixtures')
var path = require('path')
var isAbsolute = require('path-is-absolute')
var glob = require('../')

function cacheCheck(g, t) {
// verify that path cache keys are all absolute
Expand All @@ -17,7 +18,6 @@ function cacheCheck(g, t) {
}

tap.test("changing cwd and searching for **/d", function (t) {
var glob = require('../')
t.test('.', function (t) {
var g = glob('**/d', function (er, matches) {
t.ifError(er)
Expand Down Expand Up @@ -63,10 +63,28 @@ tap.test("changing cwd and searching for **/d", function (t) {
})
})

t.test('cd -', function (t) {
process.chdir(origCwd)
t.end()
})

tap.test('non-dir cwd should raise error', function (t) {
var notdir = 'a/b/c/d'
var abs = path.resolve(notdir)
var expect = new Error('ENOTDIR invalid cwd ' + abs)
expect.code = 'ENOTDIR'
expect.path = notdir
expect.stack = undefined
var msg = 'raise error when cwd is not a dir'

t.throws(function () {
glob.sync('*', { cwd: notdir })
}, expect)
glob('*', { cwd: notdir }, function (er, results) {
t.match(er, expect)
t.end()
})
})

tap.test('cd -', function (t) {
process.chdir(origCwd)
t.end()
})

0 comments on commit 0ffab1f

Please sign in to comment.