11import { logger } from '@nx/devkit' ;
2- import { execSync } from 'node:child_process' ;
32import { afterAll , afterEach , beforeEach , expect , vi } from 'vitest' ;
43import { executorContext } from '@code-pushup/test-nx-utils' ;
54import { MEMFS_VOLUME } from '@code-pushup/test-utils' ;
5+ import * as executeProcessModule from '../../internal/execute-process.js' ;
66import runAutorunExecutor from './executor.js' ;
77
8- vi . mock ( 'node:child_process' , async ( ) => {
9- const actual = await vi . importActual ( 'node:child_process' ) ;
10-
11- return {
12- ...actual ,
13- execSync : vi . fn ( ( command : string ) => {
14- if ( command . includes ( 'THROW_ERROR' ) ) {
15- throw new Error ( command ) ;
16- }
17- } ) ,
18- } ;
19- } ) ;
20-
218describe ( 'runAutorunExecutor' , ( ) => {
229 const processEnvCP = Object . fromEntries (
2310 Object . entries ( process . env ) . filter ( ( [ k ] ) => k . startsWith ( 'CP_' ) ) ,
2411 ) ;
2512 const loggerInfoSpy = vi . spyOn ( logger , 'info' ) ;
2613 const loggerWarnSpy = vi . spyOn ( logger , 'warn' ) ;
14+ const executeProcessSpy = vi . spyOn ( executeProcessModule , 'executeProcess' ) ;
2715
2816 /* eslint-disable functional/immutable-data, @typescript-eslint/no-dynamic-delete */
2917 beforeAll ( ( ) => {
@@ -34,27 +22,39 @@ describe('runAutorunExecutor', () => {
3422
3523 beforeEach ( ( ) => {
3624 vi . unstubAllEnvs ( ) ;
25+ executeProcessSpy . mockResolvedValue ( {
26+ code : 0 ,
27+ stdout : '' ,
28+ stderr : '' ,
29+ date : new Date ( ) . toISOString ( ) ,
30+ duration : 100 ,
31+ } ) ;
3732 } ) ;
3833
3934 afterEach ( ( ) => {
4035 loggerWarnSpy . mockReset ( ) ;
4136 loggerInfoSpy . mockReset ( ) ;
37+ executeProcessSpy . mockReset ( ) ;
4238 } ) ;
4339
4440 afterAll ( ( ) => {
4541 Object . entries ( processEnvCP ) . forEach ( ( [ k , v ] ) => ( process . env [ k ] = v ) ) ;
4642 } ) ;
4743 /* eslint-enable functional/immutable-data, @typescript-eslint/no-dynamic-delete */
4844
49- it ( 'should call execSync with return result' , async ( ) => {
45+ it ( 'should call executeProcess with return result' , async ( ) => {
5046 const output = await runAutorunExecutor ( { } , executorContext ( 'utils' ) ) ;
5147 expect ( output . success ) . toBe ( true ) ;
5248 expect ( output . command ) . toMatch ( 'npx @code-pushup/cli' ) ;
53- // eslint-disable-next-line n/no-sync
54- expect ( execSync ) . toHaveBeenCalledWith (
55- expect . stringContaining ( 'npx @code-pushup/cli' ) ,
56- { cwd : MEMFS_VOLUME } ,
57- ) ;
49+ expect ( executeProcessSpy ) . toHaveBeenCalledWith ( {
50+ command : 'npx' ,
51+ args : expect . arrayContaining ( [ '@code-pushup/cli' ] ) ,
52+ cwd : MEMFS_VOLUME ,
53+ observer : {
54+ onError : expect . any ( Function ) ,
55+ onStdout : expect . any ( Function ) ,
56+ } ,
57+ } ) ;
5858 } ) ;
5959
6060 it ( 'should normalize context' , async ( ) => {
@@ -67,9 +67,14 @@ describe('runAutorunExecutor', () => {
6767 ) ;
6868 expect ( output . success ) . toBe ( true ) ;
6969 expect ( output . command ) . toMatch ( 'utils' ) ;
70- // eslint-disable-next-line n/no-sync
71- expect ( execSync ) . toHaveBeenCalledWith ( expect . stringContaining ( 'utils' ) , {
70+ expect ( executeProcessSpy ) . toHaveBeenCalledWith ( {
71+ command : 'npx' ,
72+ args : expect . arrayContaining ( [ '@code-pushup/cli' ] ) ,
7273 cwd : 'cwd-form-context' ,
74+ observer : {
75+ onError : expect . any ( Function ) ,
76+ onStdout : expect . any ( Function ) ,
77+ } ,
7378 } ) ;
7479 } ) ;
7580
@@ -114,8 +119,7 @@ describe('runAutorunExecutor', () => {
114119 { verbose : true } ,
115120 { ...executorContext ( 'github-action' ) , cwd : '<CWD>' } ,
116121 ) ;
117- // eslint-disable-next-line n/no-sync
118- expect ( execSync ) . toHaveBeenCalledTimes ( 1 ) ;
122+ expect ( executeProcessSpy ) . toHaveBeenCalledTimes ( 1 ) ;
119123
120124 expect ( output . command ) . toMatch ( '--verbose' ) ;
121125 expect ( loggerWarnSpy ) . toHaveBeenCalledTimes ( 0 ) ;
0 commit comments