Skip to content

Commit

Permalink
Merge pull request #101 from Sanox/browserstack-local-windows-fix
Browse files Browse the repository at this point in the history
Windows fixes
  • Loading branch information
9ikhan committed Jul 12, 2015
2 parents fce9725 + 28bca6c commit 1527a3e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 38 deletions.
13 changes: 5 additions & 8 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function launchServer() {
}

function launchBrowser(browser, path) {
var url = 'http://localhost:' + serverPort.toString() + '/' + path;
var url = 'http://localhost:' + serverPort.toString() + '/' + path.replace(/\\/g, '/');
var browserString = utils.browserString(browser);
logger.debug('[%s] Launching', getTestBrowserInfo(browserString, path));

Expand Down Expand Up @@ -224,7 +224,7 @@ var statusPoller = {
config.status = 1;
}

process.kill(process.pid, 'SIGTERM');
process.exit('SIGTERM');
}
}
}, activityTimeout * 1000);
Expand All @@ -245,7 +245,7 @@ var statusPoller = {
config.status = 1;
}

process.kill(process.pid, 'SIGTERM');
process.exit('SIGTERM');
}
}
}, (activityTimeout * 1000));
Expand Down Expand Up @@ -308,11 +308,8 @@ try {
runTests();
var pid_file = process.cwd() + '/browserstack-run.pid';
fs.writeFileSync(pid_file, process.pid, 'utf-8');
process.on('SIGINT', function() {
cleanUpAndExit('SIGINT', 1);
});
process.on('SIGTERM', function() {
cleanUpAndExit('SIGTERM', config.status);
process.on('exit', function(signal){
cleanUpAndExit(signal, config.status);
});
} catch (e) {
console.log(e);
Expand Down
4 changes: 4 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ if (commit_id) {
});

var formatPath = function(path) {
if (/^win/.test(process.platform)) {
path = path.replace(/\//g, '\\');
}

if (path.indexOf(pwd) === 0) {
path = path.slice(pwd.length + 1);
}
Expand Down
62 changes: 34 additions & 28 deletions lib/local.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,32 @@
var Log = require('./logger'),
logger = new Log(global.logLevel),
exec = require('child_process').exec,
exec = require('child_process').execFile,
fs = require('fs'),
http = require('http'),
windows = ((process.platform.match(/win32/) || process.platform.match(/win64/)) !== null),
localBinary = process.cwd() + (windows ? '/BrowserStackTunnel.jar' : '/BrowserStackLocal'),
localBinary = process.cwd() + '/BrowserStackLocal' + (windows ? '.exe' : ''),
utils = require('./utils'),
config = require('./config');

var Tunnel = function Tunnel(key, port, uniqueIdentifier, callback) {
var that = {};

function tunnelLauncher() {
var tunnelCommand = (windows ? 'java -jar ' : '') + localBinary + ' ';
if (config.debug) {
tunnelCommand += ' -v ';
}
tunnelCommand += key + ' ';
tunnelCommand += 'localhost' + ',';
tunnelCommand += port.toString() + ',';
tunnelCommand += '0';
tunnelCommand += (typeof uniqueIdentifier === 'undefined') ? ' -force -onlyAutomate' : ' -tunnelIdentifier ' + uniqueIdentifier;
tunnelCommand += checkAndAddProxy();
var tunnelOptions = getTunnelOptions(key, uniqueIdentifier);

if (typeof callback !== 'function') {
callback = function(){};
}

logger.debug('[%s] Launching tunnel', new Date());
var subProcess = exec(tunnelCommand, function(error, stdout, stderr) {

var subProcess = exec(localBinary, tunnelOptions, function(error, stdout, stderr) {
logger.debug(stderr);
logger.debug(error);
if (stdout.indexOf('Error') >= 0 || error) {
logger.debug('[%s] Tunnel launching failed', new Date());
logger.debug(stdout);
process.kill(process.pid, 'SIGINT');
process.exit('SIGINT');
}
});

Expand Down Expand Up @@ -67,30 +59,44 @@ var Tunnel = function Tunnel(key, port, uniqueIdentifier, callback) {
that.process = subProcess;
}

function checkAndAddProxy() {
var proxy = config.proxy;
if (typeof proxy === 'undefined') {
return '';
function getTunnelOptions(key, uniqueIdentifier) {
var options = [key];

if (config.debug) {
options.push('-v');
}

if (!uniqueIdentifier) {
options.push('-force');
options.push('-onlyAutomate');
} else {
options.push('-localIdentifier ' + uniqueIdentifier);
}
var proxyCommand = '';
proxyCommand += ' -proxyHost ' + proxy.host;
proxyCommand += ' -proxyPort ' + proxy.port;
if(typeof proxy.username !== 'undefined'){
proxyCommand += ' -proxyUser ' + proxy.username;
proxyCommand += ' -proxyPass ' + proxy.password;

var proxy = config.proxy;

if (proxy) {
options.push('-proxyHost ' + proxy.host);
options.push('-proxyPort ' + proxy.port);

if (proxy.username && proxy.password) {
options.push('-proxyUser ' + proxy.username);
options.push('-proxyPass ' + proxy.password);
}
}
return proxyCommand;

return options;
}

fs.exists(localBinary, function(exists) {
if (exists) {
tunnelLauncher();
return;
}
logger.debug('Downloading BrowserStack Local to `%s`', localBinary);
logger.debug('Downloading BrowserStack Local to "%s"', localBinary);

var file = fs.createWriteStream(localBinary);
http.get(windows ? 'http://www.browserstack.com/BrowserStackTunnel.jar' : ('http://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal-' + process.platform + '-' + process.arch),
http.get((windows ? 'http://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal.exe' : ('http://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal-' + process.platform + '-' + process.arch)),
function(response) {
response.pipe(file);

Expand All @@ -101,7 +107,7 @@ var Tunnel = function Tunnel(key, port, uniqueIdentifier, callback) {
}, 100);
}).on('error', function(e) {
logger.info('Got error while downloading binary: ' + e.message);
process.kill(process.pid, 'SIGINT');
process.exit('SIGINT');
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ exports.Server = function Server(bsClient, workers) {
config.status = 1;
}

process.kill(process.pid, 'SIGTERM');
process.exit('SIGTERM');
}
});
});
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var alertBrowserStack = function alertBrowserStack(subject, content, params, fn)
if (typeof params === 'function') {
} else {
fn = function() {
process.kill(process.pid, 'SIGINT');
process.exit('SIGINT');
};
}
}
Expand Down

0 comments on commit 1527a3e

Please sign in to comment.