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

refactor(cli): improve plugin filter option validation #820

Merged
merged 1 commit into from
Sep 24, 2024
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { yellow } from 'ansis';
import type { CategoryConfig, PluginConfig } from '@code-pushup/models';
import { filterItemRefsBy, ui } from '@code-pushup/utils';

Expand Down Expand Up @@ -28,15 +27,13 @@ export function validatePluginFilterOption(
? pluginsToFilterSet.has(plugin)
: !pluginsToFilterSet.has(plugin);

if (missingPlugins.length > 0 && verbose) {
ui().logger.info(
`${yellow(
'⚠',
)} The --${filterOption} argument references plugins with "${missingPlugins.join(
'", "',
)}" slugs, but no such plugins are present in the configuration. Expected one of the following plugin slugs: "${plugins
.map(({ slug }) => slug)
.join('", "')}".`,
if (missingPlugins.length > 0) {
ui().logger.warning(
`The --${filterOption} argument references ${
missingPlugins.length === 1 ? 'a plugin that does' : 'plugins that do'
} not exist: ${missingPlugins.join(', ')}. The valid plugin ${
plugins.length === 1 ? 'slug is' : 'slugs are'
} ${plugins.map(({ slug }) => slug).join(', ')}.`,
);
}

Expand All @@ -45,10 +42,9 @@ export function validatePluginFilterOption(
filterFunction(plugin),
).map(({ slug }) => slug);
ui().logger.info(
`The --${filterOption} argument removed categories with "${removedCategorySlugs.join(
'", "',
)}" slugs.
`,
`The --${filterOption} argument removed the following categories: ${removedCategorySlugs.join(
', ',
)}.`,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,22 @@ import { validatePluginFilterOption } from './validate-plugin-filter-options.uti

describe('validatePluginFilterOption', () => {
describe('onlyPlugins', () => {
it('should warn if onlyPlugins option contains non-existing plugin', () => {
it('should log a warning if the onlyPlugins argument contains multiple nonexistent plugins', () => {
validatePluginFilterOption(
'onlyPlugins',
{
plugins: [{ slug: 'plugin1', audits: [{}] }] as PluginConfig[],
categories: [],
},
{ pluginsToFilter: ['plugin1', 'plugin3', 'plugin4'] },
);
const logs = getLogMessages(ui().logger);
expect(logs[0]).toContain(
'The --onlyPlugins argument references plugins that do not exist: plugin3, plugin4.',
);
});

it('should log a warning if the onlyPlugins argument contains one nonexistent plugin', () => {
validatePluginFilterOption(
'onlyPlugins',
{
Expand All @@ -15,31 +30,44 @@ describe('validatePluginFilterOption', () => {
] as PluginConfig[],
categories: [],
},
{
pluginsToFilter: ['plugin1', 'plugin3', 'plugin4'],
verbose: true,
},
{ pluginsToFilter: ['plugin1', 'plugin2'] },
);
const logs = getLogMessages(ui().logger);
expect(logs[0]).toContain(
'The --onlyPlugins argument references plugins with "plugin3", "plugin4" slugs',
'The --onlyPlugins argument references a plugin that does not exist: plugin2.',
);
});

it('should not log if onlyPlugins option contains only existing plugins', () => {
it('should include all valid plugin slugs in a warning', () => {
validatePluginFilterOption(
'onlyPlugins',
{
plugins: [
{ slug: 'plugin1', audits: [{ slug: 'a1-p1' }] },
{ slug: 'plugin2', audits: [{ slug: 'a1-p2' }] },
{ slug: 'plugin3', audits: [{ slug: 'a1-p3' }] },
] as PluginConfig[],
categories: [],
},
{ pluginsToFilter: ['plugin4'] },
);
const logs = getLogMessages(ui().logger);
expect(logs[0]).toContain(
'The valid plugin slugs are plugin1, plugin2, plugin3.',
);
});

it('should not log anything if the onlyPlugins argument contains only valid plugins', () => {
validatePluginFilterOption(
'onlyPlugins',
{
pluginsToFilter: ['plugin1'],
verbose: true,
plugins: [
{ slug: 'plugin1', audits: [{ slug: 'a1-p1' }] },
{ slug: 'plugin2', audits: [{ slug: 'a1-p2' }] },
] as PluginConfig[],
categories: [],
},
{ pluginsToFilter: ['plugin1'] },
);
expect(getLogMessages(ui().logger)).toHaveLength(0);
});
Expand All @@ -65,12 +93,13 @@ describe('validatePluginFilterOption', () => {
);
expect(getLogMessages(ui().logger)).toHaveLength(1);
expect(getLogMessages(ui().logger)[0]).toContain(
'The --onlyPlugins argument removed categories with "c1", "c3" slugs',
'The --onlyPlugins argument removed the following categories: c1, c3',
);
});
});

describe('skipPlugins', () => {
it('should warn if skipPlugins option contains non-existing plugin', () => {
it('should log a warning if the skipPlugins argument contains multiple nonexistent plugins', () => {
validatePluginFilterOption(
'skipPlugins',
{
Expand All @@ -79,18 +108,32 @@ describe('validatePluginFilterOption', () => {
] as PluginConfig[],
categories: [],
},
{ pluginsToFilter: ['plugin1', 'plugin3', 'plugin4'] },
);
const logs = getLogMessages(ui().logger);
expect(logs[0]).toContain(
'The --skipPlugins argument references plugins that do not exist: plugin3, plugin4.',
);
});

it('should log a warning if the skipPlugins argument contains one nonexistent plugin', () => {
validatePluginFilterOption(
'skipPlugins',
{
pluginsToFilter: ['plugin1', 'plugin3', 'plugin4'],
verbose: true,
plugins: [
{ slug: 'plugin1', audits: [{ slug: 'a1' }] },
] as PluginConfig[],
categories: [],
},
{ pluginsToFilter: ['plugin1', 'plugin2'] },
);
const logs = getLogMessages(ui().logger);
expect(logs[0]).toContain(
'The --skipPlugins argument references plugins with "plugin3", "plugin4" slugs',
'The --skipPlugins argument references a plugin that does not exist: plugin2.',
);
});

it('should not log if skipPlugins option contains only existing plugins', () => {
it('should not log anything if the skipPlugins argument contains only valid plugins', () => {
validatePluginFilterOption(
'skipPlugins',
{
Expand Down Expand Up @@ -129,7 +172,7 @@ describe('validatePluginFilterOption', () => {
);
expect(getLogMessages(ui().logger)).toHaveLength(1);
expect(getLogMessages(ui().logger)[0]).toContain(
'The --skipPlugins argument removed categories with "c1", "c3" slugs',
'The --skipPlugins argument removed the following categories: c1, c3.',
);
});
});
Expand Down
Loading