Skip to content

Commit 9f5b640

Browse files
committed
fix(nx-plugin): remove undocument env vars parsing from executor
1 parent 29a6f08 commit 9f5b640

File tree

6 files changed

+27
-81
lines changed

6 files changed

+27
-81
lines changed
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
export const ENV = {
2-
CP_SERVER: 'https://portal.code.pushup.dev',
3-
CP_ORGANIZATION: 'code-pushup',
4-
CP_PROJECT: 'utils',
5-
CP_API_KEY: '23456789098765432345678909876543',
6-
CP_TIMEOUT: '9',
2+
CP_API_KEY: 'cp_0123456789abcdefghijklmnopqrstuvwxyz',
73
};

packages/nx-plugin/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
"@code-pushup/utils": "0.79.1",
3737
"@nx/devkit": ">=17.0.0",
3838
"ansis": "^3.3.0",
39-
"nx": ">=17.0.0",
40-
"zod": "^4.0.5"
39+
"nx": ">=17.0.0"
4140
},
4241
"files": [
4342
"src",

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ describe('runAutorunExecutor', () => {
1313
const loggerWarnSpy = vi.spyOn(logger, 'warn');
1414
const executeProcessSpy = vi.spyOn(executeProcessModule, 'executeProcess');
1515

16-
/* eslint-disable functional/immutable-data, @typescript-eslint/no-dynamic-delete */
1716
beforeAll(() => {
1817
Object.entries(process.env)
1918
.filter(([k]) => k.startsWith('CP_'))
2019
.forEach(([k]) => delete process.env[k]);
2120
});
2221

22+
afterAll(() => {
23+
Object.entries(processEnvCP).forEach(([k, v]) => (process.env[k] = v));
24+
});
25+
2326
beforeEach(() => {
2427
vi.unstubAllEnvs();
2528
executeProcessSpy.mockResolvedValue({
@@ -37,11 +40,6 @@ describe('runAutorunExecutor', () => {
3740
executeProcessSpy.mockReset();
3841
});
3942

40-
afterAll(() => {
41-
Object.entries(processEnvCP).forEach(([k, v]) => (process.env[k] = v));
42-
});
43-
/* eslint-enable functional/immutable-data, @typescript-eslint/no-dynamic-delete */
44-
4543
it('should call executeProcess with return result', async () => {
4644
const output = await runAutorunExecutor({}, executorContext('utils'));
4745
expect(output.success).toBe(true);
@@ -89,7 +87,6 @@ describe('runAutorunExecutor', () => {
8987
});
9088

9189
it('should create command from context and options if no api key is set', async () => {
92-
vi.stubEnv('CP_PROJECT', 'CLI');
9390
const output = await runAutorunExecutor(
9491
{ persist: { filename: 'REPORT', format: ['md', 'json'] } },
9592
executorContext('core'),
@@ -102,9 +99,11 @@ describe('runAutorunExecutor', () => {
10299

103100
it('should create command from context, options and arguments if api key is set', async () => {
104101
vi.stubEnv('CP_API_KEY', 'cp_1234567');
105-
vi.stubEnv('CP_PROJECT', 'CLI');
106102
const output = await runAutorunExecutor(
107-
{ persist: { filename: 'REPORT', format: ['md', 'json'] } },
103+
{
104+
persist: { filename: 'REPORT', format: ['md', 'json'] },
105+
upload: { project: 'CLI' },
106+
},
108107
executorContext('core'),
109108
);
110109
expect(output.command).toMatch('--persist.filename="REPORT"');

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,11 +367,7 @@ describe('uploadConfig', () => {
367367
),
368368
).toEqual(
369369
expect.objectContaining({
370-
server: ENV.CP_SERVER,
371370
apiKey: ENV.CP_API_KEY,
372-
organization: ENV.CP_ORGANIZATION,
373-
project: ENV.CP_PROJECT,
374-
timeout: Number(ENV.CP_TIMEOUT),
375371
}),
376372
);
377373
});
Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,9 @@
1-
import { z } from 'zod';
21
import type { UploadConfig } from '@code-pushup/models';
32

4-
// load upload configuration from environment
5-
const envSchema = z
6-
.object({
7-
CP_SERVER: z.string().url().optional(),
8-
CP_API_KEY: z.string().min(1).optional(),
9-
CP_ORGANIZATION: z.string().min(1).optional(),
10-
CP_PROJECT: z.string().min(1).optional(),
11-
CP_TIMEOUT: z.string().regex(/^\d+$/).optional(),
12-
})
13-
.partial();
14-
15-
type UploadEnvVars = z.infer<typeof envSchema>;
16-
17-
export function parseEnv(env: unknown = {}): Partial<UploadConfig> {
18-
const upload: UploadEnvVars = envSchema.parse(env);
19-
return Object.fromEntries(
20-
Object.entries(upload).map(([envKey, value]) => {
21-
switch (envKey) {
22-
case 'CP_SERVER':
23-
return ['server', value];
24-
case 'CP_API_KEY':
25-
return ['apiKey', value];
26-
case 'CP_ORGANIZATION':
27-
return ['organization', value];
28-
case 'CP_PROJECT':
29-
return ['project', value];
30-
case 'CP_TIMEOUT':
31-
return Number(value) >= 0 ? ['timeout', Number(value)] : [];
32-
default:
33-
return [];
34-
}
3+
export function parseEnv(env: Partial<Record<string, string>>) {
4+
return {
5+
...(env.CP_API_KEY && {
6+
apiKey: env.CP_API_KEY,
357
}),
36-
);
8+
} satisfies Partial<UploadConfig>;
379
}

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

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,23 @@ import { describe, expect } from 'vitest';
22
import { parseEnv } from './env.js';
33

44
describe('parseEnv', () => {
5-
it('should parse empty env vars', () => {
6-
expect(parseEnv({})).toEqual({});
5+
it('should parse CP_API_KEY environment variable', () => {
6+
expect(parseEnv({ CP_API_KEY: 'cp_0123456789' })).toStrictEqual({
7+
apiKey: 'cp_0123456789',
8+
});
79
});
810

9-
it('should parse process.env.CP_SERVER option', () => {
10-
expect(parseEnv({ CP_SERVER: 'https://portal.code.pushup.dev' })).toEqual(
11-
expect.objectContaining({ server: 'https://portal.code.pushup.dev' }),
12-
);
11+
it('should not parse API key if missing CP_API_KEY in environment', () => {
12+
expect(parseEnv({})).toStrictEqual({});
1313
});
1414

15-
it('should parse process.env.CP_ORGANIZATION option', () => {
16-
expect(parseEnv({ CP_ORGANIZATION: 'code-pushup' })).toEqual(
17-
expect.objectContaining({ organization: 'code-pushup' }),
18-
);
15+
it('should ignore CP_API_KEY if empty string', () => {
16+
expect(parseEnv({ CP_API_KEY: '' })).toStrictEqual({});
1917
});
2018

21-
it('should parse process.env.CP_PROJECT option', () => {
22-
expect(parseEnv({ CP_PROJECT: 'cli-utils' })).toEqual(
23-
expect.objectContaining({ project: 'cli-utils' }),
24-
);
25-
});
26-
27-
it('should parse process.env.CP_TIMEOUT option', () => {
28-
expect(parseEnv({ CP_TIMEOUT: '3' })).toEqual(
29-
expect.objectContaining({ timeout: 3 }),
30-
);
31-
});
32-
33-
it('should throw for process.env.CP_TIMEOUT option < 0', () => {
34-
expect(() => parseEnv({ CP_TIMEOUT: '-1' })).toThrow('Invalid string');
35-
});
36-
37-
it('should throw for invalid URL in process.env.CP_SERVER option', () => {
38-
expect(() => parseEnv({ CP_SERVER: 'httptpt' })).toThrow('Invalid URL');
19+
it('should ignore other environment variables', () => {
20+
expect(
21+
parseEnv({ CP_SERVER: 'https://api.code-pushup.example.com/graphql' }),
22+
).toStrictEqual({});
3923
});
4024
});

0 commit comments

Comments
 (0)