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(fr): replace configContext with flags #14050

Merged
merged 16 commits into from
Aug 16, 2022
15 changes: 0 additions & 15 deletions core/config/config-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,20 +606,6 @@ function deepCloneConfigJson(json) {
return cloned;
}

/**
* @param {LH.Flags} flags
* @return {LH.Config.FRContext}
*/
function flagsToFRContext(flags) {
return {
configPath: flags?.configPath,
settingsOverrides: flags,
logLevel: flags?.logLevel,
hostname: flags?.hostname,
port: flags?.port,
};
}

export {
deepClone,
deepCloneConfigJson,
Expand All @@ -631,5 +617,4 @@ export {
resolveGathererToDefn,
resolveModulePath,
resolveSettings,
flagsToFRContext,
};
25 changes: 12 additions & 13 deletions core/fraggle-rock/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ const defaultConfigPath = path.join(
'../../config/default-config.js'
);

/** @typedef {LH.Config.FRContext & {gatherMode: LH.Gatherer.GatherMode}} ConfigContext */

/**
* @param {LH.Config.Json|undefined} configJSON
* @param {{configPath?: string}} context
Expand Down Expand Up @@ -176,10 +174,10 @@ async function resolveArtifactsToDefns(artifacts, configDir) {
* Overrides the settings that may not apply to the chosen gather mode.
*
* @param {LH.Config.Settings} settings
* @param {ConfigContext} context
* @param {LH.Gatherer.GatherMode} gatherMode
*/
function overrideSettingsForGatherMode(settings, context) {
if (context.gatherMode === 'timespan') {
function overrideSettingsForGatherMode(settings, gatherMode) {
if (gatherMode === 'timespan') {
if (settings.throttlingMethod === 'simulate') {
settings.throttlingMethod = 'devtools';
}
Expand Down Expand Up @@ -251,21 +249,22 @@ function resolveNavigationsToDefns(navigations, artifactDefns, settings) {
}

/**
* @param {LH.Config.Json|undefined} configJSON
* @param {ConfigContext} context
* @param {LH.Gatherer.GatherMode} gatherMode
* @param {LH.Config.Json=} configJSON
* @param {LH.Flags=} flags
* @return {Promise<{config: LH.Config.FRConfig, warnings: string[]}>}
*/
async function initializeConfig(configJSON, context) {
async function initializeConfig(gatherMode, configJSON, flags = {}) {
const status = {msg: 'Initialize config', id: 'lh:config'};
log.time(status, 'verbose');

let {configWorkingCopy, configDir} = resolveWorkingCopy(configJSON, context); // eslint-disable-line prefer-const
let {configWorkingCopy, configDir} = resolveWorkingCopy(configJSON, flags);

configWorkingCopy = resolveExtensions(configWorkingCopy);
configWorkingCopy = await mergePlugins(configWorkingCopy, configDir, context.settingsOverrides);
configWorkingCopy = await mergePlugins(configWorkingCopy, configDir, flags);

const settings = resolveSettings(configWorkingCopy.settings || {}, context.settingsOverrides);
overrideSettingsForGatherMode(settings, context);
const settings = resolveSettings(configWorkingCopy.settings || {}, flags);
overrideSettingsForGatherMode(settings, gatherMode);

const artifacts = await resolveArtifactsToDefns(configWorkingCopy.artifacts, configDir);
const navigations = resolveNavigationsToDefns(configWorkingCopy.navigations, artifacts, settings);
Expand All @@ -282,7 +281,7 @@ async function initializeConfig(configJSON, context) {

const {warnings} = assertValidConfig(config);

config = filterConfigByGatherMode(config, context.gatherMode);
config = filterConfigByGatherMode(config, gatherMode);
config = filterConfigByExplicitFilters(config, settings);

log.timeEnd(status);
Expand Down
13 changes: 6 additions & 7 deletions core/fraggle-rock/gather/navigation-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,18 +300,17 @@ async function _cleanup({requestedUrl, driver, config}) {

/**
* @param {LH.NavigationRequestor|undefined} requestor
* @param {{page?: LH.Puppeteer.Page, config?: LH.Config.Json, configContext?: LH.Config.FRContext}} options
* @param {{page?: LH.Puppeteer.Page, config?: LH.Config.Json, flags?: LH.Flags}} options
* @return {Promise<LH.Gatherer.FRGatherResult>}
*/
async function navigationGather(requestor, options) {
const {configContext = {}} = options;
log.setLevel(configContext.logLevel || 'error');
const {flags = {}} = options;
log.setLevel(flags.logLevel || 'error');

const {config} =
await initializeConfig(options.config, {...configContext, gatherMode: 'navigation'});
const {config} = await initializeConfig('navigation', options.config, flags);
const computedCache = new Map();
const internalOptions = {
skipAboutBlank: configContext.skipAboutBlank,
skipAboutBlank: flags.skipAboutBlank,
};

// We can't trigger the navigation through user interaction if we reset the page before starting.
Expand All @@ -329,7 +328,7 @@ async function navigationGather(requestor, options) {
// For navigation mode, we shouldn't connect to a browser in audit mode,
// therefore we connect to the browser in the gatherFn callback.
if (!page) {
const {hostname = DEFAULT_HOSTNAME, port = DEFAULT_PORT} = configContext;
const {hostname = DEFAULT_HOSTNAME, port = DEFAULT_PORT} = flags;
const browser = await puppeteer.connect({browserURL: `http://${hostname}:${port}`});
page = await browser.newPage();
}
Expand Down
9 changes: 4 additions & 5 deletions core/fraggle-rock/gather/snapshot-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ import {initializeConfig} from '../config/config.js';
import {getBaseArtifacts, finalizeArtifacts} from './base-artifacts.js';

/**
* @param {{page: LH.Puppeteer.Page, config?: LH.Config.Json, configContext?: LH.Config.FRContext}} options
* @param {{page: LH.Puppeteer.Page, config?: LH.Config.Json, flags?: LH.Flags}} options
* @return {Promise<LH.Gatherer.FRGatherResult>}
*/
async function snapshotGather(options) {
const {configContext = {}} = options;
log.setLevel(configContext.logLevel || 'error');
const {flags = {}} = options;
log.setLevel(flags.logLevel || 'error');

const {config} =
await initializeConfig(options.config, {...configContext, gatherMode: 'snapshot'});
const {config} = await initializeConfig('snapshot', options.config, flags);
const driver = new Driver(options.page);
await driver.connect();

Expand Down
9 changes: 4 additions & 5 deletions core/fraggle-rock/gather/timespan-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ import {initializeConfig} from '../config/config.js';
import {getBaseArtifacts, finalizeArtifacts} from './base-artifacts.js';

/**
* @param {{page: LH.Puppeteer.Page, config?: LH.Config.Json, configContext?: LH.Config.FRContext}} options
* @param {{page: LH.Puppeteer.Page, config?: LH.Config.Json, flags?: LH.Flags}} options
* @return {Promise<{endTimespanGather(): Promise<LH.Gatherer.FRGatherResult>}>}
*/
async function startTimespanGather(options) {
const {configContext = {}} = options;
log.setLevel(configContext.logLevel || 'error');
const {flags = {}} = options;
log.setLevel(flags.logLevel || 'error');

const {config} =
await initializeConfig(options.config, {...configContext, gatherMode: 'timespan'});
const {config} = await initializeConfig('timespan', options.config, flags);
const driver = new Driver(options.page);
await driver.connect();

Expand Down
20 changes: 9 additions & 11 deletions core/fraggle-rock/user-flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,22 @@ class UserFlow {
*/
_getNextNavigationOptions(stepOptions) {
const options = {...this.options, ...stepOptions};
const configContext = {...options.configContext};
const settingsOverrides = {...configContext.settingsOverrides};
const flags = {...options.flags};

if (configContext.skipAboutBlank === undefined) {
configContext.skipAboutBlank = true;
if (flags.skipAboutBlank === undefined) {
flags.skipAboutBlank = true;
}

// On repeat navigations, we want to disable storage reset by default (i.e. it's not a cold load).
const isSubsequentNavigation = this._gatherSteps
.some(step => step.artifacts.GatherContext.gatherMode === 'navigation');
if (isSubsequentNavigation) {
if (settingsOverrides.disableStorageReset === undefined) {
settingsOverrides.disableStorageReset = true;
if (flags.disableStorageReset === undefined) {
flags.disableStorageReset = true;
}
}

configContext.settingsOverrides = settingsOverrides;
options.configContext = configContext;
options.flags = flags;

return options;
}
Expand All @@ -96,7 +94,7 @@ class UserFlow {
artifacts: gatherResult.artifacts,
name: providedName || this._getDefaultStepName(gatherResult.artifacts),
config: options.config,
configContext: options.configContext,
flags: options.flags,
};
this._gatherSteps.push(gatherStep);
this._gatherStepRunnerOptions.set(gatherStep, gatherResult.runnerOptions);
Expand Down Expand Up @@ -246,7 +244,7 @@ async function auditGatherSteps(gatherSteps, options) {
/** @type {LH.FlowResult['steps']} */
const steps = [];
for (const gatherStep of gatherSteps) {
const {artifacts, name, configContext} = gatherStep;
const {artifacts, name, flags} = gatherStep;

let runnerOptions = options.gatherStepRunnerOptions?.get(gatherStep);

Expand All @@ -255,7 +253,7 @@ async function auditGatherSteps(gatherSteps, options) {
// Step specific configs take precedence over a config for the entire flow.
const configJson = gatherStep.config || options.config;
const {gatherMode} = artifacts.GatherContext;
const {config} = await initializeConfig(configJson, {...configContext, gatherMode});
const {config} = await initializeConfig(gatherMode, configJson, flags);
runnerOptions = {
config,
computedCache: new Map(),
Expand Down
7 changes: 2 additions & 5 deletions core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {Config} from './config/config.js';
import URL from './lib/url-shim.js';
import * as fraggleRock from './fraggle-rock/api.js';
import {Driver} from './gather/driver.js';
import {flagsToFRContext} from './config/config-helpers.js';
import {initializeConfig} from './fraggle-rock/config/config.js';

/** @typedef {import('./gather/connections/connection.js').Connection} Connection */
Expand Down Expand Up @@ -41,8 +40,7 @@ import {initializeConfig} from './fraggle-rock/config/config.js';
* @return {Promise<LH.RunnerResult|undefined>}
*/
async function lighthouse(url, flags = {}, configJSON, page) {
const configContext = flagsToFRContext(flags);
return fraggleRock.navigation(url, {page, config: configJSON, configContext});
return fraggleRock.navigation(url, {page, config: configJSON, flags});
}

/**
Expand Down Expand Up @@ -86,8 +84,7 @@ async function legacyNavigation(url, flags = {}, configJSON, userConnection) {
* @return {Promise<LH.Config.FRConfig>}
*/
async function generateConfig(configJson, flags = {}, gatherMode = 'navigation') {
const configContext = flagsToFRContext(flags);
const {config} = await initializeConfig(configJson, {...configContext, gatherMode});
const {config} = await initializeConfig(gatherMode, configJson, flags);
return config;
}

Expand Down
2 changes: 1 addition & 1 deletion core/scripts/print-a11y-scoring.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import {initializeConfig} from '../fraggle-rock/config/config.js';

const {config} = await initializeConfig(undefined, {gatherMode: 'navigation'});
const {config} = await initializeConfig('navigation');
if (!config.categories || !config.audits) throw new Error('wut');

const auditRefs = config.categories.accessibility.auditRefs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313239,9 +313239,8 @@
]
}
},
"configContext": {
"skipAboutBlank": true,
"settingsOverrides": {}
"flags": {
"skipAboutBlank": true
}
},
{
Expand Down Expand Up @@ -890325,12 +890324,10 @@
]
}
},
"configContext": {
"flags": {
"skipAboutBlank": true,
"settingsOverrides": {
"disableStorageReset": true
}
"disableStorageReset": true
}
}
]
}
}
Loading