diff --git a/src/core.ts b/src/core.ts index 9c5baeaa29..ffefd1ffb2 100644 --- a/src/core.ts +++ b/src/core.ts @@ -192,25 +192,6 @@ export const $: Shell & Options = new Proxy( type Resolve = (out: ProcessOutput) => void -export interface ProcessPromise extends Promise { - then( - onfulfilled?: - | ((value: ProcessOutput) => PromiseLike | R) - | undefined - | null, - onrejected?: - | ((reason: ProcessOutput) => PromiseLike | E) - | undefined - | null - ): Promise - catch( - onrejected?: - | ((reason: ProcessOutput) => PromiseLike | T) - | undefined - | null - ): Promise -} - export class ProcessPromise extends Promise { private _command = '' private _from = '' @@ -539,6 +520,29 @@ export class ProcessPromise extends Promise { return this._nothrow ?? this._snapshot.nothrow } + // Promise API + then( + onfulfilled?: + | ((value: ProcessOutput) => PromiseLike | R) + | undefined + | null, + onrejected?: + | ((reason: ProcessOutput) => PromiseLike | E) + | undefined + | null + ): Promise { + return super.then(onfulfilled, onrejected) + } + + catch( + onrejected?: + | ((reason: ProcessOutput) => PromiseLike | T) + | undefined + | null + ): Promise { + return super.catch(onrejected) + } + // Stream-like API private writable = true private emit(event: string, ...args: any[]) { diff --git a/test/smoke/bun.test.js b/test/smoke/bun.test.js index dbba6aa9d2..6d5255f1d3 100644 --- a/test/smoke/bun.test.js +++ b/test/smoke/bun.test.js @@ -30,4 +30,34 @@ describe('bun', () => { test('stdio: inherit', async () => { await $({ stdio: 'inherit' })`ls` }) + + test('ctx isolation', async () => { + await within(async () => { + const t1 = tmpdir() + const t3 = tmpdir() + $.cwd = t1 + assert.equal($.cwd, t1) + assert.equal($.cwd, t1) + + const w = within(async () => { + const t3 = tmpdir() + $.cwd = t3 + assert.equal($.cwd, t3) + + assert.ok((await $`pwd`).toString().trim().endsWith(t3)) + assert.equal($.cwd, t3) + }) + + await $`pwd` + assert.ok((await $`pwd`).toString().trim().endsWith(t1)) + assert.equal($.cwd, t1) + assert.ok((await $`pwd`).toString().trim().endsWith(t1)) + + $.cwd = t3 + assert.ok((await $`pwd`).toString().trim().endsWith(t3)) + assert.equal($.cwd, t3) + + await w + }) + }) }) diff --git a/test/smoke/node.test.mjs b/test/smoke/node.test.mjs index 05000546ff..3497908622 100644 --- a/test/smoke/node.test.mjs +++ b/test/smoke/node.test.mjs @@ -26,6 +26,37 @@ import 'zx/globals' const p = await $({ nothrow: true })`echo foo; exit 3` assert.match(p.message, /exit code: 3/) } + + // ctx isolation + { + await within(async () => { + const t1 = tmpdir() + const t3 = tmpdir() + $.cwd = t1 + assert.equal($.cwd, t1) + assert.equal($.cwd, t1) + + const w = within(async () => { + const t3 = tmpdir() + $.cwd = t3 + assert.equal($.cwd, t3) + + assert.ok((await $`pwd`).toString().trim().endsWith(t3)) + assert.equal($.cwd, t3) + }) + + await $`pwd` + assert.ok((await $`pwd`).toString().trim().endsWith(t1)) + assert.equal($.cwd, t1) + assert.ok((await $`pwd`).toString().trim().endsWith(t1)) + + $.cwd = t3 + assert.ok((await $`pwd`).toString().trim().endsWith(t3)) + assert.equal($.cwd, t3) + + await w + }) + } })() console.log('smoke mjs: ok')