Skip to content

Commit 8c4e1e4

Browse files
committed
feat(core): create report-diff.md file if specified by persist.format
1 parent 65a08ea commit 8c4e1e4

File tree

5 files changed

+94
-23
lines changed

5 files changed

+94
-23
lines changed

packages/cli/src/lib/compare/compare-command.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import chalk from 'chalk';
2-
import { join } from 'node:path';
32
import { CommandModule } from 'yargs';
43
import { compareReportFiles } from '@code-pushup/core';
54
import { PersistConfig } from '@code-pushup/models';
@@ -23,14 +22,14 @@ export function yargsCompareCommandObject() {
2322
};
2423

2524
const { before, after, persist } = options;
26-
const outputPath = join(
27-
persist.outputDir,
28-
`${persist.filename}-diff.json`,
29-
);
3025

31-
await compareReportFiles({ before, after }, outputPath);
26+
const outputPaths = await compareReportFiles({ before, after }, persist);
3227

33-
ui().logger.info(`Reports diff written to ${chalk.bold(outputPath)}`);
28+
ui().logger.info(
29+
`Reports diff written to ${outputPaths
30+
.map(path => chalk.bold(path))
31+
.join(' and ')}`,
32+
);
3433
},
3534
} satisfies CommandModule;
3635
}

packages/cli/src/lib/compare/compare-command.unit.test.ts

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import { join } from 'node:path';
1+
import chalk from 'chalk';
22
import { compareReportFiles } from '@code-pushup/core';
33
import {
44
DEFAULT_PERSIST_FILENAME,
5+
DEFAULT_PERSIST_FORMAT,
56
DEFAULT_PERSIST_OUTPUT_DIR,
67
} from '@code-pushup/models';
8+
import { getLogMessages } from '@code-pushup/test-utils';
9+
import { ui } from '@code-pushup/utils';
710
import { DEFAULT_CLI_CONFIGURATION } from '../../../mocks/constants';
811
import { yargsCli } from '../yargs-cli';
912
import { yargsCompareCommandObject } from './compare-command';
@@ -15,7 +18,12 @@ vi.mock('@code-pushup/core', async () => {
1518
return {
1619
...core,
1720
autoloadRc: vi.fn().mockResolvedValue(CORE_CONFIG_MOCK),
18-
compareReportFiles: vi.fn(),
21+
compareReportFiles: vi
22+
.fn()
23+
.mockResolvedValue([
24+
'.code-pushup/report-diff.json',
25+
'.code-pushup/report-diff.md',
26+
]),
1927
};
2028
});
2129

@@ -30,7 +38,24 @@ describe('compare-command', () => {
3038
Parameters<typeof compareReportFiles>
3139
>(
3240
{ before: 'source-report.json', after: 'target-report.json' },
33-
join(DEFAULT_PERSIST_OUTPUT_DIR, `${DEFAULT_PERSIST_FILENAME}-diff.json`),
41+
{
42+
outputDir: DEFAULT_PERSIST_OUTPUT_DIR,
43+
filename: DEFAULT_PERSIST_FILENAME,
44+
format: DEFAULT_PERSIST_FORMAT,
45+
},
46+
);
47+
});
48+
49+
it('should log output paths to stdout', async () => {
50+
await yargsCli(
51+
['compare', '--before=source-report.json', '--after=target-report.json'],
52+
{ ...DEFAULT_CLI_CONFIGURATION, commands: [yargsCompareCommandObject()] },
53+
).parseAsync();
54+
55+
expect(getLogMessages(ui().logger).at(-1)).toContain(
56+
`Reports diff written to ${chalk.bold(
57+
'.code-pushup/report-diff.json',
58+
)} and ${chalk.bold('.code-pushup/report-diff.md')}`,
3459
);
3560
});
3661
});

packages/core/src/lib/compare.ts

