From cde21fb4de55acd4ca9dbe863d37e8a2dcbb41e7 Mon Sep 17 00:00:00 2001 From: Kevin Woo Date: Tue, 15 Sep 2015 21:44:39 -0700 Subject: [PATCH] test: added dotfile tests --- lib/monitor/index.js | 4 +-- lib/monitor/run.js | 2 +- lib/monitor/watch.js | 19 ++++++++---- test/fixtures/.dotfile | 0 test/fixtures/.dotfolder/.nested_dotfile.js | 0 test/fixtures/.dotfolder/second.js | 0 test/fixtures/.dotfolder/some_file.js | 0 test/monitor/match.test.js | 34 +++++++++++++++++++++ 8 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 test/fixtures/.dotfile create mode 100644 test/fixtures/.dotfolder/.nested_dotfile.js create mode 100644 test/fixtures/.dotfolder/second.js create mode 100644 test/fixtures/.dotfolder/some_file.js diff --git a/lib/monitor/index.js b/lib/monitor/index.js index 4af283b..89db029 100644 --- a/lib/monitor/index.js +++ b/lib/monitor/index.js @@ -1,4 +1,4 @@ module.exports = { run: require('./run'), - watch: require('./watch'), -}; \ No newline at end of file + watch: require('./watch').watch, +}; diff --git a/lib/monitor/run.js b/lib/monitor/run.js index 4cd813f..deee95e 100644 --- a/lib/monitor/run.js +++ b/lib/monitor/run.js @@ -4,7 +4,7 @@ var bus = utils.bus; var childProcess = require('child_process'); var spawn = childProcess.spawn; var exec = childProcess.exec; -var watch = require('./watch'); +var watch = require('./watch').watch; var config = require('../config'); var child = null; // the actual child process we spawn var killedAfterChange = false; diff --git a/lib/monitor/watch.js b/lib/monitor/watch.js index 4699cd0..0c8c7c2 100644 --- a/lib/monitor/watch.js +++ b/lib/monitor/watch.js @@ -1,4 +1,5 @@ -module.exports = watch; +module.exports.watch = watch; +module.exports.resetWatchers = resetWatchers; var debug = require('debug')('nodemon:watch'); var debugRoot = require('debug')('nodemon'); @@ -13,13 +14,16 @@ var match = require('./match'); var watchers = []; var debouncedBus; -bus.on('reset', function () { +bus.on('reset', resetWatchers); + +function resetWatchers() { debug('resetting watchers'); watchers.forEach(function (watcher) { watcher.close(); }); watchers = []; -}); +} + function watch() { if (watchers.length) { @@ -33,6 +37,7 @@ function watch() { debugRoot('ignore dirs regex(%s)', config.options.ignore.re); var promises = []; + var watchedFiles = []; dirs.forEach(function (dir) { var promise = new Promise(function (resolve) { @@ -45,9 +50,10 @@ function watch() { var total = 0; watcher.on('change', filterAndRestart); - watcher.on('add', function (arg) { + watcher.on('add', function (file) { + watchedFiles.push(file); total++; - debug('watching dir: %s', arg); + debug('watching dir: %s', file); }); watcher.on('ready', function () { resolve(total); @@ -58,7 +64,7 @@ function watch() { promises.push(promise); }); - Promise.all(promises).then(function (res) { + return Promise.all(promises).then(function (res) { var total = res.reduce(function (acc, curr) { acc += curr; return acc; @@ -66,6 +72,7 @@ function watch() { var count = total.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); utils.log.detail('watching ' + count + ' files'); + return watchedFiles; }); } diff --git a/test/fixtures/.dotfile b/test/fixtures/.dotfile new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/.dotfolder/.nested_dotfile.js b/test/fixtures/.dotfolder/.nested_dotfile.js new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/.dotfolder/second.js b/test/fixtures/.dotfolder/second.js new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/.dotfolder/some_file.js b/test/fixtures/.dotfolder/some_file.js new file mode 100644 index 0000000..e69de29 diff --git a/test/monitor/match.test.js b/test/monitor/match.test.js index 245aff9..c6bf723 100644 --- a/test/monitor/match.test.js +++ b/test/monitor/match.test.js @@ -8,6 +8,7 @@ var assert = require('assert'), nodemonUtils = require('../../lib/utils'), defaults = require('../../lib/config/defaults'), utils = require('../utils'), + watch = require('../../lib/monitor/watch'), merge = nodemonUtils.merge; describe('match', function () { @@ -338,3 +339,36 @@ describe('match rule parser', function () { assert(matched.result.length === 1, 'no file matched'); }); }); + + +describe('watcher', function () { + afterEach(function () { + watch.resetWatchers(); + }); + + it('should match a dotfile if explicitly asked to', function (done) { + config.load({ + watch: ['test/fixtures/.dotfile'] + }, function (config) { + return watch.watch() + .then(function (files) { + assert.deepEqual(files.length, 1, 'should contain .dotfile'); + }) + .then(done) + .catch(done); + }) + }); + + it('should match a dotfolder if explicitly asked to', function (done) { + config.load({ + watch: ['test/fixtures/.dotfolder'] + }, function (config) { + return watch.watch() + .then(function (files) { + assert.deepEqual(files.length, 3, 'file lists should contain .dotfolder files'); + }) + .then(done) + .catch(done); + }) + }); +});