import { exec as _exec } from 'child_process' ; import is from '@sindresorhus/is' ; import traverse from 'traverse' ; import upath from 'upath' ; import type { ExecOptions } from '../lib/util/exec/types' ; import { regEx } from '../lib/util/regex' ; type CallOptions = ExecOptions | null | undefined ; export type ExecResult = { stdout : string ; stderr : string ; } | Error ; export type ExecMock = jest . Mock < typeof _exec > ; export const exec = _exec as any ; interface ExecSnapshot { cmd : string ; options ? : ExecOptions | null | undefined ; } export type ExecSnapshots = ExecSnapshot [ ] ; export function execSnapshot ( cmd , options ? ) { const snapshot = { cmd , options , } ; const cwd = upath . toUnix ( process . cwd ( ) ) ; return traverse ( snapshot ) . map ( function fixup ( v ) { if ( is . string ( v ) ) { const val = v . replace ( regEx ( /\\(\w)/g ) , '/$1' ) . replace ( cwd , '/root/project' ) ; this . update ( val ) ; } } ) ; } const defaultExecResult = { stdout : '' , stderr : '' } ; export function mockExecAll ( execFn , execResult = defaultExecResult ) { const snapshots = [ ] ; execFn . mockImplementation ( ( cmd , options , callback ) => { snapshots . push ( execSnapshot ( cmd , options ) ) ; if ( execResult instanceof Error ) { throw execResult ; } callback ( null , execResult ) ; return undefined as never ; } ) ; return snapshots ; } export function mockExecSequence ( execFn , execResults ) { const snapshots = [ ] ; execResults . forEach ( ( execResult ) => { execFn . mockImplementationOnce ( ( cmd , options , callback ) => { snapshots . push ( execSnapshot ( cmd , options ) ) ; if ( execResult instanceof Error ) { throw execResult ; } callback ( null , execResult ) ; return undefined as never ; } ) ; } ) ; return snapshots ; } const basicEnvMock = { HTTP_PROXY : 'http://example.com' , HTTPS_PROXY : 'https://example.com' , NO_PROXY : 'localhost' , HOME : '/home/user' , PATH : '/tmp/path' , LANG : 'en_US.UTF-8' , LC_ALL : 'en_US' , } ; const fullEnvMock = { ... basicEnvMock , SELECTED_ENV_VAR : 'Can be selected' , FILTERED_ENV_VAR : 'Should be filtered' , } ; const filteredEnvMock = { ... basicEnvMock , SELECTED_ENV_VAR : fullEnvMock . SELECTED_ENV_VAR , } ; export const envMock = { basic : basicEnvMock , full : fullEnvMock , filtered : filteredEnvMock , } ;