Skip to content

Commit

Permalink
test: check ProcessPromise getters
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Jan 11, 2025
1 parent bb6d54a commit a984acb
Showing 1 changed file with 67 additions and 66 deletions.
133 changes: 67 additions & 66 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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')
})
})
})

0 comments on commit a984acb

Please sign in to comment.