| 49 | * @returns Mock executor function |
| 50 | */ |
| 51 | export function createMockExecutor( |
| 52 | result: |
| 53 | | { |
| 54 | success?: boolean; |
| 55 | output?: string; |
| 56 | error?: string; |
| 57 | process?: unknown; |
| 58 | exitCode?: number; |
| 59 | shouldThrow?: Error; |
| 60 | onExecute?: ( |
| 61 | command: string[], |
| 62 | logPrefix?: string, |
| 63 | useShell?: boolean, |
| 64 | opts?: CommandExecOptions, |
| 65 | detached?: boolean, |
| 66 | ) => void; |
| 67 | } |
| 68 | | Error |
| 69 | | string, |
| 70 | ): CommandExecutor { |
| 71 | // If result is Error or string, return executor that rejects |
| 72 | if (result instanceof Error || typeof result === 'string') { |
| 73 | return async () => { |
| 74 | throw result; |
| 75 | }; |
| 76 | } |
| 77 | |
| 78 | // If shouldThrow is specified, return executor that rejects with that error |
| 79 | if (result.shouldThrow) { |
| 80 | return async () => { |
| 81 | throw result.shouldThrow; |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | const mockProcess = { |
| 86 | pid: 12345, |
| 87 | stdout: null, |
| 88 | stderr: null, |
| 89 | stdin: null, |
| 90 | stdio: [null, null, null], |
| 91 | killed: false, |
| 92 | connected: false, |
| 93 | exitCode: result.exitCode ?? (result.success === false ? 1 : 0), |
| 94 | signalCode: null, |
| 95 | spawnargs: [], |
| 96 | spawnfile: 'sh', |
| 97 | } as unknown as ChildProcess; |
| 98 | |
| 99 | return async (command, logPrefix, useShell, opts, detached) => { |
| 100 | // Call onExecute callback if provided |
| 101 | if (result.onExecute) { |
| 102 | result.onExecute(command, logPrefix, useShell, opts, detached); |
| 103 | } |
| 104 | |
| 105 | return { |
| 106 | success: result.success ?? true, |
| 107 | output: result.output ?? '', |
| 108 | error: result.error, |