Skip to content

Commit

Permalink
feat(plugin-lighthouse): add logLevel to lighthouse runner flags
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed Jun 19, 2024
1 parent 22b96b6 commit 120b316
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 25 deletions.
1 change: 1 addition & 0 deletions code-pushup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const config: CoreConfig = {

await lighthousePlugin('https://codepushup.dev/', {
chromeFlags: DEFAULT_FLAGS.concat(['--headless']),
verbose: true,
}),
],

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ describe('logUnsupportedFlagsInUse', () => {
describe('normalizeFlags', () => {
const normalizedDefaults = {
verbose: false,
quiet: false,
saveAssets: false,
// needed to pass CI on linux and windows (locally it works without headless too)
chromeFlags: ['--headless=shell'],
Expand All @@ -54,6 +53,7 @@ describe('normalizeFlags', () => {
view: false,
channel: 'cli',
// custom overwrites in favour of the plugin
quiet: true,
onlyAudits: [],
skipAudits: [],
onlyCategories: [],
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-lighthouse/src/lib/runner/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ export const DEFAULT_CLI_FLAGS = {
// default values extracted from
// https://github.com/GoogleChrome/lighthouse/blob/7d80178c37a1b600ea8f092fc0b098029799a659/cli/cli-flags.js#L80
verbose: false,
quiet: false,
saveAssets: false,
// needed to pass CI on linux and windows (locally it works without headless too)
chromeFlags: ['--headless=shell'],
Expand All @@ -97,6 +96,8 @@ export const DEFAULT_CLI_FLAGS = {
view: false,
channel: 'cli',
// custom overwrites in favour of the plugin
// hide logs by default
quiet: true,
onlyAudits: [],
skipAudits: [],
onlyCategories: [],
Expand Down
5 changes: 3 additions & 2 deletions packages/plugin-lighthouse/src/lib/runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { ensureDirectoryExists } from '@code-pushup/utils';
import { DEFAULT_CLI_FLAGS } from './constants';
import { LighthouseCliFlags } from './types';
import {
determineAndSetLogLevel,
getConfig,
normalizeAuditOutputs,
setLogLevel,
toAuditOutputs,
} from './utils';

Expand All @@ -24,7 +24,7 @@ export function createRunnerFunction(
...parsedFlags
}: Partial<LighthouseCliFlags> = flags;

setLogLevel(parsedFlags);
const logLevel = determineAndSetLogLevel(parsedFlags);

const config = await getConfig({ configPath, preset });
if (outputPath) {
Expand All @@ -33,6 +33,7 @@ export function createRunnerFunction(

const enrichedFlags = {
...parsedFlags,
logLevel,
outputPath,
};

Expand Down
15 changes: 10 additions & 5 deletions packages/plugin-lighthouse/src/lib/runner/runner.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@ import { expect, vi } from 'vitest';
import { DEFAULT_CLI_FLAGS } from './constants';
import { createRunnerFunction } from './runner';
import { LighthouseCliFlags } from './types';
import { getConfig, setLogLevel } from './utils';
import { determineAndSetLogLevel, getConfig } from './utils';

// used for createRunnerMocking
vi.mock('./utils', async () => {
// Import the actual 'lighthouse' module
const actual = await vi.importActual('./utils');

const actualDetermineAndSetLogLevel = actual['determineAndSetLogLevel'] as (
s: string,
) => string;
// Return the mocked module, merging the actual module with overridden parts
return {
...actual,
setLogLevel: vi.fn(),
determineAndSetLogLevel: vi
.fn()
.mockImplementation(actualDetermineAndSetLogLevel),
getBudgets: vi.fn().mockImplementation((path: string) => [{ path }]),
getConfig: vi.fn(),
};
Expand Down Expand Up @@ -72,17 +77,17 @@ describe('createRunnerFunction', () => {

expect(runLighthouse).toHaveBeenCalledWith(
'https://localhost:8080',
DEFAULT_CLI_FLAGS,
{ ...DEFAULT_CLI_FLAGS, logLevel: 'silent' },
undefined,
);
});

it('should call setLogLevel with given verbose and quiet flags', async () => {
it('should call determineAndSetLogLevel with given verbose and quiet flags', async () => {
await createRunnerFunction('https://localhost:8080', {
verbose: true,
quiet: true,
} as LighthouseCliFlags)(undefined);
expect(setLogLevel).toHaveBeenCalledWith(
expect(determineAndSetLogLevel).toHaveBeenCalledWith(
expect.objectContaining({ verbose: true, quiet: true }),
);
});
Expand Down
23 changes: 17 additions & 6 deletions packages/plugin-lighthouse/src/lib/runner/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,32 @@ export function logUnsupportedDetails(
}
}

export function setLogLevel({
export type LighthouseLogLevel =
| 'verbose'
| 'error'
| 'info'
| 'silent'
| 'warn'
| undefined;
export function determineAndSetLogLevel({
verbose,
quiet,
}: {
verbose?: boolean;
quiet?: boolean;
} = {}) {
} = {}): LighthouseLogLevel {
// eslint-disable-next-line functional/no-let
let logLevel: LighthouseLogLevel = 'info';
// set logging preferences
if (verbose) {
log.setLevel('verbose');
logLevel = 'verbose';
} else if (quiet) {
log.setLevel('silent');
} else {
log.setLevel('info');
logLevel = 'silent';
}

log.setLevel(logLevel);

return logLevel;
}

export type ConfigOptions = Partial<
Expand Down
22 changes: 12 additions & 10 deletions packages/plugin-lighthouse/src/lib/runner/utils.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { CoreConfig, auditOutputsSchema } from '@code-pushup/models';
import { MEMFS_VOLUME, getLogMessages } from '@code-pushup/test-utils';
import { ui } from '@code-pushup/utils';
import {
determineAndSetLogLevel,
getConfig,
logUnsupportedDetails,
setLogLevel,
toAuditOutputs,
unsupportedDetailTypes,
} from './utils';
Expand Down Expand Up @@ -415,7 +415,7 @@ describe('getConfig', () => {
});
});

describe('setLogLevel', () => {
describe('determineAndSetLogLevel', () => {
const debugLib = debug as { enabled: (flag: string) => boolean };
beforeEach(() => {
log.setLevel('info');
Expand All @@ -439,30 +439,32 @@ describe('setLogLevel', () => {
* debug.enable('LH:*, -LH:*:verbose');
*/

it('should set log level to info if no options are given', () => {
setLogLevel();
it('should set log level to info and return "info" as level if no options are given', () => {
expect(determineAndSetLogLevel()).toBe('info');
expect(log.isVerbose()).toBe(false);
expect(debugLib.enabled('LH:*')).toBe(true);
expect(debugLib.enabled('LH:*:verbose')).toBe(false);
});

it('should set log level to verbose', () => {
setLogLevel({ verbose: true });
it('should set log level to verbose and return "verbose" as level', () => {
expect(determineAndSetLogLevel({ verbose: true })).toBe('verbose');
expect(log.isVerbose()).toBe(true);
expect(debugLib.enabled('LH:*')).toBe(true);
expect(debugLib.enabled('LH:*:verbose')).toBe(false);
});

it('should set log level to quiet', () => {
setLogLevel({ quiet: true });
it('should set log level to quiet and return "silent" as level', () => {
expect(determineAndSetLogLevel({ quiet: true })).toBe('silent');
expect(log.isVerbose()).toBe(false);
expect(debugLib.enabled('LH:*')).toBe(true);
expect(debugLib.enabled('-LH:*')).toBe(true);
expect(debugLib.enabled('LH:*:verbose')).toBe(false);
});

it('should set log level to verbose if verbose and quiet are given', () => {
setLogLevel({ verbose: true, quiet: true });
it('should set log level to verbose if verbose and quiet are given and return "verbose" as level', () => {
expect(determineAndSetLogLevel({ verbose: true, quiet: true })).toBe(
'verbose',
);
expect(log.isVerbose()).toBe(true);
expect(debugLib.enabled('LH:*')).toBe(true);
expect(debugLib.enabled('LH:*:verbose')).toBe(false);
Expand Down

0 comments on commit 120b316

Please sign in to comment.