|
| 1 | +import { type ChildProcess, spawn } from 'node:child_process' |
| 2 | +import { EventEmitter } from 'node:events' |
| 3 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 4 | + |
| 5 | +vi.mock('node:child_process', () => ({ |
| 6 | + spawn: vi.fn(), |
| 7 | +})) |
| 8 | + |
| 9 | +const { exec, execFile } = await import('../../operations/exec.js') |
| 10 | + |
| 11 | +type StderrEmitter = EventEmitter & { on: (event: string, cb: (data: Buffer) => void) => void } |
| 12 | + |
| 13 | +function createMockChild(): ChildProcess { |
| 14 | + const child = new EventEmitter() as ChildProcess |
| 15 | + child.stderr = new EventEmitter() as StderrEmitter |
| 16 | + return child |
| 17 | +} |
| 18 | + |
| 19 | +function mockSpawnWith(child: ChildProcess): void { |
| 20 | + vi.mocked(spawn).mockReturnValue(child) |
| 21 | +} |
| 22 | + |
| 23 | +describe('exec', () => { |
| 24 | + beforeEach(() => { |
| 25 | + vi.clearAllMocks() |
| 26 | + }) |
| 27 | + |
| 28 | + it('resolves when process exits with code 0', async () => { |
| 29 | + const child = createMockChild() |
| 30 | + mockSpawnWith(child) |
| 31 | + |
| 32 | + const promise = exec('echo hello') |
| 33 | + child.emit('close', 0, null) |
| 34 | + |
| 35 | + await expect(promise).resolves.toBeUndefined() |
| 36 | + }) |
| 37 | + |
| 38 | + it('rejects with stderr when process exits with non-zero code', async () => { |
| 39 | + const child = createMockChild() |
| 40 | + mockSpawnWith(child) |
| 41 | + |
| 42 | + const promise = exec('false') |
| 43 | + child.stderr?.emit('data', Buffer.from('something went wrong')) |
| 44 | + child.emit('close', 1, null) |
| 45 | + |
| 46 | + await expect(promise).rejects.toThrow('something went wrong') |
| 47 | + }) |
| 48 | + |
| 49 | + it('rejects with exit code message when stderr is empty', async () => { |
| 50 | + const child = createMockChild() |
| 51 | + mockSpawnWith(child) |
| 52 | + |
| 53 | + const promise = exec('false') |
| 54 | + child.emit('close', 1, null) |
| 55 | + |
| 56 | + await expect(promise).rejects.toThrow('Command failed with exit code 1') |
| 57 | + }) |
| 58 | + |
| 59 | + it('rejects with signal name when process is killed by a signal', async () => { |
| 60 | + const child = createMockChild() |
| 61 | + mockSpawnWith(child) |
| 62 | + |
| 63 | + const promise = exec('sleep 999') |
| 64 | + child.emit('close', null, 'SIGTERM') |
| 65 | + |
| 66 | + await expect(promise).rejects.toThrow('Process killed by signal SIGTERM') |
| 67 | + }) |
| 68 | + |
| 69 | + it('rejects with stderr over signal message when both are available', async () => { |
| 70 | + const child = createMockChild() |
| 71 | + mockSpawnWith(child) |
| 72 | + |
| 73 | + const promise = exec('sleep 999') |
| 74 | + child.stderr?.emit('data', Buffer.from('Terminated')) |
| 75 | + child.emit('close', null, 'SIGTERM') |
| 76 | + |
| 77 | + await expect(promise).rejects.toThrow('Terminated') |
| 78 | + }) |
| 79 | + |
| 80 | + it('rejects when spawn emits an error', async () => { |
| 81 | + const child = createMockChild() |
| 82 | + mockSpawnWith(child) |
| 83 | + |
| 84 | + const promise = exec('nonexistent') |
| 85 | + child.emit('error', new Error('spawn ENOENT')) |
| 86 | + |
| 87 | + await expect(promise).rejects.toThrow('spawn ENOENT') |
| 88 | + }) |
| 89 | + |
| 90 | + it('spawns /bin/sh -c with the command string', async () => { |
| 91 | + const child = createMockChild() |
| 92 | + mockSpawnWith(child) |
| 93 | + |
| 94 | + const promise = exec('echo hello', { cwd: '/tmp' }) |
| 95 | + child.emit('close', 0, null) |
| 96 | + await promise |
| 97 | + |
| 98 | + expect(spawn).toHaveBeenCalledWith('/bin/sh', ['-c', 'echo hello'], { |
| 99 | + cwd: '/tmp', |
| 100 | + stdio: ['ignore', 'ignore', 'pipe'], |
| 101 | + }) |
| 102 | + }) |
| 103 | +}) |
| 104 | + |
| 105 | +describe('execFile', () => { |
| 106 | + beforeEach(() => { |
| 107 | + vi.clearAllMocks() |
| 108 | + }) |
| 109 | + |
| 110 | + it('spawns the file directly with args array', async () => { |
| 111 | + const child = createMockChild() |
| 112 | + mockSpawnWith(child) |
| 113 | + |
| 114 | + const promise = execFile('rm', ['-rf', 'dist'], { cwd: '/project' }) |
| 115 | + child.emit('close', 0, null) |
| 116 | + await promise |
| 117 | + |
| 118 | + expect(spawn).toHaveBeenCalledWith('rm', ['-rf', 'dist'], { |
| 119 | + cwd: '/project', |
| 120 | + stdio: ['ignore', 'ignore', 'pipe'], |
| 121 | + }) |
| 122 | + }) |
| 123 | + |
| 124 | + it('rejects with signal name when killed', async () => { |
| 125 | + const child = createMockChild() |
| 126 | + mockSpawnWith(child) |
| 127 | + |
| 128 | + const promise = execFile('sleep', ['999']) |
| 129 | + child.emit('close', null, 'SIGKILL') |
| 130 | + |
| 131 | + await expect(promise).rejects.toThrow('Process killed by signal SIGKILL') |
| 132 | + }) |
| 133 | +}) |
0 commit comments