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..bd8f204 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/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/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..fd2ca24 100644 --- a/scripts/main.ps1 +++ b/scripts/main.ps1 @@ -1,28 +1,18 @@ -# 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 = Resolve-Path -Path $env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_SettingsFilePath | Select-Object -ExpandProperty Path [pscustomobject]@{ - Settings = $settings CodePath = $codePath TestPath = $testPath SettingsFilePath = $settingsFilePath } | Format-List | Out-String -Set-GitHubOutput -Name Settings -Value $settings +if (!(Test-Path -Path $settingsFilePath)) { + 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 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' + ) +}