Skip to content

Commit 36c133b

Browse files
committed
write tests that do not stub fs
1 parent 4071645 commit 36c133b

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export async function findSshPath(useCache: boolean = true): Promise<string | un
106106
continue
107107
}
108108
if (await tryRun(p, ['-G', 'x'], 'noresult' /* "ssh -G" prints quasi-sensitive info. */)) {
109-
sshPath = p
109+
sshPath = useCache ? p : sshPath
110110
return p
111111
}
112112
}

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

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as os from 'os'
99
import * as path from 'path'
1010
import * as testutil from '../../testUtil'
1111
import { fs } from '../../../shared'
12-
import { findTypescriptCompiler, getVscodeCliPath } from '../../../shared/utilities/pathFind'
12+
import { findSshPath, findTypescriptCompiler, getVscodeCliPath } from '../../../shared/utilities/pathFind'
1313

1414
function isWin(): boolean {
1515
return process.platform === 'win32'
@@ -45,7 +45,49 @@ describe('pathFind', function () {
4545
assert.ok(regex.test(vscPath), `expected regex ${regex} to match: "${vscPath}"`)
4646
})
4747

48-
// describe('findSshPath', function () {
48+
describe('findSshPath', function () {
49+
it('first tries ssh in $PATH', async function () {
50+
const workspace = await testutil.createTestWorkspaceFolder()
51+
const fakeSshPath = path.join(workspace.uri.fsPath, `ssh${isWin() ? '.cmd' : ''}`)
4952

50-
// })
53+
await testutil.withEnvPath(workspace.uri.fsPath, async () => {
54+
const firstResult = await findSshPath(false)
55+
56+
await testutil.createExecutableFile(fakeSshPath, 'echo "this is ssh"')
57+
58+
const secondResult = await findSshPath(false)
59+
60+
assert.notStrictEqual(firstResult, secondResult)
61+
assert.strictEqual(secondResult, 'ssh')
62+
})
63+
})
64+
65+
it('only returns executable ssh path', async function () {
66+
const workspace = await testutil.createTestWorkspaceFolder()
67+
const fakeSshPath = path.join(workspace.uri.fsPath, `ssh${isWin() ? '.cmd' : ''}`)
68+
await fs.writeFile(fakeSshPath, 'this is not executable')
69+
70+
await testutil.withEnvPath(workspace.uri.fsPath, async () => {
71+
const firstResult = await findSshPath(false)
72+
assert.notStrictEqual(firstResult, 'ssh')
73+
})
74+
})
75+
76+
it('caches result from previous runs', async function () {
77+
const workspace = await testutil.createTestWorkspaceFolder()
78+
const fakeSshPath = path.join(workspace.uri.fsPath, `ssh${isWin() ? '.cmd' : ''}`)
79+
await testutil.createExecutableFile(fakeSshPath, 'echo "this is ssh"')
80+
81+
await testutil.withEnvPath(workspace.uri.fsPath, async () => {
82+
const firstResult = await findSshPath(true)
83+
84+
await fs.delete(fakeSshPath)
85+
86+
const secondResult = await findSshPath(true)
87+
88+
assert.strictEqual(firstResult, secondResult)
89+
assert.strictEqual(secondResult, 'ssh')
90+
})
91+
})
92+
})
5193
})

packages/core/src/test/testUtil.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,3 +614,21 @@ export function tryRegister(command: DeclaredCommand<() => Promise<any>>) {
614614
export function getFetchStubWithResponse(response: Partial<Response>) {
615615
return stub(request, 'fetch').returns({ response: new Promise((res, _) => res(response)) } as any)
616616
}
617+
618+
function setPath(newPath: string): void {
619+
process.env.PATH = newPath
620+
}
621+
622+
function readPath(): string {
623+
return process.env.PATH || ''
624+
}
625+
626+
export async function withEnvPath(newPath: string, task: () => Promise<void>): Promise<void | never> {
627+
const originalPath = readPath()
628+
setPath(newPath)
629+
try {
630+
await task()
631+
} finally {
632+
setPath(originalPath)
633+
}
634+
}

0 commit comments

Comments
 (0)