|
| 1 | +import { ChildProcessWithoutNullStreams, spawn } from 'node:child_process' |
| 2 | +import { resolve } from 'node:path' |
| 3 | +import t from 'tap' |
| 4 | + |
| 5 | +const f = resolve(__dirname, 'fixtures/signal-capture.js') |
| 6 | + |
| 7 | +const skip = process.platform === 'win32' ? 'skip on windows' : false |
| 8 | + |
| 9 | +type Result = { |
| 10 | + stdout: string |
| 11 | + stderr: string |
| 12 | + code: null | number |
| 13 | + signal: null | NodeJS.Signals |
| 14 | +} |
| 15 | + |
| 16 | +type PromiseWithProc<T> = Promise<T> & { |
| 17 | + proc: ChildProcessWithoutNullStreams |
| 18 | +} |
| 19 | +const run = ( |
| 20 | + exit: number | NodeJS.Signals | 'null' = 'null', |
| 21 | + capture: 'capture' | 'captureExit' | 'captureAfterExit' | 'none' = 'none' |
| 22 | +): PromiseWithProc<Result> => { |
| 23 | + const args: string[] = [f, String(exit), capture] |
| 24 | + const proc = spawn(process.execPath, args) |
| 25 | + const { stdout, stderr } = proc |
| 26 | + const out: Buffer[] = [] |
| 27 | + const err: Buffer[] = [] |
| 28 | + stdout.on('data', c => out.push(c)) |
| 29 | + stderr.on('data', c => err.push(c)) |
| 30 | + return Object.assign( |
| 31 | + new Promise<Result>(r => { |
| 32 | + proc.on('close', (code, signal) => { |
| 33 | + r({ |
| 34 | + code, |
| 35 | + signal, |
| 36 | + stdout: Buffer.concat(out).toString(), |
| 37 | + stderr: Buffer.concat(err).toString(), |
| 38 | + }) |
| 39 | + }) |
| 40 | + }), |
| 41 | + { proc } |
| 42 | + ) |
| 43 | +} |
| 44 | + |
| 45 | +t.test('graceful exit', async t => { |
| 46 | + const r = await run() |
| 47 | + t.match(r, { code: 0, signal: null }) |
| 48 | + t.equal(r.stderr, '') |
| 49 | + t.matchSnapshot(r.stdout) |
| 50 | +}) |
| 51 | + |
| 52 | +t.test('exit 0', async t => { |
| 53 | + const r = await run(0) |
| 54 | + t.match(r, { code: 0, signal: null }) |
| 55 | + t.equal(r.stderr, '') |
| 56 | + t.matchSnapshot(r.stdout) |
| 57 | +}) |
| 58 | + |
| 59 | +t.test('exit 1', async t => { |
| 60 | + const r = await run(1) |
| 61 | + t.match(r, { code: 1, signal: null }) |
| 62 | + t.equal(r.stderr, '') |
| 63 | + t.matchSnapshot(r.stdout) |
| 64 | +}) |
| 65 | + |
| 66 | +t.test('signal, no capture', { skip }, async t => { |
| 67 | + const r = await run('SIGHUP') |
| 68 | + t.match(r, { code: null, signal: 'SIGHUP' }) |
| 69 | + t.equal(r.stderr, '') |
| 70 | + t.matchSnapshot(r.stdout) |
| 71 | +}) |
| 72 | + |
| 73 | +t.test('signal, capture exit', { skip }, async t => { |
| 74 | + const r = await run('SIGHUP', 'captureExit') |
| 75 | + t.match(r, { code: 0, signal: null }) |
| 76 | + t.equal(r.stderr, '') |
| 77 | + t.matchSnapshot(r.stdout) |
| 78 | +}) |
| 79 | + |
| 80 | +t.test('signal, capture afterExit', { skip }, async t => { |
| 81 | + const r = await run('SIGHUP', 'captureAfterExit') |
| 82 | + t.match(r, { code: 0, signal: null }) |
| 83 | + t.equal(r.stderr, '') |
| 84 | + t.matchSnapshot(r.stdout) |
| 85 | +}) |
| 86 | + |
| 87 | +t.test('signal, capture both', { skip }, async t => { |
| 88 | + const r = await run('SIGHUP', 'capture') |
| 89 | + t.match(r, { code: 0, signal: null }) |
| 90 | + t.equal(r.stderr, '') |
| 91 | + t.matchSnapshot(r.stdout) |
| 92 | +}) |
0 commit comments