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): add more validation from legacy #15211

Merged
merged 1 commit into from
Jul 6, 2023
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
12 changes: 12 additions & 0 deletions core/config/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ function assertValidSettings(settings) {
throw new Error(`Screen emulation mobile setting (${settings.screenEmulation.mobile}) does not match formFactor setting (${settings.formFactor}). See https://github.com/GoogleChrome/lighthouse/blob/main/docs/emulation.md`);
}
}

const skippedAndOnlyAuditId =
settings.skipAudits?.find(auditId => settings.onlyAudits?.includes(auditId));
if (skippedAndOnlyAuditId) {
throw new Error(`${skippedAndOnlyAuditId} appears in both skipAudits and onlyAudits`);
}
}

/**
Expand Down Expand Up @@ -253,7 +259,13 @@ function assertArtifactTopologicalOrder(navigations) {
function assertValidConfig(resolvedConfig) {
const {warnings} = assertValidFRNavigations(resolvedConfig.navigations);

/** @type {Set<string>} */
const artifactIds = new Set();
for (const artifactDefn of resolvedConfig.artifacts || []) {
if (artifactIds.has(artifactDefn.id)) {
throw new Error(`Config defined multiple artifacts with id '${artifactDefn.id}'`);
}
artifactIds.add(artifactDefn.id);
assertValidFRGatherer(artifactDefn.gatherer);
}

Expand Down
22 changes: 22 additions & 0 deletions core/test/config/validation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {Audit as BaseAudit} from '../../audits/audit.js';
import BaseFRGatherer from '../../gather/base-gatherer.js';
import {Gatherer as BaseLegacyGatherer} from '../../gather/gatherers/gatherer.js';
import * as validation from '../../config/validation.js';
import {initializeConfig} from '../../config/config.js';

/** @typedef {LH.Gatherer.GathererMeta['supportedModes']} SupportedModes */

Expand All @@ -30,6 +31,20 @@ beforeEach(() => {
});

describe('Fraggle Rock Config Validation', () => {
describe('assertValidConfig', () => {
it('should throw if multiple artifacts have the same id', async () => {
const {resolvedConfig} = await initializeConfig('navigation');
if (!resolvedConfig.artifacts) throw new Error('No config artifacts');

const imageElArtifact = resolvedConfig.artifacts.find(a => a.id === 'ImageElements');
if (!imageElArtifact) throw new Error('Could not find ImageElements artifact');

resolvedConfig.artifacts.push(imageElArtifact);

expect(() => validation.assertValidConfig(resolvedConfig)).toThrow(/Config defined multiple/);
});
});

describe('isFRGathererDefn', () => {
it('should identify fraggle rock gatherer definitions', () => {
expect(validation.isFRGathererDefn({instance: new BaseFRGatherer()})).toBe(true);
Expand Down Expand Up @@ -253,6 +268,13 @@ describe('Fraggle Rock Config Validation', () => {
expect(() => validation.assertValidSettings(settings)).toThrow();
});

it('should throw if an audit in onlyAudits is also in skipAudits', () => {
const settings = {...defaultSettings};
settings.onlyAudits = ['viewport'];
settings.skipAudits = ['viewport', 'is-on-https'];
expect(() => validation.assertValidSettings(settings)).toThrow();
});

it('should throw on mismatched formFactor to screenEmulation', () => {
const settings = {...defaultSettings};

Expand Down