Skip to content

Commit c2e17bd

Browse files
committed
add tests for path utility;
1 parent a92ba99 commit c2e17bd

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
import assert from 'assert'
6+
import { createTestWorkspaceFolder, readEnvPath, withEnvPath } from '../../testUtil'
7+
8+
describe('withEnvPath', function () {
9+
it('resets path when error in task', async function () {
10+
const originalPath = readEnvPath()
11+
const tempFolder = await createTestWorkspaceFolder()
12+
try {
13+
await withEnvPath(tempFolder.uri.fsPath, async () => {
14+
throw new Error()
15+
})
16+
} catch {}
17+
assert.strictEqual(readEnvPath(), originalPath)
18+
})
19+
20+
it('changes $PATH temporarily', async function () {
21+
const originalPath = readEnvPath()
22+
const tempFolder = await createTestWorkspaceFolder()
23+
await withEnvPath(tempFolder.uri.fsPath, async () => {
24+
assert.strictEqual(readEnvPath(), tempFolder.uri.fsPath)
25+
})
26+
assert.strictEqual(readEnvPath(), originalPath)
27+
})
28+
})

packages/core/src/test/testUtil.ts

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

618-
function setPath(newPath: string): void {
618+
export function setEnvPath(newPath: string): void {
619619
process.env.PATH = newPath
620620
}
621621

622-
function readPath(): string {
622+
export function readEnvPath(): string {
623623
return process.env.PATH || ''
624624
}
625625
/**
@@ -628,11 +628,11 @@ function readPath(): string {
628628
* @param task work to be done with $PATH set.
629629
*/
630630
export async function withEnvPath(newPath: string, task: () => Promise<void>): Promise<void | never> {
631-
const originalPath = readPath()
632-
setPath(newPath)
631+
const originalPath = readEnvPath()
632+
setEnvPath(newPath)
633633
try {
634634
await task()
635635
} finally {
636-
setPath(originalPath)
636+
setEnvPath(originalPath)
637637
}
638638
}

0 commit comments

Comments
 (0)