Skip to content

Commit 4933de6

Browse files
committed
Build: Add npm-test script, run mocha and jshint (new), fix lint issues
Adds .editorconfig and .jshintrc for better code consistency. Run jshint along with moch tests using `npm test`. Fixes tons of lint issues, like double quotes instead of single quotes (there were more single then double quotes, so I went with single quotes).
1 parent 1d3f4a3 commit 4933de6

12 files changed

+227
-184
lines changed

.editorconfig

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This file is for unifying the coding style for different editors and IDEs
2+
# editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
indent_style = space
8+
indent_size = 2
9+
end_of_line = lf
10+
charset = utf-8
11+
trim_trailing_whitespace = true
12+
insert_final_newline = true

.jshintrc

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"boss": true,
3+
"curly": true,
4+
"eqeqeq": true,
5+
"eqnull": true,
6+
"expr": true,
7+
"immed": true,
8+
"noarg": true,
9+
"node": true,
10+
"quotmark": "single",
11+
"smarttabs": true,
12+
"sub": true,
13+
"trailing": true,
14+
"undef": true,
15+
"unused": true
16+
}

bin/cli.js

+50-46
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#! /usr/bin/env node
22

3-
todo = process.argv[2];
4-
5-
if (todo == '--verbose')
6-
global.logLevel = "debug";
7-
else
8-
global.logLevel = "info";
3+
var todo = process.argv[2];
94

5+
if (todo === '--verbose') {
6+
global.logLevel = 'debug';
7+
} else {
8+
global.logLevel = 'info';
9+
}
1010

11-
if (todo == 'init') {
11+
if (todo === 'init') {
1212
require('./init.js');
1313
return;
14-
} else if(todo == '--version') {
14+
} else if (todo === '--version') {
1515
require('./version.js');
1616
return;
1717
}
@@ -29,11 +29,11 @@ var Log = require('../lib/logger'),
2929
http = require('http'),
3030
ConfigParser = require('../lib/configParser').ConfigParser,
3131
serverPort = 8888,
32+
server,
3233
timeout,
3334
activityTimeout,
3435
workers = {},
3536
workerKeys = {},
36-
logLevel,
3737
tunnelingAgent,
3838
tunnel;
3939

@@ -69,34 +69,36 @@ function terminateAllWorkers(callback) {
6969
}
7070
}
7171
}
72-
};
72+
}
7373

