Skip to content

Commit 4071645

Browse files
committed
move tryRun back
1 parent 8a1131d commit 4071645

File tree

6 files changed

+40
-86
lines changed

6 files changed

+40
-86
lines changed

packages/core/src/awsService/ec2/sshKeyPair.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
import { fs } from '../../shared'
66
import { ToolkitError } from '../../shared/errors'
7-
import { tryRun } from '../../shared/utilities/programUtils'
7+
import { tryRun } from '../../shared/utilities/pathFind'
88
import { Timeout } from '../../shared/utilities/timeoutUtils'
99
import { findAsync } from '../../shared/utilities/collectionUtils'
1010
import { RunParameterContext } from '../../shared/utilities/processUtils'

packages/core/src/codewhisperer/commands/basicCommands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import { isBuilderIdConnection } from '../../auth/connection'
4343
import globals from '../../shared/extensionGlobals'
4444
import { getVscodeCliPath } from '../../shared/utilities/pathFind'
4545
import { setContext } from '../../shared/vscode/setContext'
46-
import { tryRun } from '../../shared/utilities/programUtils'
46+
import { tryRun } from '../../shared/utilities/pathFind'
4747

4848
const MessageTimeOut = 5_000
4949

packages/core/src/shared/sam/cli/samCliLocator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { getLogger, Logger } from '../../logger'
1010
import { SamCliInfoInvocation } from './samCliInfo'
1111
import { DefaultSamCliValidator, SamCliValidatorContext, SamCliVersionValidation } from './samCliValidator'
1212
import { PerfLog } from '../../logger/perfLogger'
13-
import { tryRun } from '../../utilities/programUtils'
13+
import { tryRun } from '../../utilities/pathFind'
1414

