Skip to content

Commit 3bafc89

Browse files
BioPhotonJohn Doematejchalk
authored
refactor(nx-plugin): set env vars if verbose is given (#1159)
This PR transforms the terminal args `--verbose` into a env var `CP_VERBOSE=true` --------- Co-authored-by: John Doe <john.doe@example.com> Co-authored-by: Matěj Chalk <34691111+matejchalk@users.noreply.github.com>
1 parent d1a115b commit 3bafc89

File tree

5 files changed

+65
-38
lines changed

5 files changed

+65
-38
lines changed

e2e/nx-plugin-e2e/tests/executor-cli.e2e.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ describe('executor command', () => {
192192
`${project}:code-pushup`,
193193
'collect',
194194
'--persist.filename=terminal-report',
195+
'--verbose',
195196
],
196197
cwd,
197198
});

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
@@ -27,6 +27,7 @@ describe('runAutorunExecutor', () => {
2727
});
2828

2929
it('should normalize context, parse CLI options and execute command', async () => {
30+
expect(process.env).not.toHaveProperty('CP_VERBOSE', 'true');
3031
const output = await runAutorunExecutor(
3132
{ verbose: true },
3233
executorContext('utils'),
@@ -48,5 +49,7 @@ describe('runAutorunExecutor', () => {
4849
args: expect.arrayContaining(['@code-pushup/cli']),
4950
cwd: process.cwd(),
5051
});
52+
53+
expect(process.env).toHaveProperty('CP_VERBOSE', 'true');
5154
});
5255
});

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

Lines changed: 15 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,43 @@ 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 { verbose = false, dryRun, bin, ...restArgs } = cliArgumentObject;
27+
logger.setVerbose(verbose);
28+
3229
const command = bin ? `node` : 'npx';
3330
const positionals = [
3431
bin ?? '@code-pushup/cli',
3532
...(cliCommand ? [cliCommand] : []),
3633
];
37-
const args = [...positionals, ...objectToCliArgs(cliArgumentObject)];
34+
const args = [...positionals, ...objectToCliArgs(restArgs)];
35+
const executorEnvVariables = {
36+
...(verbose && { CP_VERBOSE: 'true' }),
37+
};
3838
const commandString = formatCommandStatus([command, ...args].join(' '), {
3939
cwd: context.cwd,
40+
env: executorEnvVariables,
4041
});
41-
if (verbose) {
42-
logger.info(`Run CLI executor ${command ?? ''}`);
43-
logger.info(`Command: ${commandString}`);
44-
}
42+
4543
if (dryRun) {
4644
logger.warn(`DryRun execution of: ${commandString}`);
4745
} else {
4846
try {
47+
logger.debug(`With env vars: ${executorEnvVariables}`);
4948
await executeProcess({
5049
command,
5150
args,
5251
...(context.cwd ? { cwd: context.cwd } : {}),
5352
});
5453
} catch (error) {
55-
logger.error(error);
54+
logger.error(stringifyError(error));
5655
return {
5756
success: false,
5857
command: commandString,

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

Lines changed: 41 additions & 18 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 logger: 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 utils = await import('@code-pushup/utils');
28+
logger = utils.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

@@ -108,30 +106,55 @@ describe('runAutorunExecutor', () => {
108106
expect(output.command).toMatch('--upload.project="CLI"');
109107
});
110108

111-
it('should log information if verbose is set', async () => {
109+
it('should set env var information if verbose is set', async () => {
112110
const output = await runAutorunExecutor(
113-
{ verbose: true },
111+
{
112+
verbose: true,
113+
},
114114
{ ...executorContext('github-action'), cwd: '<CWD>' },
115115
);
116+
116117
expect(executeProcessSpy).toHaveBeenCalledTimes(1);
118+
expect(executeProcessSpy).toHaveBeenCalledWith({
119+
command: 'npx',
120+
args: expect.arrayContaining(['@code-pushup/cli']),
121+
cwd: '<CWD>',
122+
});
117123

118-
expect(output.command).toMatch('--verbose');
119-
expect(loggerWarnSpy).toHaveBeenCalledTimes(0);
120-
expect(loggerInfoSpy).toHaveBeenCalledTimes(2);
121-
expect(loggerInfoSpy).toHaveBeenCalledWith(
122-
expect.stringContaining(`Run CLI executor`),
124+
expect(process.env).toStrictEqual(
125+
expect.objectContaining({
126+
CP_VERBOSE: 'true',
127+
}),
123128
);
124-
expect(loggerInfoSpy).toHaveBeenCalledWith(
125-
expect.stringContaining('Command:'),
129+
130+
expect(output.command).not.toContain('--verbose');
131+
expect(logger.warn).toHaveBeenCalledTimes(0);
132+
});
133+
134+
it('should log env var in dryRun information if verbose is set', async () => {
135+
const output = await runAutorunExecutor(
136+
{
137+
dryRun: true,
138+
verbose: true,
139+
},
140+
{ ...executorContext('github-action'), cwd: '<CWD>' },
141+
);
142+
143+
expect(executeProcessSpy).toHaveBeenCalledTimes(0);
144+
145+
expect(output.command).not.toContain('--verbose');
146+
expect(logger.warn).toHaveBeenCalledTimes(1);
147+
expect(logger.warn).toHaveBeenCalledWith(
148+
expect.stringContaining('CP_VERBOSE="true"'),
126149
);
127150
});
128151

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

132-
expect(loggerInfoSpy).toHaveBeenCalledTimes(0);
133-
expect(loggerWarnSpy).toHaveBeenCalledTimes(1);
134-
expect(loggerWarnSpy).toHaveBeenCalledWith(
155+
expect(logger.command).toHaveBeenCalledTimes(0);
156+
expect(logger.warn).toHaveBeenCalledTimes(1);
157+
expect(logger.warn).toHaveBeenCalledWith(
135158
expect.stringContaining('DryRun execution of'),
136159
);
137160
});

0 commit comments

Comments
 (0)