Skip to content

Commit

Permalink
test(smoke): check ctx isolation (#959)
Browse files Browse the repository at this point in the history
* test(smoke): check ctx isolation

* revert: revert #956 to fix bun issue

#956
  • Loading branch information
antongolub authored Nov 28, 2024
1 parent b15179f commit b1ef206
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,25 +192,6 @@ export const $: Shell & Options = new Proxy<Shell & Options>(

type Resolve = (out: ProcessOutput) => void

export interface ProcessPromise extends Promise<ProcessOutput> {
then<R = ProcessOutput, E = ProcessOutput>(
onfulfilled?:
| ((value: ProcessOutput) => PromiseLike<R> | R)
| undefined
| null,
onrejected?:
| ((reason: ProcessOutput) => PromiseLike<E> | E)
| undefined
| null
): Promise<R | E>
catch<T = ProcessOutput>(
onrejected?:
| ((reason: ProcessOutput) => PromiseLike<T> | T)
| undefined
| null
): Promise<ProcessOutput | T>
}

export class ProcessPromise extends Promise<ProcessOutput> {
private _command = ''
private _from = ''
Expand Down Expand Up @@ -539,6 +520,29 @@ export class ProcessPromise extends Promise<ProcessOutput> {
return this._nothrow ?? this._snapshot.nothrow
}

// Promise API
then<R = ProcessOutput, E = ProcessOutput>(
onfulfilled?:
| ((value: ProcessOutput) => PromiseLike<R> | R)
| undefined
| null,
onrejected?:
| ((reason: ProcessOutput) => PromiseLike<E> | E)
| undefined
| null
): Promise<R | E> {
return super.then(onfulfilled, onrejected)
}

catch<T = ProcessOutput>(
onrejected?:
| ((reason: ProcessOutput) => PromiseLike<T> | T)
| undefined
| null
): Promise<ProcessOutput | T> {
return super.catch(onrejected)
}

// Stream-like API
private writable = true
private emit(event: string, ...args: any[]) {
Expand Down
30 changes: 30 additions & 0 deletions test/smoke/bun.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
})
})
31 changes: 31 additions & 0 deletions test/smoke/node.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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')

0 comments on commit b1ef206

Please sign in to comment.