Skip to content

Commit

Permalink
Merge branch 'release-0.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
apendua committed Jan 26, 2015
2 parents 41a8ef9 + d77452b commit 8f41ed9
Show file tree
Hide file tree
Showing 36 changed files with 1,225 additions and 187 deletions.
8 changes: 4 additions & 4 deletions .versions
25 changes: 22 additions & 3 deletions lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ function Browser (options) {
var closure = null;
var driverLocation = options.webdriver || "http://localhost:9515";
var browser = wd.remote(driverLocation); // default to chromedriver
var myLocation = options.location || "http://localhost:3000"; // default to meteor
var dontWaitForMeteor = options.dontWaitForMeteor !== undefined ? !!options.dontWaitForMeteor : false;
var meteorLoadTimeout = options.meteorLoadTimeout !== undefined ? options.meteorLoadTimeout : 2000;
var browserPromise = null;
var capabilities = options.capabilities || {};
var windowSize = options.windowSize;
var portscanner = require('portscanner');
var URL = require('url');
var myLocation = options.location || "http://localhost:3000";
var ddpSetupProvider = typeof myLocation === 'string' ? Promise.resolve(myLocation) : myLocation._ddpSetupProvider;

if (!ddpSetupProvider) {
throw new Error('the location option must be either string or a meteor server');
}

self.getBrowserPromise = function () {

Expand Down Expand Up @@ -67,8 +72,22 @@ function Browser (options) {
} else {
afterResize();
}
//----------------------

function afterResize() {
if (ddpSetupProvider) {
ddpSetupProvider().then(function (setup) {
if (setup.host) {
getLocation(setup.host);
} else {
getLocation('http://localhost:' + setup.port);
}
}).catch(_reject);
} else {
resolve({ browser: browser, closure: closure });
}
}

function getLocation(myLocation) {
browser.get(myLocation, function (err) {
if (err) {
return _reject(err);
Expand Down Expand Up @@ -122,7 +141,7 @@ function Browser (options) {

}); // setAsyncScriptTimeout
}); // get
} // afterResize
} // getLocation
}); // init
}); // checkPortStatus
}); // Promise
Expand Down
2 changes: 1 addition & 1 deletion lib/build.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var MongoServerAsPromise = require('./mongo');
var MongoServerAsPromise = require('./mongoDBProcess');
var Promise = require('es6-promise').Promise;
var chalk = require('chalk');
var spawn = require('child_process').spawn;
Expand Down
31 changes: 18 additions & 13 deletions lib/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Module dependencies.
*/

