|
| 1 | +Describe 'PowerShell Code Style' -Tag 'PSScriptAnalyzer' { |
| 2 | + BeforeAll { |
| 3 | + $formatSettings = "$PSScriptRoot\..\.psformatrules.psd1" |
| 4 | + $lintSettings = "$PSScriptRoot\..\.pslintrules.psd1" |
| 5 | + } |
| 6 | + |
| 7 | + It 'PSScriptAnalyzer rules files should exist' { |
| 8 | + $formatSettings | Should -Exist |
| 9 | + $lintSettings | Should -Exist |
| 10 | + } |
| 11 | + |
| 12 | + Context 'PowerShell code formatting' { |
| 13 | + BeforeAll { |
| 14 | + $records = Invoke-ScriptAnalyzer -Path "$PSScriptRoot\..\" ` |
| 15 | + -Recurse -Settings $formatSettings |
| 16 | + } |
| 17 | + It 'Code should be formatted' { |
| 18 | + $records.Count | Should -Be 0 |
| 19 | + } |
| 20 | + AfterAll { |
| 21 | + if ($records) { |
| 22 | + foreach ($r in $records) { |
| 23 | + $type = 'Unknown' |
| 24 | + switch -wildCard ($r.ScriptName) { |
| 25 | + '*.psm1' { $type = 'Module' } |
| 26 | + '*.ps1' { $type = 'Script' } |
| 27 | + '*.psd1' { $type = 'Manifest' } |
| 28 | + default { $type = 'Unknown' } |
| 29 | + } |
| 30 | + $scriptPath = Resolve-Path -Relative $r.ScriptPath |
| 31 | + $color = switch ($r.Severity) { |
| 32 | + 'Error' { 'Red' } |
| 33 | + 'Warning' { 'Yellow' } |
| 34 | + 'Information' { 'White' } |
| 35 | + default { 'White' } |
| 36 | + } |
| 37 | + Write-Host -f $color " [!] $($r.Severity): $($r.Message)" |
| 38 | + Write-Host -f $color " $($r.RuleName) in $type`: $($scriptPath):$($r.Line)" |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + Context 'PowerShell code linting' { |
| 45 | + BeforeAll { |
| 46 | + $records = Invoke-ScriptAnalyzer -Path "$PSScriptRoot\..\" ` |
| 47 | + -Recurse -Settings $lintSettings |
| 48 | + } |
| 49 | + It 'Code should be linted' { |
| 50 | + $records.Count | Should -Be 0 |
| 51 | + } |
| 52 | + AfterAll { |
| 53 | + if ($records) { |
| 54 | + foreach ($r in $records) { |
| 55 | + $type = 'Unknown' |
| 56 | + switch -wildCard ($r.ScriptName) { |
| 57 | + '*.psm1' { $type = 'Module' } |
| 58 | + '*.ps1' { $type = 'Script' } |
| 59 | + '*.psd1' { $type = 'Manifest' } |
| 60 | + default { $type = 'Unknown' } |
| 61 | + } |
| 62 | + $scriptPath = Resolve-Path -Relative $r.ScriptPath |
| 63 | + $color = switch ($r.Severity) { |
| 64 | + 'Error' { 'Red' } |
| 65 | + 'Warning' { 'Yellow' } |
| 66 | + 'Information' { 'White' } |
| 67 | + default { 'White' } |
| 68 | + } |
| 69 | + Write-Host -f $color " [!] $($r.Severity): $($r.Message)" |
| 70 | + Write-Host -f $color " $($r.RuleName) in $type`: $($scriptPath):$($r.Line)" |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments