From 4f1fe68882dedba662752e722b9e7b76bfed19b6 Mon Sep 17 00:00:00 2001 From: Hank Duan Date: Tue, 23 Sep 2014 13:22:26 -0700 Subject: [PATCH] feat(runner): Allow onCleanup to accept a file --- lib/runner.js | 59 ++++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/lib/runner.js b/lib/runner.js index 485e5ee08..f8cb33868 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -52,35 +52,23 @@ util.inherits(Runner, EventEmitter); /** * Internal helper for abstraction of polymorphic filenameOrFn properties. * @private - * @param {Array} source The Array that we'll be iterating through - * as we evaluate whether to require or execute each item. - * @return {q.Promise} A promise that will resolve when the test preparers - * are finished. + * @param {object} filenameOrFn The filename or function that we will execute. + * @return {object} A number or a promise that will resolve when the test + * preparers are finished. */ -Runner.prototype.runFilenamesOrFns_ = function(source) { - var filenameOrFn; - var promises = []; - var returned; - for (var i = 0; i < source.length; i++) { - filenameOrFn = source[i]; - if (filenameOrFn) { - if (typeof filenameOrFn === 'function') { - returned = filenameOrFn(); - } else if (typeof filenameOrFn === 'string') { - returned = require(path.resolve(this.config_.configDir, filenameOrFn)); - } else { - // TODO - this is not generic. - throw 'config.onPrepare must be a string or function'; - } - if (q.isPromiseAlike(returned)) { - promises.push(returned); - } +Runner.prototype.runFilenameOrFn_ = function(filenameOrFn, args) { + if (filenameOrFn) { + if (typeof filenameOrFn === 'function') { + return filenameOrFn.apply(null, args); + } else if (typeof filenameOrFn === 'string') { + return require(path.resolve(this.config_.configDir, filenameOrFn)); + } else { + // TODO - this is not generic. + throw 'config.onPrepare and config.onCleanUp must be a string or function'; } } - return q.all(promises); }; - /** * Registrar for testPreparers - executed right before tests run. * @public @@ -98,7 +86,17 @@ Runner.prototype.registerTestPreparer = function(filenameOrFn) { * are finished. */ Runner.prototype.runTestPreparers = function() { - return this.runFilenamesOrFns_(this.preparers_); + var filenameOrFn; + var promises = []; + var returned; + for (var i = 0; i < this.preparers_.length; i++) { + filenameOrFn = this.preparers_[i]; + returned = this.runFilenameOrFn_(filenameOrFn); + if (q.isPromiseAlike(returned)) { + promises.push(returned); + } + } + return q.all(promises); }; @@ -139,13 +137,12 @@ Runner.prototype.loadDriverProvider_ = function() { * @param {int} Standard unix exit code */ Runner.prototype.exit_ = function(exitCode) { - if (typeof this.config_.onCleanUp === 'function') { - var val = this.config_.onCleanUp(exitCode); - if (typeof val === 'number' || q.isPromiseAlike(val)) { - return val; - } + var returned = this.runFilenameOrFn_(this.config_.onCleanUp, [exitCode]); + if (typeof returned === 'number' || q.isPromiseAlike(returned)) { + return returned; + } else { + return exitCode; } - return exitCode; };