From 546473622f41daa1ac45144506b51e66140444b8 Mon Sep 17 00:00:00 2001 From: Alejandro Henkel Date: Sun, 26 Feb 2017 00:47:10 -0600 Subject: [PATCH] Fix: Validate series/parallel arguments aren't empty or invalid (fixes #72) (#75) --- lib/helpers/normalizeArgs.js | 5 +++- test/parallel.js | 45 ++++++++++++++++++++++++++++++++++++ test/series.js | 45 ++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) diff --git a/lib/helpers/normalizeArgs.js b/lib/helpers/normalizeArgs.js index 6e5daa5..297601f 100644 --- a/lib/helpers/normalizeArgs.js +++ b/lib/helpers/normalizeArgs.js @@ -16,7 +16,10 @@ function normalizeArgs(registry, args) { return fn; } - return map(flatten(args), getFunction); + var flattenArgs = flatten(args); + assert(flattenArgs.length, 'One or more tasks should be combined using series or parallel'); + + return map(flattenArgs, getFunction); } module.exports = normalizeArgs; diff --git a/test/parallel.js b/test/parallel.js index 90ed186..c07edd7 100644 --- a/test/parallel.js +++ b/test/parallel.js @@ -34,6 +34,51 @@ describe('parallel', function() { done(); }); + it('should throw on non-valid tasks combined with valid tasks', function(done) { + function fail() { + taker.parallel('test1', 'test2', 'test3', {}); + } + + expect(fail).toThrow(/Task never defined:/); + done(); + }); + + it('should throw on tasks array with both valid and non-valid tasks', function(done) { + function fail() { + taker.parallel(['test1', 'test2', 'test3', {}]); + } + + expect(fail).toThrow(/Task never defined:/); + done(); + }); + + it('should throw on non-valid task', function(done) { + function fail() { + taker.parallel({}); + } + + expect(fail).toThrow(/Task never defined:/); + done(); + }); + + it('should throw when no tasks specified', function(done) { + function fail() { + taker.parallel(); + } + + expect(fail).toThrow(/One or more tasks should be combined using series or parallel/); + done(); + }); + + it('should throw on empty array of registered tasks', function(done) { + function fail() { + taker.parallel([]); + } + + expect(fail).toThrow(/One or more tasks should be combined using series or parallel/); + done(); + }); + it('should take only one array of registered tasks', function(done) { taker.parallel(['test1', 'test2', 'test3'])(function(err, results) { expect(results).toEqual([1, 2, 3]); diff --git a/test/series.js b/test/series.js index 8fdf083..bb071ba 100644 --- a/test/series.js +++ b/test/series.js @@ -34,6 +34,51 @@ describe('series', function() { done(); }); + it('should throw on non-valid tasks combined with valid tasks', function(done) { + function fail() { + taker.series('test1', 'test2', 'test3', {}); + } + + expect(fail).toThrow(/Task never defined:/); + done(); + }); + + it('should throw on tasks array with both valid and non-valid tasks', function(done) { + function fail() { + taker.series(['test1', 'test2', 'test3', {}]); + } + + expect(fail).toThrow(/Task never defined:/); + done(); + }); + + it('should throw on non-valid task', function(done) { + function fail() { + taker.series({}); + } + + expect(fail).toThrow(/Task never defined:/); + done(); + }); + + it('should throw when no tasks specified', function(done) { + function fail() { + taker.series(); + } + + expect(fail).toThrow(/One or more tasks should be combined using series or parallel/); + done(); + }); + + it('should throw on empty array of registered tasks', function(done) { + function fail() { + taker.series([]); + } + + expect(fail).toThrow(/One or more tasks should be combined using series or parallel/); + done(); + }); + it('should take only one array of registered tasks', function(done) { taker.series(['test1', 'test2', 'test3'])(function(err, results) { expect(results).toEqual([1, 2, 3]);