|
| 1 | +import { spawn } from 'child_process' |
| 2 | +import { |
| 3 | + describe, |
| 4 | + it, |
| 5 | + expect |
| 6 | +} from 'vitest' |
| 7 | +import { |
| 8 | + exitCode, |
| 9 | + catchProcessError, |
| 10 | + outputStream, |
| 11 | + output |
| 12 | +} from './index.js' |
| 13 | + |
| 14 | +function child(code: string) { |
| 15 | + return spawn('node', ['-e', code]) |
| 16 | +} |
| 17 | + |
| 18 | +describe('child-process-utils', () => { |
| 19 | + describe('exitCode', () => { |
| 20 | + it('should return the exit code of a child process', async () => { |
| 21 | + const childProcess = child('process.exit(0)') |
| 22 | + const code = await exitCode(childProcess) |
| 23 | + |
| 24 | + expect(code).toBe(0) |
| 25 | + }) |
| 26 | + |
| 27 | + it('should return the exit code of a child process with an error', async () => { |
| 28 | + const childProcess = child('process.exit(1)') |
| 29 | + const code = await exitCode(childProcess) |
| 30 | + |
| 31 | + expect(code).toBe(1) |
| 32 | + }) |
| 33 | + }) |
| 34 | + |
| 35 | + describe('catchProcessError', () => { |
| 36 | + it('should catch an error from a child process', async () => { |
| 37 | + const childProcess = child('throw new Error("Test error")') |
| 38 | + const error = await catchProcessError(childProcess) |
| 39 | + |
| 40 | + expect(error).toBeInstanceOf(Error) |
| 41 | + expect(error?.message).toContain('Test error') |
| 42 | + }) |
| 43 | + |
| 44 | + it('should return null if the process exits successfully', async () => { |
| 45 | + const childProcess = child('process.exit(0)') |
| 46 | + const error = await catchProcessError(childProcess) |
| 47 | + |
| 48 | + expect(error).toBeNull() |
| 49 | + }) |
| 50 | + }) |
| 51 | + |
| 52 | + describe('outputStream', () => { |
| 53 | + it('should yield the stdout of a child process', async () => { |
| 54 | + const childProcess = child(` |
| 55 | + (async () => { |
| 56 | + for (let i = 0; i < 5; i++) { |
| 57 | + console.log('line ' + i) |
| 58 | + await new Promise(resolve => setTimeout(resolve, 10)) |
| 59 | + } |
| 60 | + })() |
| 61 | + `) |
| 62 | + const output = outputStream(childProcess) |
| 63 | + const result: string[] = [] |
| 64 | + |
| 65 | + for await (const line of output) { |
| 66 | + result.push(line.toString()) |
| 67 | + } |
| 68 | + |
| 69 | + expect(result).toHaveLength(5) |
| 70 | + expect(result[0]).toBe('line 0\n') |
| 71 | + }) |
| 72 | + |
| 73 | + it('should throw an error if the process exits with a non-zero code', async () => { |
| 74 | + const childProcess = child('process.exit(1)') |
| 75 | + |
| 76 | + await expect(async () => { |
| 77 | + for await (const _ of outputStream(childProcess)) { |
| 78 | + void _ |
| 79 | + } |
| 80 | + }).rejects.toThrow('Process exited with non-zero code') |
| 81 | + }) |
| 82 | + }) |
| 83 | + |
| 84 | + describe('output', () => { |
| 85 | + it('should return the stdout of a child process as a string', async () => { |
| 86 | + const childProcess = child(` |
| 87 | + (async () => { |
| 88 | + for (let i = 0; i < 3; i++) { |
| 89 | + console.log('output ' + i) |
| 90 | + await new Promise(resolve => setTimeout(resolve, 10)) |
| 91 | + } |
| 92 | + })() |
| 93 | + `) |
| 94 | + const result = await output(childProcess) |
| 95 | + |
| 96 | + expect(result.toString()).toBe('output 0\noutput 1\noutput 2\n') |
| 97 | + }) |
| 98 | + |
| 99 | + it('should throw an error if the process exits with a non-zero code', async () => { |
| 100 | + const childProcess = child('process.exit(1)') |
| 101 | + |
| 102 | + await expect(output(childProcess)).rejects.toThrow('Process exited with non-zero code') |
| 103 | + }) |
| 104 | + }) |
| 105 | +}) |
0 commit comments