Tests/Analyzer.Tests.ps1
|
BeforeDiscovery { # -Skip se resuelve durante el descubrimiento. Si esta variable se calculase # en BeforeAll valdria $null aqui, -not $null seria $true y las tres pruebas # de la puerta se saltarian EN SILENCIO sin que nadie lo notase. $script:TieneAnalizador = $null -ne (Get-Module -ListAvailable -Name PSScriptAnalyzer) } BeforeAll { $script:Root = Split-Path -Parent (Split-Path -Parent $PSCommandPath) $script:Settings = Join-Path -Path $script:Root -ChildPath 'PSScriptAnalyzerSettings.psd1' if ($null -ne (Get-Module -ListAvailable -Name PSScriptAnalyzer)) { Import-Module -Name PSScriptAnalyzer -Force $script:Hallazgos = @(Invoke-ScriptAnalyzer -Path $script:Root -Recurse -Settings $script:Settings) } else { $script:Hallazgos = @() } } Describe 'PSScriptAnalyzer' { It 'PSScriptAnalyzer esta instalado: sin el, la puerta no vigila nada' { # Se recalcula dentro del It en vez de leer una variable del # descubrimiento: asi la comprobacion es la misma en Pester 5 y en 6. $null -ne (Get-Module -ListAvailable -Name PSScriptAnalyzer) | Should -BeTrue -Because 'una puerta que se salta en silencio es peor que no tener puerta' } It 'el fichero de configuracion del repositorio existe y es valido' { Test-Path -LiteralPath $script:Settings | Should -BeTrue { Import-PowerShellDataFile -Path $script:Settings } | Should -Not -Throw } It 'no hay ningun hallazgo de severidad Error' -Skip:(-not $script:TieneAnalizador) { $errores = @($script:Hallazgos | Where-Object { $_.Severity -in 'Error', 'ParseError' }) $detalle = ($errores | ForEach-Object { '{0}:{1} {2}' -f (Split-Path -Leaf $_.ScriptName), $_.Line, $_.RuleName }) -join ' | ' $detalle | Should -BeNullOrEmpty $errores.Count | Should -Be 0 } It 'no hay ningun hallazgo de severidad Warning' -Skip:(-not $script:TieneAnalizador) { $avisos = @($script:Hallazgos | Where-Object { $_.Severity -eq 'Warning' }) $detalle = ($avisos | ForEach-Object { '{0}:{1} {2}' -f (Split-Path -Leaf $_.ScriptName), $_.Line, $_.RuleName }) -join ' | ' $detalle | Should -BeNullOrEmpty $avisos.Count | Should -Be 0 } It 'PSUseCompatibleSyntax esta realmente activa y detecta sintaxis de 7.x' -Skip:(-not $script:TieneAnalizador) { # Sin esta comprobacion, un fichero de configuracion mal escrito dejaria # la regla apagada y el repositorio pasaria la puerta sin vigilar la # compatibilidad con 5.1, que es la promesa del manifiesto. $prueba = 'function Test-X { $y = $true ? 1 : 2; return $y }' $r = @(Invoke-ScriptAnalyzer -ScriptDefinition $prueba -Settings $script:Settings | Where-Object { $_.RuleName -eq 'PSUseCompatibleSyntax' }) $r.Count | Should -BeGreaterThan 0 } } |