Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
101 changes: 34 additions & 67 deletions packages/cli/src/lib/implementation/filter.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@
PluginConfig,
} from '@code-pushup/models';
import { filterItemRefsBy } from '@code-pushup/utils';
import {
applyFilters,
extractSkippedItems,
filterPluginsFromCategories,
filterSkippedItems,
isValidCategoryRef,
} from './filter.middleware.utils.js';
import type { FilterOptions, Filterables } from './filter.model.js';
import {
handleConflictingOptions,
isValidCategoryRef,
validateFilterOption,
validateFilteredCategories,
validateFinalState,
validateSkippedCategories,
} from './validate-filter-options.utils.js';

// eslint-disable-next-line max-lines-per-function
Expand All @@ -27,23 +33,18 @@
verbose = false,
} = originalProcessArgs;

const plugins = processPlugins(rcPlugins);
const plugins = filterSkippedInPlugins(rcPlugins);
const categories = filterSkippedCategories(rcCategories, plugins);

if (rcCategories && categories) {
validateFilteredCategories(rcCategories, categories, {
onlyCategories,
skipCategories,
verbose,
});
}

if (
skipCategories.length === 0 &&
onlyCategories.length === 0 &&
skipPlugins.length === 0 &&
onlyPlugins.length === 0
) {
if (rcCategories && categories) {
validateSkippedCategories(rcCategories, categories, verbose);
}
return {
...originalProcessArgs,
...(categories && { categories }),
Expand All @@ -54,17 +55,18 @@
handleConflictingOptions('categories', onlyCategories, skipCategories);
handleConflictingOptions('plugins', onlyPlugins, skipPlugins);

const skippedPlugins = extractSkippedItems(rcPlugins, plugins);
const skippedCategories = extractSkippedItems(rcCategories, categories);

const filteredCategories = applyCategoryFilters(
{ categories, plugins },
skipCategories,
onlyCategories,
verbose,
skippedCategories,
{ skipCategories, onlyCategories, verbose },
);
const filteredPlugins = applyPluginFilters(
{ categories: filteredCategories, plugins },
skipPlugins,
onlyPlugins,
verbose,
skippedPlugins,
{ skipPlugins, onlyPlugins, verbose },
);
const finalCategories = filteredCategories
? filterItemRefsBy(filteredCategories, ref =>
Expand All @@ -84,53 +86,37 @@
};
}

function applyFilters<T>(
items: T[],
skipItems: string[],
onlyItems: string[],
key: keyof T,
): T[] {
return items.filter(item => {
const itemKey = item[key] as unknown as string;
return (
!skipItems.includes(itemKey) &&
(onlyItems.length === 0 || onlyItems.includes(itemKey))
);
});
}

function applyCategoryFilters(
{ categories, plugins }: Filterables,
skipCategories: string[],
onlyCategories: string[],
verbose: boolean,
skippedCategories: string[],
options: Pick<FilterOptions, 'skipCategories' | 'onlyCategories' | 'verbose'>,
): CoreConfig['categories'] {
const { skipCategories = [], onlyCategories = [], verbose = false } = options;
if (
(skipCategories.length === 0 && onlyCategories.length === 0) ||
!categories ||
categories.length === 0
((!categories || categories.length === 0) && skippedCategories.length === 0)

Check failure on line 97 in packages/cli/src/lib/implementation/filter.middleware.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.
) {
return categories;
}
validateFilterOption(
'skipCategories',
{ plugins, categories },
{ itemsToFilter: skipCategories, verbose },
{ itemsToFilter: skipCategories, skippedItems: skippedCategories, verbose },
);
validateFilterOption(
'onlyCategories',
{ plugins, categories },
{ itemsToFilter: onlyCategories, verbose },
{ itemsToFilter: onlyCategories, skippedItems: skippedCategories, verbose },
);
return applyFilters(categories, skipCategories, onlyCategories, 'slug');
return applyFilters(categories ?? [], skipCategories, onlyCategories, 'slug');

Check failure on line 111 in packages/cli/src/lib/implementation/filter.middleware.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.
}

function applyPluginFilters(
{ categories, plugins }: Filterables,
skipPlugins: string[],
onlyPlugins: string[],
verbose: boolean,
skippedPlugins: string[],
options: Pick<FilterOptions, 'skipPlugins' | 'onlyPlugins' | 'verbose'>,
): CoreConfig['plugins'] {
const { skipPlugins = [], onlyPlugins = [], verbose = false } = options;
const filteredPlugins = filterPluginsFromCategories({
categories,
plugins,
Expand All @@ -141,38 +127,19 @@
validateFilterOption(
'skipPlugins',
{ plugins: filteredPlugins, categories },
{ itemsToFilter: skipPlugins, verbose },
{ itemsToFilter: skipPlugins, skippedItems: skippedPlugins, verbose },
);
validateFilterOption(
'onlyPlugins',
{ plugins: filteredPlugins, categories },
{ itemsToFilter: onlyPlugins, verbose },
{ itemsToFilter: onlyPlugins, skippedItems: skippedPlugins, verbose },
);
return applyFilters(filteredPlugins, skipPlugins, onlyPlugins, 'slug');
}

function filterPluginsFromCategories({
categories,
plugins,
}: Filterables): CoreConfig['plugins'] {
if (!categories || categories.length === 0) {
return plugins;
}
const validPluginSlugs = new Set(
categories.flatMap(category => category.refs.map(ref => ref.plugin)),
);
return plugins.filter(plugin => validPluginSlugs.has(plugin.slug));
}

function filterSkippedItems<T extends { isSkipped?: boolean }>(
items: T[] | undefined,
): Omit<T, 'isSkipped'>[] {
return (items ?? [])
.filter(({ isSkipped }) => isSkipped !== true)
.map(({ isSkipped, ...props }) => props);
}

export function processPlugins(plugins: PluginConfig[]): PluginConfig[] {
export function filterSkippedInPlugins(
plugins: PluginConfig[],
): PluginConfig[] {
return plugins.map((plugin: PluginConfig) => {
const filteredAudits = filterSkippedItems(plugin.audits);
return {
Expand Down
132 changes: 88 additions & 44 deletions packages/cli/src/lib/implementation/filter.middleware.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ui } from '@code-pushup/utils';
import {
filterMiddleware,
filterSkippedCategories,
processPlugins,
filterSkippedInPlugins,
} from './filter.middleware.js';
import { OptionValidationError } from './validate-filter-options.utils.js';

Expand Down Expand Up @@ -204,47 +204,91 @@ describe('filterMiddleware', () => {
},
);

it('should filter plugins and categories with mixed filter options', () => {
const { plugins, categories } = filterMiddleware({
skipPlugins: ['p1'],
onlyCategories: ['c1'],
plugins: [
{
slug: 'p1',
audits: [{ slug: 'a1-p1' }],
groups: [{ slug: 'g1-p1', refs: [{ slug: 'a1-p1', weight: 1 }] }],
},
{
slug: 'p2',
audits: [{ slug: 'a1-p2' }],
groups: [{ slug: 'g1-p2', refs: [{ slug: 'a1-p2', weight: 1 }] }],
},
{
slug: 'p3',
audits: [{ slug: 'a1-p3' }],
groups: [{ slug: 'g1-p3', refs: [{ slug: 'a1-p3', weight: 1 }] }],
},
] as PluginConfig[],
categories: [
{
slug: 'c1',
refs: [
{ type: 'group', plugin: 'p1', slug: 'g1-p1', weight: 1 },
{ type: 'group', plugin: 'p2', slug: 'g1-p2', weight: 1 },
],
},
{
slug: 'c2',
refs: [{ type: 'group', plugin: 'p3', slug: 'g1-p3', weight: 1 }],
},
] as CategoryConfig[],
});
const pluginSlugs = plugins.map(({ slug }) => slug);
const categorySlugs = categories?.map(({ slug }) => slug);
it.each([
[
{ skipPlugins: ['eslint'], onlyCategories: ['performance'] },
['lighthouse'],
['performance'],
],
[
{ skipCategories: ['performance'], onlyPlugins: ['lighthouse'] },
['lighthouse'],
['best-practices'],
],
])(
'should filter plugins and categories with mixed filter options: %o',
(option, expectedPlugins, expectedCategories) => {
const { plugins, categories } = filterMiddleware({
...option,
plugins: [
{
slug: 'lighthouse',
audits: [{ slug: 'largest-contentful-paint' }, { slug: 'doctype' }],
groups: [
{
slug: 'performance',
refs: [{ slug: 'largest-contentful-paint', weight: 1 }],
},
{
slug: 'best-practices',
refs: [{ slug: 'doctype', weight: 1 }],
},
],
},
{
slug: 'eslint',
audits: [{ slug: 'no-unreachable' }],
groups: [
{
slug: 'problems',
refs: [{ slug: 'no-unreachable', weight: 1 }],
},
],
},
] as PluginConfig[],
categories: [
{
slug: 'performance',
refs: [
{
type: 'group',
plugin: 'lighthouse',
slug: 'performance',
weight: 1,
},
],
},
{
slug: 'best-practices',
refs: [
{
type: 'group',
plugin: 'lighthouse',
slug: 'best-practices',
weight: 1,
},
],
},
{
slug: 'bug-prevention',
refs: [
{
type: 'group',
plugin: 'eslint',
slug: 'problems',
weight: 1,
},
],
},
] as CategoryConfig[],
});
const pluginSlugs = plugins.map(({ slug }) => slug);
const categorySlugs = categories?.map(({ slug }) => slug);

expect(pluginSlugs).toStrictEqual(['p2']);
expect(categorySlugs).toStrictEqual(['c1']);
});
expect(pluginSlugs).toStrictEqual(expectedPlugins);
expect(categorySlugs).toStrictEqual(expectedCategories);
},
);

it('should trigger verbose logging when skipPlugins or onlyPlugins removes categories', () => {
const loggerSpy = vi.spyOn(ui().logger, 'info');
Expand Down Expand Up @@ -366,10 +410,10 @@ describe('filterMiddleware', () => {
});
});

describe('processPlugins', () => {
describe('filterSkippedInPlugins', () => {
it('should filter out skipped audits and groups', () => {
expect(
processPlugins([
filterSkippedInPlugins([
{
slug: 'p1',
audits: [
Expand Down Expand Up @@ -404,7 +448,7 @@ describe('processPlugins', () => {

it('should filter out entire groups when marked as skipped', () => {
expect(
processPlugins([
filterSkippedInPlugins([
{
slug: 'p1',
audits: [{ slug: 'a1', isSkipped: false }],
Expand Down
Loading
Loading