Skip to content

Commit

Permalink
Merge pull request #490 from nono/fix-add-for-renamed-dir
Browse files Browse the repository at this point in the history
Fix add for renamed dir
  • Loading branch information
es128 committed Jun 3, 2016
2 parents 638a529 + b6946e2 commit f0ff52e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ FSWatcher.prototype._emit = function(event, path, val1, val2, val3) {
this.options.alwaysStat && val1 === undefined &&
(event === 'add' || event === 'addDir' || event === 'change')
) {
fs.stat(path, function(error, stats) {
var fullPath = this.options.cwd ? sysPath.join(this.options.cwd, path) : path;
fs.stat(fullPath, function(error, stats) {
// Suppress event when fs.stat fails, to avoid sending undefined 'stat'
if (error || !stats) return;

Expand Down
9 changes: 6 additions & 3 deletions lib/fsevents-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function FsEventsHandler() {}

// Private method: Handle symlinks encountered during directory scan

// * wathPath - string, file/dir path to be watched with fsevents
// * watchPath - string, file/dir path to be watched with fsevents
// * realPath - string, real path (in case of symlinks)
// * transform - function, path transformer
// * globFilter - function, path filter in case a glob pattern was provided
Expand Down Expand Up @@ -202,6 +202,7 @@ function(watchPath, realPath, transform, globFilter) {
}
var eventName = info.type === 'directory' ? event + 'Dir' : event;
this._emit(eventName, path);
if (eventName === 'addDir') this._addToFsEvents(path, false, true);
}
}.bind(this);

Expand Down Expand Up @@ -362,14 +363,16 @@ function(path, transform, forceAdd, priorDepth) {
} else {
emitAdd(joinedPath, entry.stat);
}
}.bind(this)).on('end', this._emitReady);
}.bind(this)).on('error', function() {
// Ignore readdirp errors
}).on('end', this._emitReady);
} else {
emitAdd(wh.watchPath, stats);
this._emitReady();
}
}.bind(this));

if (this.options.persistent) {
if (this.options.persistent && forceAdd !== true) {
var initWatch = function(error, realPath) {
var closer = this._watchWithFsEvents(
wh.watchPath,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"devDependencies": {
"chai": "^3.2.0",
"coveralls": "^2.11.2",
"graceful-fs": "4.1.4",
"istanbul": "^0.3.20",
"mocha": "^2.0.0",
"rimraf": "^2.4.3",
Expand Down
53 changes: 51 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var expect = chai.expect;
var should = chai.should();
var sinon = require('sinon');
var rimraf = require('rimraf');
var fs = require('fs');
var fs = require('graceful-fs');
var sysPath = require('path');
chai.use(require('sinon-chai'));
var os = process.platform;
Expand Down Expand Up @@ -446,6 +446,32 @@ function runTests(baseopts) {
}));
});
});
describe('renamed directory', function() {
it('should emit `add` for a file in a renamed directory', function(done) {
options.ignoreInitial = true;
var spy = sinon.spy();
var testDir = getFixturePath('subdir');
var testPath = getFixturePath('subdir/add.txt');
var renamedDir = getFixturePath('subdir-renamed');
var expectedPath = sysPath.join(renamedDir, 'add.txt')
fs.mkdir(testDir, 0x1ed, function() {
fs.writeFile(testPath, Date.now(), function() {
watcher = chokidar.watch(fixturesPath, options)
.on('add', spy)
.on('ready', function() {
w(function() {
fs.rename(testDir, renamedDir, simpleCb);
}, 1000)();
waitFor([spy], function() {
spy.should.have.been.calledOnce;
spy.should.have.been.calledWith(expectedPath);
done();
});
});
});
});
});
});
describe('watch non-existent paths', function() {
it('should watch non-existent file and detect add', function(done) {
var spy = sinon.spy();
Expand Down Expand Up @@ -977,7 +1003,7 @@ function runTests(baseopts) {
w(function() {
spy.should.not.have.been.called;
done();
}, 500)();
}, 1000)();
});
});
it('should notice when a file appears in an empty directory', function(done) {
Expand Down Expand Up @@ -1272,6 +1298,29 @@ function runTests(baseopts) {
});
});
});
it('should emit `addDir` with alwaysStat for renamed directory', function(done) {
options.cwd = fixturesPath;
options.alwaysStat = true;
options.ignoreInitial = true;
var spy = sinon.spy();
var testDir = getFixturePath('subdir');
var renamedDir = getFixturePath('subdir-renamed');
fs.mkdir(testDir, 0x1ed, function() {
watcher = chokidar.watch('.', options)
.on('ready', function() {
w(function() {
watcher.on('addDir', spy)
fs.rename(testDir, renamedDir, simpleCb);
}, 1000)();
waitFor([spy], function() {
spy.should.have.been.calledOnce;
spy.should.have.been.calledWith('subdir-renamed');
expect(spy.args[0][1]).to.be.ok; // stats
done();
});
});
});
});
it('should allow separate watchers to have different cwds', function(done) {
options.cwd = fixturesPath;
var spy1 = sinon.spy();
Expand Down

0 comments on commit f0ff52e

Please sign in to comment.