|
| 1 | +import { parseArgString, runCli, RunCliError } from '../src/subprocess'; |
| 2 | + |
| 3 | +describe('parseArgString', () => { |
| 4 | + it('splits unquoted args on whitespace', () => { |
| 5 | + expect(parseArgString('search hello world')).toEqual([ |
| 6 | + 'search', |
| 7 | + 'hello', |
| 8 | + 'world', |
| 9 | + ]); |
| 10 | + }); |
| 11 | + |
| 12 | + it('respects double-quoted segments', () => { |
| 13 | + expect(parseArgString('search "hello world" --json')).toEqual([ |
| 14 | + 'search', |
| 15 | + 'hello world', |
| 16 | + '--json', |
| 17 | + ]); |
| 18 | + }); |
| 19 | + |
| 20 | + it('respects single-quoted segments', () => { |
| 21 | + expect(parseArgString("ask 'what is going on' --verbose")).toEqual([ |
| 22 | + 'ask', |
| 23 | + 'what is going on', |
| 24 | + '--verbose', |
| 25 | + ]); |
| 26 | + }); |
| 27 | + |
| 28 | + it('collapses runs of whitespace', () => { |
| 29 | + expect(parseArgString(' a b\tc ')).toEqual(['a', 'b', 'c']); |
| 30 | + }); |
| 31 | + |
| 32 | + it('returns an empty array for an empty / whitespace-only string', () => { |
| 33 | + expect(parseArgString('')).toEqual([]); |
| 34 | + expect(parseArgString(' \t ')).toEqual([]); |
| 35 | + }); |
| 36 | +}); |
| 37 | + |
| 38 | +describe('runCli', () => { |
| 39 | + const node = process.execPath; |
| 40 | + |
| 41 | + it('captures stdout and stderr from a successful exit', async () => { |
| 42 | + const { stdout, stderr, exitCode, signal, timedOut } = await runCli(node, [ |
| 43 | + '-e', |
| 44 | + 'process.stdout.write("hello\\n"); process.stderr.write("warn\\n");', |
| 45 | + ]); |
| 46 | + |
| 47 | + expect(stdout).toBe('hello\n'); |
| 48 | + expect(stderr).toBe('warn\n'); |
| 49 | + expect(exitCode).toBe(0); |
| 50 | + expect(signal).toBeNull(); |
| 51 | + expect(timedOut).toBe(false); |
| 52 | + }); |
| 53 | + |
| 54 | + it('rejects with RunCliError on non-zero exit by default', async () => { |
| 55 | + await expect( |
| 56 | + runCli(node, ['-e', 'process.stderr.write("boom"); process.exit(2);']) |
| 57 | + ).rejects.toMatchObject({ |
| 58 | + name: 'RunCliError', |
| 59 | + exitCode: 2, |
| 60 | + stderr: 'boom', |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + it('attaches captured streams to the rejection error', async () => { |
| 65 | + let caught: RunCliError | undefined; |
| 66 | + try { |
| 67 | + await runCli(node, [ |
| 68 | + '-e', |
| 69 | + 'process.stdout.write("partial output\\n"); process.exit(1);', |
| 70 | + ]); |
| 71 | + } catch (err) { |
| 72 | + caught = err as RunCliError; |
| 73 | + } |
| 74 | + expect(caught).toBeInstanceOf(RunCliError); |
| 75 | + expect(caught!.stdout).toBe('partial output\n'); |
| 76 | + expect(caught!.exitCode).toBe(1); |
| 77 | + }); |
| 78 | + |
| 79 | + it('with reject:false, resolves on non-zero exit', async () => { |
| 80 | + const result = await runCli( |
| 81 | + node, |
| 82 | + ['-e', 'process.stderr.write("bad"); process.exit(7);'], |
| 83 | + { reject: false } |
| 84 | + ); |
| 85 | + |
| 86 | + expect(result.exitCode).toBe(7); |
| 87 | + expect(result.stderr).toBe('bad'); |
| 88 | + expect(result.timedOut).toBe(false); |
| 89 | + }); |
| 90 | + |
| 91 | + it('writes options.stdin to the child', async () => { |
| 92 | + const { stdout } = await runCli( |
| 93 | + node, |
| 94 | + [ |
| 95 | + '-e', |
| 96 | + 'let d=""; process.stdin.on("data",c=>d+=c); process.stdin.on("end",()=>process.stdout.write(d));', |
| 97 | + ], |
| 98 | + { stdin: 'piped input' } |
| 99 | + ); |
| 100 | + expect(stdout).toBe('piped input'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('honours options.cwd', async () => { |
| 104 | + const { stdout } = await runCli(node, [ |
| 105 | + '-e', |
| 106 | + 'process.stdout.write(process.cwd());', |
| 107 | + ], { cwd: '/' }); |
| 108 | + expect(stdout).toBe('/'); |
| 109 | + }); |
| 110 | + |
| 111 | + it('honours options.env (replaces parent env)', async () => { |
| 112 | + const { stdout } = await runCli( |
| 113 | + node, |
| 114 | + ['-e', 'process.stdout.write(process.env.RUN_CLI_TEST_VAR ?? "missing");'], |
| 115 | + { env: { ...process.env, RUN_CLI_TEST_VAR: 'visible' } } |
| 116 | + ); |
| 117 | + expect(stdout).toBe('visible'); |
| 118 | + }); |
| 119 | + |
| 120 | + it('rejects with timedOut=true when the subprocess exceeds the timeout', async () => { |
| 121 | + let caught: RunCliError | undefined; |
| 122 | + try { |
| 123 | + await runCli(node, ['-e', 'setTimeout(() => {}, 60000);'], { |
| 124 | + timeout: 100, |
| 125 | + }); |
| 126 | + } catch (err) { |
| 127 | + caught = err as RunCliError; |
| 128 | + } |
| 129 | + expect(caught).toBeInstanceOf(RunCliError); |
| 130 | + expect(caught!.timedOut).toBe(true); |
| 131 | + expect(caught!.message).toMatch(/timed out after 100ms/); |
| 132 | + }); |
| 133 | + |
| 134 | + it('rejects on spawn error (binary not found)', async () => { |
| 135 | + await expect( |
| 136 | + runCli('/this/binary/does/not/exist', ['arg']) |
| 137 | + ).rejects.toThrow(); |
| 138 | + }); |
| 139 | +}); |
0 commit comments