1
1
import { logger } from '@nx/devkit' ;
2
2
import { execSync } from 'node:child_process' ;
3
- import { afterEach , expect , vi } from 'vitest' ;
3
+ import { afterAll , afterEach , beforeEach , expect , vi } from 'vitest' ;
4
4
import { executorContext } from '@code-pushup/test-nx-utils' ;
5
5
import { MEMFS_VOLUME } from '@code-pushup/test-utils' ;
6
6
import runAutorunExecutor from './executor.js' ;
@@ -19,14 +19,33 @@ vi.mock('node:child_process', async () => {
19
19
} ) ;
20
20
21
21
describe ( 'runAutorunExecutor' , ( ) => {
22
+ const processEnvCP = Object . fromEntries (
23
+ Object . entries ( process . env ) . filter ( ( [ k ] ) => k . startsWith ( 'CP_' ) ) ,
24
+ ) ;
22
25
const loggerInfoSpy = vi . spyOn ( logger , 'info' ) ;
23
26
const loggerWarnSpy = vi . spyOn ( logger , 'warn' ) ;
24
27
28
+ /* eslint-disable functional/immutable-data, @typescript-eslint/no-dynamic-delete */
29
+ beforeAll ( ( ) => {
30
+ Object . entries ( process . env )
31
+ . filter ( ( [ k ] ) => k . startsWith ( 'CP_' ) )
32
+ . forEach ( ( [ k ] ) => delete process . env [ k ] ) ;
33
+ } ) ;
34
+
35
+ beforeEach ( ( ) => {
36
+ vi . unstubAllEnvs ( ) ;
37
+ } ) ;
38
+
25
39
afterEach ( ( ) => {
26
40
loggerWarnSpy . mockReset ( ) ;
27
41
loggerInfoSpy . mockReset ( ) ;
28
42
} ) ;
29
43
44
+ afterAll ( ( ) => {
45
+ Object . entries ( processEnvCP ) . forEach ( ( [ k , v ] ) => ( process . env [ k ] = v ) ) ;
46
+ } ) ;
47
+ /* eslint-enable functional/immutable-data, @typescript-eslint/no-dynamic-delete */
48
+
30
49
it ( 'should call execSync with return result' , async ( ) => {
31
50
const output = await runAutorunExecutor ( { } , executorContext ( 'utils' ) ) ;
32
51
expect ( output . success ) . toBe ( true ) ;
@@ -63,7 +82,20 @@ describe('runAutorunExecutor', () => {
63
82
expect ( output . command ) . toMatch ( '--persist.filename="REPORT"' ) ;
64
83
} ) ;
65
84
66
- it ( 'should create command from context, options and arguments' , async ( ) => {
85
+ it ( 'should create command from context and options if no api key is set' , async ( ) => {
86
+ vi . stubEnv ( 'CP_PROJECT' , 'CLI' ) ;
87
+ const output = await runAutorunExecutor (
88
+ { persist : { filename : 'REPORT' , format : [ 'md' , 'json' ] } } ,
89
+ executorContext ( 'core' ) ,
90
+ ) ;
91
+ expect ( output . command ) . toMatch ( '--persist.filename="REPORT"' ) ;
92
+ expect ( output . command ) . toMatch (
93
+ '--persist.format="md" --persist.format="json"' ,
94
+ ) ;
95
+ } ) ;
96
+
97
+ it ( 'should create command from context, options and arguments if api key is set' , async ( ) => {
98
+ vi . stubEnv ( 'CP_API_KEY' , 'cp_1234567' ) ;
67
99
vi . stubEnv ( 'CP_PROJECT' , 'CLI' ) ;
68
100
const output = await runAutorunExecutor (
69
101
{ persist : { filename : 'REPORT' , format : [ 'md' , 'json' ] } } ,
@@ -73,6 +105,7 @@ describe('runAutorunExecutor', () => {
73
105
expect ( output . command ) . toMatch (
74
106
'--persist.format="md" --persist.format="json"' ,
75
107
) ;
108
+ expect ( output . command ) . toMatch ( '--upload.apiKey="cp_1234567"' ) ;
76
109
expect ( output . command ) . toMatch ( '--upload.project="CLI"' ) ;
77
110
} ) ;
78
111
0 commit comments