Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(core): enhance cwd related test for within #877

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 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 @@ -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
9 changes: 7 additions & 2 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -740,17 +740,22 @@ 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 () => {
process.chdir('/')
assert.ok((await $`pwd`).stdout.trim().endsWith('/tmp'))
resolve()
process.chdir(cwd)
}, 1000)
})

Expand Down