|
| 1 | +#!/usr/bin/env bun |
| 2 | +/** |
| 3 | + * Long-running smoke test for a compiled CLI binary. |
| 4 | + * |
| 5 | + * `--version` and `--help` exit via commander synchronously, before async |
| 6 | + * startup failures (e.g. the unhandled rejection from Parser.init when the |
| 7 | + * tree-sitter wasm load fails) get a chance to fire. This script spawns the |
| 8 | + * binary, lets it run for a few seconds, then kills it and asserts no fatal |
| 9 | + * startup markers showed up in stdout/stderr. |
| 10 | + * |
| 11 | + * Designed to run on every supported platform (Linux, macOS, Windows) without |
| 12 | + * extra deps. The binary doesn't need a TTY: `earlyFatalHandler` in |
| 13 | + * `cli/src/index.tsx` writes its diagnostic to stdout/stderr regardless. |
| 14 | + * |
| 15 | + * Usage: |
| 16 | + * bun cli/scripts/smoke-binary.ts <path-to-binary> [seconds] |
| 17 | + * |
| 18 | + * Exits 0 if no fatal markers detected, 1 otherwise. |
| 19 | + */ |
| 20 | + |
| 21 | +import { spawn } from 'child_process' |
| 22 | +import { existsSync } from 'fs' |
| 23 | + |
| 24 | +// Markers that indicate the CLI crashed during startup. Match what |
| 25 | +// `earlyFatalHandler` writes plus the specific tree-sitter regression. |
| 26 | +const FATAL_PATTERNS = [ |
| 27 | + /Fatal error during startup/i, |
| 28 | + /Internal error: tree-sitter\.wasm not found/i, |
| 29 | + /UnhandledPromiseRejection/i, |
| 30 | + /Cannot find module/i, |
| 31 | +] as const |
| 32 | + |
| 33 | +const DEFAULT_RUN_SECONDS = 5 |
| 34 | + |
| 35 | +async function main(): Promise<void> { |
| 36 | + const binary = process.argv[2] |
| 37 | + const runSeconds = Number(process.argv[3] ?? DEFAULT_RUN_SECONDS) |
| 38 | + |
| 39 | + if (!binary) { |
| 40 | + console.error('Usage: bun smoke-binary.ts <path-to-binary> [seconds]') |
| 41 | + process.exit(2) |
| 42 | + } |
| 43 | + if (!existsSync(binary)) { |
| 44 | + console.error(`smoke-binary: binary not found: ${binary}`) |
| 45 | + process.exit(2) |
| 46 | + } |
| 47 | + if (!Number.isFinite(runSeconds) || runSeconds <= 0) { |
| 48 | + console.error(`smoke-binary: bad seconds arg: ${process.argv[3]}`) |
| 49 | + process.exit(2) |
| 50 | + } |
| 51 | + |
| 52 | + console.log(`smoke-binary: spawning ${binary} for ${runSeconds}s…`) |
| 53 | + |
| 54 | + const proc = spawn(binary, [], { |
| 55 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 56 | + env: { ...process.env, NO_COLOR: '1', TERM: 'dumb' }, |
| 57 | + }) |
| 58 | + |
| 59 | + let captured = '' |
| 60 | + const append = (chunk: Buffer): void => { |
| 61 | + captured += chunk.toString('utf8') |
| 62 | + } |
| 63 | + proc.stdout?.on('data', append) |
| 64 | + proc.stderr?.on('data', append) |
| 65 | + |
| 66 | + let earlyExitCode: number | null = null |
| 67 | + const exited = new Promise<void>((resolve) => { |
| 68 | + proc.once('exit', (code) => { |
| 69 | + earlyExitCode = code |
| 70 | + resolve() |
| 71 | + }) |
| 72 | + }) |
| 73 | + |
| 74 | + const killTimer = setTimeout(() => { |
| 75 | + // SIGKILL is the only signal that's portable across Linux/macOS/Windows |
| 76 | + // here; SIGTERM may be ignored by the renderer on some platforms. |
| 77 | + proc.kill('SIGKILL') |
| 78 | + }, runSeconds * 1_000) |
| 79 | + |
| 80 | + await exited |
| 81 | + clearTimeout(killTimer) |
| 82 | + |
| 83 | + for (const pattern of FATAL_PATTERNS) { |
| 84 | + if (pattern.test(captured)) { |
| 85 | + console.error( |
| 86 | + `smoke-binary: FAIL — output matched ${pattern} (exit code ${earlyExitCode}).`, |
| 87 | + ) |
| 88 | + console.error('--- captured output (truncated to 8KB) ---') |
| 89 | + console.error(captured.slice(0, 8 * 1024)) |
| 90 | + process.exit(1) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + console.log( |
| 95 | + `smoke-binary: OK (exit code ${earlyExitCode}, ${captured.length} bytes captured).`, |
| 96 | + ) |
| 97 | +} |
| 98 | + |
| 99 | +main().catch((err: unknown) => { |
| 100 | + console.error('smoke-binary: unexpected error:', err) |
| 101 | + process.exit(2) |
| 102 | +}) |
0 commit comments