From b92ffafe61bb352dc4521af915489480e4605b68 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 9 Dec 2014 13:12:42 +0100 Subject: [PATCH 1/2] Changed to BrowserStackLocal.exe instead of the jar-file, the tunnel process is now killed when the tests are complete. Added path fix for Windows, it now works with "/" on Windows in the "test_path" config. Fixed the Process signals to work on Windows. --- bin/cli.js | 13 +++++------ lib/config.js | 4 ++++ lib/local.js | 62 ++++++++++++++++++++++++++++----------------------- lib/server.js | 2 +- lib/utils.js | 2 +- 5 files changed, 45 insertions(+), 38 deletions(-) diff --git a/bin/cli.js b/bin/cli.js index 1af389a..ff949e3 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -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(/\\/gi, '/'); var browserString = utils.browserString(browser); logger.debug('[%s] Launching', getTestBrowserInfo(browserString, path)); @@ -224,7 +224,7 @@ var statusPoller = { config.status = 1; } - process.kill(process.pid, 'SIGTERM'); + process.exit('SIGTERM'); } } }, activityTimeout * 1000); @@ -245,7 +245,7 @@ var statusPoller = { config.status = 1; } - process.kill(process.pid, 'SIGTERM'); + process.exit('SIGTERM'); } } }, (activityTimeout * 1000)); @@ -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); diff --git a/lib/config.js b/lib/config.js index bd00cb4..f4d9bdd 100644 --- a/lib/config.js +++ b/lib/config.js @@ -61,6 +61,10 @@ if (commit_id) { }); var formatPath = function(path) { + if(/^win/.test(process.platform)){ + path = path.replace(/\//gi, '\\'); + } + if (path.indexOf(pwd) === 0) { path = path.slice(pwd.length + 1); } diff --git a/lib/local.js b/lib/local.js index 9142429..7eb801f 100644 --- a/lib/local.js +++ b/lib/local.js @@ -1,10 +1,10 @@ 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'); @@ -12,29 +12,21 @@ 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'); } }); @@ -67,19 +59,33 @@ 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) { @@ -87,10 +93,10 @@ var Tunnel = function Tunnel(key, port, uniqueIdentifier, callback) { 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); @@ -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'); }); }); }); diff --git a/lib/server.js b/lib/server.js index 7a0da56..5e4d64f 100644 --- a/lib/server.js +++ b/lib/server.js @@ -254,7 +254,7 @@ exports.Server = function Server(bsClient, workers) { config.status = 1; } - process.kill(process.pid, 'SIGTERM'); + process.exit('SIGTERM'); } }); }); diff --git a/lib/utils.js b/lib/utils.js index dfd7c1c..3ede7e6 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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'); }; } } From 28bca6c1cecbbea9f86baf22dbff86f4b36e1001 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 11 Dec 2014 13:21:46 +0100 Subject: [PATCH 2/2] Styling fixes and removed unnecessary regexp flag --- bin/cli.js | 2 +- lib/config.js | 4 ++-- lib/local.js | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/bin/cli.js b/bin/cli.js index ff949e3..1e296ed 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -120,7 +120,7 @@ function launchServer() { } function launchBrowser(browser, path) { - var url = 'http://localhost:' + serverPort.toString() + '/' + path.replace(/\\/gi, '/'); + var url = 'http://localhost:' + serverPort.toString() + '/' + path.replace(/\\/g, '/'); var browserString = utils.browserString(browser); logger.debug('[%s] Launching', getTestBrowserInfo(browserString, path)); diff --git a/lib/config.js b/lib/config.js index f4d9bdd..52a7705 100644 --- a/lib/config.js +++ b/lib/config.js @@ -61,8 +61,8 @@ if (commit_id) { }); var formatPath = function(path) { - if(/^win/.test(process.platform)){ - path = path.replace(/\//gi, '\\'); + if (/^win/.test(process.platform)) { + path = path.replace(/\//g, '\\'); } if (path.indexOf(pwd) === 0) { diff --git a/lib/local.js b/lib/local.js index 7eb801f..eed801f 100644 --- a/lib/local.js +++ b/lib/local.js @@ -59,14 +59,14 @@ var Tunnel = function Tunnel(key, port, uniqueIdentifier, callback) { that.process = subProcess; } - function getTunnelOptions(key, uniqueIdentifier){ + function getTunnelOptions(key, uniqueIdentifier) { var options = [key]; if (config.debug) { options.push('-v'); } - if(!uniqueIdentifier){ + if (!uniqueIdentifier) { options.push('-force'); options.push('-onlyAutomate'); } else { @@ -75,11 +75,11 @@ var Tunnel = function Tunnel(key, port, uniqueIdentifier, callback) { var proxy = config.proxy; - if(proxy){ + if (proxy) { options.push('-proxyHost ' + proxy.host); options.push('-proxyPort ' + proxy.port); - if(proxy.username && proxy.password){ + if (proxy.username && proxy.password) { options.push('-proxyUser ' + proxy.username); options.push('-proxyPass ' + proxy.password); }