Skip to content

Commit 83e826c

Browse files
committed
Merge pull request #42 from browserstack/config_parser
Config parser
2 parents 2870ba4 + e33effb commit 83e826c

File tree

8 files changed

+288
-188
lines changed

8 files changed

+288
-188
lines changed

bin/runner.js

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
var BrowserStack = require('browserstack'),
44
fs = require('fs'),
5-
utils = require('../lib/utils');
6-
Server = require('../lib/server').Server;
7-
config = require('../lib/config');
8-
Tunnel = require('../lib/local').Tunnel;
9-
10-
var serverPort = 8888;
11-
var tunnel;
5+
utils = require('../lib/utils'),
6+
Server = require('../lib/server').Server,
7+
config = require('../lib/config'),
8+
Tunnel = require('../lib/local').Tunnel,
9+
ConfigParser = require('../lib/configParser').ConfigParser,
10+
serverPort = 8888,
11+
tunnel;
1212

1313
var client = BrowserStack.createClient({
1414
username: config.username,
@@ -168,22 +168,24 @@ var launchBrowsers = function(config, browser) {
168168
}
169169

170170
if (config.browsers && config.browsers.length > 0) {
171-
tunnel = new Tunnel(config.key, serverPort, config.tunnelIdentifier, function () {
172-
console.log("Launching BrowserStack workers");
173-
config.browsers.forEach(function(browser) {
174-
if (browser.browser_version === "latest") {
175-
console.log("[%s] Finding version.", utils.browserString(browser));
176-
177-
client.getLatest(browser, function(err, version) {
178-
console.log("[%s] Version is %s.",
179-
utils.browserString(browser), version);
180-
browser.browser_version = version;
181-
// So that all latest logs come in together
171+
ConfigParser.parse(client, config.browsers, function(browsers){
172+
tunnel = new Tunnel(config.key, serverPort, config.tunnelIdentifier, function () {
173+
console.log("Launching BrowserStack workers");
174+
browsers.forEach(function(browser) {
175+
if (browser.browser_version === "latest") {
176+
console.log("[%s] Finding version.", utils.browserString(browser));
177+
178+
client.getLatest(browser, function(err, version) {
179+
console.log("[%s] Version is %s.",
180+
utils.browserString(browser), version);
181+
browser.browser_version = version;
182+
// So that all latest logs come in together
183+
launchBrowsers(config, browser);
184+
});
185+
} else {
182186
launchBrowsers(config, browser);
183-
});
184-
} else {
185-
launchBrowsers(config, browser);
186-
}
187+
}
188+
});
187189
});
188190
});
189191
}

lib/_patch/mocha-plugin.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(function(){
1+
(function() {
22
function stack(err) {
33
var str = err.stack || err.toString();
44

@@ -20,39 +20,39 @@
2020
return test.fullTitle().replace(/#/g, '');
2121
}
2222

23-
return Mocha.BrowserStack = function (runner, root) {
23+
return Mocha.BrowserStack = function(runner, root) {
2424
Mocha.reporters.HTML.call(this, runner, root);
2525

2626
var count = 1,
27-
that = this,
28-
failures = 0,
29-
passes = 0,
30-
start = 0,
31-
tracebacks = [];
27+
that = this,
28+
failures = 0,
29+
passes = 0,
30+
start = 0,
31+
tracebacks = [];
3232

33-
runner.on('start', function () {
33+
runner.on('start', function() {
3434
start = (new Date).getTime();
3535
});
36-
37-
runner.on('test end', function (test) {
36+
37+
runner.on('test end', function(test) {
3838
count += 1;
3939
});
40-
41-
runner.on('pass', function (test) {
40+
41+
runner.on('pass', function(test) {
4242
passes += 1;
4343
});
44-
45-
runner.on('fail', function (test, err) {
44+
45+
runner.on('fail', function(test, err) {
4646
failures += 1;
47-
47+
4848
if (err) {
49-
tracebacks.push(err);
49+
tracebacks.push(err);
5050
}
5151
});
52-
53-
runner.on('end', function () {
54-
results = {}
55-
results.runtime = ((new Date).getTime() - start);
52+
53+
runner.on('end', function() {
54+
results = {};
55+
results.runtime = Date.now() - start;
5656
results.total = passes + failures;
5757
results.passed = passes;
5858
results.failed = failures;

lib/_patch/qunit-plugin.js

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,58 @@
1-
// For logging assertions on the console, here's what grunt-contrib-qunit uses:
1+
// For logging assertions on the console, here's what grunt-contrib-qunit uses:
22
// https://github.com/gruntjs/grunt-contrib-qunit/blob/784597023e7235337ca9c0651aa45124a2d72341/tasks/qunit.js#L45
3-
(function(){
3+
(function() {
44

55
var failedAssertions = [];
6-
var options, currentModule, currentTest, setTimeoutVariable;
6+
var options,
7+
currentModule,
8+
currentTest,
9+
setTimeoutVariable;
710
var pendingTest = {};
811

912
var testTimeout = function() {
1013
var error = {
11-
testName: currentTest, message: "Stuck on this test for 60 sec."
12-
}
13-
14-
BrowserStack.post('/_progress', {tracebacks: [error]}, function(){});
15-
}
14+
testName: currentTest,
15+
message: "Stuck on this test for 60 sec."
16+
};
17+
18+
BrowserStack.post('/_progress', {
19+
tracebacks: [error]
20+
}, function(){});
21+
};
1622

17-
QUnit.testDone(function( details ) {
23+
QUnit.testDone(function(details) {
1824
var ct = details.module + " - " + details.name;
1925
clearTimeout(pendingTest[ct]);
2026
});
2127

22-
QUnit.testStart(function( details ) {
28+
QUnit.testStart(function(details) {
2329
currentTest = details.module + " - " + details.name;
24-
pendingTest[currentTest] = setTimeout(function(){
30+
pendingTest[currentTest] = setTimeout(function() {
2531
testTimeout(currentTest);
2632
}, 60000);
2733
});
28-
29-
QUnit.log(function( details ) {
30-
if ( details.result ) {
34+
35+
QUnit.log(function(details) {
36+
if (details.result) {
3137
return;
3238
}
33-
39+
3440
var error = {
35-
actual: details.actual, expected: encodeURIComponent(details.expected), message: encodeURIComponent(details.message), source: encodeURIComponent(details.source),
36-
testName: (details.module + ": " + details.name)
37-
}
38-
39-
BrowserStack.post('/_progress', {tracebacks: [error]}, function(){});
41+
actual: details.actual,
42+
expected: encodeURIComponent(details.expected),
43+
message: encodeURIComponent(details.message),
44+
source: encodeURIComponent(details.source),
45+
testName:( details.module + ": " + details.name)
46+
};
47+
48+
BrowserStack.post('/_progress', {
49+
tracebacks: [error]
50+
}, function(){});
4051
});
41-
42-
QUnit.done(function( results ) {
52+
53+
QUnit.done(function(results) {
4354
results.url = window.location.pathname;
4455
BrowserStack.post("/_report", results, function(){});
4556
});
46-
57+
4758
})();

lib/config.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var path = require('path'),
2-
fs = require('fs');
2+
fs = require('fs');
33
var pwd = process.cwd();
44

55
config_path = process.env.BROWSERSTACK_JSON || 'browserstack.json';
@@ -46,10 +46,10 @@ if (!config.project) {
4646
var commit_id = process.env.TRAVIS_COMMIT;
4747

4848
if (commit_id) {
49-
config.build = "Commit-" + commit_id.slice(0, commit_id.length/2);
49+
config.build = "Commit-" + commit_id.slice(0, commit_id.length / 2);
5050
}
5151

52-
['username', 'key', 'test_path', 'browsers'].forEach(function (param) {
52+
['username', 'key', 'test_path', 'browsers'].forEach(function(param) {
5353
if (typeof config[param] === 'undefined') {
5454
console.error('Configuration parameter `%s` is required.', param);
5555
process.exit(1);
@@ -58,27 +58,27 @@ if (commit_id) {
5858

5959
var formatPath = function(path) {
6060
if (path.indexOf(pwd) === 0) {
61-
path= path.slice(pwd.length + 1);
61+
path = path.slice(pwd.length + 1);
6262
}
63-
if (!fs.existsSync(path)){
64-
console.error('Test path: '+ path + ' is invalid.');
63+
if (!fs.existsSync(path)) {
64+
console.error('Test path: ' + path + ' is invalid.');
6565
process.exit(1);
6666
}
6767
return path;
68-
}
68+
};
6969

7070
config.tunnelIdentifier = process.env.TUNNEL_ID || process.env.TRAVIS_JOB_ID || process.env.TRAVIS_BUILD_ID;
7171

72-
if(Object.prototype.toString.call(config.test_path) === '[object Array]') {
73-
config.test_path.forEach(function(path){
72+
if (Object.prototype.toString.call(config.test_path) === '[object Array]') {
73+
config.test_path.forEach(function(path) {
7474
path = formatPath(path);
7575
});
7676
} else {
7777
//Backward Compatibility, if test_path is not array of path
7878
config.test_path = formatPath(config.test_path);
7979
}
8080

81-
for (key in config) {
81+
for (var key in config) {
8282
if (config.hasOwnProperty(key)) {
8383
exports[key] = config[key];
8484
}

lib/configParser.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//beta browsers not handled
2+
//+ not handled
3+
var ConfigParser = {
4+
finalBrowsers: [],
5+
6+
bsBrowsers: null,
7+
8+
parse: function(client, browser_config, callback) {
9+
client.getBrowsers(function(error, browsers) {
10+
if(error) {
11+
console.log("Error getting browsers list from BrowserStack");
12+
console.log(error);
13+
process.exit(1);
14+
}
15+
ConfigParser.bsBrowsers = browsers;
16+
for (var key in browser_config) {
17+
var entry = browser_config[key];
18+
ConfigParser.finalBrowsers.push(ConfigParser.getBrowserObject(entry));
19+
}
20+
callback(ConfigParser.finalBrowsers);
21+
});
22+
return
23+
},
24+
25+
setBrowserVersion: function(browserObject, verStr) {
26+
var filteredBrowsers = ConfigParser.bsBrowsers.map(function(currentValue, index, array) {
27+
if (currentValue.browser == browserObject.browser) {
28+
return currentValue.browser_version;
29+
}
30+
}).filter(function(currentValue, index, array) {
31+
return currentValue && array.indexOf(currentValue) === index;
32+
}).sort(function(a, b) {
33+
return parseFloat(a) - parseFloat(b);
34+
});
35+
if (verStr == 'current') {
36+
return filteredBrowsers[filteredBrowsers.length - 1];
37+
}
38+
else if (verStr == 'previous') {
39+
return filteredBrowsers[filteredBrowsers.length - 2];
40+
}
41+
},
42+
43+
populateOsAndOsVersion: function(browserObject) {
44+
if (!(browserObject.os && browserObject.os_version)) {
45+
var windowsFiltered = ConfigParser.bsBrowsers.filter(function(currentValue, index, array) {
46+
return currentValue.os == 'Windows' && currentValue.browser == browserObject.browser && parseFloat(currentValue.browser_version).toPrecision(4) == parseFloat(browserObject.browser_version).toPrecision(4);
47+
});
48+
var osxFiltered = ConfigParser.bsBrowsers.filter(function(currentValue, index, array) {
49+
return currentValue.os == 'OS X' && currentValue.browser == browserObject.browser && parseFloat(currentValue.browser_version).toPrecision(4) == parseFloat(browserObject.browser_version).toPrecision(4);
50+
});
51+
browserObject = windowsFiltered.length > 0 ? windowsFiltered[Math.floor(Math.random() * windowsFiltered.length)] : osxFiltered[Math.floor(Math.random() * osxFiltered.length)];
52+
}
53+
return browserObject;
54+
},
55+
56+
getBrowserObject: function(entry) {
57+
var browserObject = {};
58+
if (typeof(entry) == 'string') {
59+
var browserData = entry.split("_");
60+
var lindex = browserData.length - 1;
61+
browserObject.browser = browserData[0];
62+
if (browserData[lindex] && browserData[lindex].indexOf("+") == -1) {
63+
if (["current", "previous"].indexOf(browserData[1]) != -1) {
64+
browserObject.browser_version = ConfigParser.setBrowserVersion(ConfigParser.bsBrowsers, browserObject, browserData[1]);
65+
}
66+
else {
67+
browserObject.browser_version = browserData.slice(1, lindex + 1).join(".");
68+
}
69+
}
70+
else {
71+
browserObject.browser_version = browserData.slice(1, lindex + 1).join(".");
72+
}
73+
} else {
74+
browserObject = entry;
75+
}
76+
browserObject = ConfigParser.populateOsAndOsVersion(browserObject);
77+
return browserObject;
78+
}
79+
};
80+
81+
exports.ConfigParser = ConfigParser;

0 commit comments

Comments
 (0)