Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions e2e/nx-plugin-e2e/tests/executor-cli.e2e.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type Tree, updateProjectConfiguration } from '@nx/devkit';
import path from 'node:path';
import { readProjectConfiguration } from 'nx/src/generators/utils/project-configuration';
import { afterAll, afterEach, beforeEach, expect, vi } from 'vitest';
import { afterAll, afterEach, beforeEach, expect } from 'vitest';
import {
type AutorunCommandExecutorOptions,
generateCodePushupConfig,
Expand All @@ -17,7 +17,7 @@ import {
removeColorCodes,
teardownTestFolder,
} from '@code-pushup/test-utils';
import { executeProcess, readJsonFile } from '@code-pushup/utils';
import { executeProcess, logger, readJsonFile } from '@code-pushup/utils';
import { INLINE_PLUGIN } from '../mocks/inline-plugin.js';

async function addTargetToWorkspace(
Expand Down Expand Up @@ -62,15 +62,16 @@ describe('executor command', () => {
Object.entries(process.env).filter(([k]) => k.startsWith('CP_')),
);

/* eslint-disable functional/immutable-data, @typescript-eslint/no-dynamic-delete */
beforeAll(() => {
Object.entries(process.env)
.filter(([k]) => k.startsWith('CP_'))
.forEach(([k]) => delete process.env[k]);
.forEach(([k]) => {
// eslint-disable-next-line functional/immutable-data, @typescript-eslint/no-dynamic-delete
delete process.env[k];
});
});

beforeEach(async () => {
vi.unstubAllEnvs();
tree = await generateWorkspaceAndProject(project);
});

Expand All @@ -79,9 +80,11 @@ describe('executor command', () => {
});

afterAll(() => {
Object.entries(processEnvCP).forEach(([k, v]) => (process.env[k] = v));
Object.entries(processEnvCP).forEach(([k, v]) => {
// eslint-disable-next-line functional/immutable-data
process.env[k] = v;
});
});
/* eslint-enable functional/immutable-data, @typescript-eslint/no-dynamic-delete */

it('should execute no specific command by default', async () => {
const cwd = path.join(testFileDir, 'execute-default-command');
Expand Down Expand Up @@ -179,6 +182,7 @@ describe('executor command', () => {
},
},
);
logger.setVerbose(true);

const { stdout, code } = await executeProcess({
command: 'npx',
Expand Down
1 change: 0 additions & 1 deletion packages/ci/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ Optionally, you can override default options for further customization:
| `nxProjectsFilter` | `string \| string[]` | `'--with-target={task}'` | Arguments passed to [`nx show projects`](https://nx.dev/nx-api/nx/documents/show#projects), only relevant for Nx in [monorepo mode](#monorepo-mode) [^2] |
| `directory` | `string` | `process.cwd()` | Directory in which Code PushUp CLI should run |
| `config` | `string \| null` | `null` [^1] | Path to config file (`--config` option) |
| `silent` | `boolean` | `false` | Hides logs from CLI commands (errors will be printed) |
| `bin` | `string` | `'npx --no-install code-pushup'` | Command for executing Code PushUp CLI |
| `detectNewIssues` | `boolean` | `true` | Toggles if new issues should be detected and returned in `newIssues` property |
| `skipComment` | `boolean` | `false` | Toggles if comparison comment is posted to PR |
Expand Down
3 changes: 1 addition & 2 deletions packages/ci/src/lib/cli/commands/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { executeProcess } from '@code-pushup/utils';
import type { CommandContext } from '../context.js';

export async function runCollect(
{ bin, config, directory, observer }: CommandContext,
{ bin, config, directory }: CommandContext,
{ hasFormats }: { hasFormats: boolean },
): Promise<void> {
await executeProcess({
Expand All @@ -15,6 +15,5 @@ export async function runCollect(
: DEFAULT_PERSIST_FORMAT.map(format => `--persist.format=${format}`)),
],
cwd: directory,
observer,
});
}
3 changes: 1 addition & 2 deletions packages/ci/src/lib/cli/commands/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { executeProcess } from '@code-pushup/utils';
import type { CommandContext } from '../context.js';

export async function runCompare(
{ bin, config, directory, observer }: CommandContext,
{ bin, config, directory }: CommandContext,
{ hasFormats }: { hasFormats: boolean },
): Promise<void> {
await executeProcess({
Expand All @@ -16,6 +16,5 @@ export async function runCompare(
: DEFAULT_PERSIST_FORMAT.map(format => `--persist.format=${format}`)),
],
cwd: directory,
observer,
});
}
3 changes: 1 addition & 2 deletions packages/ci/src/lib/cli/commands/merge-diffs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { CommandContext } from '../context.js';

export async function runMergeDiffs(
files: string[],
{ bin, config, directory, observer }: CommandContext,
{ bin, config, directory }: CommandContext,
): Promise<string> {
const outputDir = path.join(directory, DEFAULT_PERSIST_OUTPUT_DIR);
const filename = `merged-${DEFAULT_PERSIST_FILENAME}`;
Expand All @@ -23,7 +23,6 @@ export async function runMergeDiffs(
`--persist.filename=${filename}`,
],
cwd: directory,
observer,
});

return path.join(outputDir, `${filename}-diff.md`);
Expand Down
2 changes: 0 additions & 2 deletions packages/ci/src/lib/cli/commands/print-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
config,
directory,
project,
observer,
}: CommandContext): Promise<unknown> {
// unique file name per project so command can be run in parallel
const outputFile = ['code-pushup', 'config', project, 'json']
Expand All @@ -28,12 +27,11 @@
await executeProcess({
command: bin,
args: [
...(config ? [`--config=${config}`] : []),

Check warning

Code scanning / CodeQL

Unsafe shell command constructed from library input Medium

This shell argument which depends on
library input
is later used in a
shell command
.
'print-config',
`--output=${outputPath}`,

Check warning

Code scanning / CodeQL

Unsafe shell command constructed from library input Medium

This shell argument which depends on
library input
is later used in a
shell command
.
],
cwd: directory,
observer,
});

try {
Expand Down
6 changes: 1 addition & 5 deletions packages/ci/src/lib/cli/context.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import type { ProcessObserver } from '@code-pushup/utils';
import { createExecutionObserver } from '../create-execution-observer.js';
import type { Settings } from '../models.js';
import type { ProjectConfig } from '../monorepo/index.js';

export type CommandContext = Pick<Settings, 'bin' | 'config' | 'directory'> & {
project?: string;
observer?: ProcessObserver;
};

export function createCommandContext(
{ config, bin, directory, silent }: Settings,
{ config, bin, directory }: Settings,
project: ProjectConfig | null | undefined,
): CommandContext {
return {
bin: project?.bin ?? bin,
directory: project?.directory ?? directory,
config,
...(project?.name && { project: project.name }),
observer: createExecutionObserver({ silent }),
};
}
15 changes: 2 additions & 13 deletions packages/ci/src/lib/cli/context.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,29 @@ import { expect } from 'vitest';
import { type CommandContext, createCommandContext } from './context.js';

describe('createCommandContext', () => {
const expectedObserver = expect.objectContaining({
onStderr: expect.any(Function),
onStdout: expect.any(Function),
});

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,
parallel: false,
nxProjectsFilter: '--with-target={task}',
projects: null,
silent: false,
task: 'code-pushup',
skipComment: false,
configPatterns: null,
searchCommits: false,
},
null,
),
).toStrictEqual<CommandContext>({
bin: 'npx --no-install code-pushup',
directory: '/test',
config: null,
observer: expectedObserver,
});
});

Expand All @@ -42,18 +34,16 @@ describe('createCommandContext', () => {
{
bin: 'npx --no-install code-pushup',
config: null,
debug: false,
detectNewIssues: true,
directory: '/test',
logger: console,
monorepo: false,
parallel: false,
nxProjectsFilter: '--with-target={task}',
projects: null,
silent: false,
task: 'code-pushup',
skipComment: false,
configPatterns: null,
searchCommits: false,
},
{
name: 'ui',
Expand All @@ -66,7 +56,6 @@ describe('createCommandContext', () => {
directory: '/test/ui',
config: null,
project: 'ui',
observer: expectedObserver,
});
});
});
41 changes: 0 additions & 41 deletions packages/ci/src/lib/create-execution-observer.int.test.ts

