Skip to content

Commit

Permalink
Fix process not exiting calling .close() right after watching. (#600)
Browse files Browse the repository at this point in the history
* Fix process not exiting calling .close() right after watching.

* Attempt to fix failing tests on linux.

* Fix tests on windows.
  • Loading branch information
satazor authored and es128 committed May 2, 2017
1 parent f03f332 commit c716ffd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ FSWatcher.prototype.add = function(paths, _origAdd, _internal) {
}.bind(this));
}.bind(this), function(error, results) {
results.forEach(function(item) {
if (!item) return;
if (!item || this.closed) return;
this.add(sysPath.dirname(item), sysPath.basename(_origAdd || item));
}, this);
}.bind(this));
Expand Down
1 change: 1 addition & 0 deletions lib/fsevents-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ function(path, transform, forceAdd, priorDepth) {

if (this.options.persistent && forceAdd !== true) {
var initWatch = function(error, realPath) {
if (this.closed) return;
var closer = this._watchWithFsEvents(
wh.watchPath,
sysPath.resolve(realPath || wh.watchPath),
Expand Down
17 changes: 17 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var sinon = require('sinon');
var rimraf = require('rimraf');
var fs = require('graceful-fs');
var sysPath = require('path');
var cp = require('child_process');
chai.use(require('sinon-chai'));
var os = process.platform;

Expand Down Expand Up @@ -1820,6 +1821,22 @@ function runTests(baseopts) {
});
});
});
it('should not prevent the process from exiting', function(done) {
var scriptFile = getFixturePath('script.js');
var scriptContent = '\
var chokidar = require("' + __dirname.replace(/\\/g, '\\\\') + '");\n\
var watcher = chokidar.watch("' + scriptFile.replace(/\\/g, '\\\\') + '");\n\
watcher.close();\n\
process.stdout.write("closed");\n';
fs.writeFile(scriptFile, scriptContent, function (err) {
if (err) throw err;
cp.exec('node ' + scriptFile, function (err, stdout) {
if (err) throw err;
expect(stdout.toString()).to.equal('closed');
done();
});
});
});
});
describe('env variable option override', function() {
describe('CHOKIDAR_USEPOLLING', function() {
Expand Down

0 comments on commit c716ffd

Please sign in to comment.