File tree 3 files changed +84
-19
lines changed
3 files changed +84
-19
lines changed Original file line number Diff line number Diff line change @@ -192,25 +192,6 @@ export const $: Shell & Options = new Proxy<Shell & Options>(
192
192
193
193
type Resolve = ( out : ProcessOutput ) = > void
194
194
195
- export interface ProcessPromise extends Promise < ProcessOutput > {
196
- then < R = ProcessOutput , E = ProcessOutput > (
197
- onfulfilled ?:
198
- | ( ( value : ProcessOutput ) => PromiseLike < R > | R )
199
- | undefined
200
- | null ,
201
- onrejected ?:
202
- | ( ( reason : ProcessOutput ) => PromiseLike < E > | E )
203
- | undefined
204
- | null
205
- ) : Promise < R | E >
206
- catch < T = ProcessOutput > (
207
- onrejected ?:
208
- | ( ( reason : ProcessOutput ) => PromiseLike < T > | T )
209
- | undefined
210
- | null
211
- ) : Promise < ProcessOutput | T >
212
- }
213
-
214
195
export class ProcessPromise extends Promise < ProcessOutput > {
215
196
private _command = ''
216
197
private _from = ''
@@ -539,6 +520,29 @@ export class ProcessPromise extends Promise<ProcessOutput> {
539
520
return this . _nothrow ?? this . _snapshot . nothrow
540
521
}
541
522
523
+ // Promise API
524
+ then < R = ProcessOutput , E = ProcessOutput > (
525
+ onfulfilled ?:
526
+ | ( ( value : ProcessOutput ) => PromiseLike < R > | R )
527
+ | undefined
528
+ | null ,
529
+ onrejected ?:
530
+ | ( ( reason : ProcessOutput ) => PromiseLike < E > | E )
531
+ | undefined
532
+ | null
533
+ ) : Promise < R | E > {
534
+ return super . then ( onfulfilled , onrejected )
535
+ }
536
+
537
+ catch < T = ProcessOutput > (
538
+ onrejected ?:
539
+ | ( ( reason : ProcessOutput ) => PromiseLike < T > | T )
540
+ | undefined
541
+ | null
542
+ ) : Promise < ProcessOutput | T > {
543
+ return super . catch ( onrejected )
544
+ }
545
+
542
546
// Stream-like API
543
547
private writable = true
544
548
private emit ( event : string , ...args : any [ ] ) {
Original file line number Diff line number Diff line change @@ -30,4 +30,34 @@ describe('bun', () => {
30
30
test ( 'stdio: inherit' , async ( ) => {
31
31
await $ ( { stdio : 'inherit' } ) `ls`
32
32
} )
33
+
34
+ test ( 'ctx isolation' , async ( ) => {
35
+ await within ( async ( ) => {
36
+ const t1 = tmpdir ( )
37
+ const t3 = tmpdir ( )
38
+ $ . cwd = t1
39
+ assert . equal ( $ . cwd , t1 )
40
+ assert . equal ( $ . cwd , t1 )
41
+
42
+ const w = within ( async ( ) => {
43
+ const t3 = tmpdir ( )
44
+ $ . cwd = t3
45
+ assert . equal ( $ . cwd , t3 )
46
+
47
+ assert . ok ( ( await $ `pwd` ) . toString ( ) . trim ( ) . endsWith ( t3 ) )
48
+ assert . equal ( $ . cwd , t3 )
49
+ } )
50
+
51
+ await $ `pwd`
52
+ assert . ok ( ( await $ `pwd` ) . toString ( ) . trim ( ) . endsWith ( t1 ) )
53
+ assert . equal ( $ . cwd , t1 )
54
+ assert . ok ( ( await $ `pwd` ) . toString ( ) . trim ( ) . endsWith ( t1 ) )
55
+
56
+ $ . cwd = t3
57
+ assert . ok ( ( await $ `pwd` ) . toString ( ) . trim ( ) . endsWith ( t3 ) )
58
+ assert . equal ( $ . cwd , t3 )
59
+
60
+ await w
61
+ } )
62
+ } )
33
63
} )
Original file line number Diff line number Diff line change @@ -26,6 +26,37 @@ import 'zx/globals'
26
26
const p = await $ ( { nothrow : true } ) `echo foo; exit 3`
27
27
assert . match ( p . message , / e x i t c o d e : 3 / )
28
28
}
29
+
30
+ // ctx isolation
31
+ {
32
+ await within ( async ( ) => {
33
+ const t1 = tmpdir ( )
34
+ const t3 = tmpdir ( )
35
+ $ . cwd = t1
36
+ assert . equal ( $ . cwd , t1 )
37
+ assert . equal ( $ . cwd , t1 )
38
+
39
+ const w = within ( async ( ) => {
40
+ const t3 = tmpdir ( )
41
+ $ . cwd = t3
42
+ assert . equal ( $ . cwd , t3 )
43
+
44
+ assert . ok ( ( await $ `pwd` ) . toString ( ) . trim ( ) . endsWith ( t3 ) )
45
+ assert . equal ( $ . cwd , t3 )
46
+ } )
47
+
48
+ await $ `pwd`
49
+ assert . ok ( ( await $ `pwd` ) . toString ( ) . trim ( ) . endsWith ( t1 ) )
50
+ assert . equal ( $ . cwd , t1 )
51
+ assert . ok ( ( await $ `pwd` ) . toString ( ) . trim ( ) . endsWith ( t1 ) )
52
+
53
+ $ . cwd = t3
54
+ assert . ok ( ( await $ `pwd` ) . toString ( ) . trim ( ) . endsWith ( t3 ) )
55
+ assert . equal ( $ . cwd , t3 )
56
+
57
+ await w
58
+ } )
59
+ }
29
60
} ) ( )
30
61
31
62
console . log ( 'smoke mjs: ok' )
You can’t perform that action at this time.
0 commit comments