Skip to content

Commit 6de164d

Browse files
authored
test(core): enhance cwd related test for within (#877)
1 parent 8778fcc commit 6de164d

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/core.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ import {
4141
exitCodeInfo,
4242
formatCmd,
4343
getCallerLocation,
44+
isString,
4445
noop,
4546
parseDuration,
47+
preferNmBin,
4648
quote,
4749
quotePowerShell,
48-
preferNmBin,
4950
} from './util.js'
5051

5152
export interface Shell {
@@ -445,7 +446,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
445446
}
446447

447448
pipe(dest: Writable | ProcessPromise): ProcessPromise {
448-
if (typeof dest === 'string')
449+
if (isString(dest))
449450
throw new Error('The pipe() method does not take strings. Forgot $?')
450451
if (this._resolved) {
451452
if (dest instanceof ProcessPromise) dest.stdin.end() // In case of piped stdin, we may want to close stdin of dest as well.

test/core.test.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,17 +740,22 @@ describe('core', () => {
740740
await promise
741741
})
742742

743-
test('restores previous cwd', async () => {
743+
test('keeps the cwd ref for internal $ calls', async () => {
744744
let resolve, reject
745745
let promise = new Promise((...args) => ([resolve, reject] = args))
746-
746+
let cwd = process.cwd()
747747
let pwd = await $`pwd`
748748

749749
within(async () => {
750750
cd('/tmp')
751+
assert.ok(process.cwd().endsWith('/tmp'))
752+
assert.ok((await $`pwd`).stdout.trim().endsWith('/tmp'))
753+
751754
setTimeout(async () => {
755+
process.chdir('/')
752756
assert.ok((await $`pwd`).stdout.trim().endsWith('/tmp'))
753757
resolve()
758+
process.chdir(cwd)
754759
}, 1000)
755760
})
756761

0 commit comments

Comments
 (0)