Skip to content

Commit 8c1e35c

Browse files
author
Michael Hladky
committed
refactor: wip
1 parent e8033d3 commit 8c1e35c

File tree

8 files changed

+71
-10
lines changed

8 files changed

+71
-10
lines changed

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('runAutorunExecutor', () => {
2222
});
2323

2424
afterEach(() => {
25-
parseAutorunExecutorOptionsSpy.mockReset();
25+
parseAutorunExecutorOptionsSpy.mockRestore();
2626
executeProcessSpy.mockReset();
2727
});
2828

@@ -48,11 +48,25 @@ describe('runAutorunExecutor', () => {
4848
command: 'npx',
4949
args: expect.arrayContaining(['@code-pushup/cli']),
5050
cwd: process.cwd(),
51-
env: expect.objectContaining({
52-
CP_VERBOSE: 'true',
53-
}),
5451
});
52+
});
5553

56-
expect(process.env).toHaveProperty('CP_VERBOSE', 'true');
54+
it('should forward env options to executeProcess', async () => {
55+
const output = await runAutorunExecutor(
56+
{
57+
verbose: true,
58+
env: { TEST_VALUE: '42' },
59+
},
60+
executorContext('utils'),
61+
);
62+
expect(output.success).toBe(true);
63+
expect(executeProcessSpy).toHaveBeenCalledTimes(1);
64+
expect(executeProcessSpy).toHaveBeenCalledWith(
65+
expect.objectContaining({
66+
env: expect.objectContaining({
67+
TEST_VALUE: '42',
68+
}),
69+
}),
70+
);
5771
});
5872
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export default async function runAutorunExecutor(
2222
terminalAndExecutorOptions,
2323
normalizedContext,
2424
);
25-
const { command: cliCommand } = terminalAndExecutorOptions;
2625
const {
26+
command: cliCommand,
2727
verbose = false,
2828
dryRun,
2929
env: executorEnv,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ describe('runAutorunExecutor', () => {
132132
expect(logger.warn).toHaveBeenCalledTimes(0);
133133
});
134134

135-
it('should log env var in dryRun information if verbose is set', async () => {
135+
it('should log CP_VERBOSE env var in dryRun information if verbose is set', async () => {
136136
const output = await runAutorunExecutor(
137137
{
138138
dryRun: true,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ import type {
1313
export function parseAutorunExecutorOnlyOptions(
1414
options: Partial<AutorunCommandExecutorOnlyOptions>,
1515
): AutorunCommandExecutorOnlyOptions {
16-
const { projectPrefix, dryRun, onlyPlugins } = options;
16+
const { projectPrefix, dryRun, onlyPlugins, env } = options;
1717
return {
1818
...(projectPrefix && { projectPrefix }),
1919
...(dryRun != null && { dryRun }),
2020
...(onlyPlugins && { onlyPlugins }),
21+
...(env && { env }),
2122
};
2223
}
2324

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { type MockInstance, expect, vi } from 'vitest';
2+
import { executorContext } from '@code-pushup/test-nx-utils';
23
import { osAgnosticPath } from '@code-pushup/test-utils';
34
import type { Command } from '../internal/types.js';
5+
import runAutorunExecutor from './executor';
46
import {
57
parseAutorunExecutorOnlyOptions,
68
parseAutorunExecutorOptions,
@@ -59,6 +61,12 @@ describe('parseAutorunExecutorOnlyOptions', () => {
5961
parseAutorunExecutorOnlyOptions({ onlyPlugins: ['md', 'json'] }),
6062
).toStrictEqual(expect.objectContaining({ onlyPlugins: ['md', 'json'] }));
6163
});
64+
65+
it('should log env variables options if given', async () => {
66+
expect(
67+
parseAutorunExecutorOnlyOptions({ env: { TEST_ENV_VAR: '42' } }),
68+
).toStrictEqual(expect.objectContaining({ env: { TEST_ENV_VAR: '42' } }));
69+
});
6270
});
6371

6472
describe('parseAutorunExecutorOptions', () => {
@@ -84,7 +92,7 @@ describe('parseAutorunExecutorOptions', () => {
8492
projectName,
8593
workspaceRoot: 'workspaceRoot',
8694
projectConfig: {
87-
name: 'my-app',
95+
name: projectName,
8896
root: 'root',
8997
},
9098
},
@@ -111,6 +119,28 @@ describe('parseAutorunExecutorOptions', () => {
111119
);
112120
});
113121

122+
it('should include the env options', () => {
123+
const projectName = 'my-app';
124+
const env = {
125+
NODE_OPTIONS: '--import tsx',
126+
TSX_TSCONFIG_PATH: 'tsconfig.base.json',
127+
};
128+
129+
const executorOptions = parseAutorunExecutorOptions(
130+
{ env },
131+
{
132+
projectName,
133+
workspaceRoot: 'workspaceRoot',
134+
projectConfig: {
135+
name: projectName,
136+
root: 'root',
137+
},
138+
},
139+
);
140+
141+
expect(executorOptions.env).toStrictEqual(env);
142+
});
143+
114144
it.each<Command | undefined>(['upload', 'autorun', undefined])(
115145
'should include upload config for command %s if API key is provided',
116146
command => {

packages/nx-plugin/src/executors/internal/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ export function globalConfig(
1414
): GlobalExecutorOptions {
1515
const { projectConfig } = context;
1616
const { root: projectRoot = '' } = projectConfig ?? {};
17-
const { verbose, config } = options;
17+
const { verbose, config, command } = options;
1818
return {
19+
command,
1920
verbose: !!verbose,
2021
config: config ?? path.join(projectRoot, 'code-pushup.config.ts'),
2122
};

packages/nx-plugin/src/executors/internal/config.unit.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ describe('globalConfig', () => {
6767
).toEqual(expect.objectContaining({ config: 'my.config.ts' }));
6868
});
6969

70+
it('should include the command options', () => {
71+
const { command } = globalConfig(
72+
{ command: 'collect' },
73+
{
74+
workspaceRoot: '/test/root/workspace-root',
75+
projectConfig: {
76+
name: 'my-app',
77+
root: 'packages/project-root',
78+
},
79+
},
80+
);
81+
expect(command).toBe('collect');
82+
});
83+
7084
it('should work with empty projectConfig', () => {
7185
expect(
7286
globalConfig(

packages/nx-plugin/src/executors/internal/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { ProjectConfiguration } from 'nx/src/config/workspace-json-project-
55
*/
66
export type GeneralExecutorOnlyOptions = {
77
dryRun?: boolean;
8+
env?: Record<string, string>;
89
};
910

1011
/**

0 commit comments

Comments
 (0)