Skip to content

Commit af6bef3

Browse files
🚀 [Feature]: Enhance settings file handling in Invoke-ScriptAnalyzer script
1 parent 1783982 commit af6bef3

File tree

5 files changed

+130
-21
lines changed

5 files changed

+130
-21
lines changed

‎.github/workflows/Action-Test.yml‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,29 @@ jobs:
8989
Write-Host "Outcome: ${{ steps.action-test.outcome }}"
9090
Write-Host "Conclusion: ${{ steps.action-test.conclusion }}"
9191
92+
ActionTestSrcWithManifestDefault:
93+
name: Action-Test - [Src-WithManifest-Default]
94+
runs-on: ubuntu-latest
95+
outputs:
96+
Outcome: ${{ steps.action-test.outcome }}
97+
Conclusion: ${{ steps.action-test.conclusion }}
98+
steps:
99+
- name: Checkout repo
100+
uses: actions/checkout@v5
101+
102+
- name: Action-Test
103+
uses: ./
104+
id: action-test
105+
with:
106+
Path: src
107+
WorkingDirectory: tests/srcWithManifestTestRepo
108+
109+
- name: Status
110+
shell: pwsh
111+
run: |
112+
Write-Host "Outcome: ${{ steps.action-test.outcome }}"
113+
Write-Host "Conclusion: ${{ steps.action-test.conclusion }}"
114+
92115
ActionTestOutputs:
93116
name: Action-Test - [outputs]
94117
runs-on: ubuntu-latest
@@ -118,6 +141,7 @@ jobs:
118141
- ActionTestSrcSourceCode
119142
- ActionTestSrcCustom
120143
- ActionTestSrcWithManifest
144+
- ActionTestSrcWithManifestDefault
121145
- ActionTestOutputs
122146
if: always()
123147
runs-on: ubuntu-latest
@@ -128,6 +152,8 @@ jobs:
128152
ActionTestSrcCustomConclusion: ${{ needs.ActionTestSrcCustom.outputs.Conclusion }}
129153
ActionTestSrcWithManifestOutcome: ${{ needs.ActionTestSrcWithManifest.outputs.Outcome }}
130154
ActionTestSrcWithManifestConclusion: ${{ needs.ActionTestSrcWithManifest.outputs.Conclusion }}
155+
ActionTestSrcWithManifestDefaultOutcome: ${{ needs.ActionTestSrcWithManifestDefault.outputs.Outcome }}
156+
ActionTestSrcWithManifestDefaultConclusion: ${{ needs.ActionTestSrcWithManifestDefault.outputs.Conclusion }}
131157
ActionTestOutputsOutcome: ${{ needs.ActionTestOutputs.outputs.Outcome }}
132158
ActionTestOutputsConclusion: ${{ needs.ActionTestOutputs.outputs.Conclusion }}
133159
steps:

‎README.md‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,41 @@ The action provides the following outputs:
8888
Choose a path for your code to test into the `Path` input. This can be a
8989
directory or a file.
9090

91-
2. **Configure settings file**
91+
2. **Configure settings file (Optional)**
9292
Create a custom settings file to customize the analysis. The settings file is
9393
a hashtable that defines the rules to include, exclude, or customize. The
9494
settings file is in the format of a `.psd1` file.
9595

96-
By default, the action looks for a settings file at:
97-
`.github/linters/.powershell-psscriptanalyzer.psd1`
96+
**Settings File Precedence:**
9897

99-
You can override this by setting the `SettingsFilePath` input to point to your
100-
custom settings file.
98+
The action determines which settings to use in the following order:
99+
100+
1. **Custom Path**: If you provide a `SettingsFilePath` input, the action uses that file.
101+
2. **Default Action Path**: If no `SettingsFilePath` is provided, the action looks for a settings file at:
102+
`.github/linters/.powershell-psscriptanalyzer.psd1`
103+
3. **PSScriptAnalyzer Defaults**: If no settings file is found in either location, the action uses
104+
the default settings from the `Invoke-ScriptAnalyzer` cmdlet (all built-in rules with default severity).
105+
106+
**Example configurations:**
107+
108+
```yaml
109+
# Use a custom settings file
110+
- uses: PSModule/Invoke-ScriptAnalyzer@v2
111+
with:
112+
Path: src
113+
SettingsFilePath: config/custom-rules.psd1
114+
115+
# Use the default action path (.github/linters/.powershell-psscriptanalyzer.psd1)
116+
- uses: PSModule/Invoke-ScriptAnalyzer@v2
117+
with:
118+
Path: src
119+
120+
# Use PSScriptAnalyzer defaults (no settings file)
121+
- uses: PSModule/Invoke-ScriptAnalyzer@v2
122+
with:
123+
Path: src
124+
SettingsFilePath: '' # Explicitly skip settings file
125+
```
101126
102127
For more info on how to create a settings file, see the [Settings Documentation](./Settings.md) file.
103128

