|
| 1 | +[CmdletBinding()] |
| 2 | +param( |
| 3 | + # The path to the folder where your code to analyze is |
| 4 | + [string]$Path = $((Get-Module ModuleBuilder).ModuleBase), |
| 5 | + |
| 6 | + # Runs Script Analyzer on the files in the Path directory and all subdirectories recursively. |
| 7 | + [string]$Recurse, |
| 8 | + |
| 9 | + # The name (or path) of a settings file to be used. |
| 10 | + [string]$Settings = "..\PSScriptAnalyzerSettings.psd1" |
| 11 | +) |
| 12 | + |
| 13 | +if ($ModulesToImport) { |
| 14 | + Write-Verbose "Import-Module $ModulesToImport" -Verbose |
| 15 | + Import-Module $ModulesToImport |
| 16 | + $null = $PSBoundParameters.Remove("ModulesToImport") |
| 17 | +} |
| 18 | +$ExcludeRules = @() |
| 19 | +$CustomRulePath = @{} |
| 20 | +$RecurseCustomRulePath = $false |
| 21 | +if ($Settings) { |
| 22 | + Write-Verbose "Resolve settings '$Settings'" -Verbose |
| 23 | + if (Test-Path $Settings) { |
| 24 | + $Settings = Resolve-Path $Settings |
| 25 | + } else { |
| 26 | + Push-Location |
| 27 | + foreach($directory in Get-ChildItem -Directory) { |
| 28 | + Set-Location $directory |
| 29 | + if (Test-Path $Settings) { |
| 30 | + $Settings = Resolve-Path $Settings |
| 31 | + } |
| 32 | + } |
| 33 | + Pop-Location |
| 34 | + } |
| 35 | + if (!(Test-Path $Settings)) { |
| 36 | + Write-Warning "Could not resolve settings '$Settings'" |
| 37 | + Remove-Variable Settings |
| 38 | + $null = $PSBoundParameters.Remove("Settings") |
| 39 | + } else { |
| 40 | + $Config = Import-LocalizedData -BaseDirectory $pwd -FileName PSScriptAnalyzerSettings |
| 41 | + $ExcludeRules = @($Config.ExcludeRules) |
| 42 | + $CustomRulePath = @{ |
| 43 | + CustomRulePath = @($Config.CustomRulePath) |
| 44 | + RecurseCustomRulePath = [bool]$Config.RecurseCustomRulePath |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +Describe "ScriptAnalyzer" { |
| 50 | + $ScriptAnalyzer = @{ |
| 51 | + Config = @{} + $PSBoundParameters |
| 52 | + Rules = Get-ScriptAnalyzerRule | Where-Object RuleName -notin $ExcludeRules |
| 53 | + } |
| 54 | + |
| 55 | + if ($CustomRulePath) { |
| 56 | + $CustomRulePath | Should Exist |
| 57 | + if ($CustomRules = Get-ScriptAnalyzerRule @CustomRulePath) { |
| 58 | + $ScriptAnalyzer.Rules += $CustomRules |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + It "Does not throw while running Script Analyzer" { |
| 63 | + $Config = $ScriptAnalyzer.Config |
| 64 | + try { |
| 65 | + $ScriptAnalyzer.Results = Invoke-ScriptAnalyzer @Config |
| 66 | + } catch { |
| 67 | + Write-Warning "Exception running script analyzer on $($_.TargetObject)" |
| 68 | + Write-Warning $($_.Exception.StackTrace) |
| 69 | + throw |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + forEach ($Rule in $ScriptAnalyzer.Rules.RuleName) { |
| 74 | + It "Passes $Rule" { |
| 75 | + if ($Failures = $ScriptAnalyzer.Results.Where( {$_.RuleName -like "*$Rule"})) { |
| 76 | + throw ([Management.Automation.ErrorRecord]::new( |
| 77 | + ([Exception]::new(($Failures.ForEach{$_.ScriptName + ":" + $_.Line + " " + $_.Message} -join "`n"))), |
| 78 | + "ScriptAnalyzerViolation", |
| 79 | + "SyntaxError", |
| 80 | + $Failures)) |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments