Part 1 of 2 of the n/no-process-exit cleanup (umbrella #738): convert every process.exit() call in standalone scripts/** CLIs to the rule-compliant pattern. - New shared helper scripts/lib/cli-exit.cjs: ExitError(code,message) + runMain() which translates a thrown ExitError / returned number into process.exitCode (never process.exit()), flushing output and still firing process.on('exit'). - main()-based entrypoints: throw new ExitError(code) for errors, return <code> for verdicts; invoked via runMain(main). Child exit codes preserved via return. - top-level-only scripts: imperative body extracted into main() so mid-flow aborts (throw ExitError) actually halt; pure consts/helpers stay at module scope. - diff-touches-shipped-paths.cjs: stdin event handling restructured to an async read so the whole flow runs under runMain; uncaughtException/unhandledRejection nets replaced by an in-band catch that preserves EXIT_ERROR=2. Exit codes verified unchanged for every converted script (success/error/help and the 0/1/2 semantic codes in diff-touches). Rule stays warn here; flipped to error in part 2 (#738) once gsd-core/bin/** is also clean. Refs #739 Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
68 lines
1.9 KiB
JavaScript
68 lines
1.9 KiB
JavaScript
'use strict';
|
|
|
|
const { spawnSync } = require('child_process');
|
|
const { ExitError, runMain } = require('./lib/cli-exit.cjs');
|
|
|
|
const CROSS_PLATFORM_TEST_REASON = Object.freeze({
|
|
PASS: 'pass',
|
|
TEST_FAILURE: 'test_failure',
|
|
INFRA_FAILURE: 'infra_failure',
|
|
UNKNOWN_FAILURE: 'unknown_failure',
|
|
});
|
|
|
|
function classify(exitCode, output) {
|
|
if (exitCode === 0) return CROSS_PLATFORM_TEST_REASON.PASS;
|
|
if (exitCode === 2 || /infrastructure failure|worktree\.Construct/i.test(output)) {
|
|
return CROSS_PLATFORM_TEST_REASON.INFRA_FAILURE;
|
|
}
|
|
if (/\bFAIL\b|\d+\s+failures?\)/i.test(output)) {
|
|
return CROSS_PLATFORM_TEST_REASON.TEST_FAILURE;
|
|
}
|
|
return CROSS_PLATFORM_TEST_REASON.UNKNOWN_FAILURE;
|
|
}
|
|
|
|
function runCrossPlatformTests(options = {}, deps = {}) {
|
|
const {
|
|
base = 'next',
|
|
head = 'HEAD',
|
|
source = '.',
|
|
targets = 'linux,macos',
|
|
cwd = process.cwd(),
|
|
} = options;
|
|
const runner = deps.spawnSync || spawnSync;
|
|
|
|
const args = ['--targets', targets, '--base', base, '--head', head, '--source', source];
|
|
const result = runner('gsd-test', args, { cwd, encoding: 'utf8' });
|
|
|
|
const stdout = result.stdout || '';
|
|
const stderr = result.stderr || '';
|
|
const output = `${stdout}\n${stderr}`;
|
|
const exitCode = Number(result.status ?? 1);
|
|
const reason = classify(exitCode, output);
|
|
|
|
return {
|
|
ok: exitCode === 0,
|
|
reason,
|
|
exitCode,
|
|
command: ['gsd-test', ...args].join(' '),
|
|
stdout,
|
|
stderr,
|
|
};
|
|
}
|
|
|
|
if (require.main === module) {
|
|
function main() {
|
|
const result = runCrossPlatformTests();
|
|
const line = `[cross-platform-tests] reason=${result.reason} exit=${result.exitCode}`;
|
|
if (result.ok) {
|
|
process.stdout.write(`${line}\n`);
|
|
return 0;
|
|
}
|
|
process.stderr.write(`${line}\n`);
|
|
throw new ExitError(result.exitCode);
|
|
}
|
|
runMain(main);
|
|
}
|
|
|
|
module.exports = { CROSS_PLATFORM_TEST_REASON, runCrossPlatformTests };
|