Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495import { describe, expect, it,} from '@monochromatic-dev/module-test/ts';import spawn from 'nano-spawn';import { exec, } from 'node:child_process';import { existsSync, mkdirSync, rmSync, unlinkSync, writeFileSync,} from 'node:fs';import { join, } from 'node:path';
/** Runs a shell command and resolves with its captured output. Wraps `nano-spawn` with `shell: true` to mirror the previous `promisify(exec)` contract: resolves `{ stdout, stderr }` on success and rejects with a `SubprocessError` (carrying `exitCode`, `stdout`, `stderr`) on non-zero exit. @param command - Full shell command line to execute @returns Captured `stdout` and `stderr` @example ```ts const { stdout } = await execAsync('echo hi'); // stdout === 'hi' ``` */function execAsync(command: string,): Promise<{ readonly stdout: string; readonly stderr: string; }> { return spawn(command, { shell: true, },);}
const createTestScript = () => `const arg = process.argv[2];if (arg === 'fail') { console.error('Error: Test failure'); process.exit(1);} else if (arg === 'fail-with-code') { process.exit(parseInt(process.argv[3]) || 2);} else if (arg === 'output') { console.log('stdout output'); console.error('stderr output');} else { console.log('Success');}`;
//region Fixture Setup: per-test fixtures
function setup() { const testFileDir = import.meta.dirname; const cliPath = join(testFileDir, 'command.ts',);
const packageDir = join(testFileDir, '..',); const timestamp = Date.now(); const randomId = Math.random().toString(36,).slice(2, 8,); const testDir = join(packageDir, 'dist', 'temp', 'test', `cli-command-${timestamp}-${randomId}`,);
if (!existsSync(testDir,)) mkdirSync(testDir, { recursive: true, },);
const testScript = join(testDir, 'test-script.js',); writeFileSync(testScript, createTestScript(),);
return { cliPath, testDir, testScript, };}
function teardown({ testScript, testDir, }: { testScript: string; testDir: string; },) { if (existsSync(testScript,)) unlinkSync(testScript,);
if (existsSync(testDir,)) rmSync(testDir, { recursive: true, },);}
//endregion Fixture Setup
await describe({ name: 'task-command', children: [ it({ name: 'executes command successfully with exit code 0', fn: async () => { const fixtures = setup(); const { cliPath, testScript, } = fixtures;
const { stdout, stderr, } = await execAsync( `node ${cliPath} -- node ${testScript} success`, );
expect(stdout,).toContain('Success',); expect(stderr,).toBe('',);
teardown(fixtures,); }, },), it({ name: 'executes command and propagates failure exit code', fn: async () => { const fixtures = setup(); const { cliPath, testScript, } = fixtures;
await expect(execAsync(`node ${cliPath} -- node ${testScript} fail`,),) .rejects .toThrow();
try { await execAsync(`node ${cliPath} -- node ${testScript} fail`,); } catch (error: unknown) { const execError = error as { exitCode: number; stderr: string; }; expect(execError.exitCode,).toBe(1,); expect(execError.stderr,).toContain('Error: Test failure',); }
teardown(fixtures,); }, },), it({ name: 'executes command with --allowFailure flag and exits with 0', fn: async () => { const fixtures = setup(); const { cliPath, testScript, } = fixtures;
const { stdout, stderr, } = await execAsync( `node ${cliPath} --allowFailure -- node ${testScript} fail`, );
expect(stderr,).toContain('Error: Test failure',); // Command should succeed despite the script failing
teardown(fixtures,); }, },), it({ name: 'uses short flag -a for allowFailure', fn: async () => { const fixtures = setup(); const { cliPath, testScript, } = fixtures;
const { stdout, stderr, } = await execAsync( `node ${cliPath} -a -- node ${testScript} fail`, );
expect(stderr,).toContain('Error: Test failure',); // Command should succeed despite the script failing
teardown(fixtures,); }, },), it({ name: 'preserves stdout and stderr output', fn: async () => { const fixtures = setup(); const { cliPath, testScript, } = fixtures;
const { stdout, stderr, } = await execAsync( `node ${cliPath} -- node ${testScript} output`, );
expect(stdout,).toContain('stdout output',); expect(stderr,).toContain('stderr output',);
teardown(fixtures,); }, },), it({ name: 'passes multiple arguments to the command', fn: async () => { const fixtures = setup(); const { cliPath, } = fixtures;
const { stdout, } = await execAsync( `node ${cliPath} -- echo "arg1" "arg2" "arg3"`, );
expect(stdout,).toContain('arg1 arg2 arg3',);
teardown(fixtures,); }, },), it({ name: 'executes shell commands with --shell flag', fn: async () => { const fixtures = setup(); const { cliPath, } = fixtures;
const { stdout, } = await execAsync( `node ${cliPath} --shell -- "echo hello && echo world"`, );
expect(stdout,).toContain('hello',); expect(stdout,).toContain('world',);
teardown(fixtures,); }, },), it({ name: 'uses short flag -s for shell', fn: async () => { const fixtures = setup(); const { cliPath, } = fixtures;
const { stdout, } = await execAsync(`node ${cliPath} -s -- "echo test"`,);
expect(stdout,).toContain('test',);
teardown(fixtures,); }, },), it({ name: 'fails when no command is specified', fn: async () => { const fixtures = setup(); const { cliPath, } = fixtures;
try { await execAsync(`node ${cliPath}`,); // Should not reach here expect(true,).toBe(false,); } catch (error: unknown) { const execError = error as { exitCode: number; }; expect(execError.exitCode,).toBeGreaterThan(0,); }
teardown(fixtures,); }, },), it({ name: 'fails when only -- is provided without command', fn: async () => { const fixtures = setup(); const { cliPath, } = fixtures;
try { await execAsync(`node ${cliPath} --`,); // Should not reach here expect(true,).toBe(false,); } catch (error: unknown) { const execError = error as { exitCode: number; }; expect(execError.exitCode,).toBeGreaterThan(0,); }
teardown(fixtures,); }, },), it({ name: 'propagates custom exit codes', fn: async () => { const fixtures = setup(); const { cliPath, testScript, } = fixtures;
try { await execAsync(`node ${cliPath} -- node ${testScript} fail-with-code 42`,); } catch (error: unknown) { const execError = error as { exitCode: number; }; expect(execError.exitCode,).toBe(42,); }
teardown(fixtures,); }, },), it({ name: 'handles non-existent command', fn: async () => { const fixtures = setup(); const { cliPath, } = fixtures;
try { await execAsync(`node ${cliPath} -- nonexistentcommand123`,); // Should not reach here expect(true,).toBe(false,); } catch (error: unknown) { const execError = error as { exitCode: number; }; expect(execError.exitCode,).toBeGreaterThan(0,); }
teardown(fixtures,); }, },), it({ name: 'handles non-existent command with allowFailure', fn: async () => { const fixtures = setup(); const { cliPath, } = fixtures;
// Should not throw with allowFailure const result = await execAsync( `node ${cliPath} --allowFailure -- nonexistentcommand123`, ); // Command succeeds even though the subcommand doesn't exist
teardown(fixtures,); }, },), it({ name: 'executes commands with special characters', fn: async () => { const fixtures = setup(); const { cliPath, } = fixtures;
const { stdout, } = await execAsync(`node ${cliPath} -- echo "Hello $USER!"`,);
expect(stdout,).toContain('Hello',); expect(stdout,).toContain('!',);
teardown(fixtures,); }, },), it({ name: 'handles command termination by signal', skip: process.platform === 'win32', fn: async () => { const fixtures = setup(); const { cliPath, testDir, } = fixtures;
// Create a script that sleeps and can be killed const sleepScript = join(testDir, 'sleep-script.js',); writeFileSync(sleepScript, ` process.on('SIGTERM', () => { console.log('Received SIGTERM'); process.exit(0); }); setTimeout(() => {}, 10000); // Sleep for 10 seconds `,);
// Start the command in background and kill it const childProcess = exec(`node ${cliPath} -- node ${sleepScript}`,);
// Give it time to start await new Promise(function delayResolve(resolve,): void { setTimeout(resolve, 100,); },);
// Kill the process if ((childProcess.pid !== undefined) && (childProcess.pid !== 0)) process.kill(childProcess.pid, 'SIGTERM',);
// Wait for the process to finish await new Promise((resolve, reject,) => { childProcess.on('exit', resolve,); childProcess.on('error', reject,); },);
// Clean up if (existsSync(sleepScript,)) unlinkSync(sleepScript,);
teardown(fixtures,); }, },), it({ name: 'combines multiple flags', fn: async () => { const fixtures = setup(); const { cliPath, } = fixtures;
const { stdout, } = await execAsync( `node ${cliPath} -a -s -- "echo combined && exit 1"`, );
expect(stdout,).toContain('combined',); // Should succeed despite exit 1 due to allowFailure
teardown(fixtures,); }, },), it({ name: 'executes command with timeout', fn: async () => { const fixtures = setup(); const { cliPath, testScript, } = fixtures;
const { stdout, } = await execAsync( `node ${cliPath} --timeout 5000 -- node ${testScript} success`, );
expect(stdout,).toContain('Success',);
teardown(fixtures,); }, },), it({ name: 'uses short flag -t for timeout', fn: async () => { const fixtures = setup(); const { cliPath, testScript, } = fixtures;
const { stdout, } = await execAsync( `node ${cliPath} -t 5000 -- node ${testScript} success`, );
expect(stdout,).toContain('Success',);
teardown(fixtures,); }, },), it({ name: 'timeout terminates long-running command', fn: async () => { const fixtures = setup(); const { cliPath, testDir, } = fixtures;
// Create a script that runs longer than timeout const longScript = join(testDir, 'long-script.js',); writeFileSync(longScript, ` console.log('Starting long task'); setTimeout(() => { console.log('This should not print'); }, 2000); `,);
await expect( execAsync(`node ${cliPath} --timeout 500 -- node ${longScript}`,), ) .rejects .toThrow();
// Clean up if (existsSync(longScript,)) unlinkSync(longScript,);
teardown(fixtures,); }, },), it({ name: 'timeout with allowFailure exits with 0', fn: async () => { const fixtures = setup(); const { cliPath, testDir, } = fixtures;
// Create a script that runs longer than timeout const longScript = join(testDir, 'long-script-allow.js',); writeFileSync(longScript, ` console.log('Starting long task'); setTimeout(() => { console.log('This should not print'); }, 2000); `,);
const { stdout, } = await execAsync( `node ${cliPath} --allowFailure --timeout 500 -- node ${longScript}`, );
expect(stdout,).toContain('Starting long task',);
// Clean up if (existsSync(longScript,)) unlinkSync(longScript,);
teardown(fixtures,); }, },), it({ name: 'command without timeout runs to completion', fn: async () => { const fixtures = setup(); const { cliPath, testDir, } = fixtures;
// Create a script that takes some time but completes const timedScript = join(testDir, 'timed-script.js',); writeFileSync(timedScript, ` console.log('Start'); setTimeout(() => { console.log('End'); process.exit(0); }, 100); `,);
const { stdout, } = await execAsync( `node ${cliPath} -- node ${timedScript}`, );
expect(stdout,).toContain('Start',); expect(stdout,).toContain('End',);
// Clean up if (existsSync(timedScript,)) unlinkSync(timedScript,);
teardown(fixtures,); }, },), ],},);