Commit b1ef206

Anton Golub <antongolub@antongolub.com>
2024-11-28 21:27:02
test(smoke): check ctx isolation (#959)
* test(smoke): check ctx isolation * revert: revert #956 to fix bun issue https://github.com/google/zx/pull/956
1 parent b15179f
Changed files (3)
src/core.ts
@@ -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 = ''
@@ -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[]) {
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
+    })
+  })
 })
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')