Skip to content

Commit c38c431

Browse files
committed
switch to version that adapts based on os
1 parent a28c33e commit c38c431

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ describe('pathFind', function () {
4646
let previousPath: string
4747

4848
beforeEach(function () {
49-
previousPath = testutil.copyEnv().PATH!
49+
previousPath = testutil.getEnvPath(testutil.copyEnv())
5050
})
5151

5252
afterEach(function () {
53-
process.env.PATH = previousPath
53+
testutil.setEnvPath(previousPath)
5454
})
5555

5656
it('first tries ssh in $PATH (Non-Windows)', async function () {
@@ -61,7 +61,7 @@ describe('pathFind', function () {
6161
const workspace = await testutil.createTestWorkspaceFolder()
6262
const fakeSshPath = path.join(workspace.uri.fsPath, `ssh`)
6363

64-
process.env.PATH = workspace.uri.fsPath
64+
testutil.setEnvPath(workspace.uri.fsPath)
6565
const firstResult = await findSshPath(false)
6666

6767
await testutil.createExecutableFile(fakeSshPath, '')
@@ -80,7 +80,7 @@ describe('pathFind', function () {
8080
const workspace = await testutil.createTestWorkspaceFolder()
8181
const fakeSshPath = path.join(workspace.uri.fsPath, `ssh`)
8282

83-
process.env.PATH = workspace.uri.fsPath
83+
testutil.setEnvPath(workspace.uri.fsPath)
8484

8585
await testutil.createExecutableFile(fakeSshPath, '')
8686

@@ -99,7 +99,7 @@ describe('pathFind', function () {
9999
// We move the ssh to a temp directory temporarily to test if cache works.
100100
const fakeSshPath = path.join(workspace.uri.fsPath, `ssh`)
101101

102-
process.env.PATH = workspace.uri.fsPath
102+
testutil.setEnvPath(workspace.uri.fsPath)
103103

104104
await testutil.createExecutableFile(fakeSshPath, '')
105105

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55
import assert from 'assert'
6-
import { createExecutableFile, createTestWorkspaceFolder, copyEnv } from '../../testUtil'
6+
import { createExecutableFile, createTestWorkspaceFolder, copyEnv, setEnvPath, getEnvPath } from '../../testUtil'
77
import path from 'path'
88
import { fs } from '../../../shared'
99
import { isWin } from '../../../shared/vscode/env'
1010
import { tryRun } from '../../../shared/utilities/pathFind'
1111

1212
describe('setEnv', function () {
1313
it('modifies the node environment variables', function () {
14-
const originalPath = copyEnv().PATH
14+
const originalPath = getEnvPath(copyEnv())
1515
const fakePath = 'fakePath'
16-
process.env.PATH = fakePath
16+
setEnvPath(fakePath)
1717
assert.strictEqual(copyEnv().PATH, fakePath)
1818

19-
process.env.PATH = originalPath
19+
setEnvPath(originalPath)
2020
assert.strictEqual(copyEnv().PATH, originalPath)
2121
})
2222
})

packages/core/src/test/testUtil.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { mkdirSync, existsSync } from 'fs' // eslint-disable-line no-restricted-
1919
import { randomBytes } from 'crypto'
2020
import request from '../shared/request'
2121
import { stub } from 'sinon'
22+
import { isWin } from '../shared/vscode/env'
2223

2324
const testTempDirs: string[] = []
2425

@@ -634,3 +635,21 @@ export function getFetchStubWithResponse(response: Partial<Response>) {
634635
export function copyEnv(): NodeJS.ProcessEnv {
635636
return { ...process.env }
636637
}
638+
639+
/**
640+
* Returns the PATH env variable regardless of OS.
641+
* Appears that env.PATH doesn't work on widows and instead expects env.Path.
642+
* @param env NodeJs env
643+
* @returns PATH var (not a copy)
644+
*/
645+
export function getEnvPath(env: NodeJS.ProcessEnv): string {
646+
return (isWin() ? env.Path : env.PATH) || ''
647+
}
648+
649+
export function setEnvPath(newPath: string): void {
650+
if (isWin()) {
651+
process.env.Path = newPath
652+
} else {
653+
process.env.PATH = newPath
654+
}
655+
}

0 commit comments

Comments
 (0)