From 6de164d003c2c82ecd39860d181cc62cd0022b42 Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Mon, 26 Aug 2024 10:18:51 +0300 Subject: [PATCH] test(core): enhance cwd related test for `within` (#877) --- src/core.ts | 5 +++-- test/core.test.js | 9 +++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/core.ts b/src/core.ts index 7352777b00..44d4a4d21d 100644 --- a/src/core.ts +++ b/src/core.ts @@ -41,11 +41,12 @@ import { exitCodeInfo, formatCmd, getCallerLocation, + isString, noop, parseDuration, + preferNmBin, quote, quotePowerShell, - preferNmBin, } from './util.js' export interface Shell { @@ -445,7 +446,7 @@ export class ProcessPromise extends Promise { } 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. diff --git a/test/core.test.js b/test/core.test.js index 866eac36a1..3658719d16 100644 --- a/test/core.test.js +++ b/test/core.test.js @@ -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) })