diff --git a/packages/core/src/awsService/ec2/sshKeyPair.ts b/packages/core/src/awsService/ec2/sshKeyPair.ts index cb67ca39ba1..75e1426bf26 100644 --- a/packages/core/src/awsService/ec2/sshKeyPair.ts +++ b/packages/core/src/awsService/ec2/sshKeyPair.ts @@ -79,10 +79,16 @@ export class SshKeyPair { const overrideKeys = async (_t: string, proc: RunParameterContext) => { await proc.send('yes') } - return !(await tryRun('ssh-keygen', ['-t', keyType, '-N', '', '-q', '-f', keyPath], 'yes', 'unknown key type', { - onStdout: overrideKeys, - timeout: new Timeout(5000), - })) + return await tryRun( + 'ssh-keygen', + ['-t', keyType, '-N', '', '-q', '-f', keyPath], + 'yes', + /^(?!.*Unknown key type).*/i, + { + onStdout: overrideKeys, + timeout: new Timeout(5000), + } + ) } public static async tryKeyTypes(keyPath: string, keyTypes: sshKeyType[]): Promise { diff --git a/packages/core/src/shared/utilities/pathFind.ts b/packages/core/src/shared/utilities/pathFind.ts index b1097810d3d..04622733a66 100644 --- a/packages/core/src/shared/utilities/pathFind.ts +++ b/packages/core/src/shared/utilities/pathFind.ts @@ -11,6 +11,7 @@ import { GitExtension } from '../extensions/git' import { Settings } from '../settings' import { getLogger } from '../logger/logger' import { mergeResolvedShellPath } from '../env/resolveEnv' +import { matchesPattern } from './textUtilities' /** Full path to VSCode CLI. */ let vscPath: string @@ -32,7 +33,7 @@ export async function tryRun( p: string, args: string[], logging: 'yes' | 'no' | 'noresult' = 'yes', - expected?: string, + expected?: string | RegExp, opt?: ChildProcessOptions ): Promise { const proc = new ChildProcess(p, args, { logging: 'no' }) @@ -40,7 +41,7 @@ export async function tryRun( ...opt, spawnOptions: { env: await mergeResolvedShellPath(opt?.spawnOptions?.env ?? process.env) }, }) - const ok = r.exitCode === 0 && (expected === undefined || r.stdout.includes(expected)) + const ok = r.exitCode === 0 && (expected === undefined || matchesPattern(r.stdout, expected)) if (logging === 'noresult') { getLogger().info('tryRun: %s: %s', ok ? 'ok' : 'failed', proc) } else if (logging !== 'no') { diff --git a/packages/core/src/shared/utilities/textUtilities.ts b/packages/core/src/shared/utilities/textUtilities.ts index e5746df0983..53c2e2be32c 100644 --- a/packages/core/src/shared/utilities/textUtilities.ts +++ b/packages/core/src/shared/utilities/textUtilities.ts @@ -273,3 +273,10 @@ export function extractFileAndCodeSelectionFromMessage(message: any) { const selection = message?.context?.focusAreaContext?.selectionInsideExtendedCodeBlock as vscode.Selection return { filePath, selection } } + +export function matchesPattern(source: string, target: string | RegExp) { + if (typeof target === 'string') { + return source.includes(target) + } + return target.test(source) +} diff --git a/packages/core/src/test/shared/utilities/pathFind.test.ts b/packages/core/src/test/shared/utilities/pathFind.test.ts index 335b4a9327b..43ec92cf680 100644 --- a/packages/core/src/test/shared/utilities/pathFind.test.ts +++ b/packages/core/src/test/shared/utilities/pathFind.test.ts @@ -42,6 +42,24 @@ describe('pathFind', function () { assert.ok(regex.test(vscPath), `expected regex ${regex} to match: "${vscPath}"`) }) + describe('tryRun', function () { + it('returns true if output matches expected', async function () { + const posResult = await tryRun('echo', ['hello'], 'no', 'hello') + assert.ok(posResult) + + const negResult = await tryRun('echo', ['hi'], 'no', 'hello') + assert.ok(!negResult) + }) + + it('supports regex on output match', async function () { + const posResult = await tryRun('echo', ['hello'], 'no', /hel*o/) + assert.ok(posResult) + + const negResult = await tryRun('echo', ['hi'], 'no', /^(?!(hi)).*$/) + assert.ok(!negResult) + }) + }) + describe('findSshPath', function () { let previousPath: string | undefined