Skip to content

Commit 0898f28

Browse files
committed
refactor: generalize expected to positive and negative pattern
1 parent a512982 commit 0898f28

File tree

5 files changed

+46
-9
lines changed

5 files changed

+46
-9
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,16 @@ export class SshKeyPair {
7979
const overrideKeys = async (_t: string, proc: RunParameterContext) => {
8080
await proc.send('yes')
8181
}
82-
return !(await tryRun('ssh-keygen', ['-t', keyType, '-N', '', '-q', '-f', keyPath], 'yes', 'unknown key type', {
83-
onStdout: overrideKeys,
84-
timeout: new Timeout(5000),
85-
}))
82+
return await tryRun(
83+
'ssh-keygen',
84+
['-t', keyType, '-N', '', '-q', '-f', keyPath],
85+
'yes',
86+
{ negative: 'unknown key type' },
87+
{
88+
onStdout: overrideKeys,
89+
timeout: new Timeout(5000),
90+
}
91+
)
8692
}
8793

8894
public static async tryKeyTypes(keyPath: string, keyTypes: sshKeyType[]): Promise<boolean> {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class SamCliLocationProvider {
2424

2525
/** Checks that the given `sam` actually works by invoking `sam --version`. */
2626
private static async isValidSamLocation(samPath: string) {
27-
const isValid = await tryRun(samPath, ['--version'], 'no', 'SAM CLI')
27+
const isValid = await tryRun(samPath, ['--version'], 'no', { positive: 'SAM CLI' })
2828
this.samLocationValid = isValid
2929
return isValid
3030
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { GitExtension } from '../extensions/git'
1111
import { Settings } from '../settings'
1212
import { getLogger } from '../logger/logger'
1313
import { mergeResolvedShellPath } from '../env/resolveEnv'
14+
import { AcceptPattern, matchesPattern } from './textUtilities'
1415

1516
/** Full path to VSCode CLI. */
1617
let vscPath: string
@@ -26,21 +27,23 @@ const pathMap = new Map<string, string>()
2627
* @param p path to a program to execute
2728
* @param args program args
2829
* @param doLog log failures
29-
* @param expected output must contain this string
30+
* @param expectedPattern pattern to match in the output
31+
* @param opt additional options for the Child Process
32+
* @returns true if the program was found and executed successfully
3033
*/
3134
export async function tryRun(
3235
p: string,
3336
args: string[],
3437
logging: 'yes' | 'no' | 'noresult' = 'yes',
35-
expected?: string,
38+
expectedPattern?: AcceptPattern,
3639
opt?: ChildProcessOptions
3740
): Promise<boolean> {
3841
const proc = new ChildProcess(p, args, { logging: 'no' })
3942
const r = await proc.run({
4043
...opt,
4144
spawnOptions: { env: await mergeResolvedShellPath(opt?.spawnOptions?.env ?? process.env) },
4245
})
43-
const ok = r.exitCode === 0 && (expected === undefined || r.stdout.includes(expected))
46+
const ok = r.exitCode === 0 && matchesPattern(r.stdout, expectedPattern ?? {})
4447
if (logging === 'noresult') {
4548
getLogger().info('tryRun: %s: %s', ok ? 'ok' : 'failed', proc)
4649
} else if (logging !== 'no') {
@@ -108,7 +111,7 @@ export async function findTypescriptCompiler(): Promise<string | undefined> {
108111

109112
for (const tsc of tscPaths) {
110113
// Try to run "tsc -v".
111-
if (await tryRun(tsc, ['-v'], 'yes', 'Version')) {
114+
if (await tryRun(tsc, ['-v'], 'yes', { positive: 'Version' })) {
112115
return tsc
113116
}
114117
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,15 @@ export function extractFileAndCodeSelectionFromMessage(message: any) {
273273
const selection = message?.context?.focusAreaContext?.selectionInsideExtendedCodeBlock as vscode.Selection
274274
return { filePath, selection }
275275
}
276+
277+
export type AcceptPattern = Partial<{
278+
positive: string
279+
negative: string
280+
}>
281+
282+
export function matchesPattern(text: string, pattern: AcceptPattern): boolean {
283+
const { positive, negative } = pattern
284+
const matchesPositive = !positive || text.includes(positive)
285+
const matchesNegative = !negative || !text.includes(negative)
286+
return matchesPositive && matchesNegative
287+
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
sanitizeFilename,
1414
toSnakeCase,
1515
undefinedIfEmpty,
16+
matchesPattern,
1617
} from '../../../shared/utilities/textUtilities'
1718

1819
describe('textUtilities', async function () {
@@ -70,6 +71,21 @@ describe('textUtilities', async function () {
7071
assert.deepStrictEqual(indent(' abc\n\n \n123\nfoo\n', 4, false), ' abc\n\n \n 123\n foo\n')
7172
assert.deepStrictEqual(indent(' abc\n\n \n123\nfoo\n', 4, true), ' abc\n\n \n 123\n foo\n')
7273
})
74+
75+
it('matchesPattern', function () {
76+
assert.ok(matchesPattern('abc', { positive: 'ab', negative: 'cd' }))
77+
assert.ok(matchesPattern('abc', { positive: 'ab' }))
78+
assert.ok(matchesPattern('abc', { negative: 'cd' }))
79+
assert.ok(matchesPattern('abc', { negative: 'abcd' }))
80+
assert.ok(matchesPattern('abc', { positive: 'abc' }))
81+
assert.ok(matchesPattern('abc', {}))
82+
83+
assert.ok(!matchesPattern('abc', { positive: 'abcd' }))
84+
assert.ok(!matchesPattern('abc', { positive: 'abc', negative: 'a' }))
85+
assert.ok(!matchesPattern('abcde', { negative: 'ab' }))
86+
assert.ok(!matchesPattern('abc', { negative: 'ab' }))
87+
assert.ok(!matchesPattern('a a a a', { negative: 'a' }))
88+
})
7389
})
7490

7591
describe('removeAnsi', async function () {

0 commit comments

Comments
 (0)