7474
function cleanUpAndExit(signal, status) {
7575
try {
7676
server.close();
7777
} catch (e) {
78-
logger.debug("Server already closed");
78+
logger.debug('Server already closed');
7979
}
8080

81-
if (statusPoller) statusPoller.stop();
81+
if (statusPoller) {
82+
statusPoller.stop();
83+
}
8284

8385
try {
8486
process.kill(tunnel.process.pid, 'SIGKILL');
8587
} catch (e) {
86-
logger.debug("Non existent tunnel");
88+
logger.debug('Non existent tunnel');
8789
}
8890
try {
8991
fs.unlinkSync(pid_file);
9092
} catch (e) {
91-
logger.debug("Non existent pid file.");
93+
logger.debug('Non existent pid file.');
9294
}
9395

94-
if (signal == 'SIGTERM') {
95-
logger.debug("Exiting");
96+
if (signal === 'SIGTERM') {
97+
logger.debug('Exiting');
9698
process.exit(status);
9799
} else {
98100
terminateAllWorkers(function() {
99-
logger.debug("Exiting");
101+
logger.debug('Exiting');
100102
process.exit(1);
101103
});
102104
}
@@ -105,22 +107,22 @@ function cleanUpAndExit(signal, status) {
105107
function getTestBrowserInfo(browserString, path) {
106108
var info = browserString;
107109
if (config.multipleTest) {
108-
info += ", " + path;
110+
info += ', ' + path;
109111
}
110112
return info;
111113
}
112114

113115
function launchServer() {
114-
logger.debug("Launching server on port:", serverPort);
116+
logger.debug('Launching server on port:', serverPort);
115117

116-
var server = new Server(client, workers);
118+
server = new Server(client, workers);
117119
server.listen(parseInt(serverPort, 10));
118120
}
119121

120122
function launchBrowser(browser, path) {
121123
var url = 'http://localhost:' + serverPort.toString() + '/' + path;
122124
var browserString = utils.browserString(browser);
123-
logger.debug("[%s] Launching", getTestBrowserInfo(browserString, path));
125+
logger.debug('[%s] Launching', getTestBrowserInfo(browserString, path));
124126

125127
var key = utils.uuid();
126128

@@ -141,7 +143,7 @@ function launchBrowser(browser, path) {
141143
}
142144

143145
if(config.tunnelIdentifier) {
144-
browser["tunnel_identifier"] = config.tunnelIdentifier;
146+
browser['tunnel_identifier'] = config.tunnelIdentifier;
145147
}
146148

147149
timeout = parseInt(config.timeout);
@@ -154,9 +156,9 @@ function launchBrowser(browser, path) {
154156

155157
client.createWorker(browser, function (err, worker) {
156158
if (err || typeof worker !== 'object') {
157-
logger.info("Error from BrowserStack: ", err);
158-
utils.alertBrowserStack("Failed to launch worker",
159-
"Arguments: " + JSON.stringify({
159+
logger.info('Error from BrowserStack: ', err);
160+
utils.alertBrowserStack('Failed to launch worker',
161+
'Arguments: ' + JSON.stringify({
160162
err: err,
161163
worker: worker
162164
}, null, 4));
@@ -191,11 +193,9 @@ var statusPoller = {
191193
start: function() {
192194
statusPoller.poller = setInterval(function () {
193195
client.getWorkers(function (err, _workers) {
194-
_workers = _workers.filter(function(currentValue, index, array) {
195-
return currentValue.status == 'running' && workerKeys[currentValue.id] && !workerKeys[currentValue.id].marked;
196-
});
197-
for (var i in _workers) {
198-
var _worker = _workers[i];
196+
_workers.filter(function(currentValue) {
197+
return currentValue.status === 'running' && workerKeys[currentValue.id] && !workerKeys[currentValue.id].marked;
198+
}).forEach(function(_worker) {
199199
var workerData = workerKeys[_worker.id];
200200
var worker = workers[workerData.key];
201201
if (worker.launched) {
@@ -210,15 +210,15 @@ var statusPoller = {
210210

211211
worker.activityTimeout = setTimeout(function () {
212212
if (!worker.acknowledged) {
213-
var subject = "Worker inactive for too long: " + worker.string;
214-
var content = "Worker details:\n" + JSON.stringify(worker.config, null, 4);
213+
var subject = 'Worker inactive for too long: ' + worker.string;
214+
var content = 'Worker details:\n' + JSON.stringify(worker.config, null, 4);
215215
utils.alertBrowserStack(subject, content, null, function(){});
216216
delete workers[workerData.key];
217217
delete workerKeys[worker.id];
218218
config.status += 1;
219219
if (utils.objectSize(workers) === 0) {
220-
var color = config.status > 0 ? "red" : "green";
221-
logger.info(chalk[color]("All tests done, failures: %d."), config.status);
220+
var color = config.status > 0 ? 'red' : 'green';
221+
logger.info(chalk[color]('All tests done, failures: %d.'), config.status);
222222

223223
if (config.status > 0) {
224224
config.status = 1;
@@ -231,15 +231,15 @@ var statusPoller = {
231231

232232
worker.testActivityTimeout = setTimeout(function () {
233233
if (worker.acknowledged) {
234-
var subject = "Tests timed out on: " + worker.string;
235-
var content = "Worker details:\n" + JSON.stringify(worker.config, null, 4);
234+
var subject = 'Tests timed out on: ' + worker.string;
235+
var content = 'Worker details:\n' + JSON.stringify(worker.config, null, 4);
236236
utils.alertBrowserStack(subject, content, null, function(){});
237237
delete workers[workerData.key];
238238
delete workerKeys[worker.id];
239239
config.status += 1;
240240
if (utils.objectSize(workers) === 0) {
241-
var color = config.status > 0 ? "red" : "green";
242-
logger.info(chalk[color]("All tests done, failures: %d."), config.status);
241+
var color = config.status > 0 ? 'red' : 'green';
242+
logger.info(chalk[color]('All tests done, failures: %d.'), config.status);
243243

244244
if (config.status > 0) {
245245
config.status = 1;
@@ -250,7 +250,7 @@ var statusPoller = {
250250
}
251251
}, (activityTimeout * 1000));
252252
}
253-
}
253+
});
254254
});
255255
}, 2000);
256256
},
@@ -277,13 +277,13 @@ function runTests() {
277277
tunnel = new Tunnel(config.key, serverPort, config.tunnelIdentifier, function () {
278278
statusPoller.start();
279279
var total_runs = config.browsers.length * (Object.prototype.toString.call(config.test_path) === '[object Array]' ? config.test_path.length : 1);
280-
logger.info("Launching " + config.browsers.length + " worker(s) for " + total_runs + " run(s).");
280+
logger.info('Launching ' + config.browsers.length + ' worker(s) for ' + total_runs + ' run(s).');
281281
browsers.forEach(function(browser) {
282-
if (browser.browser_version === "latest") {
283-
logger.debug("[%s] Finding version.", utils.browserString(browser));
282+
if (browser.browser_version === 'latest') {
283+
logger.debug('[%s] Finding version.', utils.browserString(browser));
284284

285285
client.getLatest(browser, function(err, version) {
286-
logger.debug("[%s] Version is %s.",
286+
logger.debug('[%s] Version is %s.',
287287
utils.browserString(browser), version);
288288
browser.browser_version = version;
289289
// So that all latest logs come in together
@@ -307,9 +307,13 @@ try {
307307
});
308308
runTests();
309309
var pid_file = process.cwd() + '/browserstack-run.pid';
310-
fs.writeFileSync(pid_file, process.pid, 'utf-8')
311-
process.on('SIGINT', function() { cleanUpAndExit('SIGINT', 1) });
312-
process.on('SIGTERM', function() { cleanUpAndExit('SIGTERM', config.status) });
310+
fs.writeFileSync(pid_file, process.pid, 'utf-8');
311+
process.on('SIGINT', function() {
312+
cleanUpAndExit('SIGINT', 1);
313+
});
314+
process.on('SIGTERM', function() {
315+
cleanUpAndExit('SIGTERM', config.status);
316+
});
313317
} catch (e) {
314318
console.log(e);
315319
console.log('Invalid command.');

bin/init.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ var config = {
99
key: 'BROWSERSTACK_KEY',
1010
test_path: 'path/to/test/runner',
1111
browsers: browsers
12-
}
12+
};
1313

1414
var configString = JSON.stringify(config, null, 4);
1515

16-
fs.writeFile('browserstack.json', configString, function (err, written, buffer) {
16+
fs.writeFile('browserstack.json', configString, function (err) {
17+
if (err) {
18+
console.log('Failed to generate `browserstack.json`', err);
19+
return;
20+
}
1721
console.log('Generated `browserstack.json` using preset "%s" having %d browsers.',
1822
preset, browsers.length);
1923
});

bin/version.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#! /usr/bin/env node
22

3-
var packagePath = require('path').resolve(__dirname, "../package.json"),
3+
var packagePath = require('path').resolve(__dirname, '../package.json'),
44
packageJson = require(packagePath);
5-
console.log("browserstack-runner @", packageJson["version"]);
5+
console.log('browserstack-runner @', packageJson['version']);

lib/config.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ var Log = require('./logger'),
44
fs = require('fs'),
55
pwd = process.cwd();
66

7-
config_path = process.env.BROWSERSTACK_JSON || 'browserstack.json';
7+
var config_path = process.env.BROWSERSTACK_JSON || 'browserstack.json';
88
config_path = path.resolve(path.relative(process.cwd(), config_path));
9-
logger.debug("Using config:", config_path);
9+
logger.debug('Using config:', config_path);
1010

1111
try {
1212
var config = require(config_path);
1313
} catch (e) {
14-
if (e.code == 'MODULE_NOT_FOUND') {
14+
if (e.code === 'MODULE_NOT_FOUND') {
1515
logger.info('Configuration file `browserstack.json` is missing.');
1616
} else {
1717
logger.info('Invalid configuration in `browserstack.json` file');
@@ -21,10 +21,10 @@ try {
2121
process.exit(1);
2222
}
2323

24+
var package_json = {};
2425
try {
25-
var package_json = require(process.cwd() + '/package.json');
26+
package_json = require(process.cwd() + '/package.json');
2627
} catch (e) {
27-
var package_json = {};
2828
}
2929

3030
if (process.env.BROWSERSTACK_KEY) {
@@ -48,9 +48,9 @@ if (!config.project) {
4848
var commit_id = process.env.TRAVIS_COMMIT;
4949

5050
if (commit_id) {
51-
config.build = "Commit-" + commit_id.slice(0, commit_id.length / 2);
51+
config.build = 'Commit-' + commit_id.slice(0, commit_id.length / 2);
5252
} {
53-
config.build = "Local run, " + new Date().toISOString();
53+
config.build = 'Local run, ' + new Date().toISOString();
5454
}
5555

5656
['username', 'key', 'test_path', 'browsers'].forEach(function(param) {

0 commit comments

Comments
 (0)