Skip to content

Commit

Permalink
More tests around _runTask() and _readyToRunTask()
Browse files Browse the repository at this point in the history
  • Loading branch information
robrich committed Oct 9, 2013
1 parent b1ac88c commit f658bb8
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 16 deletions.
17 changes: 9 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*jshint node:true */
/*exports gulp */
/*global gulp:true, console:false */
// TODO: refactor to use async.auto()
module.exports = gulp = {
verbose: false,
reset: function() {
Expand Down Expand Up @@ -55,7 +56,7 @@ module.exports = gulp = {
gulp._runSequencer(gulp.tasks, names, seq, []);
gulp.taskQueue = seq;
if (gulp.verbose) {
console.log('task queue: '+gulp.taskQueue.join(','));
console.log('[gulp tasks: '+gulp.taskQueue.join(',')+']');
}
if (!gulp.isRunning) {
gulp.isRunning = true;
Expand Down Expand Up @@ -96,11 +97,11 @@ module.exports = gulp = {
if (!task.done && !task.running) {
if (gulp._readyToRunTask(task)) {
gulp._runTask(task);
if (!task.done) {
allDone = false;
}
}
}
if (!task.done) {
allDone = false;
}
}
if (allDone) {
if (gulp.verbose) {
Expand Down Expand Up @@ -131,7 +132,7 @@ module.exports = gulp = {
_runTask: function (task) {
"use strict";
if (gulp.verbose) {
console.log('starting ['+task.name+']');
console.log('['+task.name+' started]');
}
task.running = true;
var p = task.fn.call(gulp);
Expand All @@ -142,14 +143,14 @@ module.exports = gulp = {
task.running = false;
task.done = true;
if (gulp.verbose) {
console.log('resolved ['+task.name+']');
console.log('['+task.name+' resolved]');
}
gulp._runStep();
}); // .done() with no onRejected so failure is thrown
} else {
// no promise, just do the next task, setTimeout to clear call stack
// no promise, we're done
if (gulp.verbose) {
console.log('finished ['+task.name+']');
console.log('['+task.name+' finished]');
}
task.running = false;
task.done = true;
Expand Down
68 changes: 68 additions & 0 deletions test/readyToRunTask.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*jshint node:true */
/*global describe:false, it:false, beforeEach:false, afterEach:false */

"use strict";

var gulp = require('../');
require('should');
require('mocha');

describe('gulp task is ready when dependencies are resolved', function() {
// Don't bleed previous test into subsequent test
beforeEach(gulp.reset);
afterEach(gulp.reset);
describe('_readyToRunTask()', function() {
it('should be ready if no dependencies', function(done) {
var task, expected, actual;
expected = true;
task = {
name: 'a',
dep: []
};
gulp.tasks = { a: task };

actual = gulp._readyToRunTask(task);

actual.should.equal(expected);
done();
});
it('should be ready if dependency is done', function(done) {
var task, dep, expected, actual;
expected = true;
task = {
name: 'a',
dep: ['b']
};
dep = {
name: 'b',
dep: [],
done: true
};
gulp.tasks = { a: task, b: dep };

actual = gulp._readyToRunTask(task);

actual.should.equal(expected);
done();
});
it('should not be ready if dependency is not done', function(done) {
var task, dep, expected, actual;
expected = false;
task = {
name: 'a',
dep: ['b']
};
dep = {
name: 'b',
dep: []
//done: lack of var is falsey
};
gulp.tasks = { a: task, b: dep };

actual = gulp._readyToRunTask(task);

actual.should.equal(expected);
done();
});
});
});
94 changes: 94 additions & 0 deletions test/runTask.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*jshint node:true */
/*global describe:false, it:false, beforeEach:false, afterEach:false, before:false, after:false */

"use strict";

var gulp = require('../');
var Q = require('q');
var should = require('should');
require('mocha');

describe('gulp tasks execute as expected', function() {
// Don't bleed previous test into subsequent test
var real_runStep;
var hasRunStep = false;
before(function () {
real_runStep = gulp._runStep;
gulp._runStep = function () {
hasRunStep = true;
};
});
after(function () {
gulp._runStep = real_runStep;
});
beforeEach(function () {
hasRunStep = false;
gulp.reset();
});
afterEach(gulp.reset);
describe('_runTask()', function() {
it('calls task function', function(done) {
var a, task;
a = 0;
task = {
name: 'test',
fn: function() {
++a;
}
};
gulp._runTask(task);
a.should.equal(1);
done();
});
it('sets .running correctly', function(done) {
var task;
task = {
name: 'test',
fn: function() {
should.exist(task.running);
task.running.should.equal(true);
}
};
gulp._runTask(task);
should.exist(task.running);
task.running.should.equal(false);
done();
});
it('sync task sets done after calling function', function(done) {
var task;
task = {
name: 'test',
fn: function() {
should.not.exist(task.done);
}
};
gulp._runTask(task);
should.exist(task.done);
task.done.should.equal(true);
hasRunStep.should.equal(false);
done();
});
it('async task sets done after task resolves', function(done) {
var task, timeout = 5;
task = {
name: 'test',
fn: function() {
var deferred = Q.defer();
setTimeout(function () {
should.not.exist(task.done);
deferred.resolve();
}, timeout);
return deferred.promise;
}
};
gulp._runTask(task);
should.not.exist(task.done);
setTimeout(function () {
should.exist(task.done);
task.done.should.equal(true);
hasRunStep.should.equal(true);
done();
}, timeout*2);
});
});
});
51 changes: 47 additions & 4 deletions test/taskDependencies.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*jshint node:true */
/*global describe:false, it:false, beforeEach:false */
/*global describe:false, it:false, beforeEach:false, afterEach:false */

"use strict";

Expand All @@ -9,9 +9,9 @@ require('should');
require('mocha');

describe('gulp task dependencies', function() {
beforeEach(function () {
gulp.reset(); // Don't bleed previous test into subsequent test
});
// Don't bleed previous test into subsequent test
beforeEach(gulp.reset);
afterEach(gulp.reset);
describe('run()', function() {
// Technically these are duplicated from test/runSequencer.js,
// but those are unit tests and these are integration tests
Expand Down Expand Up @@ -81,5 +81,48 @@ describe('gulp task dependencies', function() {
});
gulp.isRunning.should.equal(true);
});
it('should run all tasks of complex dependency chain', function(done) {
var a, fn1, fn2, fn3, fn4, timeout = 2;
a = 0;
// fn1 is a long-running task, fn2 and 3 run quickly, fn4 is synchronous
// If shorter tasks mark it done before the longer task finishes that's wrong
fn1 = function() {
var deferred = Q.defer();
setTimeout(function () {
++a;
deferred.resolve();
}, timeout*5);
return deferred.promise;
};
fn2 = function() {
var deferred = Q.defer();
setTimeout(function () {
++a;
deferred.resolve();
}, timeout);
return deferred.promise;
};
fn3 = function() {
var deferred = Q.defer();
setTimeout(function () {
++a;
deferred.resolve();
}, timeout);
return deferred.promise;
};
fn4 = function() {
++a;
};
gulp.task('fn1', fn1);
gulp.task('fn2', fn2);
gulp.task('fn3', ['fn1', 'fn2'], fn3);
gulp.task('fn4', ['fn3'], fn4);
gulp.run('fn4', function () {
gulp.isRunning.should.equal(false);
a.should.equal(4);
done();
});
gulp.isRunning.should.equal(true);
});
});
});
8 changes: 4 additions & 4 deletions test/tasks.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*jshint node:true */
/*global describe:false, it:false, beforeEach:false */
/*global describe:false, it:false, beforeEach:false, afterEach:false */
"use strict";

var gulp = require('../');
Expand All @@ -8,9 +8,9 @@ var should = require('should');
require('mocha');

describe('gulp tasks', function() {
beforeEach(function () {
gulp.reset(); // Don't bleed previous test into subsequent test
});
// Don't bleed previous test into subsequent test
beforeEach(gulp.reset);
afterEach(gulp.reset);
describe('task()', function() {
it('should define a task', function(done) {
var fn;
Expand Down

0 comments on commit f658bb8

Please sign in to comment.