Skip to content

Commit b5a90b8

Browse files
committed
feat: add support for regex in tryRun
1 parent a512982 commit b5a90b8

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

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

Lines changed: 3 additions & 2 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 { matchesPattern } from './textUtilities'
1415

1516
/** Full path to VSCode CLI. */
1617
let vscPath: string
@@ -32,15 +33,15 @@ export async function tryRun(
3233
p: string,
3334
args: string[],
3435
logging: 'yes' | 'no' | 'noresult' = 'yes',
35-
expected?: string,
36+
expected?: string | RegExp,
3637
opt?: ChildProcessOptions
3738
): Promise<boolean> {
3839
const proc = new ChildProcess(p, args, { logging: 'no' })
3940
const r = await proc.run({
4041
...opt,
4142
spawnOptions: { env: await mergeResolvedShellPath(opt?.spawnOptions?.env ?? process.env) },
4243
})
43-
const ok = r.exitCode === 0 && (expected === undefined || r.stdout.includes(expected))
44+
const ok = r.exitCode === 0 && (expected === undefined || matchesPattern(r.stdout, expected))
4445
if (logging === 'noresult') {
4546
getLogger().info('tryRun: %s: %s', ok ? 'ok' : 'failed', proc)
4647
} else if (logging !== 'no') {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,10 @@ export function extractFileAndCodeSelectionFromMessage(message: any) {
273273
const selection = message?.context?.focusAreaContext?.selectionInsideExtendedCodeBlock as vscode.Selection
274274
return { filePath, selection }
275275
}
276+
277+
export function matchesPattern(source: string, target: string | RegExp) {
278+
if (typeof target === 'string') {
279+
return source.includes(target)
280+
}
281+
return target.test(source)
282+
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ describe('pathFind', function () {
4242
assert.ok(regex.test(vscPath), `expected regex ${regex} to match: "${vscPath}"`)
4343
})
4444

45+
describe('tryRun', function () {
46+
it('returns true if output matches expected', async function () {
47+
const posResult = await tryRun('echo', ['hello'], 'no', 'hello')
48+
assert.ok(posResult)
49+
50+
const negResult = await tryRun('echo', ['hi'], 'no', 'hello')
51+
assert.ok(!negResult)
52+
})
53+
54+
it('supports regex on output match', async function () {
55+
const posResult = await tryRun('echo', ['hello'], 'no', /hel*o/)
56+
assert.ok(posResult)
57+
58+
const negResult = await tryRun('echo', ['hi'], 'no', /^(?!(hi)).*$/)
59+
assert.ok(!negResult)
60+
})
61+
})
62+
4563
describe('findSshPath', function () {
4664
let previousPath: string | undefined
4765

0 commit comments

Comments
 (0)