var MongoServerAsPromise = require('./mongo');
var MongoDBProcess = require('./mongoDBProcess');
var MongoClient = require('mongodb').MongoClient;
var Promise = require('es6-promise').Promise;
var url = require('url');
Expand All @@ -17,33 +17,33 @@ var url = require('url');
* - {String} pathToApp
* - {String} dbName
*/
module.exports = function DatabaseAsPromise (options) {
module.exports = function MongoDatabase (options) {
"use strict";

options = options || {};

var dbName = options.dbName || getRandomName();
var mongoUrl = options.mongoUrl || '';
var pathToApp = options.pathToApp || path.resolve('.');
var parsedUrl = null;
var getMongoUrl = null;
var needsCleanup = false;
var dbName = options.dbName || getRandomName();
var mongoUrl = options.mongoUrl || '';
var pathToApp = options.pathToApp || path.resolve('.');
var parsedUrl = null;
var mongoUrlPromise = null;
var needsCleanup = false;

if (mongoUrl) {
parsedUrl = url.parse(mongoUrl);
parsedUrl.path = parsedUrl.path || dbName;
//---------------------------------------------------
getMongoUrl = Promise.resolve(url.format(parsedUrl));
//-------------------------------------------------------
mongoUrlPromise = Promise.resolve(url.format(parsedUrl));
} else {
needsCleanup = true;
//--------------------------------------------------------------
getMongoUrl = new MongoServerAsPromise({ pathToApp: pathToApp })
//------------------------------------------------------------
mongoUrlPromise = new MongoDBProcess({ pathToApp: pathToApp })
.then(function (mongoUrl) {
return mongoUrl + '/' + dbName;
});
}

return getMongoUrl.then(function (mongoUrl) {
var databasePromise = mongoUrlPromise.then(function (mongoUrl) {
return new Promise(function (resolve, reject) {
MongoClient.connect(mongoUrl, function (err, db) {
if (err) {
Expand All @@ -61,6 +61,11 @@ module.exports = function DatabaseAsPromise (options) {
});
});

databasePromise.getMongoUrlPromise = function () {
return mongoUrlPromise;
};

return databasePromise;
};

function getRandomName() {
Expand Down
32 changes: 32 additions & 0 deletions lib/generic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var genericPromiseChain = require('./genericPromiseChain');
var Promise = require('es6-promise').Promise;
var either = require('./tools').either;

module.exports = function generic(methods, myPrototype) {

var GenericPromiseChain = genericPromiseChain(methods, myPrototype);

function Generic (operand) {
"use strict";

this._operand = operand;
}

Generic.prototype = Object.create(myPrototype, {
methods: { value: GenericPromiseChain.prototype.methods }
});

GenericPromiseChain.prototype.methods.forEach(function (name) {
"use strict";

Generic.prototype[name] = function () {
var chain = new GenericPromiseChain(this._operand);
return chain[name].apply(chain, arguments);
};

});

return Generic;

}

137 changes: 137 additions & 0 deletions lib/genericPromiseChain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
var Promise = require('es6-promise').Promise;
var either = require('./tools').either;

module.exports = function genericPromiseChain(methods, myPrototype) {

function GenericPromiseChain (operand) {
"use strict";

var self = this;

this._operand = operand;
this._promise = typeof operand === 'function' ? operand() : operand;
}

GenericPromiseChain.prototype = Object.create(myPrototype);

[ 'then', 'catch' ].forEach(function (name) {

GenericPromiseChain.prototype[name] = function () {
"use strict";
this._promise = this._promise[name].apply(this._promise, arguments);
return this;
};

});

GenericPromiseChain.prototype.always = function (callback) {
"use strict";

return this.then(function (result) { callback(null, result) }, callback);
};

GenericPromiseChain.prototype.sleep = function (timeout) {
"use strict";

var self = this;
return self.then(function () {
return new Promise(function (resolve) {
setTimeout(resolve, timeout);
});
});
};

GenericPromiseChain.prototype.expectError = function (callback) {
"use strict";

var self = this;
return self.then(function () {
throw new Error('exception was not thrown');
}, callback);
};

GenericPromiseChain.prototype.noWait = function () {
"use strict";

return new GenericPromiseChain(this._operand);
};

GenericPromiseChain.prototype.branch = function () {
"use strict";

return new GenericPromiseChain(this._operand, this._promise);
};

GenericPromiseChain.prototype.yet = function (code, args) {
"use strict";

var args = Array.prototype.slice.call(arguments, 0);
var self = this;
//--------------------------------
return self.catch(function (err) {
return self.noWait().execute(code, args).then(function (errMessage) {
throw new Error(err.message + ' ' + errMessage);
});
});
};

GenericPromiseChain.prototype.methods = methods.concat([
'__custom__',
'catch',
'then',
'always',
'sleep',
'expectError',
'noWait',
'branch',
'yet',
]);

GenericPromiseChain.prototype.__custom__ = function (action) {
var self = this;
self._promise = Promise.all([
typeof self._operand === 'function' ? self._operand() : self._operand, self._promise
]).then(function (all) {
return new Promise(function (resolve, reject) {
var operand = all[0];
if (!operand || typeof operand !== 'object') {
reject(new Error('GenericPromiseChain: invalid operand'));
}
action(operand, either(cleanError(reject)).or(resolve));
});
});
return self;
};

methods.forEach(function (name) {
"use strict";

/**
* Update the current promise and return this to allow chaining.
*/
GenericPromiseChain.prototype[name] = function () {
var args = Array.prototype.slice.call(arguments, 0);
return this.__custom__(function (operand) {
if (!operand[name]) {
reject(new Error('GenericPromiseChain: operand does not implement method: ' + name));
} else {
args.push(either(cleanError(reject)).or(resolve));
operand[name].apply(operand, args);
}
});
};

});

return GenericPromiseChain;

}

function cleanError(reject) {
return function (err) {
if (err && !(err instanceof Error)) {
err = new Error(err.message || err.toString());
}
reject(err);
}
}
Loading

0 comments on commit 8f41ed9

Please sign in to comment.