Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
feat(runner): Allow onCleanup to accept a file
Browse files Browse the repository at this point in the history
  • Loading branch information
hankduan committed Sep 23, 2014
1 parent 189c912 commit 4f1fe68
Showing 1 changed file with 28 additions and 31 deletions.
59 changes: 28 additions & 31 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));

This comment has been minimized.

Copy link
@elgalu

elgalu Sep 24, 2014

Contributor

Looks like you're not applying args here.
This feature is not working for me, I've merged from master.

feat(runner): Allow onCleanup to accept a file

Could you add a test case so I figure what should I be exporting from the custom onCleanUp.js external file?

This comment has been minimized.

Copy link
@hankduan

hankduan Sep 24, 2014

Author Contributor

Unfortunately if you are running an external script instead of just passing in a function, it wouldn't be able to take in an arg. Unless we want to pass the arg as either ENV variable or parameter and have the script parse that. The use for this is restricted to some kind of shutdown after the test (i.e. clean up server, etc). Do you want to suggest an alternate scheme?

This comment has been minimized.

Copy link
@elgalu

elgalu Sep 24, 2014

Contributor

I think I misunderstood this feature.
I expected this to work:

  onCleanUp: '../onCleanUp.js', // had to add '../' even though file is in the same path, same issue with onPrepare

Given my onCleanUp.js

module.exports = function(exitCode) {
  // ... stuff with exitCode

However this does work:

  onCleanUp: require('./onCleanUp.js'),

So I'm fine with requiring it myself ;)

This comment has been minimized.

Copy link
@hankduan

hankduan Oct 6, 2014

Author Contributor

I've fixed it in #1404. Thanks for pointing out the use case!

} 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
Expand All @@ -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);
};


Expand Down Expand Up @@ -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;
};


Expand Down

0 comments on commit 4f1fe68

Please sign in to comment.