Skip to content

Commit 5954d1b

Browse files
committed
fix(commandDiscovery): 修复在 PATH 为空时命令解析失败的问题
- 为 PathExtValue、PathValue 等参数添加 AllowEmptyString() 属性,允许空字符串输入 - 在测试中预先获取 chmod 命令的绝对路径,避免因临时覆写 PATH 导致 Unix 下创建测试命令失败
1 parent 478de52 commit 5954d1b

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

psutils/modules/commandDiscovery.psm1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ function Get-ExecutableCommandExtensions {
6868
[Parameter(Mandatory)]
6969
[string]$Name,
7070
[Parameter(Mandatory)]
71+
[AllowEmptyString()]
7172
[string]$PathExtValue
7273
)
7374

@@ -107,6 +108,7 @@ function Get-ExecutableCommandSearchPaths {
107108
[CmdletBinding()]
108109
param(
109110
[Parameter(Mandatory)]
111+
[AllowEmptyString()]
110112
[string]$PathValue
111113
)
112114

@@ -197,6 +199,7 @@ function Resolve-ExecutableCommand {
197199
[Parameter(Mandatory)]
198200
[string[]]$SearchPaths,
199201
[Parameter(Mandatory)]
202+
[AllowEmptyString()]
200203
[string]$PathExtValue,
201204
[switch]$AllMatches
202205
)
@@ -257,6 +260,7 @@ function Resolve-ExecutableCommandsBatch {
257260
[Parameter(Mandatory)]
258261
[string[]]$SearchPaths,
259262
[Parameter(Mandatory)]
263+
[AllowEmptyString()]
260264
[string]$PathExtValue,
261265
[switch]$AllMatches
262266
)

psutils/tests/commandDiscovery.Tests.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ BeforeAll {
22
Import-Module "$PSScriptRoot\..\modules\commandDiscovery.psm1" -Force
33
$script:OriginalPath = [Environment]::GetEnvironmentVariable('PATH', 'Process')
44
$script:OriginalPathExt = [Environment]::GetEnvironmentVariable('PATHEXT', 'Process')
5+
# 测试会临时覆写 PATH,因此在进入各个用例前先解析 chmod 的绝对路径,避免 Unix 下创建测试命令失败。
6+
$script:ChmodPath = if ($IsWindows) { $null } else { (Get-Command chmod -ErrorAction Stop).Source }
57
}
68

79
AfterAll {
@@ -26,7 +28,7 @@ function global:New-TestExecutableCommand {
2628

2729
$commandPath = Join-Path $Directory $Name
2830
Set-Content -Path $commandPath -Value "#!/usr/bin/env sh`necho ok" -Encoding ascii
29-
& chmod +x $commandPath
31+
& $script:ChmodPath +x $commandPath
3032
return $commandPath
3133
}
3234

0 commit comments

Comments
 (0)