1111 Justification = ' Write-Host is used for log output.'
1212)]
1313[CmdLetBinding ()]
14- Param (
14+ param (
1515 [Parameter (Mandatory )]
1616 [string ] $Path ,
1717
18- [Parameter (Mandatory )]
18+ [Parameter ()]
1919 [string ] $SettingsFilePath
2020)
2121
2222BeforeDiscovery {
23- LogGroup " PSScriptAnalyzer tests using settings file [$SettingsFilePath ]" {
24- $settings = Import-PowerShellDataFile - Path $SettingsFilePath
23+ $hasSettingsFile = -not [string ]::IsNullOrEmpty($SettingsFilePath )
24+ $settingsDescription = $hasSettingsFile ? " settings file [$SettingsFilePath ]" : ' default settings'
25+
26+ LogGroup " PSScriptAnalyzer tests using $settingsDescription " {
27+ if ($hasSettingsFile ) {
28+ $settings = Import-PowerShellDataFile - Path $SettingsFilePath
29+ } else {
30+ $settings = @ {}
31+ }
32+
2533 $rules = [Collections.Generic.List [System.Collections.Specialized.OrderedDictionary ]]::new()
2634 $ruleObjects = Get-ScriptAnalyzerRule - Verbose:$false | Sort-Object - Property Severity, CommonName
2735 $Severeties = $ruleObjects | Select-Object - ExpandProperty Severity - Unique
@@ -65,13 +73,18 @@ BeforeDiscovery {
6573
6674Describe ' PSScriptAnalyzer' {
6775 BeforeAll {
68- $relativeSettingsFilePath = if ($SettingsFilePath.StartsWith ($PSScriptRoot )) {
69- $SettingsFilePath.Replace ($PSScriptRoot , ' Action:' ).Trim(' \' ).Trim(' /' )
70- } elseif ($SettingsFilePath.StartsWith ($env: GITHUB_WORKSPACE )) {
71- $SettingsFilePath.Replace ($env: GITHUB_WORKSPACE , ' Workspace:' ).Trim(' \' ).Trim(' /' )
72- } else {
73- $SettingsFilePath
76+ $hasSettingsFile = -not [string ]::IsNullOrEmpty($SettingsFilePath )
77+
78+ if ($hasSettingsFile ) {
79+ $relativeSettingsFilePath = if ($SettingsFilePath.StartsWith ($PSScriptRoot )) {
80+ $SettingsFilePath.Replace ($PSScriptRoot , ' Action:' ).Trim(' \' ).Trim(' /' )
81+ } elseif ($SettingsFilePath.StartsWith ($env: GITHUB_WORKSPACE )) {
82+ $SettingsFilePath.Replace ($env: GITHUB_WORKSPACE , ' Workspace:' ).Trim(' \' ).Trim(' /' )
83+ } else {
84+ $SettingsFilePath
85+ }
7486 }
87+
7588 $Path = Resolve-Path - Path $Path | Select-Object - ExpandProperty Path
7689 $relativePath = if ($Path.StartsWith ($PSScriptRoot )) {
7790 $Path.Replace ($PSScriptRoot , ' Action:' ).Trim(' \' ).Trim(' /' ).Replace(' \' , ' /' )
@@ -88,9 +101,26 @@ Describe 'PSScriptAnalyzer' {
88101 GITHUB_WORKSPACE = $env: GITHUB_WORKSPACE
89102 }
90103
91- LogGroup " Invoke-ScriptAnalyzer -Path [$relativePath ] -Settings [$relativeSettingsFilePath ]" {
92- $testResults = Invoke-ScriptAnalyzer - Path $Path - Settings $SettingsFilePath - Recurse - Verbose
104+ $invokeParams = @ {
105+ Path = $Path
106+ Recurse = $true
107+ Verbose = $true
108+ }
109+
110+ if ($hasSettingsFile ) {
111+ $invokeParams [' Settings' ] = $SettingsFilePath
93112 }
113+
114+ $logMessage = if ($hasSettingsFile ) {
115+ " Invoke-ScriptAnalyzer -Path [$relativePath ] -Settings [$relativeSettingsFilePath ]"
116+ } else {
117+ " Invoke-ScriptAnalyzer -Path [$relativePath ] -Recurse (using default settings)"
118+ }
119+
120+ LogGroup $logMessage {
121+ $testResults = Invoke-ScriptAnalyzer @invokeParams
122+ }
123+
94124 LogGroup " TestResults [$ ( $testResults.Count ) ]" {
95125 $testResults | Select-Object - Property * | Format-List | Out-String - Stream | ForEach-Object {
96126 Write-Verbose $_ - Verbose
0 commit comments