11import { logger } from '@nx/devkit' ;
22import { execSync } from 'node:child_process' ;
3- import { afterEach , expect , vi } from 'vitest' ;
3+ import { afterAll , afterEach , beforeEach , expect , vi } from 'vitest' ;
44import { executorContext } from '@code-pushup/test-nx-utils' ;
55import { MEMFS_VOLUME } from '@code-pushup/test-utils' ;
66import runAutorunExecutor from './executor.js' ;
@@ -19,14 +19,33 @@ vi.mock('node:child_process', async () => {
1919} ) ;
2020
2121describe ( 'runAutorunExecutor' , ( ) => {
22+ const processEnvCP = Object . fromEntries (
23+ Object . entries ( process . env ) . filter ( ( [ k ] ) => k . startsWith ( 'CP_' ) ) ,
24+ ) ;
2225 const loggerInfoSpy = vi . spyOn ( logger , 'info' ) ;
2326 const loggerWarnSpy = vi . spyOn ( logger , 'warn' ) ;
2427
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+
2539 afterEach ( ( ) => {
2640 loggerWarnSpy . mockReset ( ) ;
2741 loggerInfoSpy . mockReset ( ) ;
2842 } ) ;
2943
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+
3049 it ( 'should call execSync with return result' , async ( ) => {
3150 const output = await runAutorunExecutor ( { } , executorContext ( 'utils' ) ) ;
3251 expect ( output . success ) . toBe ( true ) ;
@@ -63,7 +82,20 @@ describe('runAutorunExecutor', () => {
6382 expect ( output . command ) . toMatch ( '--persist.filename="REPORT"' ) ;
6483 } ) ;
6584
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' ) ;
6799 vi . stubEnv ( 'CP_PROJECT' , 'CLI' ) ;
68100 const output = await runAutorunExecutor (
69101 { persist : { filename : 'REPORT' , format : [ 'md' , 'json' ] } } ,
@@ -73,6 +105,7 @@ describe('runAutorunExecutor', () => {
73105 expect ( output . command ) . toMatch (
74106 '--persist.format="md" --persist.format="json"' ,
75107 ) ;
108+ expect ( output . command ) . toMatch ( '--upload.apiKey="cp_1234567"' ) ;
76109 expect ( output . command ) . toMatch ( '--upload.project="CLI"' ) ;
77110 } ) ;
78111
0 commit comments