Skip to content

Commit

Permalink
feat(core): convert all issue file paths after executing plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
matejchalk committed Feb 15, 2024
1 parent b12df6d commit d82c68d
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 31 deletions.
40 changes: 20 additions & 20 deletions e2e/cli-e2e/tests/__snapshots__/collect.e2e.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ exports[`CLI collect > should run ESLint plugin and create report.json 1`] = `
"message": "'loading' is assigned a value but never used.",
"severity": "warning",
"source": {
"file": "src/App.jsx",
"file": "examples/react-todos-app/src/App.jsx",
"position": {
"endColumn": 18,
"endLine": 8,
Expand Down Expand Up @@ -519,7 +519,7 @@ exports[`CLI collect > should run ESLint plugin and create report.json 1`] = `
"message": "Unexpected block statement surrounding arrow body; move the returned value immediately after the \`=>\`.",
"severity": "warning",
"source": {
"file": "src/components/TodoFilter.jsx",
"file": "examples/react-todos-app/src/components/TodoFilter.jsx",
"position": {
"endColumn": 2,
"endLine": 25,
Expand Down Expand Up @@ -569,7 +569,7 @@ exports[`CLI collect > should run ESLint plugin and create report.json 1`] = `
"message": "Expected '===' and instead saw '=='.",
"severity": "warning",
"source": {
"file": "src/hooks/useTodos.js",
"file": "examples/react-todos-app/src/hooks/useTodos.js",
"position": {
"endColumn": 43,
"endLine": 41,
Expand All @@ -595,7 +595,7 @@ exports[`CLI collect > should run ESLint plugin and create report.json 1`] = `
"message": "Arrow function has too many lines (71). Maximum allowed is 50.",
"severity": "warning",
"source": {
"file": "src/hooks/useTodos.js",
"file": "examples/react-todos-app/src/hooks/useTodos.js",
"position": {
"endColumn": 2,
"endLine": 73,
Expand Down Expand Up @@ -633,7 +633,7 @@ exports[`CLI collect > should run ESLint plugin and create report.json 1`] = `
"message": "'data' is already declared in the upper scope on line 5 column 10.",
"severity": "warning",
"source": {
"file": "src/hooks/useTodos.js",
"file": "examples/react-todos-app/src/hooks/useTodos.js",
"position": {
"endColumn": 17,
"endLine": 11,
Expand All @@ -646,7 +646,7 @@ exports[`CLI collect > should run ESLint plugin and create report.json 1`] = `
"message": "'data' is already declared in the upper scope on line 5 column 10.",
"severity": "warning",
"source": {
"file": "src/hooks/useTodos.js",
"file": "examples/react-todos-app/src/hooks/useTodos.js",
"position": {
"endColumn": 21,
"endLine": 29,
Expand All @@ -659,7 +659,7 @@ exports[`CLI collect > should run ESLint plugin and create report.json 1`] = `
"message": "'data' is already declared in the upper scope on line 5 column 10.",
"severity": "warning",
"source": {
"file": "src/hooks/useTodos.js",
"file": "examples/react-todos-app/src/hooks/useTodos.js",
"position": {
"endColumn": 17,
"endLine": 41,
Expand Down Expand Up @@ -697,7 +697,7 @@ exports[`CLI collect > should run ESLint plugin and create report.json 1`] = `
"message": "Expected property shorthand.",
"severity": "warning",
"source": {
"file": "src/hooks/useTodos.js",
"file": "examples/react-todos-app/src/hooks/useTodos.js",
"position": {
"endColumn": 19,
"endLine": 19,
Expand All @@ -710,7 +710,7 @@ exports[`CLI collect > should run ESLint plugin and create report.json 1`] = `
"message": "Expected property shorthand.",
"severity": "warning",
"source": {
"file": "src/hooks/useTodos.js",
"file": "examples/react-todos-app/src/hooks/useTodos.js",
"position": {
"endColumn": 19,
"endLine": 32,
Expand All @@ -723,7 +723,7 @@ exports[`CLI collect > should run ESLint plugin and create report.json 1`] = `
"message": "Expected property shorthand.",
"severity": "warning",
"source": {
"file": "src/hooks/useTodos.js",
"file": "examples/react-todos-app/src/hooks/useTodos.js",
"position": {
"endColumn": 25,
"endLine": 33,
Expand Down Expand Up @@ -761,7 +761,7 @@ exports[`CLI collect > should run ESLint plugin and create report.json 1`] = `
"message": "'root' is never reassigned. Use 'const' instead.",
"severity": "warning",
"source": {
"file": "src/index.jsx",
"file": "examples/react-todos-app/src/index.jsx",
"position": {
"endColumn": 9,
"endLine": 5,
Expand Down Expand Up @@ -811,7 +811,7 @@ exports[`CLI collect > should run ESLint plugin and create report.json 1`] = `
"message": "Missing \\"key\\" prop for element in iterator",
"severity": "warning",
"source": {
"file": "src/components/TodoList.jsx",
"file": "examples/react-todos-app/src/components/TodoList.jsx",
"position": {
"endColumn": 12,
"endLine": 28,
Expand All @@ -837,7 +837,7 @@ exports[`CLI collect > should run ESLint plugin and create report.json 1`] = `
"message": "'onCreate' is missing in props validation",
"severity": "warning",
"source": {
"file": "src/components/CreateTodo.jsx",
"file": "examples/react-todos-app/src/components/CreateTodo.jsx",
"position": {
"endColumn": 23,
"endLine": 15,
Expand All @@ -850,7 +850,7 @@ exports[`CLI collect > should run ESLint plugin and create report.json 1`] = `
"message": "'setQuery' is missing in props validation",
"severity": "warning",
"source": {
"file": "src/components/TodoFilter.jsx",
"file": "examples/react-todos-app/src/components/TodoFilter.jsx",
"position": {
"endColumn": 25,
"endLine": 10,
Expand All @@ -863,7 +863,7 @@ exports[`CLI collect > should run ESLint plugin and create report.json 1`] = `
"message": "'setHideComplete' is missing in props validation",
"severity": "warning",
"source": {
"file": "src/components/TodoFilter.jsx",
"file": "examples/react-todos-app/src/components/TodoFilter.jsx",
"position": {
"endColumn": 34,
"endLine": 18,
Expand All @@ -876,7 +876,7 @@ exports[`CLI collect > should run ESLint plugin and create report.json 1`] = `
"message": "'todos' is missing in props validation",
"severity": "warning",
"source": {
"file": "src/components/TodoList.jsx",
"file": "examples/react-todos-app/src/components/TodoList.jsx",
"position": {
"endColumn": 17,
"endLine": 6,
Expand All @@ -889,7 +889,7 @@ exports[`CLI collect > should run ESLint plugin and create report.json 1`] = `
"message": "'todos.map' is missing in props validation",
"severity": "warning",
"source": {
"file": "src/components/TodoList.jsx",
"file": "examples/react-todos-app/src/components/TodoList.jsx",
"position": {
"endColumn": 21,
"endLine": 6,
Expand All @@ -902,7 +902,7 @@ exports[`CLI collect > should run ESLint plugin and create report.json 1`] = `
"message": "'onEdit' is missing in props validation",
"severity": "warning",
"source": {
"file": "src/components/TodoList.jsx",
"file": "examples/react-todos-app/src/components/TodoList.jsx",
"position": {
"endColumn": 27,
"endLine": 13,
Expand Down Expand Up @@ -952,7 +952,7 @@ exports[`CLI collect > should run ESLint plugin and create report.json 1`] = `
"message": "React Hook useCallback does nothing when called with only one argument. Did you forget to pass an array of dependencies?",
"severity": "warning",
"source": {
"file": "src/hooks/useTodos.js",
"file": "examples/react-todos-app/src/hooks/useTodos.js",
"position": {
"endColumn": 31,
"endLine": 17,
Expand All @@ -965,7 +965,7 @@ exports[`CLI collect > should run ESLint plugin and create report.json 1`] = `
"message": "React Hook useCallback does nothing when called with only one argument. Did you forget to pass an array of dependencies?",
"severity": "warning",
"source": {
"file": "src/hooks/useTodos.js",
"file": "examples/react-todos-app/src/hooks/useTodos.js",
"position": {
"endColumn": 29,
"endLine": 40,
Expand Down
1 change: 1 addition & 0 deletions examples/plugins/vite.config.integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default defineConfig({
globalSetup: ['global-setup.ts'],
setupFiles: [
'../../testing-utils/src/lib/setup/fs.mock.ts',
'../../testing-utils/src/lib/setup/git.mock.ts',
'../../testing-utils/src/lib/setup/console.mock.ts',
'../../testing-utils/src/lib/setup/reset.mocks.ts',
],
Expand Down
1 change: 1 addition & 0 deletions examples/plugins/vite.config.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default defineConfig({
globalSetup: ['global-setup.ts'],
setupFiles: [
'../../testing-utils/src/lib/setup/fs.mock.ts',
'../../testing-utils/src/lib/setup/git.mock.ts',
'../../testing-utils/src/lib/setup/console.mock.ts',
'../../testing-utils/src/lib/setup/reset.mocks.ts',
],
Expand Down
5 changes: 2 additions & 3 deletions packages/cli/docs/custom-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ We will extend the `fileSizeAuditOutput` with `details` show which files exceed
// file-size.plugin.ts
// ...
import { basename } from 'path';
import { formatBytes, toUnixPath } from '@code-pushup/utils';
import { formatBytes } from '@code-pushup/utils';
import { AuditOutput } from './plugin-process-output';

async function runnerFunction(options: Options): Promise<AuditOutputs> {
Expand All @@ -521,8 +521,7 @@ async function runnerFunction(options: Options): Promise<AuditOutputs> {
export function assertFileSize(file: string, size: number, budget?: number): Issue {
const auditOutputBase = {
source: {
// format path to be in scope of the repository
file: toUnixPath(file, { toRelative: true }),
file,
},
} satisfies AuditOutput['source'];

Expand Down
1 change: 1 addition & 0 deletions packages/cli/vite.config.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default defineConfig({
globalSetup: ['global-setup.ts'],
setupFiles: [
'../../testing-utils/src/lib/setup/fs.mock.ts',
'../../testing-utils/src/lib/setup/git.mock.ts',
'../../testing-utils/src/lib/setup/console.mock.ts',
'../../testing-utils/src/lib/setup/portal-client.mock.ts',
'../../testing-utils/src/lib/setup/reset.mocks.ts',
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/lib/implementation/execute-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
groupByStatus,
logMultipleResults,
} from '@code-pushup/utils';
import { normalizeAuditOutputs } from '../normalize';
import { executeRunnerConfig, executeRunnerFunction } from './runner';

/**
Expand Down Expand Up @@ -71,8 +72,10 @@ export async function executePlugin(
const auditOutputs = auditOutputsSchema.parse(unvalidatedAuditOutputs);
auditOutputsCorrelateWithPluginOutput(auditOutputs, pluginConfigAudits);

const normalizedAuditOutputs = await normalizeAuditOutputs(auditOutputs);

// enrich `AuditOutputs` to `AuditReport`
const auditReports: AuditReport[] = auditOutputs.map(
const auditReports: AuditReport[] = normalizedAuditOutputs.map(
(auditOutput: AuditOutput) => ({
...auditOutput,
...(pluginConfigAudits.find(
Expand Down
50 changes: 43 additions & 7 deletions packages/core/src/lib/normalize.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,51 @@
import {
type AuditOutputs,
PERSIST_FILENAME,
PERSIST_FORMAT,
PERSIST_OUTPUT_DIR,
PersistConfig,
} from '@code-pushup/models';
import { formatGitPath, getGitRoot } from '@code-pushup/utils';

export const normalizePersistConfig = (
export function normalizePersistConfig(
cfg?: Partial<PersistConfig>,
): Required<PersistConfig> => ({
outputDir: PERSIST_OUTPUT_DIR,
filename: PERSIST_FILENAME,
format: PERSIST_FORMAT,
...cfg,
});
): Required<PersistConfig> {
return {
outputDir: PERSIST_OUTPUT_DIR,
filename: PERSIST_FILENAME,
format: PERSIST_FORMAT,
...cfg,
};
}

export async function normalizeAuditOutputs(
audits: AuditOutputs,
): Promise<AuditOutputs> {
const gitRoot = await getGitRoot();

return audits.map(audit => {
if (
audit.details?.issues == null ||
audit.details.issues.every(issue => issue.source == null)
) {
return audit;
}
return {
...audit,
details: {
...audit.details,
issues: audit.details.issues.map(issue =>
issue.source == null
? issue
: {
...issue,
source: {
...issue.source,
file: formatGitPath(issue.source.file, gitRoot),
},
},
),
},
};
});
}
1 change: 1 addition & 0 deletions packages/core/vite.config.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default defineConfig({
globalSetup: ['global-setup.ts'],
setupFiles: [
'../../testing-utils/src/lib/setup/fs.mock.ts',
'../../testing-utils/src/lib/setup/git.mock.ts',
'../../testing-utils/src/lib/setup/console.mock.ts',
'../../testing-utils/src/lib/setup/reset.mocks.ts',
'../../testing-utils/src/lib/setup/portal-client.mock.ts',
Expand Down
17 changes: 17 additions & 0 deletions testing-utils/src/lib/setup/git.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { vi } from 'vitest';
import { MEMFS_VOLUME } from '../constants';

vi.mock('simple-git', () => ({
simpleGit: () => ({
revparse: () => Promise.resolve(MEMFS_VOLUME),
log: () =>
Promise.resolve({
latest: {
hash: '0123456789abcdef0123456789abcdef01234567',
message: 'Minor fixes',
author: 'John Doe',
date: 'Wed Feb 14 16:00:00 2024 +0100',
},
}),
}),
}));

0 comments on commit d82c68d

Please sign in to comment.