fix: do not pass --{enable,disable}-features twice when user-provided (#11230)

This commit is contained in:
Thiago Perrotta 2023-10-23 09:27:21 -04:00 committed by GitHub
parent 014c72ae1d
commit edec7d53f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 3 deletions

View File

@ -17,7 +17,7 @@ import {describe, it} from 'node:test';
import expect from 'expect';
import {getFeatures} from './ChromeLauncher.js';
import {getFeatures, removeMatchingFlags} from './ChromeLauncher.js';
describe('getFeatures', () => {
it('returns an empty array when no options are provided', () => {
@ -45,3 +45,25 @@ describe('getFeatures', () => {
expect(result).toEqual(['bar', 'baz']);
});
});
describe('removeMatchingFlags', () => {
it('empty', () => {
const a: string[] = [];
expect(removeMatchingFlags(a, '--foo')).toEqual([]);
});
it('with one match', () => {
const a: string[] = ['--foo=1', '--bar=baz'];
expect(removeMatchingFlags(a, '--foo')).toEqual(['--bar=baz']);
});
it('with multiple matches', () => {
const a: string[] = ['--foo=1', '--foo=2', '--bar=baz'];
expect(removeMatchingFlags(a, '--foo')).toEqual(['--bar=baz']);
});
it('with no matches', () => {
const a: string[] = ['--foo=1', '--bar=baz'];
expect(removeMatchingFlags(a, '--baz')).toEqual(['--foo=1', '--bar=baz']);
});
});

View File

@ -166,6 +166,14 @@ export class ChromeLauncher extends ProductLauncher {
override defaultArgs(options: BrowserLaunchArgumentOptions = {}): string[] {
// See https://github.com/GoogleChrome/chrome-launcher/blob/main/docs/chrome-flags-for-tools.md
const userDisabledFeatures = getFeatures(
'--disable-features',
options.args
);
if (options.args && userDisabledFeatures.length > 0) {
removeMatchingFlags(options.args, '--disable-features');
}
// Merge default disabled features with user-provided ones, if any.
const disabledFeatures = [
'Translate',
@ -175,13 +183,18 @@ export class ChromeLauncher extends ProductLauncher {
'OptimizationHints',
// https://crbug.com/1492053
'ProcessPerSiteUpToMainFrameThreshold',
...getFeatures('--disable-features', options.args),
...userDisabledFeatures,
];
const userEnabledFeatures = getFeatures('--enable-features', options.args);
if (options.args && userEnabledFeatures.length > 0) {
removeMatchingFlags(options.args, '--enable-features');
}
// Merge default enabled features with user-provided ones, if any.
const enabledFeatures = [
'NetworkServiceInProcess2',
...getFeatures('--enable-features', options.args),
...userEnabledFeatures,
];
const chromeArguments = [
@ -297,3 +310,22 @@ export function getFeatures(flag: string, options: string[] = []): string[] {
return s;
}) as string[];
}
/**
* Removes all elements in-place from the given string array
* that match the given command-line flag.
*
* @internal
*/
export function removeMatchingFlags(array: string[], flag: string): string[] {
const regex = new RegExp(`^${flag}=.*`);
let i = 0;
while (i < array.length) {
if (regex.test(array[i]!)) {
array.splice(i, 1);
} else {
i++;
}
}
return array;
}