PowerShell / PowerShell/PSScriptAnalyzer

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

Aperta
#1,366 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Area - Configuration Issue - Bug
Lingua principale
C#
Stelle
2.2k
Fork
414
Merge medio
13h 1m
PR unite (30g)
2

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo il test Pester descritto in issue #1366 con PSAvoidUsingCmdletAliases e Whitelist = '%'. Traccia il modo in cui vengono lette le impostazioni di test.psd1 e in cui viene applicato Whitelist; il lavoro è completato quando il valore scalare sopprime l'avviso relativo all'alias e il test di regressione ha esito positivo.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
powershell
Ambito
tooling
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
55/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.