Permalink
Browse files

Make absolute work with sync, results, mark:true

This involes a refactor of the GlobSync._emitMatch method, which it turns
out, was not being used at all.

Close #293
  • Loading branch information...
1 parent 74db9ce commit efdfead73ab1c62435ce2c6bea357b3c2ac8cf41 @isaacs committed Sep 20, 2016
Showing with 65 additions and 26 deletions.
  1. +3 −3 README.md
  2. +1 −1 common.js
  3. +10 −7 glob.js
  4. +13 −3 sync.js
  5. +38 −12 test/absolute.js
View
@@ -273,9 +273,9 @@ the filesystem.
In the case of a symlink that cannot be resolved, the full absolute
path to the matched entry is returned (though it will usually be a
broken symlink)
-* `absolute` Set to true to always receive absolute paths for the
- `'match'` event. Will not pass absolute paths to the callback
- or returned from `glob.sync`.
+* `absolute` Set to true to always receive absolute paths for matched
+ files. Unlike `realpath`, this also affects the values returned in
+ the `match` event.
## Comparisons to other fnmatch/glob implementations
View
@@ -80,7 +80,7 @@ function setopts (self, pattern, options) {
self.nocase = !!options.nocase
self.stat = !!options.stat
self.noprocess = !!options.noprocess
- self._absolute = !!options.absolute
+ self.absolute = !!options.absolute
self.maxLength = options.maxLength || Infinity
self.cache = options.cache || Object.create(null)
View
@@ -457,9 +457,6 @@ Glob.prototype._emitMatch = function (index, e) {
if (this.aborted)
return
- if (this.matches[index][e])
- return
-
if (isIgnored(this, e))
return
@@ -470,22 +467,28 @@ Glob.prototype._emitMatch = function (index, e) {
var abs = this._makeAbs(e)
+ if (this.mark)
+ e = this._mark(e)
+
+ if (this.absolute)
+ e = abs
+
+ if (this.matches[index][e])
+ return
+
if (this.nodir) {
var c = this.cache[abs]
if (c === 'DIR' || Array.isArray(c))
return
}
- if (this.mark)
- e = this._mark(e)
-
this.matches[index][e] = true
var st = this.statCache[abs]
if (st)
this.emit('stat', e, st)
- this.emit('match', this._absolute ? abs : e)
+ this.emit('match', e)
}
Glob.prototype._readdirInGlobStar = function (abs, cb) {
View
@@ -16,6 +16,7 @@ var alphasorti = common.alphasorti
var setopts = common.setopts
var ownProp = common.ownProp
var childrenIgnored = common.childrenIgnored
+var isIgnored = common.isIgnored
function globSync (pattern, options) {
if (typeof options === 'function' || arguments.length === 3)
@@ -187,7 +188,7 @@ GlobSync.prototype._processReaddir = function (prefix, read, abs, remain, index,
if (e.charAt(0) === '/' && !this.nomount) {
e = path.join(this.root, e)
}
- this.matches[index][e] = true
+ this._emitMatch(index, e)
}
// This was the last one, and no stats were needed
return
@@ -209,20 +210,29 @@ GlobSync.prototype._processReaddir = function (prefix, read, abs, remain, index,
GlobSync.prototype._emitMatch = function (index, e) {
+ if (isIgnored(this, e))
+ return
+
var abs = this._makeAbs(e)
+
if (this.mark)
e = this._mark(e)
+ if (this.absolute) {
+ e = abs
+ }
+
if (this.matches[index][e])
return
if (this.nodir) {
- var c = this.cache[this._makeAbs(e)]
+ var c = this.cache[abs]
if (c === 'DIR' || Array.isArray(c))
return
}
this.matches[index][e] = true
+
if (this.stat)
this._stat(e)
}
@@ -399,7 +409,7 @@ GlobSync.prototype._processSimple = function (prefix, index) {
prefix = prefix.replace(/\\/g, '/')
// Mark this as a match
- this.matches[index][prefix] = true
+ this._emitMatch(index, prefix)
}
// Returns either 'DIR', 'FILE', or false
View
@@ -1,23 +1,49 @@
require('./global-leakage.js')
-var test = require('tap').test
-var Glob = require('../').Glob
+var t = require('tap')
+var glob = require('../')
var common = require('../common.js')
var pattern = 'a/b/**';
var bashResults = require('./bash-results.json')
var isAbsolute = require('path-is-absolute')
process.chdir(__dirname + '/fixtures')
-test('Emits absolute matches if option set', function (t) {
- var g = new Glob(pattern, { absolute: true })
+t.Test.prototype.addAssert('isAbsolute', 1, function (file, message, extra) {
+ extra.found = file
+ return this.ok(isAbsolute(file), message || 'must be absolute', extra)
+})
- var matchCount = 0
- g.on('match', function (m) {
- t.ok(isAbsolute(m), 'must be absolute')
- matchCount++
- })
+var marks = [ true, false ]
+marks.forEach(function (mark) {
+ t.test('mark=' + mark, function (t) {
+ t.plan(2)
+
+ t.test('Emits absolute matches if option set', function (t) {
+ var g = new glob.Glob(pattern, { absolute: true })
+
+ var matchCount = 0
+ g.on('match', function (m) {
+ t.isAbsolute(m)
+ matchCount++
+ })
+
+ g.on('end', function (results) {
+ t.equal(matchCount, bashResults[pattern].length, 'must match all files')
+ t.equal(results.length, bashResults[pattern].length, 'must match all files')
+ results.forEach(function (m) {
+ t.isAbsolute(m)
+ })
+ t.end()
+ })
+ })
+
+ t.test('returns absolute results synchronously', function (t) {
+ var results = glob.sync(pattern, { absolute: true })
- g.on('end', function () {
- t.equal(matchCount, bashResults[pattern].length, 'must match all files')
- t.end()
+ t.equal(results.length, bashResults[pattern].length, 'must match all files')
+ results.forEach(function (m) {
+ t.ok(isAbsolute(m), 'must be absolute', { found: m })
+ })
+ t.end()
+ })
})
})

0 comments on commit efdfead

Please sign in to comment.