From a984acb48bb23cc4ef6974b2283c54e2e5927d91 Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Sun, 12 Jan 2025 00:19:05 +0300 Subject: [PATCH] test: check `ProcessPromise` getters --- test/core.test.js | 133 +++++++++++++++++++++++----------------------- 1 file changed, 67 insertions(+), 66 deletions(-) diff --git a/test/core.test.js b/test/core.test.js index c2e585de90..218782c428 100644 --- a/test/core.test.js +++ b/test/core.test.js @@ -19,6 +19,7 @@ import { basename } from 'node:path' import { WriteStream } from 'node:fs' import { Readable, Transform, Writable } from 'node:stream' import { Socket } from 'node:net' +import { ChildProcess } from 'node:child_process' import { $, ProcessPromise, @@ -393,6 +394,72 @@ describe('core', () => { }) describe('ProcessPromise', () => { + test('getters', async () => { + const p = $`echo foo` + assert.ok(p.pid > 0) + assert.ok(typeof p.id === 'string') + assert.ok(typeof p.cmd === 'string') + assert.ok(typeof p.fullCmd === 'string') + assert.ok(typeof p.stage === 'string') + assert.ok(p.child instanceof ChildProcess) + assert.ok(p.stdout instanceof Socket) + assert.ok(p.stderr instanceof Socket) + assert.ok(p.exitCode instanceof Promise) + assert.ok(p.signal instanceof AbortSignal) + assert.equal(p.output, null) + + await p + assert.ok(p.output instanceof ProcessOutput) + }) + + describe('state machine transitions', () => { + it('running > fulfilled', async () => { + const p = $`echo foo` + assert.equal(p.stage, 'running') + await p + assert.equal(p.stage, 'fulfilled') + }) + + it('running > rejected', async () => { + const p = $`foo` + assert.equal(p.stage, 'running') + + try { + await p + } catch {} + assert.equal(p.stage, 'rejected') + }) + + it('halted > running > fulfilled', async () => { + const p = $({ halt: true })`echo foo` + assert.equal(p.stage, 'halted') + p.run() + assert.equal(p.stage, 'running') + await p + assert.equal(p.stage, 'fulfilled') + }) + + it('all transition', async () => { + const { promise, resolve, reject } = Promise.withResolvers() + const process = new ProcessPromise(noop, noop) + + assert.equal(process.stage, 'initial') + process._bind('echo foo', 'test', resolve, reject, { + ...resolveDefaults(), + halt: true, + }) + + assert.equal(process.stage, 'halted') + process.run() + + assert.equal(process.stage, 'running') + await promise + + assert.equal(process.stage, 'fulfilled') + assert.equal(process.output?.stdout, 'foo\n') + }) + }) + test('inherits native Promise', async () => { const p1 = $`echo 1` const p2 = p1.then((v) => v) @@ -425,12 +492,6 @@ describe('core', () => { assert.equal(p.fullCmd, "set -euo pipefail;echo $'#bar' --t 1") }) - test('exposes pid & id', () => { - const p = $`echo foo` - assert.ok(p.pid > 0) - assert.ok(typeof p.id === 'string') - }) - test('stdio() works', async () => { const p1 = $`printf foo` await p1 @@ -1283,64 +1344,4 @@ describe('core', () => { assert.equal($.quote, quote) }) }) - - describe('stage machine stage', () => { - it(`handle the transition 'running' -> 'fulfilled'`, async () => { - const p = $`echo foo` - - assert.equal(p.stage, 'running') - await p - - assert.equal(p.stage, 'fulfilled') - }) - - it(`handle the transition 'running' -> 'rejected'`, async () => { - const p = $`wft` - - assert.equal(p.stage, 'running') - - try { - await p - } catch (e) {} - - assert.equal(p.stage, 'rejected') - }) - - it(`handle the transition 'halted' -> 'running' -> 'fulfilled'`, async () => { - const p = $({ halt: true })`echo foo` - - assert.equal(p.stage, 'halted') - - p.run() - - assert.equal(p.stage, 'running') - - await p - - assert.equal(p.stage, 'fulfilled') - }) - - it('handle all transition', async () => { - const { promise, resolve, reject } = Promise.withResolvers() - const process = new ProcessPromise(noop, noop) - - assert.equal(process.stage, 'initial') - - process._bind('echo foo', 'test', resolve, reject, { - ...resolveDefaults(), - halt: true, - }) - - assert.equal(process.stage, 'halted') - - process.run() - - assert.equal(process.stage, 'running') - - await promise - - assert.equal(process.stage, 'fulfilled') - assert.equal(process.output?.stdout, 'foo\n') - }) - }) })