PowerShell / PowerShell/PSScriptAnalyzer

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

Offen
#1,366 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Area - Configuration Issue - Bug
Vorherrschende Sprache
C#
Sterne
2.2k
Forks
414
Ø Merge
13 Std. 1 Min.
Gemergte PRs (30 T.)
2

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie damit, den in issue #1366 beschriebenen Pester-Test mit PSAvoidUsingCmdletAliases und Whitelist = '%' zu reproduzieren. Verfolgen Sie, wie die Einstellungen in test.psd1 gelesen werden und wie Whitelist angewendet wird; abgeschlossen ist die Aufgabe, wenn der skalare Wert die Alias-Warnung unterdrückt und der Regressionstest erfolgreich ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
powershell
Bereich
tooling
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
55/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.