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(plugins): allow supportedModes in category #13921

Merged
merged 6 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
23 changes: 23 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,17 @@ 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} has an invalid category supportedModes.`);
adamraine marked this conversation as resolved.
Show resolved Hide resolved
}
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 has an invalid category supportedModes/);
});

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 has an invalid category supportedModes/);
});

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