Skip to content

Commit f4b8cdd

Browse files
author
John Doe
committed
Merge branch 'refactor/nx-plugin/fix-verbose-handling' into fix/nx-plugin/add-env-vars-to-executor
2 parents 893b1d3 + 986ce0e commit f4b8cdd

File tree

4 files changed

+47
-40
lines changed

4 files changed

+47
-40
lines changed

e2e/nx-plugin-e2e/tests/plugin-create-nodes.e2e.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,14 @@ describe('nx-plugin', () => {
173173
});
174174

175175
const cleanStdout = removeColorCodes(stdout);
176+
176177
// Nx command
177-
expect(cleanStdout).toContain('nx run my-lib:code-pushup');
178+
expect(cleanStdout).toContain('nx run my-lib:code-pushup --dryRun');
178179
// Run CLI executor
179-
expect(cleanStdout).toContain('Command:');
180+
expect(cleanStdout).toContain('DryRun execution of:');
180181
expect(cleanStdout).toContain('npx @code-pushup/cli');
181-
expect(cleanStdout).toContain('--verbose');
182-
expect(cleanStdout).toContain('--dryRun ');
182+
expect(cleanStdout).not.toContain('--verbose');
183+
expect(cleanStdout).toContain('CP_VERBOSE="true"');
183184
});
184185

185186
it('should consider plugin option bin in executor target', async () => {

packages/nx-plugin/src/executors/cli/executor.int.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ describe('runAutorunExecutor', () => {
4747
command: 'npx',
4848
args: expect.arrayContaining(['@code-pushup/cli']),
4949
cwd: process.cwd(),
50+
env: expect.objectContaining({
51+
CP_VERBOSE: 'true',
52+
}),
5053
});
5154
});
5255
});

