Skip to content

Commit 4509c27

Browse files
committed
fix(jsFlags): un-quote --js-flags flag and automatically merge with presets
1 parent ed5ad17 commit 4509c27

3 files changed

Lines changed: 93 additions & 4 deletions

File tree

index.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ var ChromeBrowser = function(baseBrowserDecorator, args) {
88
this._getOptions = function(url) {
99
// Chrome CLI options
1010
// http://peter.sh/experiments/chromium-command-line-switches/
11+
flags.forEach(function(flag, i) {
12+
if(isJSFlags(flag)) flags[i] = sanitizeJSFlags(flag);
13+
});
14+
1115
return [
1216
'--user-data-dir=' + this._tempDir,
1317
'--no-default-browser-check',
@@ -19,6 +23,19 @@ var ChromeBrowser = function(baseBrowserDecorator, args) {
1923
};
2024
};
2125

26+
function isJSFlags (flag) {
27+
return flag.indexOf('--js-flags=') === 0;
28+
}
29+
30+
function sanitizeJSFlags (flag) {
31+
var test = /--js-flags=(['"])/.exec(flag);
32+
if (!test) return flag;
33+
var escapeChar = test[1];
34+
var endExp = new RegExp(escapeChar + '$');
35+
var startExp = new RegExp('--js-flags=' + escapeChar);
36+
return flag.replace(startExp, '--js-flags=').replace(endExp, '');
37+
}
38+
2239
// Return location of chrome.exe file for a given Chrome directory (available: "Chrome", "Chrome SxS").
2340
function getChromeExe(chromeDirName) {
2441
if (process.platform !== 'win32') {
@@ -58,11 +75,25 @@ var ChromeCanaryBrowser = function(baseBrowserDecorator, args) {
5875

5976
var parentOptions = this._getOptions;
6077
this._getOptions = function(url) {
61-
// disable crankshaft optimizations, as it causes lot of memory leaks (as of Chrome 23.0)
62-
return parentOptions.call(this, url).concat(['--js-flags="--nocrankshaft --noopt"']);
78+
return canaryGetOptions.call(this, url, args, parentOptions);
6379
};
6480
};
6581

82+
function canaryGetOptions (url, args, parent) {
83+
// disable crankshaft optimizations, as it causes lot of memory leaks (as of Chrome 23.0)
84+
var flags = args.flags || [];
85+
var augmentedFlags;
86+
var customFlags = '--nocrankshaft --noopt';
87+
88+
flags.forEach(function(flag, i) {
89+
if (isJSFlags(flag)) {
90+
augmentedFlags = sanitizeJSFlags(flag) + ' ' + customFlags;
91+
}
92+
});
93+
94+
return parent.call(this, url).concat([augmentedFlags || '--js-flags=' + customFlags]);
95+
}
96+
6697
ChromeCanaryBrowser.prototype = {
6798
name: 'ChromeCanary',
6899

@@ -103,3 +134,9 @@ module.exports = {
103134
'launcher:ChromeCanary': ['type', ChromeCanaryBrowser],
104135
'launcher:Dartium': ['type', DartiumBrowser]
105136
};
137+
138+
module.exports.test = {
139+
isJSFlags: isJSFlags,
140+
sanitizeJSFlags: sanitizeJSFlags,
141+
canaryGetOptions: canaryGetOptions
142+
}

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "A Karma plugin. Launcher for Chrome and Chrome Canary.",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "./node_modules/.bin/jasmine-node test"
88
},
99
"repository": {
1010
"type": "git",
@@ -25,7 +25,8 @@
2525
"grunt": "~0.4.1",
2626
"grunt-npm": "~0.0.2",
2727
"grunt-bump": "~0.0.6",
28-
"grunt-auto-release": "~0.0.2"
28+
"grunt-auto-release": "~0.0.2",
29+
"jasmine-node": "~1.14.5"
2930
},
3031
"contributors": [
3132
"Friedel Ziegelmayer <[email protected]>",

test/jsflags.spec.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
var launcher = require('../index');
2+
3+
describe('isJSFlags()', function() {
4+
var isJSFlags = launcher.test.isJSFlags;
5+
6+
it('should return true if flag begins with --js-flags=', function() {
7+
expect(isJSFlags('--js-flags=--expose-gc')).toBe(true);
8+
expect(isJSFlags('--js-flags="--expose-gc"')).toBe(true);
9+
expect(isJSFlags("--js-flags='--expose-gc'")).toBe(true);
10+
});
11+
12+
it('should return false if flag does not begin with --js-flags=', function(){
13+
expect(isJSFlags(' --js-flags=--expose-gc')).toBe(false);
14+
expect(isJSFlags('--js-flags"--expose-gc"')).toBe(false);
15+
expect(isJSFlags('--jsflags="--expose-gc"')).toBe(false);
16+
});
17+
});
18+
19+
20+
describe('sanitizeJSFlags()', function() {
21+
var sanitizeJSFlags = launcher.test.sanitizeJSFlags;
22+
23+
it('should do nothing if flags are not contained in quotes', function() {
24+
expect(sanitizeJSFlags('--js-flags=--expose-gc')).toBe('--js-flags=--expose-gc');
25+
});
26+
27+
it('should symmetrically remove single or double quote if wraps all flags', function() {
28+
expect(sanitizeJSFlags("--js-flags='--expose-gc'")).toBe("--js-flags=--expose-gc");
29+
expect(sanitizeJSFlags('--js-flags="--expose-gc"')).toBe('--js-flags=--expose-gc');
30+
});
31+
32+
it('should NOT remove anything if the flags are not contained within quote', function() {
33+
expect(sanitizeJSFlags('--js-flags=--expose-gc="true"')).toBe('--js-flags=--expose-gc="true"');
34+
expect(sanitizeJSFlags("--js-flags=--expose-gc='true'")).toBe("--js-flags=--expose-gc='true'");
35+
});
36+
});
37+
38+
describe('canaryGetOptions', function() {
39+
var canaryGetOptions = launcher.test.canaryGetOptions;
40+
41+
it('should return a merged version of --js-flags', function() {
42+
var parent = jasmine.createSpy('parent').andReturn(['-incognito']);
43+
var context = {};
44+
var url = 'http://localhost:9876';
45+
var args = {flags: ['--js-flags="--expose-gc"']};
46+
expect(canaryGetOptions.call(context, url, args, parent)).toEqual([
47+
'-incognito',
48+
'--js-flags=--expose-gc --nocrankshaft --noopt'
49+
]);
50+
});
51+
});

0 commit comments

Comments
 (0)