Skip to content

Commit d969231

Browse files
committed
test: add $.cwdHook test
1 parent 5cf68b7 commit d969231

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/core.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import assert from 'node:assert'
1616
import { spawn, spawnSync, StdioNull, StdioPipe } from 'node:child_process'
17-
import { AsyncLocalStorage, createHook } from 'node:async_hooks'
17+
import { AsyncHook, AsyncLocalStorage, createHook } from 'node:async_hooks'
1818
import { Readable, Writable } from 'node:stream'
1919
import { inspect } from 'node:util'
2020
import {
@@ -76,14 +76,13 @@ export interface Options {
7676
}
7777

7878
const storage = new AsyncLocalStorage<Options>()
79-
const cwdHook = createHook({
79+
const cwdHook: AsyncHook & { enabled?: boolean } = createHook({
8080
init: syncCwd,
8181
before: syncCwd,
8282
promiseResolve: syncCwd,
8383
after: syncCwd,
8484
destroy: syncCwd,
8585
})
86-
cwdHook.enable()
8786

8887
export const defaults: Options = {
8988
[processCwd]: process.cwd(),
@@ -172,6 +171,7 @@ export const $: Shell & Options = new Proxy<Shell & Options>(
172171
{
173172
set(_, key, value) {
174173
if (key === 'cwdHook') {
174+
cwdHook.enabled = !!value
175175
if (value) cwdHook.enable()
176176
else cwdHook.disable()
177177
return true
@@ -184,13 +184,14 @@ export const $: Shell & Options = new Proxy<Shell & Options>(
184184
},
185185
get(_, key) {
186186
if (key === 'sync') return $({ sync: true })
187+
if (key === 'cwdHook') return cwdHook.enabled
187188

188189
const target = key in Function.prototype ? _ : getStore()
189190
return Reflect.get(target, key)
190191
},
191192
}
192193
)
193-
194+
$.cwdHook = true
194195
try {
195196
setupBash()
196197
} catch (err) {}

test/core.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,13 @@ describe('core', () => {
232232
assert.ok((await p) == 'foo')
233233
})
234234

235+
test('$.cwdHook is configurable', () => {
236+
$.cwdHook = false
237+
assert.equal($.cwdHook, false)
238+
$.cwdHook = true
239+
assert.equal($.cwdHook, true)
240+
})
241+
235242
test('cd() works with relative paths', async () => {
236243
let cwd = process.cwd()
237244
try {

0 commit comments

Comments
 (0)