This file was deleted.

20 changes: 0 additions & 20 deletions packages/ci/src/lib/create-execution-observer.ts

This file was deleted.

33 changes: 0 additions & 33 deletions packages/ci/src/lib/create-execution-observer.unit.test.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/ci/src/lib/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ export type Options = {
bin?: string;
config?: string | null;
directory?: string;
silent?: boolean;
debug?: boolean;
detectNewIssues?: boolean;
skipComment?: boolean;
configPatterns?: ConfigPatterns | null;
Expand Down
4 changes: 1 addition & 3 deletions packages/ci/src/lib/monorepo/handlers/nx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ export const nxHandler: MonorepoToolHandler = {
command: 'npx',
args: ['nx', 'report'],
cwd: options.cwd,
observer: options.observer,
ignoreExitCode: true,
})
).code === 0
);
},

async listProjects({ cwd, task, nxProjectsFilter, observer }) {
async listProjects({ cwd, task, nxProjectsFilter }) {
const { stdout } = await executeProcess({
command: 'npx',
args: [
Expand All @@ -37,7 +36,6 @@ export const nxHandler: MonorepoToolHandler = {
'--json',
],
cwd,
observer,
});
const projects = parseProjects(stdout);
return projects.toSorted().map(project => ({
Expand Down
3 changes: 0 additions & 3 deletions packages/ci/src/lib/monorepo/list-projects.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { glob } from 'glob';
import path from 'node:path';
import { logger } from '@code-pushup/utils';
import { createExecutionObserver } from '../create-execution-observer.js';
import type { Settings } from '../models.js';
import { detectMonorepoTool } from './detect-tool.js';
import { getToolHandler } from './handlers/index.js';
Expand Down Expand Up @@ -90,14 +89,12 @@ function createMonorepoHandlerOptions({
directory,
parallel,
nxProjectsFilter,
silent,
}: Settings): MonorepoHandlerOptions {
return {
task,
cwd: directory,
parallel,
nxProjectsFilter,
observer: createExecutionObserver({ silent }),
};
}

Expand Down
1 change: 0 additions & 1 deletion packages/ci/src/lib/monorepo/list-projects.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ describe('listMonorepoProjects', () => {
command: 'npx',
args: ['nx', 'show', 'projects', '--with-target=code-pushup', '--json'],
cwd: process.cwd(),
observer: expect.any(Object),
});
});

Expand Down
3 changes: 0 additions & 3 deletions packages/ci/src/lib/monorepo/tools.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { ProcessObserver } from '@code-pushup/utils';

export const MONOREPO_TOOLS = ['nx', 'turbo', 'yarn', 'pnpm', 'npm'] as const;
export type MonorepoTool = (typeof MONOREPO_TOOLS)[number];

Expand All @@ -17,7 +15,6 @@ export type MonorepoHandlerOptions = {
task: string;
cwd: string;
parallel: boolean | number;
observer?: ProcessObserver;
nxProjectsFilter: string | string[];
};

Expand Down
Loading
Loading