Skip to content

Commit

Permalink
disableGlobbing option: treat glob-like paths as literal paths (#598)
Browse files Browse the repository at this point in the history
* Added ignoreGlobs option, which causes glob-like paths to be treated as literal paths, not glob patterns

* Renamed ignoreGlobs option to disableGlobbing

* More test cases for disableGlobbing

* More test cases for disableGlobbing

* Added disableGlobbing to readme
  • Loading branch information
octachrome authored and es128 committed Apr 25, 2017
1 parent d90d112 commit 0faec86
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ chokidar.watch('file', {
ignoreInitial: false,
followSymlinks: true,
cwd: '.',
disableGlobbing: false,

usePolling: true,
interval: 100,
Expand Down Expand Up @@ -168,6 +169,8 @@ symlinks themselves will be watched for changes instead of following
the link references and bubbling events through the link's path.
* `cwd` (no default). The base directory from which watch `paths` are to be
derived. Paths emitted with events will be relative to this.
* `disableGlobbing` (default: `false`). If set to `true` then the strings passed to `.watch()` and `.add()` are treated as
literal path names, even if they look like globs.

#### Performance

Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function FSWatcher(_opts) {
if (undef('ignorePermissionErrors')) opts.ignorePermissionErrors = false;
if (undef('interval')) opts.interval = 100;
if (undef('binaryInterval')) opts.binaryInterval = 300;
if (undef('disableGlobbing')) opts.disableGlobbing = false;
this.enableBinaryInterval = opts.binaryInterval !== opts.interval;

// Enable fsevents on OS X when polling isn't explicitly enabled.
Expand Down Expand Up @@ -377,7 +378,7 @@ FSWatcher.prototype._isIgnored = function(path, stats) {
var replacerRe = /^\.[\/\\]/;
FSWatcher.prototype._getWatchHelpers = function(path, depth) {
path = path.replace(replacerRe, '');
var watchPath = depth || !isGlob(path) ? path : globParent(path);
var watchPath = depth || this.options.disableGlobbing || !isGlob(path) ? path : globParent(path);
var fullWatchPath = sysPath.resolve(watchPath);
var hasGlob = watchPath !== path;
var globFilter = hasGlob ? anymatch(path) : false;
Expand Down
53 changes: 53 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,59 @@ function runTests(baseopts) {
});
}));
});
it('should treat glob-like directory names as literal directory names when globbing is disabled', function(done) {
options.disableGlobbing = true;
var spy = sinon.spy();
var filePath = getFixturePath('nota[glob]/a.txt');
var watchPath = getFixturePath('nota[glob]');
var matchingDir = getFixturePath('notag');
var matchingFile = getFixturePath('notag/b.txt');
var matchingFile2 = getFixturePath('notal');
fs.mkdirSync(watchPath, 0x1ed);
fs.writeFileSync(filePath, 'b');
fs.mkdirSync(matchingDir, 0x1ed);
fs.writeFileSync(matchingFile, 'c');
fs.writeFileSync(matchingFile2, 'd');
watcher = chokidar.watch(watchPath, options)
.on('all', spy)
.on('ready', function() {
spy.should.have.been.calledWith('add', filePath);
spy.should.not.have.been.calledWith('addDir', matchingDir);
spy.should.not.have.been.calledWith('add', matchingFile);
spy.should.not.have.been.calledWith('add', matchingFile2);
w(fs.writeFile.bind(fs, filePath, Date.now(), simpleCb))();
waitFor([spy.withArgs('change', filePath)], function() {
spy.should.have.been.calledWith('change', filePath);
done();
});
});
});
it('should treat glob-like filenames as literal filenames when globbing is disabled', function(done) {
options.disableGlobbing = true;
var spy = sinon.spy();
var filePath = getFixturePath('nota[glob]');
var watchPath = getFixturePath('nota[glob]');
var matchingDir = getFixturePath('notag');
var matchingFile = getFixturePath('notag/a.txt');
var matchingFile2 = getFixturePath('notal');
fs.writeFileSync(filePath, 'b');
fs.mkdirSync(matchingDir, 0x1ed);
fs.writeFileSync(matchingFile, 'c');
fs.writeFileSync(matchingFile2, 'd');
watcher = chokidar.watch(watchPath, options)
.on('all', spy)
.on('ready', function() {
spy.should.have.been.calledWith('add', filePath);
spy.should.not.have.been.calledWith('addDir', matchingDir);
spy.should.not.have.been.calledWith('add', matchingFile);
spy.should.not.have.been.calledWith('add', matchingFile2);
w(fs.writeFile.bind(fs, filePath, Date.now(), simpleCb))();
waitFor([spy.withArgs('change', filePath)], function() {
spy.should.have.been.calledWith('change', filePath);
done();
});
});
});
it('should not prematurely filter dirs against complex globstar patterns', function(done) {
var spy = sinon.spy();
var deepFile = getFixturePath('subdir/subsub/subsubsub/a.txt');
Expand Down

0 comments on commit 0faec86

Please sign in to comment.