Skip to content

Commit

Permalink
Support using root and absolute options together
Browse files Browse the repository at this point in the history
* Add failing test for root + absolute option
* root fixes and more tests
* cleanup root tests

Fix #294
  • Loading branch information
phated authored and isaacs committed Sep 29, 2016
1 parent 12be3fc commit b2aa29b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
6 changes: 5 additions & 1 deletion common.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ function setopts (self, pattern, options) {
if (process.platform === "win32")
self.root = self.root.replace(/\\/g, "/")

self.cwdAbs = makeAbs(self, self.cwd)
// TODO: is an absolute `cwd` supposed to be resolved against `root`?
// e.g. { cwd: '/test', root: __dirname } === path.join(__dirname, '/test')
self.cwdAbs = isAbsolute(self.cwd) ? self.cwd : makeAbs(self, self.cwd)
if (process.platform === "win32")
self.cwdAbs = self.cwdAbs.replace(/\\/g, "/")
self.nomount = !!options.nomount

// disable comments and negation in Minimatch.
Expand Down
2 changes: 1 addition & 1 deletion glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ Glob.prototype._emitMatch = function (index, e) {
return
}

var abs = this._makeAbs(e)
var abs = isAbsolute(e) ? e : this._makeAbs(e)

if (this.mark)
e = this._mark(e)
Expand Down
33 changes: 30 additions & 3 deletions test/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function cacheCheck(g, t) {
t.test('.', function (t) {
var g = glob('/b*/**', { root: '.' }, function (er, matches) {
t.ifError(er)
t.like(matches, [])
t.same(matches, [])
cacheCheck(g, t)
t.end()
})
Expand All @@ -36,7 +36,7 @@ t.test('a', function (t) {
return path.join(path.resolve('a'), m).replace(/\\/g, '/')
})

t.like(matches, wanted)
t.same(matches, wanted)
cacheCheck(g, t)
t.end()
})
Expand All @@ -45,10 +45,37 @@ t.test('a', function (t) {
t.test('root=a, cwd=a/b', function (t) {
var g = glob('/b*/**', { root: 'a', cwd: path.resolve('a/b') }, function (er, matches) {
t.ifError(er)
t.like(matches, [ '/b', '/b/c', '/b/c/d', '/bc', '/bc/e', '/bc/e/f' ].map(function (m) {
t.same(matches, [ '/b', '/b/c', '/b/c/d', '/bc', '/bc/e', '/bc/e/f' ].map(function (m) {
return path.join(path.resolve('a'), m).replace(/\\/g, '/')
}))
cacheCheck(g, t)
t.end()
})
})

t.test('combined with absolute option', function(t) {
var g = glob('/b*/**', { root: path.resolve('a'), absolute: true }, function (er, matches) {
t.ifError(er)
t.same(matches, [ '/b', '/b/c', '/b/c/d', '/bc', '/bc/e', '/bc/e/f' ].map(function (m) {
return path.join(path.resolve('a'), m).replace(/\\/g, '/')
}))
cacheCheck(g, t)
t.end()
})
})

t.test('cwdAbs when root=a, absolute=true', function(t) {
var g = glob('/b*/**', { root: path.resolve('a'), absolute: true }, function (er, matches) {
t.ifError(er)
t.same(g.cwdAbs, process.cwd().replace(/\\/g, '/'))
t.end()
})
})

t.test('cwdAbs when root=a, absolute=true, cwd=__dirname', function(t) {
var g = glob('/b*/**', { root: path.resolve('a'), absolute: true, cwd: __dirname }, function (er, matches) {
t.ifError(er)
t.same(g.cwdAbs, __dirname.replace(/\\/g, '/'))
t.end()
})
})

0 comments on commit b2aa29b

Please sign in to comment.