/
main.js
312 lines (285 loc) · 8.29 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* jshint node: true */
/* global describe, it, beforeEach */
'use strict';
var runSequence = require('../');
var gulp = require('gulp');
var should = require('should');
var simpleTask = require('./simpleTask');
var submodule = require('./submodule');
require('mocha');
function extend(copy, obj) {
copy = copy || {};
Object.keys(obj).forEach(function(k) {
copy[k] = obj[k];
});
return copy;
}
describe('runSequence', function() {
var task1 = simpleTask();
var task2 = simpleTask();
var task3 = simpleTask();
var task4 = simpleTask();
gulp.task('task1', task1);
gulp.task('task2', task2);
gulp.task('task3', task3);
gulp.task('task4', ['task3'], task4);
gulp.task('errTask', function() {
throw new Error('Error Task');
});
beforeEach(function() {
simpleTask.resetRunCounter();
task1.reset();
task2.reset();
task3.reset();
task4.reset();
submodule.task.reset();
});
describe('Basics', function() {
it('should run a single task', function() {
runSequence('task1');
task1.counter.should.eql(1);
});
it('should run multiple tasks', function() {
runSequence('task1', 'task2');
task1.counter.should.eql(1);
task2.counter.should.eql(2);
});
it('should run simultaneous tasks', function() {
runSequence(['task1', 'task2'], 'task3');
task1.counter.should.not.eql(-1);
task2.counter.should.not.eql(-1);
task3.counter.should.eql(3);
});
it('should run task dependencies', function() {
runSequence('task4');
task1.counter.should.eql(-1);
task2.counter.should.eql(-1);
task3.counter.should.eql(1);
task4.counter.should.eql(2);
});
it('should run task dependencies after previous tasks', function() {
runSequence('task1', 'task4');
task1.counter.should.eql(1);
task2.counter.should.eql(-1);
task3.counter.should.eql(2);
task4.counter.should.eql(3);
});
it('should handle the callback', function() {
var wasCalled = false;
runSequence('task1', 'task4', function(err) {
should.equal(err, undefined);
wasCalled = true;
});
task1.counter.should.eql(1);
task2.counter.should.eql(-1);
task3.counter.should.eql(2);
task4.counter.should.eql(3);
//noinspection BadExpressionStatementJS
wasCalled.should.be.true;
})
});
describe('Input Array Handling', function() {
it('should not modify passed-in parallel task arrays', function() {
var taskArray = ['task1', 'task2'];
runSequence(taskArray);
taskArray.should.eql(['task1', 'task2']);
});
});
describe('Asynchronous Tasks', function() {
it('should run a single task', function() {
task1.shouldPause = true;
runSequence('task1');
task1.counter.should.eql(-1);
task1.continue();
task1.counter.should.eql(1);
});
it('should run multiple tasks', function() {
task1.shouldPause = true;
task2.shouldPause = true;
runSequence('task1', 'task2');
task1.counter.should.eql(-1);
task2.counter.should.eql(-1);
task1.continue();
task1.counter.should.eql(1);
task2.counter.should.eql(-1);
task2.continue();
task2.counter.should.eql(2);
});
it('should run simultaneous tasks', function() {
task1.shouldPause = true;
runSequence(['task1', 'task2'], 'task3');
task1.counter.should.eql(-1);
task2.counter.should.eql(1);
task3.counter.should.eql(-1);
task1.continue();
task1.counter.should.eql(2);
task3.counter.should.eql(3);
});
it('should run task dependencies', function() {
task3.shouldPause = true;
runSequence('task4');
task3.counter.should.eql(-1);
task4.counter.should.eql(-1);
task3.continue();
task3.counter.should.eql(1);
task4.counter.should.eql(2);
});
it('should run task dependencies after previous tasks', function() {
task1.shouldPause = true;
task3.shouldPause = true;
task4.shouldPause = true;
runSequence('task1', 'task4');
task1.counter.should.eql(-1);
task2.counter.should.eql(-1);
task3.counter.should.eql(-1);
task4.counter.should.eql(-1);
task1.continue();
task1.counter.should.eql(1);
task3.counter.should.eql(-1);
task4.counter.should.eql(-1);
task3.continue();
task3.counter.should.eql(2);
task4.counter.should.eql(-1);
task4.continue();
task4.counter.should.eql(3);
});
});
describe('Submodules', function() {
it('should not be able to see tasks on a different gulp by default', function() {
// Ensure that the subtask is NOT seen here
(function() {
runSequence('runSubtask');
}).should.throw();
submodule.task.counter.should.eql(-1);
});
it('should be able to see tasks on a different gulp when configured', function() {
// Now make sure it runs with the internal run-sequence
(function() {
submodule.runSequence('runSubtask');
}).should.not.throw();
submodule.task.counter.should.eql(1);
});
});
describe('Duplicate Tasks', function() {
it('should not error if a task is duplicated across tasks-lists', function() {
(function() {
runSequence(['task1', 'task2'], ['task3', 'task1']);
}).should.not.throw();
task1.counter.should.eql(4);
task2.counter.should.eql(2);
task3.counter.should.eql(3);
});
it('should not error if a task is duplicated serially', function() {
(function() {
runSequence('task1', 'task2', 'task1');
}).should.not.throw();
task1.counter.should.eql(3);
task2.counter.should.eql(2);
});
it('should error if a task is duplicated within a parallel array', function() {
(function() {
runSequence(['task1', 'task1'], 'task2');
}).should.throw(/more than once/i);
shouldNotRunAnything();
});
});
function shouldNotRunAnything() {
task1.counter.should.eql(-1);
task2.counter.should.eql(-1);
task3.counter.should.eql(-1);
task4.counter.should.eql(-1);
}
describe('Error Handling', function() {
it('should error if no tasks are provided', function() {
(function() {
runSequence();
}).should.throw(/no tasks/i);
shouldNotRunAnything();
});
it('should error if a non-string task is provided', function() {
(function() {
runSequence(null);
}).should.throw(/not a valid task string/i);
shouldNotRunAnything();
});
it('should error if a a non-string task is provided as part of a task-list', function() {
(function() {
runSequence([true]);
}).should.throw(/not a valid task string/i);
shouldNotRunAnything();
});
it('should error if an empty task-list is provided', function() {
(function() {
runSequence([]);
}).should.throw();
(function() {
runSequence('task1', []);
}).should.throw(/empty array/i);
shouldNotRunAnything();
});
it('should error if an undefined task is provided', function() {
(function() {
runSequence('task1', 'hello world');
}).should.throw(/not configured/i);
shouldNotRunAnything();
});
it('should error if an undefined task is provided as part of a task-list', function() {
(function() {
runSequence(['task1', 'hello world']);
}).should.throw(/not configured/i);
shouldNotRunAnything();
});
it('should pass errors to the callback', function() {
var called = false;
(function() {
runSequence('task1', 'errTask', function(err) {
called = true;
should(err).be.ok;
});
}).should.throw(/Error Task/i);
called.should.eql(true);
})
it('should pass error if gulp execution halted in second execution', function(done) {
var stopTask = gulp.task('stopTask', function() {
if(stopTask.shouldStop) {
gulp.stop();
}
});
stopTask.shouldStop = false;
var outerTask = gulp.task('outerTask', function(cb) {
runSequence('task2', ['stopTask', 'task3'], function(err) {
if(stopTask.shouldStop) {
try {
should(err).be.ok;
err.message.should.equal('orchestration aborted');
} catch(e) {
cb();
return done(e);
}
cb();
done();
} else {
cb();
}
});
});
gulp.start('outerTask', function() {
stopTask.shouldStop = true;
task3.shouldPause = true;
gulp.start('outerTask');
});
})
});
describe('Options', function() {
var defaultOptions = extend({}, runSequence.options);
afterEach(function() {
extend(runSequence.options, defaultOptions);
});
it('should ignore empty errors when configured', function() {
runSequence.options.ignoreUndefinedTasks = true;
runSequence('task1', null, undefined, 'task2');
task1.counter.should.eql(1);
task2.counter.should.eql(2);
});
});
});