The test.js task starts the development server and runs the tests in PhantomJS. As mentioned in the readme, when running the tests continuously during development, it is more convenient to start the dev server and visit the root of the test directory in your browser. Later we can bring in Karma to drive PhantomJS and other browsers, but this simple "run once" task is useful for the CI job.
69 lines
1.6 KiB
JavaScript
69 lines
1.6 KiB
JavaScript
/**
|
|
* This task starts a dev server that provides a script loader for OpenLayers
|
|
* and Closure Library and runs tests in PhantomJS.
|
|
*/
|
|
|
|
var path = require('path');
|
|
var spawn = require('child_process').spawn;
|
|
|
|
var phantomjs = require('phantomjs');
|
|
|
|
var serve = require('./serve');
|
|
|
|
/**
|
|
* Try listening for incoming connections on a range of ports.
|
|
* @param {number} min Minimum port to try.
|
|
* @param {number} max Maximum port to try.
|
|
* @param {http.Server} server The server.
|
|
* @param {function(Error)} callback Callback called with any error.
|
|
*/
|
|
function listen(min, max, server, callback) {
|
|
function _listen(port) {
|
|
server.once('error', function(err) {
|
|
if (err.code === 'EADDRINUSE') {
|
|
++port;
|
|
if (port < max) {
|
|
_listen(port);
|
|
} else {
|
|
callback(new Error('Could not find an open port'));
|
|
}
|
|
} else {
|
|
callback(err);
|
|
}
|
|
});
|
|
server.listen(port, callback);
|
|
}
|
|
_listen(min);
|
|
}
|
|
|
|
|
|
/**
|
|
* Create the debug server and run tests.
|
|
*/
|
|
serve.createServer(function(err, server) {
|
|
if (err) {
|
|
console.error(err.message);
|
|
process.exit(1);
|
|
}
|
|
|
|
listen(3001, 3005, server, function(err) {
|
|
if (err) {
|
|
console.error('Server failed to start: ' + err.message);
|
|
process.exit(1);
|
|
}
|
|
|
|
var address = server.address();
|
|
var url = 'http://' + address.address + ':' + address.port;
|
|
var args = [
|
|
path.join(__dirname, '..', 'test', 'mocha-phantomjs.js'),
|
|
url + '/test/index.html'
|
|
];
|
|
|
|
var child = spawn(phantomjs.path, args, {stdio: 'inherit'});
|
|
child.on('exit', function(code) {
|
|
process.exit(code);
|
|
});
|
|
});
|
|
|
|
});
|