Skip to content

Commit

Permalink
Correct slash behavior for dirs passed as pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Dec 19, 2014
1 parent 71020a1 commit 67cba0a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,10 +569,13 @@ Glob.prototype._processSimple2 = function (prefix, index, er, exists, cb) {
return cb()

if (prefix && isAbsolute(prefix) && !this.nomount) {
var trail = /[\/\\]$/.test(prefix)
if (prefix.charAt(0) === "/") {
prefix = path.join(this.root, prefix)
} else {
prefix = path.resolve(this.root, prefix)
if (trail)
prefix += '/'
}
}

Expand Down
3 changes: 3 additions & 0 deletions sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,13 @@ GlobSync.prototype._processSimple = function (prefix, index) {
return

if (prefix && isAbsolute(prefix) && !this.nomount) {
var trail = /[\/\\]$/.test(prefix)
if (prefix.charAt(0) === "/") {
prefix = path.join(this.root, prefix)
} else {
prefix = path.resolve(this.root, prefix)
if (trail)
prefix += '/'
}
}

Expand Down
17 changes: 17 additions & 0 deletions test/mark-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,20 @@ test("mark=false, / on pattern", function (t) {
t.same(results, expect)
t.end()
})

var cwd = process.cwd().replace(/[\/\\]+$/, '').replace(/\\/g, '/')
;[true,false].forEach(function (mark) {
;[true,false].forEach(function (slash) {
test("cwd mark:" + mark + " slash:" + slash, function (t) {
var pattern = cwd + (slash ? '/' : '')
var results = glob.sync(pattern, {mark:mark})
t.equal(results.length, 1)
var res = results[0].replace(/\\/g, '/')
if (slash || mark)
t.equal(res, cwd + '/')
else
t.equal(res.indexOf(cwd), 0)
t.end()
})
})
})
18 changes: 18 additions & 0 deletions test/mark.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,21 @@ test("mark=false, / on pattern", function (t) {
t.similar(m, /\/$/)
})
})

var cwd = process.cwd().replace(/[\/\\]+$/, '').replace(/\\/g, '/')
;[true,false].forEach(function (mark) {
;[true,false].forEach(function (slash) {
test("cwd mark:" + mark + " slash:" + slash, function (t) {
var pattern = cwd + (slash ? '/' : '')
glob(pattern, {mark:mark}, function (er, results) {
t.equal(results.length, 1)
var res = results[0].replace(/\\/g, '/')
if (slash || mark)
t.equal(res, cwd + '/')
else
t.equal(res.indexOf(cwd), 0)
t.end()
})
})
})
})

0 comments on commit 67cba0a

Please sign in to comment.