|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { spawn } from 'node:child_process'; |
| 3 | +import { once } from 'node:events'; |
| 4 | +import { test } from 'vitest'; |
| 5 | + |
| 6 | +test.each([false, true])('script exit flushes piped diagnostics (throws=%s)', async (throws) => { |
| 7 | + const bytes = 4 * 1024 * 1024; |
| 8 | + const stdoutTail = 'stdout diagnostic tail\n'; |
| 9 | + const stderrTail = 'stderr diagnostic tail\n'; |
| 10 | + const failure = 'fixture: expected failure\n'; |
| 11 | + const source = [ |
| 12 | + `import { runEntrypoint } from ${JSON.stringify(new URL('./cli-entrypoint.ts', import.meta.url).href)};`, |
| 13 | + 'runEntrypoint("fixture", async () => {', |
| 14 | + `process.stdout.write('o'.repeat(${bytes}) + ${JSON.stringify(stdoutTail)});`, |
| 15 | + `process.stderr.write('e'.repeat(${bytes}) + ${JSON.stringify(stderrTail)});`, |
| 16 | + throws ? 'throw new Error("expected failure");' : 'return 7;', |
| 17 | + '});', |
| 18 | + ].join('\n'); |
| 19 | + const child = spawn( |
| 20 | + process.execPath, |
| 21 | + ['--experimental-strip-types', '--input-type=module', '--eval', source], |
| 22 | + { stdio: ['ignore', 'pipe', 'pipe'] }, |
| 23 | + ); |
| 24 | + let stdout = ''; |
| 25 | + let stderr = ''; |
| 26 | + child.stdout.setEncoding('utf8').on('data', (chunk: string) => (stdout += chunk)); |
| 27 | + child.stderr.setEncoding('utf8').on('data', (chunk: string) => (stderr += chunk)); |
| 28 | + const [code, signal] = await once(child, 'close'); |
| 29 | + assert.equal(signal, null); |
| 30 | + assert.equal(code, throws ? 1 : 7); |
| 31 | + assert.equal(stdout.length, bytes + stdoutTail.length); |
| 32 | + assert.ok(stdout.endsWith(stdoutTail)); |
| 33 | + assert.equal(stderr.length, bytes + stderrTail.length + (throws ? failure.length : 0)); |
| 34 | + assert.ok(stderr.endsWith(throws ? failure : stderrTail)); |
| 35 | +}); |
0 commit comments