Skip to content

Commit

Permalink
test(ci): add missing unit tests for CLI helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
matejchalk committed Nov 25, 2024
1 parent db3fcce commit e735bf4
Show file tree
Hide file tree
Showing 2 changed files with 253 additions and 0 deletions.
101 changes: 101 additions & 0 deletions packages/ci/src/lib/cli/context.unit.test.ts
Original file line number Diff line number Diff line change
@@ -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<CommandContext>({
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<CommandContext>({
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<Partial<CommandContext>>({
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<Partial<CommandContext>>({
output: '.code-pushup/',
}),
);
});
});
152 changes: 152 additions & 0 deletions packages/ci/src/lib/cli/persist.unit.test.ts
Original file line number Diff line number Diff line change
@@ -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<PersistedCliFiles>({
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<PersistedCliFiles>({
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<PersistedCliFiles<'md'>>({
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<PersistedCliFiles>({
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<PersistedCliFiles>({
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'),
],
},
});
});
});

0 comments on commit e735bf4

Please sign in to comment.