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

Commit 4f1fe68

Browse files
committed
feat(runner): Allow onCleanup to accept a file
1 parent 189c912 commit 4f1fe68

File tree

1 file changed

+28
-31
lines changed

1 file changed

+28
-31
lines changed

lib/runner.js

+28-31
Original file line numberDiff line numberDiff line change
@@ -52,35 +52,23 @@ util.inherits(Runner, EventEmitter);
5252
/**
5353
* Internal helper for abstraction of polymorphic filenameOrFn properties.
5454
* @private
55-
* @param {Array} source The Array that we'll be iterating through
56-
* as we evaluate whether to require or execute each item.
57-
* @return {q.Promise} A promise that will resolve when the test preparers
58-
* are finished.
55+
* @param {object} filenameOrFn The filename or function that we will execute.
56+
* @return {object} A number or a promise that will resolve when the test
57+
* preparers are finished.
5958
*/
60-
Runner.prototype.runFilenamesOrFns_ = function(source) {
61-
var filenameOrFn;
62-
var promises = [];
63-
var returned;
64-
for (var i = 0; i < source.length; i++) {
65-
filenameOrFn = source[i];
66-
if (filenameOrFn) {
67-
if (typeof filenameOrFn === 'function') {
68-
returned = filenameOrFn();
69-
} else if (typeof filenameOrFn === 'string') {
70-
returned = require(path.resolve(this.config_.configDir, filenameOrFn));
71-
} else {
72-
// TODO - this is not generic.
73-
throw 'config.onPrepare must be a string or function';
74-
}
75-
if (q.isPromiseAlike(returned)) {
76-
promises.push(returned);
77-
}
59+
Runner.prototype.runFilenameOrFn_ = function(filenameOrFn, args) {
60+
if (filenameOrFn) {
61+
if (typeof filenameOrFn === 'function') {
62+
return filenameOrFn.apply(null, args);
63+
} else if (typeof filenameOrFn === 'string') {
64+
return require(path.resolve(this.config_.configDir, filenameOrFn));
65+
} else {
66+
// TODO - this is not generic.
67+
throw 'config.onPrepare and config.onCleanUp must be a string or function';
7868
}
7969
}
80-
return q.all(promises);
8170
};
8271

83-
8472
/**
8573
* Registrar for testPreparers - executed right before tests run.
8674
* @public
@@ -98,7 +86,17 @@ Runner.prototype.registerTestPreparer = function(filenameOrFn) {
9886
* are finished.
9987
*/
10088
Runner.prototype.runTestPreparers = function() {
101-
return this.runFilenamesOrFns_(this.preparers_);
89+
var filenameOrFn;
90+
var promises = [];
91+
var returned;
92+
for (var i = 0; i < this.preparers_.length; i++) {
93+
filenameOrFn = this.preparers_[i];
94+
returned = this.runFilenameOrFn_(filenameOrFn);
95+
if (q.isPromiseAlike(returned)) {
96+
promises.push(returned);
97+
}
98+
}
99+
return q.all(promises);
102100
};
103101

104102

@@ -139,13 +137,12 @@ Runner.prototype.loadDriverProvider_ = function() {
139137
* @param {int} Standard unix exit code
140138
*/
141139
Runner.prototype.exit_ = function(exitCode) {
142-
if (typeof this.config_.onCleanUp === 'function') {
143-
var val = this.config_.onCleanUp(exitCode);
144-
if (typeof val === 'number' || q.isPromiseAlike(val)) {
145-
return val;
146-
}
140+
var returned = this.runFilenameOrFn_(this.config_.onCleanUp, [exitCode]);
141+
if (typeof returned === 'number' || q.isPromiseAlike(returned)) {
142+
return returned;
143+
} else {
144+
return exitCode;
147145
}
148-
return exitCode;
149146
};
150147

151148

0 commit comments

Comments
 (0)