‎scripts/main.ps1‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,31 @@
22
$testPath = Resolve-Path -Path "$PSScriptRoot/tests/PSScriptAnalyzer" | Select-Object -ExpandProperty Path
33
$path = [string]::IsNullOrEmpty($env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_Path) ? '.' : $env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_Path
44
$codePath = Resolve-Path -Path $path | Select-Object -ExpandProperty Path
5-
$settingsFilePath = Resolve-Path -Path $env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_SettingsFilePath | Select-Object -ExpandProperty Path
5+
6+
# Try to resolve the settings file path, but allow it to be null if not found
7+
$settingsFilePath = $null
8+
if (-not [string]::IsNullOrEmpty($env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_SettingsFilePath)) {
9+
try {
10+
$resolvedPath = Resolve-Path -Path $env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_SettingsFilePath -ErrorAction Stop | Select-Object -ExpandProperty Path
11+
if (Test-Path -Path $resolvedPath) {
12+
$settingsFilePath = $resolvedPath
13+
Write-Information "Using settings file: $settingsFilePath"
14+
}
15+
} catch {
16+
Write-Warning "Settings file not found at path: $($env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_SettingsFilePath). Using default settings."
17+
}
18+
}
19+
20+
if ($null -eq $settingsFilePath) {
21+
Write-Information 'No settings file specified or found. Using default PSScriptAnalyzer settings.'
22+
}
623

724
[pscustomobject]@{
825
CodePath = $codePath
926
TestPath = $testPath
1027
SettingsFilePath = $settingsFilePath
1128
} | Format-List | Out-String
1229

13-
if (!(Test-Path -Path $settingsFilePath)) {
14-
throw "Settings file not found at path: $settingsFilePath"
15-
}
1630
Set-GitHubOutput -Name CodePath -Value $codePath
1731
Set-GitHubOutput -Name TestPath -Value $testPath
1832
Set-GitHubOutput -Name SettingsFilePath -Value $settingsFilePath

‎scripts/tests/PSScriptAnalyzer/PSScriptAnalyzer.Tests.ps1‎

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,25 @@
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

2222
BeforeDiscovery {
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

6674
Describe '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

‎tests/Get-AggregatedStatus.ps1‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ $ActionTestSrcWithManifestOutcomeResult = $env:ActionTestSrcWithManifestOutcome
3535
$ActionTestSrcWithManifestExpectedConclusion = 'success'
3636
$ActionTestSrcWithManifestConclusionResult = $env:ActionTestSrcWithManifestConclusion -eq $ActionTestSrcWithManifestExpectedConclusion
3737

38+
$ActionTestSrcWithManifestDefaultExpectedOutcome = 'success'
39+
$ActionTestSrcWithManifestDefaultOutcomeResult = $env:ActionTestSrcWithManifestDefaultOutcome -eq $ActionTestSrcWithManifestDefaultExpectedOutcome
40+
$ActionTestSrcWithManifestDefaultExpectedConclusion = 'success'
41+
$ActionTestSrcWithManifestDefaultConclusionResult = $env:ActionTestSrcWithManifestDefaultConclusion -eq $ActionTestSrcWithManifestDefaultExpectedConclusion
42+
3843
$ActionTestOutputsExpectedOutcome = 'success'
3944
$ActionTestOutputsOutcomeResult = $env:ActionTestOutputsOutcome -eq $ActionTestOutputsExpectedOutcome
4045
$ActionTestOutputsExpectedConclusion = 'success'
@@ -68,6 +73,15 @@ $jobs = @(
6873
ExpectedConclusion = $ActionTestSrcWithManifestExpectedConclusion
6974
PassedConclusion = $ActionTestSrcWithManifestConclusionResult
7075
},
76+
[PSCustomObject]@{
77+
Name = 'Action-Test - [Src-WithManifest-Default]'
78+
Outcome = $env:ActionTestSrcWithManifestDefaultOutcome
79+
ExpectedOutcome = $ActionTestSrcWithManifestDefaultExpectedOutcome
80+
PassedOutcome = $ActionTestSrcWithManifestDefaultOutcomeResult
81+
Conclusion = $env:ActionTestSrcWithManifestDefaultConclusion
82+
ExpectedConclusion = $ActionTestSrcWithManifestDefaultExpectedConclusion
83+
PassedConclusion = $ActionTestSrcWithManifestDefaultConclusionResult
84+
},
7185
[PSCustomObject]@{
7286
Name = 'Action-Test - [outputs]'
7387
Outcome = $env:ActionTestOutputsOutcome

0 commit comments

Comments
 (0)