Skip to content

Commit

Permalink
Properly exclude files from /-ending patterns
Browse files Browse the repository at this point in the history
When the cwd was set, it'd change the absolute path to always be
slash-free, so filtering based on `/` at the end of a pattern wasn't
working.

Fix #158
  • Loading branch information
isaacs committed Mar 6, 2015
1 parent ba29ec3 commit 0b729c8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
25 changes: 20 additions & 5 deletions glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@ Glob.prototype._processSimple2 = function (prefix, index, er, exists, cb) {
// Returns either 'DIR', 'FILE', or false
Glob.prototype._stat = function (f, cb) {
var abs = this._makeAbs(f)
var needDir = f.slice(-1) === '/'

if (f.length > this.maxLength)
return cb()
Expand All @@ -669,20 +670,29 @@ Glob.prototype._stat = function (f, cb) {
if (Array.isArray(c))
c = 'DIR'

// It exists, but not how we need it
if (abs.slice(-1) === '/' && c !== 'DIR')
// It exists, but maybe not how we need it
if (!needDir || c === 'DIR')
return cb(null, c)

if (needDir && c === 'FILE')
return cb()

return cb(null, c)
// otherwise we have to stat, because maybe c=true
// if we know it exists, but not what it is.
}

var exists
var stat = this.statCache[abs]
if (stat !== undefined) {
if (stat === false)
return cb(null, stat)
else
return cb(null, stat.isDirectory() ? 'DIR' : 'FILE', stat)
else {
var type = stat.isDirectory() ? 'DIR' : 'FILE'
if (needDir && type === 'FILE')
return cb()
else
return cb(null, type, stat)
}
}

var self = this
Expand Down Expand Up @@ -712,12 +722,17 @@ Glob.prototype._stat2 = function (f, abs, er, stat, cb) {
return cb()
}

var needDir = f.slice(-1) === '/'
this.statCache[abs] = stat

if (abs.slice(-1) === '/' && !stat.isDirectory())
return cb(null, false, stat)

var c = stat.isDirectory() ? 'DIR' : 'FILE'
this.cache[abs] = this.cache[abs] || c

if (needDir && c !== 'DIR')
return cb()

return cb(null, c, stat)
}
18 changes: 12 additions & 6 deletions sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ GlobSync.prototype._processSimple = function (prefix, index) {
// Returns either 'DIR', 'FILE', or false
GlobSync.prototype._stat = function (f) {
var abs = this._makeAbs(f)
var needDir = f.slice(-1) === '/'

if (f.length > this.maxLength)
return false
Expand All @@ -398,11 +399,15 @@ GlobSync.prototype._stat = function (f) {
if (Array.isArray(c))
c = 'DIR'

// It exists, but not how we need it
if (abs.slice(-1) === '/' && c !== 'DIR')
// It exists, but maybe not how we need it
if (!needDir || c === 'DIR')
return c

if (needDir && c === 'FILE')
return false

return c
// otherwise we have to stat, because maybe c=true
// if we know it exists, but not what it is.
}

var exists
Expand All @@ -428,11 +433,12 @@ GlobSync.prototype._stat = function (f) {

this.statCache[abs] = stat

if (abs.slice(-1) === '/' && !stat.isDirectory())
return false

var c = stat.isDirectory() ? 'DIR' : 'FILE'
this.cache[abs] = this.cache[abs] || c

if (needDir && c !== 'DIR')
return false

return c
}

Expand Down
1 change: 1 addition & 0 deletions test/bash-results.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
"./test/realpath.js",
"./test/root-nomount.js",
"./test/root.js",
"./test/slash-cwd.js",
"./test/stat.js",
"./test/sync-cb-throw.js",
"./test/zz-cleanup.js",
Expand Down
20 changes: 20 additions & 0 deletions test/slash-cwd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// regression test to make sure that slash-ended patterns
// don't match files when using a different cwd.
var glob = require('../')
var test = require('tap').test
var pattern = '../{*.md,test}/'
var expect = [ '../test/' ]
var cwd = __dirname
var opt = { cwd: cwd }
process.chdir(__dirname + '/..')

test('slashes only match directories', function (t) {
var sync = glob.sync(pattern, { cwd: cwd })
t.same(sync, expect, 'sync test')
glob(pattern, { cwd: cwd }, function (er, async) {
if (er)
throw er
t.same(async, expect, 'async test')
t.end()
})
})

0 comments on commit 0b729c8

Please sign in to comment.