Skip to content

Commit b2890bd

Browse files
committed
add more testing
1 parent d6c052b commit b2890bd

File tree

3 files changed

+75
-45
lines changed

3 files changed

+75
-45
lines changed

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

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,16 @@
66
import * as vscode from 'vscode'
77
import * as path from 'path'
88
import fs from '../../shared/fs/fs'
9-
import { ChildProcess, ChildProcessOptions } from './processUtils'
109
import { GitExtension } from '../extensions/git'
1110
import { Settings } from '../settings'
12-
import { getLogger } from '../logger/logger'
11+
import * as processUtils from './processUtils'
1312

1413
/** Full path to VSCode CLI. */
1514
let vscPath: string
1615
let sshPath: string
1716
let gitPath: string
1817
let bashPath: string
1918

20-
/**
21-
* Tries to execute a program at path `p` with the given args and
22-
* optionally checks the output for `expected`.
23-
*
24-
* @param p path to a program to execute
25-
* @param args program args
26-
* @param doLog log failures
27-
* @param expected output must contain this string
28-
*/
29-
export async function tryRun(
30-
p: string,
31-
args: string[],
32-
logging: 'yes' | 'no' | 'noresult' = 'yes',
33-
expected?: string,
34-
opt?: ChildProcessOptions
35-
): Promise<boolean> {
36-
const proc = new ChildProcess(p, args, { logging: 'no' })
37-
const r = await proc.run(opt)
38-
const ok = r.exitCode === 0 && (expected === undefined || r.stdout.includes(expected))
39-
if (logging === 'noresult') {
40-
getLogger().info('tryRun: %s: %s', ok ? 'ok' : 'failed', proc)
41-
} else if (logging !== 'no') {
42-
getLogger().info('tryRun: %s: %s %O', ok ? 'ok' : 'failed', proc, proc.result())
43-
}
44-
return ok
45-
}
46-
4719
/**
4820
* Gets the fullpath to `code` (VSCode CLI), or falls back to "code" (not
4921
* absolute) if it works.
@@ -78,7 +50,7 @@ export async function getVscodeCliPath(): Promise<string | undefined> {
7850
if (!vsc || (vsc !== 'code' && !(await fs.exists(vsc)))) {
7951
continue
8052
}
81-
if (await tryRun(vsc, ['--version'])) {
53+
if (await processUtils.tryRun(vsc, ['--version'])) {
8254
vscPath = vsc
8355
return vsc
8456
}
@@ -103,7 +75,7 @@ export async function findTypescriptCompiler(): Promise<string | undefined> {
10375

10476
for (const tsc of tscPaths) {
10577
// Try to run "tsc -v".
106-
if (await tryRun(tsc, ['-v'], 'yes', 'Version')) {
78+
if (await processUtils.tryRun(tsc, ['-v'], 'yes', 'Version')) {
10779
return tsc
10880
}
10981
}
@@ -115,8 +87,8 @@ export async function findTypescriptCompiler(): Promise<string | undefined> {
11587
* Gets the configured `ssh` path, or falls back to "ssh" (not absolute),
11688
* or tries common locations, or returns undefined.
11789
*/
118-
export async function findSshPath(): Promise<string | undefined> {
119-
if (sshPath !== undefined) {
90+
export async function findSshPath(useCache: boolean = true): Promise<string | undefined> {
91+
if (useCache && sshPath !== undefined) {
12092
return sshPath
12193
}
12294

@@ -132,7 +104,7 @@ export async function findSshPath(): Promise<string | undefined> {
132104
if (!p || ('ssh' !== p && !(await fs.exists(p)))) {
133105
continue
134106
}
135-
if (await tryRun(p, ['-G', 'x'], 'noresult' /* "ssh -G" prints quasi-sensitive info. */)) {
107+
if (await processUtils.tryRun(p, ['-G', 'x'], 'noresult' /* "ssh -G" prints quasi-sensitive info. */)) {
136108
sshPath = p
137109
return p
138110
}
@@ -154,7 +126,7 @@ export async function findGitPath(): Promise<string | undefined> {
154126
if (!p || ('git' !== p && !(await fs.exists(p)))) {
155127
continue
156128
}
157-
if (await tryRun(p, ['--version'])) {
129+
if (await processUtils.tryRun(p, ['--version'])) {
158130
gitPath = p
159131
return p
160132
}
@@ -174,7 +146,7 @@ export async function findBashPath(): Promise<string | undefined> {
174146
if (!p || ('bash' !== p && !(await fs.exists(p)))) {
175147
continue
176148
}
177-
if (await tryRun(p, ['--version'])) {
149+
if (await processUtils.tryRun(p, ['--version'])) {
178150
bashPath = p
179151
return p
180152
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as proc from 'child_process'
77
import * as crossSpawn from 'cross-spawn'
88
import * as logger from '../logger'
99
import { Timeout, CancellationError, waitUntil } from './timeoutUtils'
10+
import { getLogger } from '..'
1011

1112
export interface RunParameterContext {
1213
/** Reports an error parsed from the stdin/stdout streams. */
@@ -368,3 +369,30 @@ export class ChildProcess {
368369
return `${pid} [${this.#command} ${noparams ? '...' : this.#args.join(' ')}]`
369370
}
370371
}
372+
373+
/**
374+
* Tries to execute a program at path `p` with the given args and
375+
* optionally checks the output for `expected`.
376+
*
377+
* @param p path to a program to execute
378+
* @param args program args
379+
* @param doLog log failures
380+
* @param expected output must contain this string
381+
*/
382+
export async function tryRun(
383+
p: string,
384+
args: string[],
385+
logging: 'yes' | 'no' | 'noresult' = 'yes',
386+
expected?: string,
387+
opt?: ChildProcessOptions
388+
): Promise<boolean> {
389+
const proc = new ChildProcess(p, args, { logging: 'no' })
390+
const r = await proc.run(opt)
391+
const ok = r.exitCode === 0 && (expected === undefined || r.stdout.includes(expected))
392+
if (logging === 'noresult') {
393+
getLogger().info('tryRun: %s: %s', ok ? 'ok' : 'failed', proc)
394+
} else if (logging !== 'no') {
395+
getLogger().info('tryRun: %s: %s %O', ok ? 'ok' : 'failed', proc, proc.result())
396+
}
397+
return ok
398+
}

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

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
*/
55

66
import assert from 'assert'
7+
import sinon from 'sinon'
78
import * as vscode from 'vscode'
89
import * as os from 'os'
910
import * as path from 'path'
1011
import * as testutil from '../../testUtil'
1112
import { fs } from '../../../shared'
12-
import { findTypescriptCompiler, getVscodeCliPath } from '../../../shared/utilities/pathFind'
13+
import { findSshPath, findTypescriptCompiler, getVscodeCliPath } from '../../../shared/utilities/pathFind'
14+
import * as processUtils from '../../../shared/utilities/processUtils'
1315

1416
describe('pathFind', function () {
1517
it('findTypescriptCompiler()', async function () {
@@ -42,12 +44,40 @@ describe('pathFind', function () {
4244
assert.ok(regex.test(vscPath), `expected regex ${regex} to match: "${vscPath}"`)
4345
})
4446

45-
// it('does a dry run of ssh before returning it', async function () {
46-
// const tryRunStub = sinon.stub(PathFinder, 'tryRun')
47-
// tryRunStub.resolves(true)
48-
// const path = await pathFinder.findSshPath()
49-
// assert.ok(path)
50-
// assert.ok(tryRunStub.calledOnce)
51-
// assert.ok(tryRunStub.calledWith(path))
52-
// })
47+
describe('findSshPath', function () {
48+
let tryRunStub: sinon.SinonStub
49+
before(function () {
50+
tryRunStub = sinon.stub(processUtils, 'tryRun')
51+
})
52+
53+
after(function () {
54+
tryRunStub.restore()
55+
})
56+
57+
it('first tries $PATH', async function () {
58+
tryRunStub.onFirstCall().resolves(true)
59+
60+
const result = await findSshPath(false)
61+
assert.ok(result)
62+
testutil.assertEqualPaths(result, 'ssh')
63+
tryRunStub.resetHistory()
64+
})
65+
66+
it('if $PATH fails, tries /usr/bin/ssh', async function () {
67+
tryRunStub.onFirstCall().resolves(false)
68+
tryRunStub.onSecondCall().resolves(true)
69+
70+
const result = await findSshPath(false)
71+
assert.ok(result)
72+
testutil.assertEqualPaths(result, '/usr/bin/ssh')
73+
tryRunStub.resetHistory()
74+
})
75+
76+
it('dry runs the resulting ssh', async function () {
77+
tryRunStub.onFirstCall().resolves(true)
78+
await findSshPath(false)
79+
assert.ok(tryRunStub.calledOnce)
80+
tryRunStub.resetHistory()
81+
})
82+
})
5383
})

0 commit comments

Comments
 (0)