diff --git a/packages/ci/src/lib/cli/context.unit.test.ts b/packages/ci/src/lib/cli/context.unit.test.ts new file mode 100644 index 000000000..bdd2e8356 --- /dev/null +++ b/packages/ci/src/lib/cli/context.unit.test.ts @@ -0,0 +1,101 @@ +import { DEFAULT_SETTINGS } from '../constants'; +import { type CommandContext, createCommandContext } from './context'; + +describe('createCommandContext', () => { + it('should pick CLI-related settings in standalone mode', () => { + expect( + createCommandContext( + { + bin: 'npx --no-install code-pushup', + config: null, + debug: false, + detectNewIssues: true, + directory: '/test', + logger: console, + monorepo: false, + output: '.code-pushup', + projects: null, + silent: false, + task: 'code-pushup', + }, + null, + ), + ).toStrictEqual({ + project: undefined, + bin: 'npx --no-install code-pushup', + directory: '/test', + config: null, + silent: false, + output: '.code-pushup', + }); + }); + + it('should override some settings when given monorepo project config', () => { + expect( + createCommandContext( + { + bin: 'npx --no-install code-pushup', + config: null, + debug: false, + detectNewIssues: true, + directory: '/test', + logger: console, + monorepo: false, + output: '.code-pushup', + projects: null, + silent: false, + task: 'code-pushup', + }, + { + name: 'ui', + directory: '/test/ui', + bin: 'yarn code-pushup', + }, + ), + ).toStrictEqual({ + project: 'ui', + bin: 'yarn code-pushup', + directory: '/test/ui', + config: null, + silent: false, + output: '.code-pushup', + }); + }); + + it('should interpolate project name in output path for monorepo project', () => { + expect( + createCommandContext( + { + ...DEFAULT_SETTINGS, + output: '.code-pushup/{project}', + }, + { + name: 'website', + bin: 'npx nx run website:code-pushup --', + }, + ), + ).toEqual( + expect.objectContaining>({ + project: 'website', + bin: 'npx nx run website:code-pushup --', + output: '.code-pushup/website', + }), + ); + }); + + it('should omit {project} placeholder in output path when in standalone mode', () => { + expect( + createCommandContext( + { + ...DEFAULT_SETTINGS, + output: '.code-pushup/{project}', + }, + undefined, + ), + ).toEqual( + expect.objectContaining>({ + output: '.code-pushup/', + }), + ); + }); +}); diff --git a/packages/ci/src/lib/cli/persist.unit.test.ts b/packages/ci/src/lib/cli/persist.unit.test.ts new file mode 100644 index 000000000..48cf96592 --- /dev/null +++ b/packages/ci/src/lib/cli/persist.unit.test.ts @@ -0,0 +1,152 @@ +import { join } from 'node:path'; +import { + type PersistedCliFiles, + findPersistedFiles, + persistCliOptions, + persistedCliFiles, +} from './persist'; + +describe('persistCliOptions', () => { + it('should create CLI arguments for standalone project', () => { + expect( + persistCliOptions({ + directory: process.cwd(), + output: '.code-pushup', + }), + ).toEqual([ + `--persist.outputDir=${join(process.cwd(), '.code-pushup')}`, + '--persist.filename=report', + '--persist.format=json', + '--persist.format=md', + ]); + }); + + it('should create CLI arguments for monorepo project', () => { + expect( + persistCliOptions({ + project: 'utils', + directory: process.cwd(), + output: '.code-pushup', + }), + ).toEqual([ + `--persist.outputDir=${join(process.cwd(), '.code-pushup')}`, + '--persist.filename=utils-report', + '--persist.format=json', + '--persist.format=md', + ]); + }); +}); + +describe('persistedCliFiles', () => { + it('should determine persisted files for standalone report', () => { + expect( + persistedCliFiles({ + directory: process.cwd(), + output: '.code-pushup', + }), + ).toEqual({ + jsonFilePath: join(process.cwd(), '.code-pushup/report.json'), + mdFilePath: join(process.cwd(), '.code-pushup/report.md'), + artifactData: { + rootDir: join(process.cwd(), '.code-pushup'), + files: [ + join(process.cwd(), '.code-pushup/report.json'), + join(process.cwd(), '.code-pushup/report.md'), + ], + }, + }); + }); + + it('should determine persisted files for monorepo report', () => { + expect( + persistedCliFiles({ + directory: process.cwd(), + output: '.code-pushup/auth', + project: 'auth', + }), + ).toEqual({ + jsonFilePath: join(process.cwd(), '.code-pushup/auth/auth-report.json'), + mdFilePath: join(process.cwd(), '.code-pushup/auth/auth-report.md'), + artifactData: { + rootDir: join(process.cwd(), '.code-pushup/auth'), + files: [ + join(process.cwd(), '.code-pushup/auth/auth-report.json'), + join(process.cwd(), '.code-pushup/auth/auth-report.md'), + ], + }, + }); + }); + + it('should determine persisted files for diff in Markdown format only', () => { + expect( + persistedCliFiles({ + directory: process.cwd(), + output: '.code-pushup', + isDiff: true, + formats: ['md'], + }), + ).toEqual>({ + mdFilePath: join(process.cwd(), '.code-pushup/report-diff.md'), + artifactData: { + rootDir: join(process.cwd(), '.code-pushup'), + files: [join(process.cwd(), '.code-pushup/report-diff.md')], + }, + }); + }); +}); + +describe('findPersistedFiles', () => { + it('should find report files in artifact data for standalone project', () => { + expect( + findPersistedFiles({ + rootDir: join(process.cwd(), '.code-pushup'), + files: [ + 'report-diff.json', + 'report-diff.md', + 'report.json', + 'report.md', + ], + }), + ).toEqual({ + jsonFilePath: join(process.cwd(), '.code-pushup/report.json'), + mdFilePath: join(process.cwd(), '.code-pushup/report.md'), + artifactData: { + rootDir: join(process.cwd(), '.code-pushup'), + files: [ + join(process.cwd(), '.code-pushup/report.json'), + join(process.cwd(), '.code-pushup/report.md'), + ], + }, + }); + }); + + it('should find report files in artifact data for monorepo project', () => { + expect( + findPersistedFiles({ + rootDir: join(process.cwd(), '.code-pushup'), + files: [ + 'backend-report-diff.json', + 'backend-report-diff.md', + 'backend-report.json', + 'backend-report.md', + 'frontend-report-diff.json', + 'frontend-report-diff.md', + 'frontend-report.json', + 'frontend-report.md', + 'report-diff.md', + ], + project: 'frontend', + }), + ).toEqual({ + jsonFilePath: join(process.cwd(), '.code-pushup/frontend-report.json'), + mdFilePath: join(process.cwd(), '.code-pushup/frontend-report.md'), + artifactData: { + rootDir: join(process.cwd(), '.code-pushup'), + files: [ + join(process.cwd(), '.code-pushup/frontend-report.json'), + join(process.cwd(), '.code-pushup/frontend-report.md'), + ], + }, + }); + }); +});