-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): scaffold merge-diffs command and test argument parsing
- Loading branch information
1 parent
e5036f1
commit 074c50f
Showing
10 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type MergeDiffsOptions = { | ||
files: string[]; | ||
}; |
15 changes: 15 additions & 0 deletions
15
packages/cli/src/lib/implementation/merge-diffs.options.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Options } from 'yargs'; | ||
import type { MergeDiffsOptions } from './merge-diffs.model'; | ||
|
||
export function yargsMergeDiffsOptionsDefinition(): Record< | ||
keyof MergeDiffsOptions, | ||
Options | ||
> { | ||
return { | ||
files: { | ||
describe: 'List of report-diff.json paths', | ||
type: 'array', | ||
demandOption: true, | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { bold, gray } from 'ansis'; | ||
import { CommandModule } from 'yargs'; | ||
import { mergeDiffs } from '@code-pushup/core'; | ||
import { PersistConfig } from '@code-pushup/models'; | ||
import { ui } from '@code-pushup/utils'; | ||
import { CLI_NAME } from '../constants'; | ||
import { MergeDiffsOptions } from '../implementation/merge-diffs.model'; | ||
import { yargsMergeDiffsOptionsDefinition } from '../implementation/merge-diffs.options'; | ||
|
||
export function yargsMergeDiffsCommandObject() { | ||
const command = 'merge-diffs'; | ||
return { | ||
command, | ||
describe: 'Combine many report diffs into single report-diff.md', | ||
builder: yargsMergeDiffsOptionsDefinition(), | ||
handler: async (args: unknown) => { | ||
ui().logger.log(bold(CLI_NAME)); | ||
ui().logger.info(gray(`Run ${command}...`)); | ||
|
||
const options = args as MergeDiffsOptions & { | ||
persist: Required<PersistConfig>; | ||
}; | ||
const { files, persist } = options; | ||
|
||
const outputPath = await mergeDiffs(files, persist); | ||
|
||
ui().logger.info(`Reports diff written to ${bold(outputPath)}`); | ||
}, | ||
} satisfies CommandModule; | ||
} |
72 changes: 72 additions & 0 deletions
72
packages/cli/src/lib/merge-diffs/merge-diffs-command.unit.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { bold } from 'ansis'; | ||
import { mergeDiffs } from '@code-pushup/core'; | ||
import { | ||
DEFAULT_PERSIST_FILENAME, | ||
DEFAULT_PERSIST_FORMAT, | ||
DEFAULT_PERSIST_OUTPUT_DIR, | ||
} from '@code-pushup/models'; | ||
import { getLogMessages } from '@code-pushup/test-utils'; | ||
import { ui } from '@code-pushup/utils'; | ||
import { DEFAULT_CLI_CONFIGURATION } from '../../../mocks/constants'; | ||
import { yargsCli } from '../yargs-cli'; | ||
import { yargsMergeDiffsCommandObject } from './merge-diffs-command'; | ||
|
||
vi.mock('@code-pushup/core', async () => { | ||
const core: object = await vi.importActual('@code-pushup/core'); | ||
const { CORE_CONFIG_MOCK }: typeof import('@code-pushup/test-utils') = | ||
await vi.importActual('@code-pushup/test-utils'); | ||
return { | ||
...core, | ||
autoloadRc: vi.fn().mockResolvedValue(CORE_CONFIG_MOCK), | ||
mergeDiffs: vi.fn().mockResolvedValue('.code-pushup/report-diff.md'), | ||
}; | ||
}); | ||
|
||
describe('merge-diffs-command', () => { | ||
it('should parse input paths from command line and output path from persist config', async () => { | ||
await yargsCli( | ||
[ | ||
'merge-diffs', | ||
'--files', | ||
'frontoffice/report-diff.json', | ||
'backoffice/report-diff.json', | ||
'api/report-diff.json', | ||
], | ||
{ | ||
...DEFAULT_CLI_CONFIGURATION, | ||
commands: [yargsMergeDiffsCommandObject()], | ||
}, | ||
).parseAsync(); | ||
|
||
expect(mergeDiffs).toHaveBeenCalledWith<Parameters<typeof mergeDiffs>>( | ||
[ | ||
'frontoffice/report-diff.json', | ||
'backoffice/report-diff.json', | ||
'api/report-diff.json', | ||
], | ||
{ | ||
outputDir: DEFAULT_PERSIST_OUTPUT_DIR, | ||
filename: DEFAULT_PERSIST_FILENAME, | ||
format: DEFAULT_PERSIST_FORMAT, | ||
}, | ||
); | ||
}); | ||
|
||
it('should log output path to stdout', async () => { | ||
await yargsCli( | ||
[ | ||
'merge-diffs', | ||
'--files=frontoffice/report-diff.json', | ||
'--files=backoffice/report-diff.json', | ||
], | ||
{ | ||
...DEFAULT_CLI_CONFIGURATION, | ||
commands: [yargsMergeDiffsCommandObject()], | ||
}, | ||
).parseAsync(); | ||
|
||
expect(getLogMessages(ui().logger).at(-1)).toContain( | ||
`Reports diff written to ${bold('.code-pushup/report-diff.md')}`, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { PersistConfig, reportsDiffSchema } from '@code-pushup/models'; | ||
import { readJsonFile } from '@code-pushup/utils'; | ||
|
||
export async function mergeDiffs( | ||
files: string[], | ||
persistConfig: Required<PersistConfig>, | ||
): Promise<string> { | ||
const diffs = await Promise.all( | ||
files.map(file => readJsonFile(file).then(reportsDiffSchema.parseAsync)), | ||
); | ||
// TODO: implement | ||
return '<path>'; | ||
} |