+33-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import { writeFile } from 'node:fs/promises';
2-
import { Report, ReportsDiff, reportSchema } from '@code-pushup/models';
2+
import { join } from 'node:path';
3+
import {
4+
type Format,
5+
type PersistConfig,
6+
Report,
7+
ReportsDiff,
8+
reportSchema,
9+
} from '@code-pushup/models';
310
import {
411
Diff,
512
calcDuration,
13+
generateMdReportsDiff,
614
readJsonFile,
715
scoreReport,
816
} from '@code-pushup/utils';
@@ -16,8 +24,10 @@ import {
1624

1725
export async function compareReportFiles(
1826
inputPaths: Diff<string>,
19-
outputPath: string,
20-
): Promise<void> {
27+
persistConfig: Required<PersistConfig>,
28+
): Promise<string[]> {
29+
const { outputDir, filename, format } = persistConfig;
30+
2131
const [reportBefore, reportAfter] = await Promise.all([
2232
readJsonFile(inputPaths.before),
2333
readJsonFile(inputPaths.after),
@@ -29,7 +39,14 @@ export async function compareReportFiles(
2939

3040
const reportsDiff = compareReports(reports);
3141

32-
await writeFile(outputPath, JSON.stringify(reportsDiff, null, 2));
42+
return Promise.all(
43+
format.map(async fmt => {
44+
const outputPath = join(outputDir, `${filename}-diff.${fmt}`);
45+
const content = reportsDiffToFileContent(reportsDiff, fmt);
46+
await writeFile(outputPath, content);
47+
return outputPath;
48+
}),
49+
);
3350
}
3451

3552
export function compareReports(reports: Diff<Report>): ReportsDiff {
@@ -63,3 +80,15 @@ export function compareReports(reports: Diff<Report>): ReportsDiff {
6380
duration,
6481
};
6582
}
83+
84+
function reportsDiffToFileContent(
85+
reportsDiff: ReportsDiff,
86+
format: Format,
87+
): string {
88+
switch (format) {
89+
case 'json':
90+
return JSON.stringify(reportsDiff, null, 2);
91+
case 'md':
92+
return generateMdReportsDiff(reportsDiff);
93+
}
94+
}

packages/core/src/lib/compare.unit.test.ts

+21-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
REPORT_MOCK,
1010
reportMock,
1111
} from '@code-pushup/test-utils';
12-
import { Diff, readJsonFile } from '@code-pushup/utils';
12+
import { Diff, fileExists, readJsonFile } from '@code-pushup/utils';
1313
import { compareReportFiles, compareReports } from './compare';
1414

1515
describe('compareReportFiles', () => {
@@ -23,23 +23,40 @@ describe('compareReportFiles', () => {
2323
);
2424
});
2525

26-
it('should create valid reports-diff.json from report.json files', async () => {
26+
it('should create valid report-diff.json from report.json files', async () => {
2727
await compareReportFiles(
2828
{
2929
before: join(MEMFS_VOLUME, 'source-report.json'),
3030
after: join(MEMFS_VOLUME, 'target-report.json'),
3131
},
32-
join(MEMFS_VOLUME, 'reports-diff.json'),
32+
{ outputDir: MEMFS_VOLUME, filename: 'report', format: ['json'] },
3333
);
3434

3535
const reportsDiffPromise = readJsonFile(
36-
join(MEMFS_VOLUME, 'reports-diff.json'),
36+
join(MEMFS_VOLUME, 'report-diff.json'),
3737
);
3838
await expect(reportsDiffPromise).resolves.toBeTruthy();
3939

4040
const reportsDiff = await reportsDiffPromise;
4141
expect(() => reportsDiffSchema.parse(reportsDiff)).not.toThrow();
4242
});
43+
44+
it('should create all diff files specified by persist.format', async () => {
45+
await compareReportFiles(
46+
{
47+
before: join(MEMFS_VOLUME, 'source-report.json'),
48+
after: join(MEMFS_VOLUME, 'target-report.json'),
49+
},
50+
{ outputDir: MEMFS_VOLUME, filename: 'report', format: ['json', 'md'] },
51+
);
52+
53+
await expect(
54+
fileExists(join(MEMFS_VOLUME, 'report-diff.json')),
55+
).resolves.toBeTruthy();
56+
await expect(
57+
fileExists(join(MEMFS_VOLUME, 'report-diff.md')),
58+
).resolves.toBeTruthy();
59+
});
4360
});
4461

4562
describe('compareReports', () => {

packages/utils/src/index.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export { exists } from '@code-pushup/models';
2-
export { Diff, matchArrayItemsByKey, comparePairs } from './lib/diff';
2+
export { Diff, comparePairs, matchArrayItemsByKey } from './lib/diff';
33
export {
44
ProcessConfig,
55
ProcessError,
@@ -37,20 +37,20 @@ export {
3737
} from './lib/formatting';
3838
export {
3939
formatGitPath,
40+
getCurrentBranchOrTag,
4041
getGitRoot,
4142
getLatestCommit,
42-
toGitPath,
43-
getCurrentBranchOrTag,
4443
safeCheckout,
44+
toGitPath,
4545
} from './lib/git';
4646
export { groupByStatus } from './lib/group-by-status';
4747
export {
4848
isPromiseFulfilledResult,
4949
isPromiseRejectedResult,
5050
} from './lib/guards';
5151
export { logMultipleResults } from './lib/log-results';
52+
export { CliUi, Column, link, ui } from './lib/logging';
5253
export { ProgressBar, getProgressBar } from './lib/progress';
53-
export { logStdoutSummary } from './lib/reports/log-stdout-summary';
5454
export {
5555
CODE_PUSHUP_DOMAIN,
5656
FOOTER_PREFIX,
@@ -62,6 +62,8 @@ export {
6262
listGroupsFromAllPlugins,
6363
} from './lib/reports/flatten-plugins';
6464
export { generateMdReport } from './lib/reports/generate-md-report';
65+
export { generateMdReportsDiff } from './lib/reports/generate-md-reports-diff';
66+
export { logStdoutSummary } from './lib/reports/log-stdout-summary';
6567
export { scoreReport } from './lib/reports/scoring';
6668
export { sortReport } from './lib/reports/sorting';
6769
export {
@@ -90,4 +92,3 @@ export {
9092
toUnixPath,
9193
} from './lib/transform';
9294
export { verboseUtils } from './lib/verbose-utils';
93-
export { link, ui, CliUi, Column } from './lib/logging';

0 commit comments

Comments
 (0)