|
| 1 | +import {x} from '../main.js'; |
| 2 | +import * as assert from 'node:assert/strict'; |
| 3 | +import {test} from 'node:test'; |
| 4 | + |
| 5 | +test('exec', async (t) => { |
| 6 | + await t.test('resolves to stdout', async () => { |
| 7 | + const result = await x('echo', ['foo']); |
| 8 | + assert.equal(result.stdout, 'foo\n'); |
| 9 | + assert.equal(result.stderr, ''); |
| 10 | + }); |
| 11 | + |
| 12 | + await t.test('times out after defined timeout (ms)', async () => { |
| 13 | + const proc = x('sleep', ['0.2s'], {timeout: 100}); |
| 14 | + await assert.rejects(async () => { |
| 15 | + await proc; |
| 16 | + }); |
| 17 | + assert.equal(proc.killed, true); |
| 18 | + assert.equal(proc.process!.signalCode, 'SIGTERM'); |
| 19 | + }); |
| 20 | + |
| 21 | + await t.test('throws spawn errors', async () => { |
| 22 | + const proc = x('definitelyNonExistent'); |
| 23 | + await assert.rejects(async () => { |
| 24 | + await proc; |
| 25 | + }, 'spawn definitelyNonExistent NOENT'); |
| 26 | + }); |
| 27 | + |
| 28 | + await t.test('captures stderr', async () => { |
| 29 | + const result = await x('cat', ['nonexistentforsure']); |
| 30 | + assert.equal( |
| 31 | + result.stderr, |
| 32 | + 'cat: nonexistentforsure: No such file or directory\n' |
| 33 | + ); |
| 34 | + assert.equal(result.stdout, ''); |
| 35 | + }); |
| 36 | + |
| 37 | + await t.test('pid is number', async () => { |
| 38 | + const proc = x('echo', ['foo']); |
| 39 | + await proc; |
| 40 | + assert.ok(typeof proc.pid === 'number'); |
| 41 | + }); |
| 42 | + |
| 43 | + await t.test('exitCode is set correctly', async () => { |
| 44 | + const proc = x('echo', ['foo']); |
| 45 | + assert.equal(proc.exitCode, undefined); |
| 46 | + await proc; |
| 47 | + assert.equal(proc.exitCode, 0); |
| 48 | + }); |
| 49 | + |
| 50 | + await t.test('kill terminates the process', async () => { |
| 51 | + const proc = x('sleep', ['5s']); |
| 52 | + const result = proc.kill(); |
| 53 | + assert.ok(result); |
| 54 | + assert.ok(proc.killed); |
| 55 | + assert.equal(proc.aborted, false); |
| 56 | + }); |
| 57 | + |
| 58 | + await t.test('pipe correctly pipes output', async () => { |
| 59 | + const echoProc = x('echo', ['foo\nbar']); |
| 60 | + const grepProc = echoProc.pipe('grep', ['foo']); |
| 61 | + const result = await grepProc; |
| 62 | + |
| 63 | + assert.equal(result.stderr, ''); |
| 64 | + assert.equal(result.stdout, 'foo\n'); |
| 65 | + assert.equal(echoProc.exitCode, 0); |
| 66 | + assert.equal(grepProc.exitCode, 0); |
| 67 | + }); |
| 68 | + |
| 69 | + await t.test('async iterator gets correct output', async () => { |
| 70 | + const proc = x('echo', ['foo\nbar']); |
| 71 | + const lines = []; |
| 72 | + for await (const line of proc) { |
| 73 | + lines.push(line); |
| 74 | + } |
| 75 | + |
| 76 | + assert.deepEqual(lines, ['foo', 'bar']); |
| 77 | + }); |
| 78 | + |
| 79 | + await t.test('async iterator receives errors', async () => { |
| 80 | + const proc = x('nonexistentforsure'); |
| 81 | + await assert.rejects(async () => { |
| 82 | + for await (const line of proc) { |
| 83 | + line; |
| 84 | + } |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + await t.test('signal can be used to abort execution', async () => { |
| 89 | + const controller = new AbortController(); |
| 90 | + const proc = x('sleep', ['4s'], {signal: controller.signal}); |
| 91 | + controller.abort(); |
| 92 | + const result = await proc; |
| 93 | + assert.ok(proc.aborted); |
| 94 | + assert.ok(proc.killed); |
| 95 | + assert.equal(result.stderr, ''); |
| 96 | + assert.equal(result.stdout, ''); |
| 97 | + }); |
| 98 | +}); |
0 commit comments