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

When adding a previously-deleted file, emit 'add', not 'change' #382

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,8 @@ FSWatcher.prototype._remove = function(directory, item) {
var eventName = isDirectory ? 'unlinkDir' : 'unlink';
if (wasTracked && !this._isIgnored(path)) this._emit(eventName, path);

// Avoid conflicts if we later create another directory with the same name
if (isDirectory && !this.options.usePolling) {
// Avoid conflicts if we later create another file with the same name
if (!this.options.useFsEvents) {
this.unwatch(path);
}
};
Expand Down
26 changes: 26 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,32 @@ function runTests(options) {
});
}));
});
it('should emit `add`, but not `change` when previously deleted file is re-added', function(done) {
var unlinkSpy = sinon.spy(function unlink(){});
var addSpy = sinon.spy(function add(){});
var changeSpy = sinon.spy(function change(){});
var testPath = getFixturePath('add.txt');
fs.writeFileSync(testPath, 'hello');
watcher
.on('unlink', unlinkSpy)
.on('add', addSpy)
.on('change', changeSpy)
.on('ready', d(function() {
unlinkSpy.should.not.have.been.called;
addSpy.should.not.have.been.called;
changeSpy.should.not.have.been.called;
fs.unlinkSync(testPath);
waitFor([unlinkSpy], d(function() {
unlinkSpy.should.have.been.calledWith(testPath);
fs.writeFileSync(testPath, 'b');
waitFor([addSpy], d(function() {
addSpy.should.have.been.calledWith(testPath);
changeSpy.should.not.have.been.called;
done();
}));
}));
}));
});
it('should not emit `unlink` for previously moved files', function(done) {
var unlinkSpy = sinon.spy(function unlink(){});
var testPath = getFixturePath('change.txt');
Expand Down