Skip to content

Commit 0997a16

Browse files
committed
add test for cmd outside workspace
1 parent 0dc4ce8 commit 0997a16

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

packages/core/src/codewhispererChat/tools/executeBash.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,31 +189,34 @@ export class ExecuteBash {
189189
if (cmdArgs.length === 0) {
190190
return { requiresAcceptance: true }
191191
}
192+
193+
const command = cmdArgs[0]
194+
const category = commandCategories.get(command)
192195
let hasOutsideWorkspacePath = false
193-
// For each command, validate arguments for path safety within workspace
196+
194197
for (const arg of cmdArgs) {
195198
if (this.looksLikePath(arg)) {
196199
let fullPath = arg
197200
if (!path.isAbsolute(arg) && this.workingDirectory) {
198201
fullPath = path.join(this.workingDirectory, arg)
199202
}
203+
200204
const workspaceFolders = vscode.workspace.workspaceFolders
201205
if (!workspaceFolders || workspaceFolders.length === 0) {
202206
hasOutsideWorkspacePath = true
203-
continue
207+
break
204208
}
209+
205210
const isInWorkspace = workspaceFolders.some((folder) =>
206211
isInDirectory(folder.uri.fsPath, fullPath)
207212
)
208213
if (!isInWorkspace) {
209214
hasOutsideWorkspacePath = true
215+
break
210216
}
211217
}
212218
}
213219

214-
const command = cmdArgs[0]
215-
const category = commandCategories.get(command)
216-
217220
switch (category) {
218221
case CommandCategory.Destructive:
219222
return { requiresAcceptance: true, warning: destructiveCommandWarningMessage }
@@ -228,6 +231,7 @@ export class ExecuteBash {
228231
return { requiresAcceptance: true }
229232
}
230233
}
234+
231235
return { requiresAcceptance: false }
232236
} catch (error) {
233237
this.logger.warn(`Error while checking acceptance: ${(error as Error).message}`)

packages/core/src/test/codewhispererChat/tools/executeBash.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ describe('ExecuteBash Tool', () => {
5454
)
5555
})
5656

57+
it('requiresAcceptance=true without destructive warning for read-only command outside workspace', () => {
58+
const execBash = new ExecuteBash({
59+
command: 'ls /',
60+
cwd: '/do/not/exist/dir',
61+
})
62+
const result = execBash.requiresAcceptance()
63+
assert.equal(result.requiresAcceptance, true, 'Should require acceptance due to path outside workspace')
64+
assert.equal(result.warning, undefined, 'Should not show destructive warning for read-only command')
65+
})
66+
5767
it('set requiresAcceptance=false if it is a read-only command', () => {
5868
const execBash = new ExecuteBash({ command: 'cat file.txt' })
5969
const needsAcceptance = execBash.requiresAcceptance().requiresAcceptance

0 commit comments

Comments
 (0)