PowerShell / PowerShell/PSScriptAnalyzer

Settings file (.psd1) sometimes requires arrays even when specifying a single value

未關閉
#1,366 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

Area - Configuration Issue - Bug
主要語言
C#
星號
2.2k
分支
415
平均合併
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

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

首先,使用 PSAvoidUsingCmdletAliases 和 Whitelist = '%' 重現 issue #1366 中描述的 Pester 測試。追蹤 test.psd1 設定的讀取方式以及 Whitelist 的套用方式;當純量值抑制別名警告且迴歸測試通過時,即表示完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
powershell
領域
tooling
Issue 類型
缺陷
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
55/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。