1515
export class SamCliLocationProvider {
1616
private static samCliLocator: BaseSamCliLocator | undefined

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

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import * as path from 'path'
88
import fs from '../../shared/fs/fs'
99
import { GitExtension } from '../extensions/git'
1010
import { Settings } from '../settings'
11-
import * as programUtils from './programUtils'
11+
import { ChildProcess, ChildProcessOptions } from './processUtils'
12+
import { getLogger } from '..'
1213

1314
/** Full path to VSCode CLI. */
1415
let vscPath: string
@@ -50,7 +51,7 @@ export async function getVscodeCliPath(): Promise<string | undefined> {
5051
if (!vsc || (vsc !== 'code' && !(await fs.exists(vsc)))) {
5152
continue
5253
}
53-
if (await programUtils.tryRun(vsc, ['--version'])) {
54+
if (await tryRun(vsc, ['--version'])) {
5455
vscPath = vsc
5556
return vsc
5657
}
@@ -75,7 +76,7 @@ export async function findTypescriptCompiler(): Promise<string | undefined> {
7576

7677
for (const tsc of tscPaths) {
7778
// Try to run "tsc -v".
78-
if (await programUtils.tryRun(tsc, ['-v'], 'yes', 'Version')) {
79+
if (await tryRun(tsc, ['-v'], 'yes', 'Version')) {
7980
return tsc
8081
}
8182
}
@@ -104,7 +105,7 @@ export async function findSshPath(useCache: boolean = true): Promise<string | un
104105
if (!p || ('ssh' !== p && !(await fs.exists(p)))) {
105106
continue
106107
}
107-
if (await programUtils.tryRun(p, ['-G', 'x'], 'noresult' /* "ssh -G" prints quasi-sensitive info. */)) {
108+
if (await tryRun(p, ['-G', 'x'], 'noresult' /* "ssh -G" prints quasi-sensitive info. */)) {
108109
sshPath = p
109110
return p
110111
}
@@ -126,7 +127,7 @@ export async function findGitPath(): Promise<string | undefined> {
126127
if (!p || ('git' !== p && !(await fs.exists(p)))) {
127128
continue
128129
}
129-
if (await programUtils.tryRun(p, ['--version'])) {
130+
if (await tryRun(p, ['--version'])) {
130131
gitPath = p
131132
return p
132133
}
@@ -146,9 +147,36 @@ export async function findBashPath(): Promise<string | undefined> {
146147
if (!p || ('bash' !== p && !(await fs.exists(p)))) {
147148
continue
148149
}
149-
if (await programUtils.tryRun(p, ['--version'])) {
150+
if (await tryRun(p, ['--version'])) {
150151
bashPath = p
151152
return p
152153
}
153154
}
154155
}
156+
157+
/**
158+
* Tries to execute a program at path `p` with the given args and
159+
* optionally checks the output for `expected`.
160+
*
161+
* @param p path to a program to execute
162+
* @param args program args
163+
* @param doLog log failures
164+
* @param expected output must contain this string
165+
*/
166+
export async function tryRun(
167+
p: string,
168+
args: string[],
169+
logging: 'yes' | 'no' | 'noresult' = 'yes',
170+
expected?: string,
171+
opt?: ChildProcessOptions
172+
): Promise<boolean> {
173+
const proc = new ChildProcess(p, args, { logging: 'no' })
174+
const r = await proc.run(opt)
175+
const ok = r.exitCode === 0 && (expected === undefined || r.stdout.includes(expected))
176+
if (logging === 'noresult') {
177+
getLogger().info('tryRun: %s: %s', ok ? 'ok' : 'failed', proc)
178+
} else if (logging !== 'no') {
179+
getLogger().info('tryRun: %s: %s %O', ok ? 'ok' : 'failed', proc, proc.result())
180+
}
181+
return ok
182+
}

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

Lines changed: 0 additions & 34 deletions
This file was deleted.

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

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

66
import assert from 'assert'
7-
import sinon from 'sinon'
87
import * as vscode from 'vscode'
98
import * as os from 'os'
109
import * as path from 'path'
1110
import * as testutil from '../../testUtil'
1211
import { fs } from '../../../shared'
13-
import { findSshPath, findTypescriptCompiler, getVscodeCliPath } from '../../../shared/utilities/pathFind'
14-
import * as programUtils from '../../../shared/utilities/programUtils'
12+
import { findTypescriptCompiler, getVscodeCliPath } from '../../../shared/utilities/pathFind'
1513

1614
function isWin(): boolean {
1715
return process.platform === 'win32'
@@ -47,45 +45,7 @@ describe('pathFind', function () {
4745
assert.ok(regex.test(vscPath), `expected regex ${regex} to match: "${vscPath}"`)
4846
})
4947

50-
describe('findSshPath', function () {
51-
let tryRunStub: sinon.SinonStub
52-
// On Windows the first call to tryRun gives a valid path, which changes func behavior.
53-
// We can offset our call checks based on this.
54-
let callOffset: number
48+
// describe('findSshPath', function () {
5549

56-
before(function () {
57-
tryRunStub = sinon.stub(programUtils, 'tryRun')
58-
callOffset = isWin() ? 1 : 0
59-
})
60-
61-
after(function () {
62-
tryRunStub.restore()
63-
})
64-
65-
it('first tries $PATH', async function () {
66-
tryRunStub.onCall(callOffset).resolves(true)
67-
68-
const result = await findSshPath(false)
69-
assert.ok(result)
70-
testutil.assertEqualPaths(result, 'ssh')
71-
tryRunStub.resetHistory()
72-
})
73-
74-
it('if $PATH fails, tries /usr/bin/ssh', async function () {
75-
tryRunStub.onCall(callOffset).resolves(false)
76-
tryRunStub.onCall(callOffset + 1).resolves(true)
77-
78-
const result = await findSshPath(false)
79-
assert.ok(result)
80-
testutil.assertEqualPaths(result, '/usr/bin/ssh')
81-
tryRunStub.resetHistory()
82-
})
83-
84-
it('dry runs the resulting ssh', async function () {
85-
tryRunStub.onCall(callOffset).resolves(true)
86-
await findSshPath(false)
87-
assert.ok(tryRunStub.calledOnce)
88-
tryRunStub.resetHistory()
89-
})
90-
})
50+
// })
9151
})

0 commit comments

Comments
 (0)