Skip to content

Commit

Permalink
feat(config): support running on a custom hostname
Browse files Browse the repository at this point in the history
Add an optional 'hostname' parameter to the configuration
Usecases: running testacular on cloud9 and other PaaS;
legacy code that impose specific hostnames.
  • Loading branch information
hmalphettes committed Jan 17, 2013
1 parent 2be0f3a commit b8c5fe8
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 deletions.
1 change: 1 addition & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ var parseConfig = function(configFilePath, cliOptions) {
var config = {
port: constant.DEFAULT_PORT,
runnerPort: constant.DEFAULT_RUNNER_PORT,
hostname: constant.DEFAULT_HOSTNAME,
basePath: '',
files: [],
exclude: [],
Expand Down
1 change: 1 addition & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ exports.VERSION = pkg.version;

exports.DEFAULT_PORT = 9876;
exports.DEFAULT_RUNNER_PORT = 9100;
exports.DEFAULT_HOSTNAME = 'localhost';

// log levels
exports.LOG_DISABLE = 'OFF';
Expand Down
4 changes: 2 additions & 2 deletions lib/launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ var Launcher = function(emitter) {
var browsers = [];


this.launch = function(names, port, urlRoot, timeout, retryLimit) {
var url = 'http://localhost:' + port + urlRoot;
this.launch = function(names, hostname, port, urlRoot, timeout, retryLimit) {
var url = 'http://' + hostname + ':' + port + urlRoot;
var Cls, browser;

names.forEach(function(name) {
Expand Down
10 changes: 5 additions & 5 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ exports.start = function(cliOptions, done) {
});

webServer.listen(config.port, function() {
log.info('Testacular server started at http://localhost:' + config.port + config.urlRoot);
log.info('Testacular server started at http://' + config.hostname + ':' + config.port + config.urlRoot);

if (config.browsers && config.browsers.length) {
launcher.launch(config.browsers, config.port, config.urlRoot, config.captureTimeout, 3);
launcher.launch(config.browsers, config.hostname, config.port, config.urlRoot, config.captureTimeout, 3);
}
});

Expand Down Expand Up @@ -98,7 +98,7 @@ exports.start = function(cliOptions, done) {
var nonReady = [];

if (!capturedBrowsers.length) {
log.warn('No captured browser, open http://localhost:' + config.port + config.urlRoot);
log.warn('No captured browser, open http://' + config.hostname + ':' + config.port + config.urlRoot);
return false;
} else if (capturedBrowsers.areAllReady(nonReady)) {
log.debug('All browsers are ready, executing');
Expand Down Expand Up @@ -150,8 +150,8 @@ exports.start = function(cliOptions, done) {
log.debug('Execution (fired by runner)');

if (!capturedBrowsers.length) {
log.warn('No captured browser, open http://localhost:' + config.port + config.urlRoot);
socket.end('No captured browser, open http://localhost:' + config.port + config.urlRoot + '\n');
log.warn('No captured browser, open http://' + config.hostname + ':' + config.port + config.urlRoot);
socket.end('No captured browser, open http://' + config.hostname + ':' + config.port + config.urlRoot + '\n');
return;
}

Expand Down
18 changes: 11 additions & 7 deletions test/unit/launcher.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,22 @@ describe 'launcher', ->
describe 'launch', ->

it 'should start all browsers', ->
l.launch ['Chrome', 'ChromeCanary'], 1234
l.launch ['Chrome', 'ChromeCanary'], 'localhost', 1234

expect(mockSpawn).to.have.been.calledTwice
expect(mockSpawn.getCall(0).args[0]).to.equal 'google-chrome'
expect(mockSpawn.getCall(1).args[0]).to.equal 'google-chrome-canary'


it 'should allow launching a script', ->
l.launch ['/usr/local/bin/special-browser'], 1234, '/'
l.launch ['/usr/local/bin/special-browser'], 'localhost', 1234, '/'
expect(mockSpawn).to.have.been.calledWith '/usr/local/bin/special-browser', ['http://localhost:1234/?id=1']


it 'should use the non default host', ->
l.launch ['/usr/local/bin/special-browser'], '127.0.0.1', 1234, '/'
expect(mockSpawn).to.have.been.calledWith '/usr/local/bin/special-browser', ['http://127.0.0.1:1234/?id=1']


describe 'kill', ->
exitSpy = null
Expand All @@ -86,7 +90,7 @@ describe 'launcher', ->
exitSpy = sinon.spy()

it 'should kill all running processe', ->
l.launch ['Chrome', 'ChromeCanary'], 1234
l.launch ['Chrome', 'ChromeCanary'], 'localhost', 1234
l.kill()

expect(mockSpawn._processes.length).to.equal 2
Expand All @@ -95,7 +99,7 @@ describe 'launcher', ->


it 'should call callback when all processes killed', ->
l.launch ['Chrome', 'ChromeCanary'], 1234
l.launch ['Chrome', 'ChromeCanary'], 'localhost', 1234
l.kill exitSpy

expect(exitSpy).not.to.have.been.called
Expand All @@ -112,7 +116,7 @@ describe 'launcher', ->


it 'should call callback even if a process had already been killed', (done) ->
l.launch ['Chrome', 'ChromeCanary'], 1234, '/', 0, 1 # disable retry
l.launch ['Chrome', 'ChromeCanary'], 'localhost', 1234, '/', 0, 1 # disable retry
mockSpawn._processes[0].emit 'close', 1
mockSpawn._processes[1].emit 'close', 1

Expand All @@ -126,7 +130,7 @@ describe 'launcher', ->
describe 'areAllCaptured', ->

it 'should return true if only if all browsers captured', ->
l.launch ['Chrome', 'ChromeCanary'], 1234
l.launch ['Chrome', 'ChromeCanary'], 'localhost', 1234

expect(l.areAllCaptured()).to.equal false

Expand All @@ -140,7 +144,7 @@ describe 'launcher', ->
describe 'onExit', ->

it 'should kill all browsers', (done) ->
l.launch ['Chrome', 'ChromeCanary'], 1234, '/', 0, 1
l.launch ['Chrome', 'ChromeCanary'], 'localhost', 1234, '/', 0, 1

emitter.emitAsync('exit').then done

Expand Down

0 comments on commit b8c5fe8

Please sign in to comment.