-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathexec-suite.ts
executable file
·422 lines (328 loc) · 13.8 KB
/
exec-suite.ts
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
'use strict';
//dts
import {IGlobalSumanObj, ISumanDomain, ICurrentPaddingCount} from "suman-types/dts/global";
import {ISuman, Suman} from "./suman";
import {ICreateOpts, TCreateHook} from "suman-types/dts/index-init";
import {IInjectionDeps} from "suman-types/dts/injection";
//polyfills
const process = require('suman-browser-polyfills/modules/process');
const global = require('suman-browser-polyfills/modules/global');
//core
import domain = require('domain');
import path = require('path');
import assert = require('assert');
import EE = require('events');
import fs = require('fs');
import util = require('util');
//npm
import {VamootProxy} from 'vamoot';
import McProxy = require('proxy-mcproxy');
import chalk from 'chalk';
import * as async from 'async';
const fnArgs = require('function-arguments');
const pragmatik = require('pragmatik');
//project
const _suman: IGlobalSumanObj = global.__suman = (global.__suman || {});
import rules = require('./helpers/handle-varargs');
import {constants} from './config/suman-constants';
import su = require('suman-utils');
import {makeGracefulExit} from './make-graceful-exit';
import {acquireIocDeps} from './acquire-dependencies/acquire-ioc-deps';
import {TestBlock} from './test-suite-helpers/test-suite';
import {fatalRequestReply} from './helpers/general';
import {handleInjections} from './test-suite-helpers/handle-injections';
import {makeOnSumanCompleted} from './helpers/general';
import {evalOptions} from './helpers/general';
import general = require('./helpers/general');
import {makeSumanMethods} from "./test-suite-helpers/suman-methods";
import {makeHandleBeforesAndAfters} from './test-suite-helpers/make-handle-befores-afters';
import {makeNotifyParent} from './test-suite-helpers/notify-parent-that-child-is-complete';
//////////////////////////////////////////////////////////////////////////////////////////////////////////
export const execSuite = function (suman: ISuman): Function {
// we set this so that after.always hooks can run
_suman.whichSuman = suman;
const sumanConfig = suman.config;
suman.dateSuiteStarted = Date.now();
const onSumanCompleted = makeOnSumanCompleted(suman);
const gracefulExit = makeGracefulExit(suman);
const handleBeforesAndAfters = makeHandleBeforesAndAfters(suman, gracefulExit);
const notifyParent = makeNotifyParent(suman, gracefulExit, handleBeforesAndAfters);
const createInjector = makeSumanMethods(suman, gracefulExit, handleBeforesAndAfters, notifyParent);
const allDescribeBlocks = suman.allDescribeBlocks;
////////////////////////////////////////////////////////////////////////////////////////
return function runRootSuite($$desc: any, $$opts: any): void {
const sumanOpts = suman.opts;
const isPreParsed = su.isObject($$opts) && $$opts.__preParsed;
const args = pragmatik.parse(arguments, rules.createSignature, isPreParsed);
const vetted = general.parseArgs(args);
const [$desc, opts, cb] = vetted.args;
const arrayDeps = vetted.arrayDeps;
let iocDepNames: Array<string>;
assert(opts.__preParsed, 'Suman implementation error. ' +
'Options should be pre-parsed at this point in the program. Please report.');
delete opts.__preParsed;
if (arrayDeps && arrayDeps.length > 0) {
iocDepNames = evalOptions(arrayDeps, opts);
}
else {
iocDepNames = [];
}
if (opts.__toBeSourcedForIOC) {
Object.keys(opts.__toBeSourcedForIOC).forEach(function (v: string) {
iocDepNames.push(v);
});
}
if (su.isObject(opts.inject)) {
Object.keys(opts.inject).forEach(function (v: string) {
iocDepNames.push(v);
});
}
if (Array.isArray(opts.inject)) {
opts.inject.forEach(function (v: string) {
iocDepNames.push(v);
});
}
const desc = ($desc === '[suman-placeholder]') ? suman.slicedFileName : $desc;
// suman description is the same as the description of the top level test block
suman.desc = desc;
if (su.isGeneratorFn(cb) || su.isAsyncFn(cb)) {
const msg = constants.ERROR_MESSAGES.INVALID_FUNCTION_TYPE_USAGE;
return fatalRequestReply({
type: constants.runner_message_type.FATAL,
data: {
errors: [msg],
msg: msg
}
},
function () {
console.error('\n');
_suman.log.error(msg);
let err = new Error('Suman usage error => invalid arrow/generator function usage.').stack;
_suman.log.error(err);
_suman.writeTestError(err);
process.exit(constants.EXIT_CODES.INVALID_ARROW_FUNCTION_USAGE);
});
}
const deps = suman.deps = fnArgs(cb);
const delayOptionElected = opts.delay;
//TODO: we need to warn the user when the give an option to describe or it that is not recognized
suman.rootSuiteDescription = desc;
if (!opts.only && _suman.describeOnlyIsTriggered) {
_suman.writeTestError(' => Suite with description => "' + desc + '" was skipped because another test suite in this file\n' +
'invoked the only option.');
onSumanCompleted(0, ' => skipped due to "only" option invoked on another test suite');
return;
}
if (opts.skip) {
_suman.writeTestError(' => Suite with description => "' + desc + '" was skipped because because you\n' +
'passed the "skip" option to the test suite.');
onSumanCompleted(0, ' => skipped due to explicit call of "skip" option');
return;
}
const suite = new TestBlock({
desc,
isTopLevel: true,
opts,
suman,
gracefulExit,
handleBeforesAndAfters,
notifyParent
});
suite.bIsFirstArg = deps[0] === 'b';
suite.isRootSuite = true;
suite.bindExtras();
allDescribeBlocks.push(suite);
// Object.defineProperty(suite, '__inject',{
// writable: false,
// value: {}
// });
//
// Object.defineProperty(suite, '$inject',{
// writable: false,
// value: McProxy.create(suite.__inject)
// });
const v = suite.__supply = {};
suite.supply = McProxy.create(v);
try {
assert(typeof _suman.globalHooksFn === 'function', '<suman.hooks.js> file must export a function.');
_suman.globalHooksFn.call(null, suite);
}
catch (err) {
_suman.log.error(chalk.yellow('warning: Could not load your "suman.hooks.js" file'));
if (!/Cannot find module/i.test(err.message)) {
throw err;
}
}
if (deps.length < 1) {
process.nextTick(startWholeShebang, null, []);
}
else {
const d = domain.create();
d.once('error', function (err: Error) {
console.error(err.stack || err);
_suman.writeTestError(err.stack || err);
d.exit();
process.nextTick(function () {
err = new Error('Suman usage error => Error acquiring IOC deps => \n' + (err.stack || err));
err.sumanFatal = true;
err.sumanExitCode = constants.EXIT_CODES.IOC_DEPS_ACQUISITION_ERROR;
_suman.log.error(err.stack || err);
gracefulExit(err, null);
});
});
d.run(function acquireIocDepsDomainRun() {
acquireIocDeps(suman, iocDepNames, suite, {}, function (err: any, iocDeps: IInjectionDeps) {
if (err) {
_suman.log.error('Error acquiring IoC deps:', err.stack || err);
return process.exit(constants.EXIT_CODES.ERROR_ACQUIRING_IOC_DEPS);
}
suite.ioc = iocDeps;
let mappedDeps: Array<any> = createInjector(suite, deps);
try {
d.exit();
}
finally {
process.nextTick(startWholeShebang, mappedDeps);
}
});
});
}
//TODO: http://stackoverflow.com/questions/27192917/using-a-domain-to-test-for-an-error-thrown-deep-in-the-call-stack-in-node
function startWholeShebang(deps: Array<any>) {
const d = domain.create();
d.once('error', function ($err: any) {
d.exit();
process.nextTick(gracefulExit, {
message: $err.message || $err,
stack: $err.stack || $err,
sumanFatal: true,
sumanExitCode: constants.EXIT_CODES.ERROR_IN_ROOT_SUITE_BLOCK
});
});
Object.defineProperty(suite, 'shared', {value: new VamootProxy(), writable: false});
d.run(function () {
suite.fatal = function (err: any) {
process.nextTick(gracefulExit, {
message: 'Fatal error experienced in root suite => ' + (err.message || err),
stack: err.stack || err,
sumanExitCode: constants.EXIT_CODES.ERROR_PASSED_AS_FIRST_ARG_TO_DELAY_FUNCTION
});
};
if (delayOptionElected) {
suite.isDelayed = true;
const to = setTimeout(function () {
console.log('\n\n => Suman fatal error => suite.resume() function was not called within alloted time.');
process.exit(constants.EXIT_CODES.DELAY_FUNCTION_TIMED_OUT);
}, _suman.weAreDebugging ? 5000000 : 11000);
if (sumanOpts.verbosity > 8) {
console.log(' => Waiting for delay() function to be called...');
}
let callable = true;
suite.__resume = function (val: any) {
if (callable) {
callable = false;
clearTimeout(to);
process.nextTick(function () {
suman.ctx = null; // no suite here; don't need to call bindExtras here, because root suite has no parent
suite.isSetupComplete = true; // keep this, needs be called asynchronously
//pass start function all the way through program until last child delay call is invoked!
suite.invokeChildren(val, start);
});
}
else {
_suman.log.error('Suman usage warning => suite.resume() was called more than once.');
}
};
const str = cb.toString();
if (!su.checkForValInStr(str, /resume/g, 0)) { //TODO this will not work when delay is simply commented out
process.nextTick(function () {
console.error(new Error(' => Suman usage error => suite.resume() method needs to be called to continue,' +
' but the resume method was never referenced, so your test cases would never be invoked before timing out.').stack
+ '\n =>' + str);
process.exit(constants.EXIT_CODES.DELAY_NOT_REFERENCED);
});
}
else {
cb.apply(suite, deps);
}
}
else {
suite.__resume = function () {
_suman.log.warning('usage warning => suite.resume() has become a noop since delay option is falsy.');
};
cb.apply(null, deps);
suite.isSetupComplete = true;
handleInjections(suite, function (err: any) {
if (err) {
_suman.log.error(err.stack || err);
return gracefulExit(err, null);
}
process.nextTick(function () {
suite.invokeChildren(null, start); //pass start function all the way through program until last child delay call is invoked!
});
});
}
}
);
}
const start = function () {
_suman.suiteResultEmitter.emit('suman-test-registered', function () {
const sumanOpts = suman.opts;
const currentPaddingCount = _suman.currentPaddingCount
= (_suman.currentPaddingCount || ({} as ICurrentPaddingCount));
currentPaddingCount.val = 1; // always reset
const runSuite = function (suite: TestBlock, cb: Function) {
if (_suman.uncaughtExceptionTriggered) {
_suman.log.error(`"UncaughtException:Triggered" => halting program.\n[${__filename}]`);
return;
}
let limit = 1;
if (suite.parallel) {
if (suite.limit) {
limit = Math.min(suite.limit, 300);
}
else {
limit = sumanConfig.DEFAULT_PARALLEL_BLOCK_LIMIT || constants.DEFAULT_PARALLEL_BLOCK_LIMIT;
}
}
assert(Number.isInteger(limit) && limit > 0 && limit < 100, 'limit must be an integer between 1 and 100, inclusive.');
suite.__startSuite((err: any, results: any) => { // results are object from async.series
results && _suman.log.error('Suman extraneous results:', results);
err && _suman.log.error('Suman extraneous test error:', suite);
const children = suite.getChildren().filter(function (child: TestBlock) {
return !child.skipped;
});
if (children.length < 1) {
return process.nextTick(cb)
}
sumanOpts.series && (currentPaddingCount.val += 3);
async.eachLimit(children, limit, function (child: TestBlock, cb: Function) {
// this could be reduced, but leave it for clarity
runSuite(child, cb);
},
function (err: any) {
sumanOpts.series && (currentPaddingCount.val -= 3);
err && _suman.log.error('Suman implementation error:', err.stack || err);
process.nextTick(cb);
});
});
};
runSuite(allDescribeBlocks[0], function complete() {
suman.dateSuiteFinished = Date.now();
if (_suman.uncaughtExceptionTriggered) {
_suman.log.error(`"UncaughtException" event => halting program.\n[${__filename}]`);
return;
}
if (sumanOpts.parallel_max) {
suman.getQueue().drain = function () {
onSumanCompleted(0, null);
}
}
else {
onSumanCompleted(0, null);
}
});
});
}
};
};