packages/nx-plugin/src/executors/cli/executor.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type ExecutorContext, logger } from '@nx/devkit';
1+
import type { ExecutorContext } from '@nx/devkit';
22
import { executeProcess } from '../../internal/execute-process.js';
33
import { normalizeContext } from '../internal/context.js';
44
import type { AutorunCommandExecutorOptions } from './schema.js';
@@ -15,44 +15,49 @@ export default async function runAutorunExecutor(
1515
terminalAndExecutorOptions: AutorunCommandExecutorOptions,
1616
context: ExecutorContext,
1717
): Promise<ExecutorOutput> {
18-
const { objectToCliArgs, formatCommandStatus } = await import(
19-
'@code-pushup/utils'
20-
);
18+
const { objectToCliArgs, formatCommandStatus, logger, stringifyError } =
19+
await import('@code-pushup/utils');
2120
const normalizedContext = normalizeContext(context);
2221
const cliArgumentObject = parseAutorunExecutorOptions(
2322
terminalAndExecutorOptions,
2423
normalizedContext,
2524
);
26-
const {
27-
dryRun,
28-
verbose,
29-
command: cliCommand,
30-
bin,
31-
} = terminalAndExecutorOptions;
25+
const { command: cliCommand } = terminalAndExecutorOptions;
26+
const { dryRun, verbose, bin, ...restArgs } = cliArgumentObject;
3227
const command = bin ? `node` : 'npx';
3328
const positionals = [
3429
bin ?? '@code-pushup/cli',
3530
...(cliCommand ? [cliCommand] : []),
3631
];
37-
const args = [...positionals, ...objectToCliArgs(cliArgumentObject)];
32+
const args = [...positionals, ...objectToCliArgs(restArgs)];
33+
const executorEnvVariables = {
34+
...(verbose && { CP_VERBOSE: 'true' }),
35+
};
3836
const commandString = formatCommandStatus([command, ...args].join(' '), {
3937
cwd: context.cwd,
38+
env: executorEnvVariables,
4039
});
41-
if (verbose) {
42-
logger.info(`Run CLI executor ${command ?? ''}`);
43-
logger.info(`Command: ${commandString}`);
44-
}
40+
4541
if (dryRun) {
4642
logger.warn(`DryRun execution of: ${commandString}`);
4743
} else {
4844
try {
45+
logger.debug(`With env vars: ${executorEnvVariables}`);
4946
await executeProcess({
5047
command,
5148
args,
5249
...(context.cwd ? { cwd: context.cwd } : {}),
50+
...(verbose
51+
? {
52+
env: {
53+
...process.env,
54+
...executorEnvVariables,
55+
},
56+
}
57+
: {}),
5358
});
5459
} catch (error) {
55-
logger.error(error);
60+
logger.error(stringifyError(error));
5661
return {
5762
success: false,
5863
command: commandString,

packages/nx-plugin/src/executors/cli/executor.unit.test.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { logger } from '@nx/devkit';
21
import { afterAll, afterEach, beforeEach, expect, vi } from 'vitest';
32
import { executorContext } from '@code-pushup/test-nx-utils';
43
import { MEMFS_VOLUME } from '@code-pushup/test-utils';
@@ -9,9 +8,8 @@ describe('runAutorunExecutor', () => {
98
const processEnvCP = Object.fromEntries(
109
Object.entries(process.env).filter(([k]) => k.startsWith('CP_')),
1110
);
12-
const loggerInfoSpy = vi.spyOn(logger, 'info');
13-
const loggerWarnSpy = vi.spyOn(logger, 'warn');
1411
const executeProcessSpy = vi.spyOn(executeProcessModule, 'executeProcess');
12+
let loggerSpy: Awaited<typeof import('@code-pushup/utils')>['logger'];
1513

1614
beforeAll(() => {
1715
Object.entries(process.env)
@@ -25,7 +23,9 @@ describe('runAutorunExecutor', () => {
2523
);
2624
});
2725

28-
beforeEach(() => {
26+
beforeEach(async () => {
27+
const { logger } = await import('@code-pushup/utils');
28+
loggerSpy = logger;
2929
vi.unstubAllEnvs();
3030
executeProcessSpy.mockResolvedValue({
3131
bin: 'npx ...',
@@ -37,8 +37,6 @@ describe('runAutorunExecutor', () => {
3737
});
3838

3939
afterEach(() => {
40-
loggerWarnSpy.mockReset();
41-
loggerInfoSpy.mockReset();
4240
executeProcessSpy.mockReset();
4341
});
4442

@@ -113,30 +111,30 @@ describe('runAutorunExecutor', () => {
113111
expect(output.command).toMatch('--upload.project="CLI"');
114112
});
115113

116-
it('should log information if verbose is set', async () => {
114+
it('should set env var information if verbose is set', async () => {
117115
const output = await runAutorunExecutor(
118-
{ verbose: true },
116+
{
117+
dryRun: true, // here to produce log
118+
verbose: true,
119+
},
119120
{ ...executorContext('github-action'), cwd: '<CWD>' },
120121
);
121-
expect(executeProcessSpy).toHaveBeenCalledTimes(1);
122122

123-
expect(output.command).toMatch('--verbose');
124-
expect(loggerWarnSpy).toHaveBeenCalledTimes(0);
125-
expect(loggerInfoSpy).toHaveBeenCalledTimes(2);
126-
expect(loggerInfoSpy).toHaveBeenCalledWith(
127-
expect.stringContaining(`Run CLI executor`),
128-
);
129-
expect(loggerInfoSpy).toHaveBeenCalledWith(
130-
expect.stringContaining('Command:'),
123+
expect(executeProcessSpy).toHaveBeenCalledTimes(0);
124+
125+
expect(output.command).not.toContain('--verbose');
126+
expect(loggerSpy.warn).toHaveBeenCalledTimes(1);
127+
expect(loggerSpy.warn).toHaveBeenCalledWith(
128+
expect.stringContaining('CP_VERBOSE="true"'),
131129
);
132130
});
133131

134132
it('should log command if dryRun is set', async () => {
135133
await runAutorunExecutor({ dryRun: true }, executorContext('utils'));
136134

137-
expect(loggerInfoSpy).toHaveBeenCalledTimes(0);
138-
expect(loggerWarnSpy).toHaveBeenCalledTimes(1);
139-
expect(loggerWarnSpy).toHaveBeenCalledWith(
135+
expect(loggerSpy.command).toHaveBeenCalledTimes(0);
136+
expect(loggerSpy.warn).toHaveBeenCalledTimes(1);
137+
expect(loggerSpy.warn).toHaveBeenCalledWith(
140138
expect.stringContaining('DryRun execution of'),
141139
);
142140
});

0 commit comments

Comments
 (0)