Skip to content

Commit

Permalink
fix(cli): fix json format handling (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed Dec 19, 2023
1 parent c35a3ee commit bc9f101
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 31 deletions.
3 changes: 1 addition & 2 deletions e2e/cli-e2e/tests/collect.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ describe('CLI collect', () => {
expect(stderr).toBe('');

expect(stdout).toContain('Code PushUp Report');
expect(stdout).toContain('Generated reports');
expect(stdout).toContain('report.json');
expect(stdout).not.toContain('Generated reports');
expect(stdout).toContain(exampleCategoryTitle);
expect(stdout).toContain(exampleAuditTitle);
}, 120000);
Expand Down
14 changes: 13 additions & 1 deletion packages/cli/src/lib/autorun/autorun-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,19 @@ export function yargsAutorunCommandObject() {
console.info(chalk.bold(CLI_NAME));
console.info(chalk.gray(`Run ${command}...`));
const options = args as unknown as AutorunOptions;
await collectAndPersistReports(options);

// we need to ensure `json` is part of the formats as we want to upload
const optionsWithFormat: AutorunOptions = {
...options,
persist: {
...options.persist,
format: [
...new Set([...options.persist.format, 'json']),
] as AutorunOptions['persist']['format'],
},
};

await collectAndPersistReports(optionsWithFormat);
if (!options.upload) {
console.warn('Upload skipped because configuration is not set.'); // @TODO log verbose
} else {
Expand Down
32 changes: 19 additions & 13 deletions packages/cli/src/lib/autorun/autorun-command.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
import { PortalUploadArgs, uploadToPortal } from '@code-pushup/portal-client';
import { collectAndPersistReports } from '@code-pushup/core';
import { MINIMAL_REPORT_MOCK } from '@code-pushup/testing-utils';
import { objectToCliArgs } from '@code-pushup/utils';
import { DEFAULT_CLI_CONFIGURATION } from '../../../mocks/constants';
import { yargsCli } from '../yargs-cli';
import { yargsAutorunCommandObject } from './autorun-command';
Expand All @@ -16,6 +17,21 @@ vi.mock('@code-pushup/core', async () => {
};
});

const cli = (options = {}) =>
yargsCli(
objectToCliArgs({
_: 'autorun',
verbose: true,
config: '/test/code-pushup.config.ts',
'persist.outputDir': '/test',
...options,
}),
{
...DEFAULT_CLI_CONFIGURATION,
commands: [yargsAutorunCommandObject()],
},
);

describe('autorun-command', () => {
beforeEach(() => {
vol.fromJSON(
Expand All @@ -28,19 +44,9 @@ describe('autorun-command', () => {
});

it('should call collect and upload with correct parameters', async () => {
await yargsCli(
[
'autorun',
'--verbose',
'--config=/test/code-pushup.config.ts',
'--persist.filename=my-report',
'--persist.outputDir=/test',
],
{
...DEFAULT_CLI_CONFIGURATION,
commands: [yargsAutorunCommandObject()],
},
).parseAsync();
await cli({
'persist.filename': 'my-report',
}).parseAsync();

expect(bundleRequire).toHaveBeenCalledWith({
format: 'esm',
Expand Down
26 changes: 17 additions & 9 deletions packages/core/src/lib/implementation/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,23 @@ export async function persistReport(
): Promise<MultipleFileResults> {
const { outputDir, filename, format } = options;

let scoredReport = scoreReport(report);
const scoredReport = scoreReport(report);
console.info(reportToStdout(scoredReport));

// collect physical format outputs
const results: { format: string; content: string }[] = [
// JSON is always persisted
{ format: 'json', content: JSON.stringify(report, null, 2) },
];
const results: { format: string; content: string }[] = [];

if (format.includes('json')) {
results.push({
format: 'json',
content: JSON.stringify(report, null, 2),
});
}

if (format.includes('md')) {
scoredReport = scoredReport || scoreReport(report);
const commitData = await getLatestCommit();
if (!commitData) {
console.warn('no commit data available');
}
validateCommitData(commitData);

results.push({
format: 'md',
content: reportToMd(scoredReport, commitData),
Expand Down Expand Up @@ -81,3 +83,9 @@ export async function persistReport(
export function logPersistedResults(persistResults: MultipleFileResults) {
logMultipleFileResults(persistResults, 'Generated reports');
}

function validateCommitData(commitData?: unknown) {
if (!commitData) {
console.warn('no commit data available');
}
}
19 changes: 17 additions & 2 deletions packages/core/src/lib/implementation/persist.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ describe('persistReport', () => {
);
});

it('should create a report in json format by default', async () => {
it('should create a report in json format', async () => {
await persistReport(MINIMAL_REPORT_MOCK, {
outputDir: MEMFS_VOLUME,
filename: 'report',
format: [],
format: ['json'],
});

const jsonReport: Report = JSON.parse(
Expand All @@ -58,6 +58,21 @@ describe('persistReport', () => {
);
});

it('should create a report in md format', async () => {
await persistReport(MINIMAL_REPORT_MOCK, {
outputDir: MEMFS_VOLUME,
filename: 'report',
format: ['md'],
});

const mdReport = readFileSync(join(MEMFS_VOLUME, 'report.md')).toString();
expect(mdReport).toContain('Code PushUp Report');

expect(() => readFileSync(join(MEMFS_VOLUME, 'report.json'))).toThrow(
'no such file or directory',
);
});

it('should create a report in all formats', async () => {
await persistReport(MINIMAL_REPORT_MOCK, {
outputDir: MEMFS_VOLUME,
Expand Down
2 changes: 1 addition & 1 deletion packages/models/src/lib/persist-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const persistConfigSchema = z.object({
filename: fileNameSchema(
'Artifacts file name (without extension)',
).optional(),
format: z.array(formatSchema).default(['json']).optional(), // @TODO remove default or optional value and otherwise it will not set defaults.
format: z.array(formatSchema).optional(),
});

export type PersistConfig = z.infer<typeof persistConfigSchema>;
6 changes: 6 additions & 0 deletions packages/models/src/lib/persist-config.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ describe('persistConfigSchema', () => {
expect(() => persistConfigSchema.parse(persistConfigMock)).not.toThrow();
});

it('should fill defaults', () => {
const persistConfigMock = persistConfigSchema.parse(persistConfig());
expect(persistConfigMock.filename).toBe('report');
expect(persistConfigMock.format).toEqual(['json']);
});

it('should throw if outputDir is invalid', () => {
const persistConfigMock = persistConfig();
persistConfigMock.outputDir = ' ';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type {
AuditOutput,
CoreConfig,
PluginConfig,
RunnerConfig,
RunnerFunction,
Expand Down Expand Up @@ -46,8 +45,7 @@ export const MINIMAL_PLUGIN_CONFIG_MOCK: PluginConfig = {
runner: MINIMAL_RUNNER_FUNCTION_MOCK,
};

export const MINIMAL_CONFIG_MOCK: CoreConfig = {
persist: { outputDir: '/test' },
export const MINIMAL_CONFIG_MOCK = {
upload: {
organization: 'code-pushup',
project: 'cli',
Expand Down

0 comments on commit bc9f101

Please sign in to comment.