From c716ffd3d01ceab2622c5d916e227afca3231d5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Cruz?= Date: Tue, 2 May 2017 16:48:44 +0100 Subject: [PATCH] Fix process not exiting calling .close() right after watching. (#600) * Fix process not exiting calling .close() right after watching. * Attempt to fix failing tests on linux. * Fix tests on windows. --- index.js | 2 +- lib/fsevents-handler.js | 1 + test.js | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index edb44632..4006e080 100644 --- a/index.js +++ b/index.js @@ -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)); diff --git a/lib/fsevents-handler.js b/lib/fsevents-handler.js index dbf6adee..ddda6ef8 100644 --- a/lib/fsevents-handler.js +++ b/lib/fsevents-handler.js @@ -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), diff --git a/test.js b/test.js index 576747d1..7fdcb21b 100644 --- a/test.js +++ b/test.js @@ -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; @@ -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() {