Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disableGlobbing option: treat glob-like paths as literal paths #598

Merged
merged 5 commits into from
Apr 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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