Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core(config): only allow lighthouse:default extension #11835

Merged
merged 3 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,17 @@ lighthouse('https://example.com/', {port: 9222}, config);

| Name | Type |
| - | - |
| extends | <code>string&#124;boolean&#124;undefined</code> |
| extends | <code>string&#124;undefined</code> |
| settings | <code>Object&#124;undefined</code> |
| passes | <code>Object[]</code> |
| audits | <code>string[]</code> |
| categories | <code>Object&#124;undefined</code> |
| groups | <code>Object&#124;undefined</code> |

### `extends: "lighthouse:default"|boolean|undefined`
### `extends: "lighthouse:default"|undefined`

The `extends` property controls if your configuration should inherit from the default Lighthouse configuration. [Learn more.](#config-extension)

Both the values `"lighthouse:default"` and `true` will enable inheritance, while `false` and `undefined` will not.

#### Example
```js
{
Expand Down
3 changes: 3 additions & 0 deletions lighthouse-core/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ class Config {

// Extend the default config if specified
if (configJSON.extends) {
if (configJSON.extends !== 'lighthouse:default') {
throw new Error('`lighthouse:default` is the only valid extension method.');
}
configJSON = Config.extendConfigJSON(deepCloneConfigJson(defaultConfig), configJSON);
}

Expand Down
33 changes: 18 additions & 15 deletions lighthouse-core/test/config/config-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ describe('Config', () => {
const saveWarning = evt => warnings.push(evt);
log.events.addListener('warning', saveWarning);
const config = new Config({
extends: true,
extends: 'lighthouse:default',
settings: {
onlyCategories: ['accessibility'],
},
Expand All @@ -638,7 +638,7 @@ describe('Config', () => {
const saveWarning = evt => warnings.push(evt);
log.events.addListener('warning', saveWarning);
const config = new Config({
extends: true,
extends: 'lighthouse:default',
settings: {
onlyCategories: ['performance', 'pwa'],
},
Expand All @@ -655,7 +655,7 @@ describe('Config', () => {

it('filters works with extension', () => {
const config = new Config({
extends: true,
extends: 'lighthouse:default',
settings: {
onlyCategories: ['performance'],
onlyAudits: ['is-on-https'],
Expand All @@ -672,7 +672,7 @@ describe('Config', () => {
const saveWarning = evt => warnings.push(evt);
log.events.addListener('warning', saveWarning);
const config = new Config({
extends: true,
extends: 'lighthouse:default',
settings: {
onlyCategories: ['performance', 'missing-category'],
onlyAudits: ['first-cpu-idle', 'missing-audit'],
Expand All @@ -687,7 +687,7 @@ describe('Config', () => {
it('throws for invalid use of skipAudits and onlyAudits', () => {
assert.throws(() => {
new Config({
extends: true,
extends: 'lighthouse:default',
settings: {
onlyAudits: ['first-meaningful-paint'],
skipAudits: ['first-meaningful-paint'],
Expand All @@ -697,22 +697,17 @@ describe('Config', () => {
});

it('cleans up flags for settings', () => {
const config = new Config({extends: true}, {nonsense: 1, foo: 2, throttlingMethod: 'provided'});
const config = new Config({extends: 'lighthouse:default'},
{nonsense: 1, foo: 2, throttlingMethod: 'provided'});
assert.equal(config.settings.throttlingMethod, 'provided');
assert.ok(config.settings.nonsense === undefined, 'did not cleanup settings');
});

it('allows overriding of array-typed settings', () => {
const config = new Config({extends: true}, {output: ['html']});
const config = new Config({extends: 'lighthouse:default'}, {output: ['html']});
assert.deepStrictEqual(config.settings.output, ['html']);
});

it('does not throw on "lighthouse:full"', () => {
const config = new Config({extends: 'lighthouse:full'}, {output: ['html', 'json']});
assert.deepStrictEqual(config.settings.throttlingMethod, 'simulate');
assert.deepStrictEqual(config.settings.output, ['html', 'json']);
});

it('extends the config', () => {
class CustomAudit extends Audit {
static get meta() {
Expand Down Expand Up @@ -783,6 +778,14 @@ describe('Config', () => {
assert.equal(config.passes[0].networkQuietThresholdMs, 10003);
});

it('only supports `lighthouse:default` extension', () => {
const createConfig = extendsValue => new Config({extends: extendsValue});

expect(() => createConfig(true)).toThrowError(/default` is the only valid extension/);
expect(() => createConfig('lighthouse')).toThrowError(/default` is the only valid/);
expect(() => createConfig('lighthouse:full')).toThrowError(/default` is the only valid/);
});

it('merges settings with correct priority', () => {
const config = new Config(
{
Expand Down Expand Up @@ -897,9 +900,9 @@ describe('Config', () => {
devtoolsLogs: {defaultPass: 'path/to/devtools/log'},
};
const configA = {};
const configB = {extends: true, artifacts};
const configB = {extends: 'lighthouse:default', artifacts};
const merged = Config.extendConfigJSON(configA, configB);
assert.equal(merged.extends, true);
assert.equal(merged.extends, 'lighthouse:default');
assert.equal(merged.artifacts, configB.artifacts);
});
});
Expand Down
2 changes: 1 addition & 1 deletion types/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ declare global {
* The pre-normalization Lighthouse Config format.
*/
export interface Json {
extends?: 'lighthouse:default' | string | boolean;
extends?: 'lighthouse:default' | string;
settings?: SharedFlagsSettings;
passes?: PassJson[] | null;
audits?: Config.AuditJson[] | null;
Expand Down