From 9abb0a4f96cc3d0989494e91b51c312ca1d5a589 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Sun, 24 Aug 2014 18:56:00 -0700 Subject: [PATCH] Update: Improve gulp.watch implementation & tests --- index.js | 16 +++-- package.json | 2 +- test/watch.js | 189 +++++++++++++++++++------------------------------- 3 files changed, 83 insertions(+), 124 deletions(-) diff --git a/index.js b/index.js index a27af337f..13bc263c1 100644 --- a/index.js +++ b/index.js @@ -11,13 +11,21 @@ util.inherits(Gulp, Undertaker); Gulp.prototype.src = vfs.src; Gulp.prototype.dest = vfs.dest; -Gulp.prototype.watch = function(glob, opt, fn) { - if (typeof opt === 'function' || Array.isArray(opt)) { - fn = opt; +Gulp.prototype.watch = function(glob, opt, task) { + var isFunction = (typeof opt === 'function'); + var isString = (typeof opt === 'string'); + var isArray = Array.isArray(opt); + if (isFunction || isString || isArray) { + task = opt; opt = null; } - return vfs.watch(glob, opt, this.parallel(fn)); + var fn; + if (task) { + fn = this.parallel(task); + } + + return vfs.watch(glob, opt, fn); }; // Let people use this class from our instance diff --git a/package.json b/package.json index 666f8f271..12cb3f376 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "should": "^5.0.1" }, "scripts": { - "lint": "eslint . && jscs *.js bin/ lib/ test/", + "lint": "eslint . && jscs *.js bin/ test/", "pretest": "npm run lint", "test": "mocha --reporter spec", "coveralls": "istanbul cover _mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage" diff --git a/test/watch.js b/test/watch.js index e299cf907..335ca771a 100644 --- a/test/watch.js +++ b/test/watch.js @@ -11,168 +11,119 @@ require('mocha'); var outpath = path.join(__dirname, './out-fixtures'); +var tempFileContent = 'A test generated this file and it is safe to delete'; + +function createTempFile(path) { + fs.writeFileSync(path, tempFileContent); +} + +function updateTempFile(path) { + var gazeTimeout = 125; + setTimeout(function() { + fs.appendFileSync(path, ' changed'); + }, gazeTimeout); +} + describe('gulp', function() { describe('watch()', function() { beforeEach(rimraf.bind(null, outpath)); beforeEach(mkdirp.bind(null, outpath)); afterEach(rimraf.bind(null, outpath)); - var tempFileContent = 'A test generated this file and it is safe to delete'; - - var writeTimeout = 125; // Wait for it to get to the filesystem - var writeFileWait = function(name, content, cb) { - if (!cb) { - cb = function() {}; - } - setTimeout(function() { - fs.writeFile(name, content, cb); - }, writeTimeout); - }; - it('should call the function when file changes: no options', function(done) { - - // Arrange var tempFile = path.join(outpath, 'watch-func.txt'); - fs.writeFile(tempFile, tempFileContent, function() { - // Assert: it works if it calls done - var watcher = gulp.watch(tempFile, function(evt) { - should.exist(evt); - should.exist(evt.path); - should.exist(evt.type); - evt.type.should.equal('changed'); - evt.path.should.equal(path.resolve(tempFile)); - watcher.end(); - done(); - }); + createTempFile(tempFile); - // Act: change file - writeFileWait(tempFile, tempFileContent + ' changed'); + var watcher = gulp.watch(tempFile, function(cb) { + watcher.end(); + cb(); + done(); }); + + updateTempFile(tempFile); }); it('should call the function when file changes: w/ options', function(done) { - - // Arrange var tempFile = path.join(outpath, 'watch-func-options.txt'); - fs.writeFile(tempFile, tempFileContent, function() { - // Assert: it works if it calls done - var watcher = gulp.watch(tempFile, { debounceDelay: 5 }, function(evt) { - should.exist(evt); - should.exist(evt.path); - should.exist(evt.type); - evt.type.should.equal('changed'); - evt.path.should.equal(path.resolve(tempFile)); - watcher.end(); - done(); - }); + createTempFile(tempFile); - // Act: change file - writeFileWait(tempFile, tempFileContent + ' changed'); + var watcher = gulp.watch(tempFile, {debounceDelay: 5}, function(cb) { + watcher.end(); + cb(); + done(); }); + + updateTempFile(tempFile); }); it('should not drop options when no callback specified', function(done) { - // Arrange var tempFile = path.join(outpath, 'watch-func-nodrop-options.txt'); // By passing a cwd option, ensure options are not lost to gaze var relFile = '../watch-func-nodrop-options.txt'; var cwd = outpath + '/subdir'; - fs.writeFile(tempFile, tempFileContent, function() { - - // Assert: it works if it calls done - var watcher = gulp.watch(relFile, { debounceDelay: 5, cwd: cwd }) - .on('change', function(evt) { - should.exist(evt); - should.exist(evt.path); - should.exist(evt.type); - evt.type.should.equal('changed'); - evt.path.should.equal(path.resolve(tempFile)); - watcher.end(); - done(); - }); - - // Act: change file - writeFileWait(tempFile, tempFileContent + ' changed'); - }); + + createTempFile(tempFile); + + var watcher = gulp.watch(relFile, {debounceDelay: 5, cwd: cwd}) + .on('change', function(evt) { + should.exist(evt); + should.exist(evt.path); + should.exist(evt.type); + evt.type.should.equal('changed'); + evt.path.should.equal(path.resolve(tempFile)); + watcher.end(); + done(); + }); + + updateTempFile(tempFile); }); it('should run many tasks: w/ options', function(done) { - // Arrange var tempFile = path.join(outpath, 'watch-task-options.txt'); - var task1 = 'task1'; - var task2 = 'task2'; - var task3 = 'task3'; var a = 0; - var timeout = writeTimeout * 2.5; - fs.writeFile(tempFile, tempFileContent, function() { + createTempFile(tempFile); - gulp.task(task1, function() { - a++; - }); - gulp.task(task2, function() { - a += 10; - }); - gulp.task(task3, function() { - throw new Error('task3 called!'); - }); - - // It works if it calls the task - var config = { debounceDelay: timeout / 2 }; - var watcher = gulp.watch(tempFile, config, [task1, task2]); - - // Assert - setTimeout(function() { - a.should.equal(11); // Task1 and task2 + gulp.task('task1', function(cb) { + a++; + cb(); + }); + gulp.task('task2', function(cb) { + a += 10; + a.should.equal(11); + watcher.end(); + cb(); + done(); + }); - gulp.reset(); - watcher.end(); - done(); - }, timeout); + var watcher = gulp.watch(tempFile, {debounceDelay: 25}, gulp.series('task1', 'task2')); - // Act: change file - writeFileWait(tempFile, tempFileContent + ' changed'); - }); + updateTempFile(tempFile); }); it('should run many tasks: no options', function(done) { - // Arrange var tempFile = path.join(outpath, 'watch-many-tasks-no-options.txt'); - var task1 = 'task1'; - var task2 = 'task2'; - var task3 = 'task3'; var a = 0; - var timeout = writeTimeout * 2.5; - - fs.writeFile(tempFile, tempFileContent, function() { - - gulp.task(task1, function() { - a++; - }); - gulp.task(task2, function() { - a += 10; - }); - gulp.task(task3, function() { - throw new Error('task3 called!'); - }); - // It works if it calls the task - var watcher = gulp.watch(tempFile, [task1, task2]); + createTempFile(tempFile); - // Assert - setTimeout(function() { - a.should.equal(11); // Task1 and task2 + gulp.task('task1', function(cb) { + a++; + cb(); + }); + gulp.task('task2', function(cb) { + a += 10; + a.should.equal(11); + watcher.end(); + cb(); + done(); + }); - gulp.reset(); - watcher.end(); - done(); - }, timeout); + var watcher = gulp.watch(tempFile, gulp.series('task1', 'task2')); - // Act: change file - writeFileWait(tempFile, tempFileContent + ' changed'); - }); + updateTempFile(tempFile); }); });