Skip to content

Commit 60c2bed

Browse files
committed
refactor
1 parent 4cb7b3c commit 60c2bed

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

packages/core/src/test/shared/utilities/pathFind.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('pathFind', function () {
4747
const workspace = await testutil.createTestWorkspaceFolder()
4848
const fakeSshPath = path.join(workspace.uri.fsPath, `ssh${isWin() ? '.cmd' : ''}`)
4949

50-
await testutil.withEnvPath(workspace.uri.fsPath, async () => {
50+
await testutil.withEnv(testutil.envWithNewPath(workspace.uri.fsPath), async () => {
5151
const firstResult = await findSshPath(false)
5252

5353
await testutil.createExecutableFile(fakeSshPath, 'echo "this is ssh"')
@@ -64,7 +64,7 @@ describe('pathFind', function () {
6464
const fakeSshPath = path.join(workspace.uri.fsPath, `ssh${isWin() ? '.cmd' : ''}`)
6565
await fs.writeFile(fakeSshPath, 'this is not executable')
6666

67-
await testutil.withEnvPath(workspace.uri.fsPath, async () => {
67+
await testutil.withEnv(testutil.envWithNewPath(workspace.uri.fsPath), async () => {
6868
const firstResult = await findSshPath(false)
6969
assert.notStrictEqual(firstResult, 'ssh')
7070
})
@@ -75,7 +75,7 @@ describe('pathFind', function () {
7575
const fakeSshPath = path.join(workspace.uri.fsPath, `ssh${isWin() ? '.cmd' : ''}`)
7676
await testutil.createExecutableFile(fakeSshPath, 'echo "this is ssh"')
7777

78-
await testutil.withEnvPath(workspace.uri.fsPath, async () => {
78+
await testutil.withEnv(testutil.envWithNewPath(workspace.uri.fsPath), async () => {
7979
const firstResult = await findSshPath(true)
8080

8181
await fs.delete(fakeSshPath)

packages/core/src/test/shared/utilities/testUtil.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,26 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55
import assert from 'assert'
6-
import { createTestWorkspaceFolder, readEnvPath, withEnvPath } from '../../testUtil'
6+
import { createTestWorkspaceFolder, envWithNewPath, readEnv, withEnv } from '../../testUtil'
77

8-
describe('withEnvPath', function () {
8+
describe('withEnv', function () {
99
it('resets path when error in task', async function () {
10-
const originalPath = readEnvPath()
10+
const originalEnv = readEnv()
1111
const tempFolder = await createTestWorkspaceFolder()
1212
try {
13-
await withEnvPath(tempFolder.uri.fsPath, async () => {
13+
await withEnv(envWithNewPath(tempFolder.uri.fsPath), async () => {
1414
throw new Error()
1515
})
1616
} catch {}
17-
assert.strictEqual(readEnvPath(), originalPath)
17+
assert.strictEqual(readEnv().PATH, originalEnv.PATH)
1818
})
1919

2020
it('changes $PATH temporarily', async function () {
21-
const originalPath = readEnvPath()
21+
const originalEnv = readEnv()
2222
const tempFolder = await createTestWorkspaceFolder()
23-
await withEnvPath(tempFolder.uri.fsPath, async () => {
24-
assert.strictEqual(readEnvPath(), tempFolder.uri.fsPath)
23+
await withEnv(envWithNewPath(tempFolder.uri.fsPath), async () => {
24+
assert.strictEqual(readEnv().PATH, tempFolder.uri.fsPath)
2525
})
26-
assert.strictEqual(readEnvPath(), originalPath)
26+
assert.strictEqual(readEnv().PATH, originalEnv.PATH)
2727
})
2828
})

packages/core/src/test/testUtil.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -615,24 +615,28 @@ export function getFetchStubWithResponse(response: Partial<Response>) {
615615
return stub(request, 'fetch').returns({ response: new Promise((res, _) => res(response)) } as any)
616616
}
617617

618-
export function setEnvPath(newPath: string): void {
619-
process.env.PATH = newPath
618+
export function setEnv(newEnv: NodeJS.ProcessEnv): void {
619+
process.env = newEnv
620620
}
621621

622-
export function readEnvPath(): string {
623-
return process.env.PATH || ''
622+
export function readEnv(): NodeJS.ProcessEnv {
623+
return process.env
624624
}
625625
/**
626626
* Overwrite the $PATH environment variable for the span of the provided task, then reset it.
627627
* @param newPath temporary $PATH value
628628
* @param task work to be done with $PATH set.
629629
*/
630-
export async function withEnvPath(newPath: string, task: () => Promise<void>): Promise<void | never> {
631-
const originalPath = readEnvPath()
632-
setEnvPath(newPath)
630+
export async function withEnv(newEnv: NodeJS.ProcessEnv, task: () => Promise<void>): Promise<void | never> {
631+
const originalEnv = readEnv()
632+
setEnv(newEnv)
633633
try {
634634
await task()
635635
} finally {
636-
setEnvPath(originalPath)
636+
setEnv(originalEnv)
637637
}
638638
}
639+
640+
export function envWithNewPath(newPath: string): NodeJS.ProcessEnv {
641+
return { ...process.env, PATH: newPath }
642+
}

0 commit comments

Comments
 (0)