Skip to content

Commit

Permalink
test(core): enhance cwd related test fot within
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Aug 25, 2024
1 parent 8778fcc commit 301f571
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ import {
exitCodeInfo,
formatCmd,
getCallerLocation,
isString,
noop,
parseDuration,
preferNmBin,
quote,
quotePowerShell,
preferNmBin,
} from './util.js'

export interface Shell {
Expand Down Expand Up @@ -283,7 +284,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
cwd: $.cwd ?? $[processCwd],
ac: $.ac,
signal: $.signal,
shell: typeof $.shell === 'string' ? $.shell : true,
shell: isString($.shell) ? $.shell : true,

Check failure on line 287 in src/core.ts

View workflow job for this annotation

GitHub Actions / build

Type 'string | boolean' is not assignable to type 'string | true | undefined'.
env: $.env,
spawn: $.spawn,
spawnSync: $.spawnSync,
Expand Down Expand Up @@ -445,7 +446,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
}

pipe(dest: Writable | ProcessPromise): ProcessPromise {
if (typeof dest === 'string')
if (isString(dest))
throw new Error('The pipe() method does not take strings. Forgot $?')
if (this._resolved) {
if (dest instanceof ProcessPromise) dest.stdin.end() // In case of piped stdin, we may want to close stdin of dest as well.
Expand Down
11 changes: 9 additions & 2 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -740,22 +740,29 @@ describe('core', () => {
await promise
})

test('restores previous cwd', async () => {
test('keeps the cwd ref for internal $ calls', async () => {
let resolve, reject
let promise = new Promise((...args) => ([resolve, reject] = args))

let cwd = process.cwd()
let pwd = await $`pwd`

within(async () => {
cd('/tmp')
assert.ok(process.cwd().endsWith('/tmp'))
assert.ok((await $`pwd`).stdout.trim().endsWith('/tmp'))

setTimeout(async () => {
assert.equal(process.cwd(), '/')
assert.ok((await $`pwd`).stdout.trim().endsWith('/tmp'))
resolve()
}, 1000)
})

setTimeout(async () => cd('/'), 500)

assert.equal((await $`pwd`).stdout, pwd.stdout)
await promise
assert.equal((await $`pwd`).stdout.trim(), '/')
})

test(`isolates nested context and returns cb result`, async () => {
Expand Down

0 comments on commit 301f571

Please sign in to comment.