From eaf82d0b9609ae77c99f0722b42a24a5cf0c6a93 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 11 Oct 2025 16:58:08 +0200 Subject: [PATCH 1/7] Remove `Settings` with predefined settings --- .github/workflows/Action-Test.yml | 5 +- README.md | 24 ++++---- action.yml | 10 +--- scripts/main.ps1 | 20 ++----- .../linters/.powershell-psscriptanalyzer.psd1 | 0 .../linters/.powershell-psscriptanalyzer.psd1 | 0 .../tests/SourceCode.Settings.psd1 | 56 +++++++++++++++++++ 7 files changed, 76 insertions(+), 39 deletions(-) rename scripts/tests/PSScriptAnalyzer/Module.Settings.psd1 => tests/outputTestRepo/.github/linters/.powershell-psscriptanalyzer.psd1 (100%) rename scripts/tests/PSScriptAnalyzer/SourceCode.Settings.psd1 => tests/srcTestRepo/.github/linters/.powershell-psscriptanalyzer.psd1 (100%) create mode 100644 tests/srcWithManifestTestRepo/tests/SourceCode.Settings.psd1 diff --git a/.github/workflows/Action-Test.yml b/.github/workflows/Action-Test.yml index 6a4aee7..1ee2812 100644 --- a/.github/workflows/Action-Test.yml +++ b/.github/workflows/Action-Test.yml @@ -32,7 +32,6 @@ jobs: id: action-test with: Path: src - Settings: SourceCode WorkingDirectory: tests/srcTestRepo - name: Status @@ -56,7 +55,6 @@ jobs: id: action-test with: Path: src - Settings: Custom SettingsFilePath: tests/Custom.Settings.psd1 WorkingDirectory: tests/srcTestRepo @@ -82,7 +80,7 @@ jobs: id: action-test with: Path: src - Settings: SourceCode + SettingsFilePath: tests/SourceCode.Settings.psd1 WorkingDirectory: tests/srcWithManifestTestRepo - name: Status @@ -106,7 +104,6 @@ jobs: id: action-test with: Path: outputs/modules/PSModuleTest - Settings: Module WorkingDirectory: tests/outputTestRepo - name: Status diff --git a/README.md b/README.md index a7e8c71..6530bae 100644 --- a/README.md +++ b/README.md @@ -18,8 +18,7 @@ customize rule selection, severity filtering, and custom rule inclusion. | Input | Description | Required | Default | |--------------------------------------|--------------------------------------------------------------------------------|----------|-----------------------------------------------------------------------------| | `Path` | The path to the code to test. | false | `'.'` | -| `Settings` | The type of tests to run: `Module`, `SourceCode`, or `Custom`. | false | `Custom` | -| `SettingsFilePath` | If `Custom` is selected, the path to the settings file. | false | `${{ github.workspace }}/.github/linters/.powershell-psscriptanalyzer.psd1` | +| `SettingsFilePath` | The path to the settings file. | false | `${{ github.workspace }}/.github/linters/.powershell-psscriptanalyzer.psd1` | | `Debug` | Enable debug output. | false | `'false'` | | `Verbose` | Enable verbose output. | false | `'false'` | | `Version` | Specifies the exact version of the GitHub module to install. | false | | @@ -89,19 +88,18 @@ The action provides the following outputs: Choose a path for your code to test into the `Path` input. This can be a directory or a file. -2. **Choose settings** - Choose the type of tests to run by setting the `Settings` input. The options - are `Module`, `SourceCode`, or `Custom`. The default is `Custom`. +2. **Configure settings file** + Create a custom settings file to customize the analysis. The settings file is + a hashtable that defines the rules to include, exclude, or customize. The + settings file is in the format of a `.psd1` file. - The predefined settings: - - [`Module`](./scripts/tests/PSScriptAnalyzer/Module.Settings.psd1): Analyzes a module following PSModule standards. - - [`SourceCode`](./scripts/tests/PSScriptAnalyzer/SourceCode.Settings.psd1): Analyzes the source code following PSModule standards. + By default, the action looks for a settings file at: + `${{ github.workspace }}/.github/linters/.powershell-psscriptanalyzer.psd1` - You can also create a custom settings file to customize the analysis. The - settings file is a hashtable that defines the rules to include, exclude, or - customize. The settings file is in the format of a `.psd1` file. + You can override this by setting the `SettingsFilePath` input to point to your + custom settings file. - For more info on how to create a settings file, see the [Settings Documentation](./Settings.md) file. + For more info on how to create a settings file, see the [Settings Documentation](./Settings.md) file. 3. **Run the Action** The tests import the settings file and use `Invoke-ScriptAnalyzer` to analyze @@ -139,7 +137,7 @@ jobs: uses: PSModule/Invoke-ScriptAnalyzer@v2 with: Path: src - Settings: SourceCode + SettingsFilePath: .github/linters/.powershell-psscriptanalyzer.psd1 ``` ## References and Links diff --git a/action.yml b/action.yml index 015de2b..bdc803f 100644 --- a/action.yml +++ b/action.yml @@ -9,14 +9,10 @@ inputs: Path: description: The path to the code to test. required: false - Settings: - description: The type of tests to run. Can be either 'Module', 'SourceCode' or 'Custom'. - required: false - default: Custom SettingsFilePath: - description: If 'Custom' is selected, the path to the settings file. + description: The path to the settings file. required: false - default: ${{ github.workspace }}/.github/linters/.powershell-psscriptanalyzer.psd1 + default: .github/linters/.powershell-psscriptanalyzer.psd1 Debug: description: Enable debug output. required: false @@ -251,7 +247,6 @@ runs: id: paths env: PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_Path: ${{ inputs.Path }} - PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_Settings: ${{ inputs.Settings }} PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_SettingsFilePath: ${{ inputs.SettingsFilePath }} with: Debug: ${{ inputs.Debug }} @@ -265,7 +260,6 @@ runs: uses: PSModule/Invoke-Pester@v4 id: test env: - Settings: ${{ fromJson(steps.paths.outputs.result).Settings }} SettingsFilePath: ${{ fromJson(steps.paths.outputs.result).SettingsFilePath }} with: Debug: ${{ inputs.Debug }} diff --git a/scripts/main.ps1 b/scripts/main.ps1 index 8d0d543..7e84a75 100644 --- a/scripts/main.ps1 +++ b/scripts/main.ps1 @@ -1,28 +1,20 @@ -# If test type is module, the code we ought to test is in the path/name folder, otherwise it's in the path folder. -$settings = $env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_Settings +# Resolve paths for testing $testPath = Resolve-Path -Path "$PSScriptRoot/tests/PSScriptAnalyzer" | Select-Object -ExpandProperty Path $path = [string]::IsNullOrEmpty($env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_Path) ? '.' : $env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_Path $codePath = Resolve-Path -Path $path | Select-Object -ExpandProperty Path -$settingsFilePath = switch -Regex ($settings) { - 'Module|SourceCode' { - "$testPath/$settings.Settings.psd1" - } - 'Custom' { - Resolve-Path -Path $env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_SettingsFilePath | Select-Object -ExpandProperty Path - } - default { - throw "Invalid test type: [$settings]" - } +$settingsFilePath = Join-Path -Path $codePath -ChildPath $env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_SettingsFilePath + +if (!(Test-Path -Path $settingsFilePath)) { + Write-Error "Settings file not found at path: $settingsFilePath" + exit 1 } [pscustomobject]@{ - Settings = $settings CodePath = $codePath TestPath = $testPath SettingsFilePath = $settingsFilePath } | Format-List | Out-String -Set-GitHubOutput -Name Settings -Value $settings Set-GitHubOutput -Name CodePath -Value $codePath Set-GitHubOutput -Name TestPath -Value $testPath Set-GitHubOutput -Name SettingsFilePath -Value $settingsFilePath diff --git a/scripts/tests/PSScriptAnalyzer/Module.Settings.psd1 b/tests/outputTestRepo/.github/linters/.powershell-psscriptanalyzer.psd1 similarity index 100% rename from scripts/tests/PSScriptAnalyzer/Module.Settings.psd1 rename to tests/outputTestRepo/.github/linters/.powershell-psscriptanalyzer.psd1 diff --git a/scripts/tests/PSScriptAnalyzer/SourceCode.Settings.psd1 b/tests/srcTestRepo/.github/linters/.powershell-psscriptanalyzer.psd1 similarity index 100% rename from scripts/tests/PSScriptAnalyzer/SourceCode.Settings.psd1 rename to tests/srcTestRepo/.github/linters/.powershell-psscriptanalyzer.psd1 diff --git a/tests/srcWithManifestTestRepo/tests/SourceCode.Settings.psd1 b/tests/srcWithManifestTestRepo/tests/SourceCode.Settings.psd1 new file mode 100644 index 0000000..09cc3d0 --- /dev/null +++ b/tests/srcWithManifestTestRepo/tests/SourceCode.Settings.psd1 @@ -0,0 +1,56 @@ +@{ + Rules = @{ + PSAlignAssignmentStatement = @{ + Enable = $true + CheckHashtable = $true + } + PSAvoidLongLines = @{ + Enable = $true + MaximumLineLength = 150 + } + PSAvoidSemicolonsAsLineTerminators = @{ + Enable = $true + } + PSPlaceCloseBrace = @{ + Enable = $true + NewLineAfter = $false + IgnoreOneLineBlock = $true + NoEmptyLineBefore = $false + } + PSPlaceOpenBrace = @{ + Enable = $true + OnSameLine = $true + NewLineAfter = $true + IgnoreOneLineBlock = $true + } + PSProvideCommentHelp = @{ + Enable = $true + ExportedOnly = $false + BlockComment = $true + VSCodeSnippetCorrection = $false + Placement = 'begin' + } + PSUseConsistentIndentation = @{ + Enable = $true + IndentationSize = 4 + PipelineIndentation = 'IncreaseIndentationForFirstPipeline' + Kind = 'space' + } + PSUseConsistentWhitespace = @{ + Enable = $true + CheckInnerBrace = $true + CheckOpenBrace = $true + CheckOpenParen = $true + CheckOperator = $true + CheckPipe = $true + CheckPipeForRedundantWhitespace = $true + CheckSeparator = $true + CheckParameter = $true + IgnoreAssignmentOperatorInsideHashTable = $true + } + } + ExcludeRules = @( + 'PSMissingModuleManifestField', # This rule is not applicable until the module is built. + 'PSUseToExportFieldsInManifest' + ) +} From a2b93ad388237281e2f5ec178bbbd0ce68a25ccf Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 11 Oct 2025 17:03:20 +0200 Subject: [PATCH 2/7] Update README.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6530bae..0b234ef 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ The action provides the following outputs: settings file is in the format of a `.psd1` file. By default, the action looks for a settings file at: - `${{ github.workspace }}/.github/linters/.powershell-psscriptanalyzer.psd1` + `.github/linters/.powershell-psscriptanalyzer.psd1` You can override this by setting the `SettingsFilePath` input to point to your custom settings file. From 1ffbf287413e92bcf6668704a7349d1d904a166a Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 11 Oct 2025 17:10:00 +0200 Subject: [PATCH 3/7] Reorder settings file validation to improve script flow --- scripts/main.ps1 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/scripts/main.ps1 b/scripts/main.ps1 index 7e84a75..c5646b4 100644 --- a/scripts/main.ps1 +++ b/scripts/main.ps1 @@ -4,17 +4,17 @@ $path = [string]::IsNullOrEmpty($env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_Path) $codePath = Resolve-Path -Path $path | Select-Object -ExpandProperty Path $settingsFilePath = Join-Path -Path $codePath -ChildPath $env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_SettingsFilePath -if (!(Test-Path -Path $settingsFilePath)) { - Write-Error "Settings file not found at path: $settingsFilePath" - exit 1 -} - [pscustomobject]@{ CodePath = $codePath TestPath = $testPath SettingsFilePath = $settingsFilePath } | Format-List | Out-String +if (!(Test-Path -Path $settingsFilePath)) { + Write-Error "Settings file not found at path: $settingsFilePath" + exit 1 +} + Set-GitHubOutput -Name CodePath -Value $codePath Set-GitHubOutput -Name TestPath -Value $testPath Set-GitHubOutput -Name SettingsFilePath -Value $settingsFilePath From af4f33ca59d77f400367daafd4b37e3c9a003c46 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 11 Oct 2025 17:16:49 +0200 Subject: [PATCH 4/7] Refactor settings file path handling to use environment variable directly --- scripts/main.ps1 | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/scripts/main.ps1 b/scripts/main.ps1 index c5646b4..568b3ec 100644 --- a/scripts/main.ps1 +++ b/scripts/main.ps1 @@ -2,19 +2,18 @@ $testPath = Resolve-Path -Path "$PSScriptRoot/tests/PSScriptAnalyzer" | Select-Object -ExpandProperty Path $path = [string]::IsNullOrEmpty($env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_Path) ? '.' : $env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_Path $codePath = Resolve-Path -Path $path | Select-Object -ExpandProperty Path -$settingsFilePath = Join-Path -Path $codePath -ChildPath $env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_SettingsFilePath [pscustomobject]@{ CodePath = $codePath TestPath = $testPath - SettingsFilePath = $settingsFilePath + SettingsFilePath = $env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_SettingsFilePath } | Format-List | Out-String -if (!(Test-Path -Path $settingsFilePath)) { - Write-Error "Settings file not found at path: $settingsFilePath" +if (!(Test-Path -Path $env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_SettingsFilePath)) { + Write-Error "Settings file not found at path: $env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_SettingsFilePath" exit 1 } Set-GitHubOutput -Name CodePath -Value $codePath Set-GitHubOutput -Name TestPath -Value $testPath -Set-GitHubOutput -Name SettingsFilePath -Value $settingsFilePath +Set-GitHubOutput -Name SettingsFilePath -Value $env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_SettingsFilePath From 14b3bb268e9cdbb0546aa778e111411c0fa584e9 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 11 Oct 2025 17:22:29 +0200 Subject: [PATCH 5/7] Refactor settings file path handling to use resolved path variable --- scripts/main.ps1 | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/scripts/main.ps1 b/scripts/main.ps1 index 568b3ec..8facb1b 100644 --- a/scripts/main.ps1 +++ b/scripts/main.ps1 @@ -2,18 +2,19 @@ $testPath = Resolve-Path -Path "$PSScriptRoot/tests/PSScriptAnalyzer" | Select-Object -ExpandProperty Path $path = [string]::IsNullOrEmpty($env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_Path) ? '.' : $env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_Path $codePath = Resolve-Path -Path $path | Select-Object -ExpandProperty Path +$settingsFilePath = Resolve-Path -Path $env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_SettingsFilePath | Select-Object -ExpandProperty Path [pscustomobject]@{ CodePath = $codePath TestPath = $testPath - SettingsFilePath = $env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_SettingsFilePath + SettingsFilePath = $settingsFilePath } | Format-List | Out-String -if (!(Test-Path -Path $env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_SettingsFilePath)) { - Write-Error "Settings file not found at path: $env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_SettingsFilePath" +if (!(Test-Path -Path $settingsFilePath)) { + Write-Error "Settings file not found at path: $settingsFilePath" exit 1 } Set-GitHubOutput -Name CodePath -Value $codePath Set-GitHubOutput -Name TestPath -Value $testPath -Set-GitHubOutput -Name SettingsFilePath -Value $env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_SettingsFilePath +Set-GitHubOutput -Name SettingsFilePath -Value $settingsFilePath From 353cbcc18b1812c297abcdb164bef9963eee8a7f Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 11 Oct 2025 17:29:35 +0200 Subject: [PATCH 6/7] Update README.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0b234ef..bd8f204 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ customize rule selection, severity filtering, and custom rule inclusion. | Input | Description | Required | Default | |--------------------------------------|--------------------------------------------------------------------------------|----------|-----------------------------------------------------------------------------| | `Path` | The path to the code to test. | false | `'.'` | -| `SettingsFilePath` | The path to the settings file. | false | `${{ github.workspace }}/.github/linters/.powershell-psscriptanalyzer.psd1` | +| `SettingsFilePath` | The path to the settings file. | false | `.github/linters/.powershell-psscriptanalyzer.psd1` | | `Debug` | Enable debug output. | false | `'false'` | | `Verbose` | Enable verbose output. | false | `'false'` | | `Version` | Specifies the exact version of the GitHub module to install. | false | | From 7ed9e4c612f798144e0d488b413d2a88ba2b87c9 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 11 Oct 2025 17:32:50 +0200 Subject: [PATCH 7/7] Update scripts/main.ps1 Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- scripts/main.ps1 | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/scripts/main.ps1 b/scripts/main.ps1 index 8facb1b..fd2ca24 100644 --- a/scripts/main.ps1 +++ b/scripts/main.ps1 @@ -11,10 +11,8 @@ $settingsFilePath = Resolve-Path -Path $env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT } | Format-List | Out-String if (!(Test-Path -Path $settingsFilePath)) { - Write-Error "Settings file not found at path: $settingsFilePath" - exit 1 + throw "Settings file not found at path: $settingsFilePath" } - Set-GitHubOutput -Name CodePath -Value $codePath Set-GitHubOutput -Name TestPath -Value $testPath Set-GitHubOutput -Name SettingsFilePath -Value $settingsFilePath