PowerShell / PowerShell/PSScriptAnalyzer
Settings file (.psd1) sometimes requires arrays even when specifying a single value
还没有人认领这个 Issue。
- 主要语言
- C#
- 星标
- 2.2k
- 派生
- 414
- 平均合并
- 13 小时 1 分钟
- 30 天内合并 PR
- 2
描述
Generally, in .psd1 files - and in PowerShell as a whole - it is fine to specify a scalar where an array is ultimately bound.
This is how it works in a module manifest's FunctionsToExport entry, for instance: instead of having to explicitly specify an array for a single entry - @('Get-Foo') or , 'Get-Foo - just 'Get-Foo' (scalar) is also accepted.
This is currently not the case in PSSA, at least for the PSAvoidUsingCmdletAliases rule's Whitelist property:
A scalar value is quietly ignored.
Not sure what other properties are affected.
Steps to reproduce
Run the following Pester test:
Describe "Scalar property-value test" {
BeforeAll {
Push-Location TestDrive:/
Import-Module -ErrorAction Stop PSScriptAnalyzer
}
It "Acceptes a scalar as a single value" {
@'
Rules = @{
Rules = @{
# Do not warn about using the '%' alias
# !! `@('%')` or `, '%'` are currently required - scalar '%' does NOT work.
PSAvoidUsingCmdletAliases = @{ Whitelist = '%' }
}
}
'@ > test.psd1
@'
1, 2 | % { $_ }
'@ > test.ps1
Invoke-ScriptAnalyzer -Settings test.psd1 test.ps1 | Should -BeNullOrEmpty
}
AfterAll {
Pop-Location
}
}
Expected behavior
The test should pass.
Actual behavior
The test fails, because a PSAvoidUsingCmdletAliases warning is still emitted, because the non-array value '%' didn't take effect.
Environment data
> $PSVersionTable
PowerShell Core v7.0.0-preview.5 (7.0.0-preview.5) on macOS 10.15.1
> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.18.3
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先,使用 PSAvoidUsingCmdletAliases 和 Whitelist = '%' 重现 issue #1366 中描述的 Pester 测试。跟踪 test.psd1 设置的读取方式以及 Whitelist 的应用方式;当标量值抑制别名警告且回归测试通过时,即表示完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- powershell
- 领域
- tooling
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 55/100