Skip to content

Commit

Permalink
core(plugins): allow supportedModes in category (#13921)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamraine authored Jun 30, 2022
1 parent 3b8b90b commit 675a828
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lighthouse-core/config/config-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ function isObjectOfUnknownProperties(val) {
return typeof val === 'object' && val !== null && !Array.isArray(val);
}

/**
* @param {unknown} str
* @return {str is LH.Gatherer.GatherMode}
*/
function objectIsGatherMode(str) {
if (typeof str !== 'string') return false;
return str === 'navigation' || str === 'timespan' || str === 'snapshot';
}

/**
* @param {unknown} arr
* @return {arr is Array<LH.Gatherer.GatherMode>}
*/
function isArrayOfGatherModes(arr) {
if (!Array.isArray(arr)) return false;
return arr.every(objectIsGatherMode);
}

/**
* Asserts that obj has no own properties, throwing a nice error message if it does.
* Plugin and object name are included for nicer logging.
Expand Down Expand Up @@ -124,6 +142,7 @@ class ConfigPlugin {
description,
manualDescription,
auditRefs: auditRefsJson,
supportedModes,
...invalidRest
} = categoryJson;

Expand All @@ -138,13 +157,20 @@ class ConfigPlugin {
if (!i18n.isStringOrIcuMessage(manualDescription) && manualDescription !== undefined) {
throw new Error(`${pluginName} has an invalid category manualDescription.`);
}
if (!isArrayOfGatherModes(supportedModes) && supportedModes !== undefined) {
throw new Error(
`${pluginName} supportedModes must be an array, ` +
`valid array values are "navigation", "timespan", and "snapshot".`
);
}
const auditRefs = ConfigPlugin._parseAuditRefsList(auditRefsJson, pluginName);

return {
title,
auditRefs,
description: description,
manualDescription: manualDescription,
supportedModes,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ Object {
],
"description": "A nice plugin for nice testing",
"manualDescription": undefined,
"supportedModes": Array [
"navigation",
],
"title": "Nice Plugin",
},
},
Expand Down
23 changes: 23 additions & 0 deletions lighthouse-core/test/config/config-plugin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const nicePlugin = {
{id: 'nice-audit', weight: 1, group: 'group-a'},
{id: 'installable-manifest', weight: 220},
],
supportedModes: ['navigation'],
},
};

Expand Down Expand Up @@ -115,6 +116,7 @@ describe('ConfigPlugin', () => {
auditRefs: [
{id: 'evil-audit', weight: 0, group: undefined},
],
supportedModes: ['navigation'],
};

const evilPlugin = {
Expand Down Expand Up @@ -242,13 +244,34 @@ describe('ConfigPlugin', () => {
assert.ok(pluginJson);
});

it('accepts a category with no supportedModes', () => {
const pluginClone = deepClone(nicePlugin);
delete pluginClone.category.supportedModes;
const pluginJson = ConfigPlugin.parsePlugin(pluginClone, nicePluginName);
assert.ok(pluginJson);
});

it('throws if category has an invalid manualDescription', () => {
const pluginClone = deepClone(nicePlugin);
pluginClone.category.manualDescription = 55;
assert.throws(() => ConfigPlugin.parsePlugin(pluginClone, nicePluginName),
/^Error: lighthouse-plugin-nice-plugin has an invalid category manualDescription/);
});

it('throws if supported modes is not an array', () => {
const pluginClone = deepClone(nicePlugin);
pluginClone.category.supportedModes = 55;
assert.throws(() => ConfigPlugin.parsePlugin(pluginClone, nicePluginName),
/^Error: lighthouse-plugin-nice-plugin supportedModes must be an array/);
});

it('throws if supported modes is not an array of valid gather modes', () => {
const pluginClone = deepClone(nicePlugin);
pluginClone.category.supportedModes = ['invalid-mode'];
assert.throws(() => ConfigPlugin.parsePlugin(pluginClone, nicePluginName),
/^Error: lighthouse-plugin-nice-plugin supportedModes must be an array/);
});

describe('`category.auditRefs`', () => {
it('correctly passes through the contained auditRefs', () => {
const pluginJson = ConfigPlugin.parsePlugin(nicePlugin, nicePluginName);
Expand Down

0 comments on commit 675a828

Please sign in to comment.