Skip to content

Commit 4134cab

Browse files
committed
fix(skills): 修复外部命令退出码误判
1 parent 30f4d5b commit 4134cab

2 files changed

Lines changed: 26 additions & 6 deletions

File tree

ai/skills/Install-Skills.ps1

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,8 +1115,12 @@ function Confirm-SkillsInstallPlan {
11151115
return $false
11161116
}
11171117

1118-
$answer = Read-Host '继续执行以上安装计划?输入 y 确认'
1119-
return $answer -in @('y', 'Y', 'yes', 'YES')
1118+
$choices = [System.Management.Automation.Host.ChoiceDescription[]]@(
1119+
[System.Management.Automation.Host.ChoiceDescription]::new('&Yes', '执行以上安装计划。'),
1120+
[System.Management.Automation.Host.ChoiceDescription]::new('&No', '取消安装计划。')
1121+
)
1122+
$selected = $Host.UI.PromptForChoice('确认安装计划', '继续执行以上安装计划?', $choices, 1)
1123+
return $selected -eq 0
11201124
}
11211125

11221126
function New-SkillsLogFile {
@@ -1237,6 +1241,7 @@ function Invoke-SkillsExternalCommand {
12371241
$process.WaitForExit()
12381242
$stdout = $stdoutTask.GetAwaiter().GetResult()
12391243
$stderr = $stderrTask.GetAwaiter().GetResult()
1244+
$exitCode = $process.ExitCode
12401245
}
12411246
finally {
12421247
$process.Dispose()
@@ -1251,15 +1256,15 @@ function Invoke-SkillsExternalCommand {
12511256
Write-SkillsLogLine -LogPath $LogPath -Message "STDERR $($stderr.TrimEnd())"
12521257
}
12531258

1254-
Write-SkillsLogLine -LogPath $LogPath -Message "EXIT $($process.ExitCode)"
1259+
Write-SkillsLogLine -LogPath $LogPath -Message "EXIT $exitCode"
12551260
$result = [pscustomobject]@{
1256-
ExitCode = $process.ExitCode
1261+
ExitCode = $exitCode
12571262
StdOut = $stdout
12581263
StdErr = $stderr
12591264
}
12601265

1261-
if ($process.ExitCode -ne 0 -and -not $AllowFailure) {
1262-
throw "外部命令执行失败($($process.ExitCode)): $commandLine"
1266+
if ($exitCode -ne 0 -and -not $AllowFailure) {
1267+
throw "外部命令执行失败($exitCode): $commandLine"
12631268
}
12641269

12651270
return $result

tests/SkillsInstaller.Tests.ps1

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,21 @@ Describe 'Skills 安装计划执行' {
245245
Invoke-SkillsToolStep -Tool $tool -LogPath '' -CommandRunner $runner -WhatIf
246246
}
247247

248+
It '外部命令成功退出时正确记录退出码' {
249+
$logPath = Join-Path $TestDrive 'external-command.log'
250+
$pwshPath = [System.Diagnostics.Process]::GetCurrentProcess().MainModule.FileName
251+
252+
$result = Invoke-SkillsExternalCommand `
253+
-Command $pwshPath `
254+
-Arguments @('-NoLogo', '-NoProfile', '-Command', 'Write-Output "ok"') `
255+
-WorkingDirectory $TestDrive `
256+
-LogPath $logPath
257+
258+
$result.ExitCode | Should -Be 0
259+
$result.StdOut | Should -Match 'ok'
260+
Get-Content -Raw -LiteralPath $logPath | Should -Match 'EXIT 0'
261+
}
262+
248263
It '附带命令按 pre/install/post 顺序进入执行计划' {
249264
$configPath = Join-Path $TestDrive 'skills.config.json'
250265
Set-Content -LiteralPath $configPath -Encoding utf8NoBOM -Value @'

0 commit comments

Comments
 (0)