Skip to content

Commit f741c1e

Browse files
🩹 [Patch]: Obey settings for skips (#2)
## Description This pull request includes several changes to improve the GitHub Actions workflows, update the documentation, and enhance the PSScriptAnalyzer settings. ### Documentation Updates * Updated `README.md` to improve clarity, add dependencies, and update the example workflow section. * Added a new `Settings.md` file to document the PSScriptAnalyzer settings file format and key configuration options. ### Custom PSScriptAnalyzer Settings * Added a new workflow configuration file `.github/workflows/Action-Test-Src-Default-Custom.yml` to run tests showing that the `Custom` settings and a `SettingsPath` works as it should. * Added a new custom settings file `tests/srcTestRepo/tests/Custom.Settings.psd1` with specific rules and configurations for PSScriptAnalyzer. * Fixed `scripts/tests/PSScriptAnalyzer/PSScriptAnalyzer.Tests.ps1` to include new log outputs and rule evaluation logic. This now skips tests as per the settings file that is used. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [ ] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas
1 parent 8671d8b commit f741c1e

File tree

9 files changed

+304
-199
lines changed

9 files changed

+304
-199
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Action-Test [Src-Default-Custom]
2+
3+
run-name: "Action-Test [Src-Default-Custom] - [${{ github.event.pull_request.title }} #${{ github.event.pull_request.number }}] by @${{ github.actor }}"
4+
5+
on:
6+
workflow_dispatch:
7+
pull_request:
8+
schedule:
9+
- cron: '0 0 * * *'
10+
11+
concurrency:
12+
group: ${{ github.workflow }}-${{ github.ref }}
13+
cancel-in-progress: true
14+
15+
permissions: {}
16+
17+
jobs:
18+
ActionTestCustom:
19+
uses: ./.github/workflows/ActionTestWorkflow.yml
20+
with:
21+
TestType: Src-Default-Custom
22+
Path: tests/srcTestRepo/src
23+
Settings: Custom
24+
SettingsFilePath: tests/srcTestRepo/tests/Custom.Settings.psd1

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,3 @@ jobs:
2121
TestType: outputs
2222
Path: tests/outputTestRepo/outputs/modules/PSModuleTest
2323
Settings: Module
24-
SettingsFilePath:

README.md

Lines changed: 48 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,66 @@
1-
# Invoke-ScriptAnalyzer (by PSModule)
1+
# Invoke-ScriptAnalyzer
22

3-
This repository contains a GitHub Action that runs PSScriptAnalyzer on your code.
3+
This repository contains a GitHub Action that runs [`PSScriptAnalyzer`](https://github.com/PowerShell/PSScriptAnalyzer) on your code.
44
The action analyzes PowerShell scripts using a hashtable-based settings file to
55
customize rule selection, severity filtering, and custom rule inclusion.
66

7-
> **Note:** This repository includes automated tests that run via Pester to ensure
8-
> your settings file is working as expected.
7+
## Dependencies
98

10-
## Action Details
11-
12-
- **Name:** Invoke-ScriptAnalyzer (by PSModule)
13-
- **Description:** Runs PSScriptAnalyzer on the code.
14-
- **Author:** PSModule
15-
- **Branding:**
16-
Icon: `check-square`
17-
Color: `gray-dark`
9+
- This action.
10+
- [`PSScriptAnalyzer` module](https://github.com/PowerShell/PSScriptAnalyzer).
11+
- [`Invoke-Pester` action](https://github.com/PSModule/Invoke-Pester)
12+
- [`Pester` module](https://github.com/Pester/Pester)
13+
- [`GitHub-Script` action](https://github.com/PSModule/GitHub-Script)
14+
- [`GitHub` module](https://github.com/PSModule/GitHub)
1815

1916
## Inputs
2017

21-
| Input | Description | Required | Default |
22-
|---------------------|-------------------------------------------------------------------|----------|-----------------------------------------------------------------------------|
23-
| **Path** | The path to the code to test. | Yes | `${{ github.workspace }}` |
24-
| **Settings** | The type of tests to run: `Module`, `SourceCode`, or `Custom`. | No | `Custom` |
25-
| **SettingsFilePath**| If `Custom` is selected, the path to the settings file. | No | `${{ github.workspace }}/.github/linters/.powershell-psscriptanalyzer.psd1` |
18+
| Input | Description | Required | Default |
19+
|---------------------|----------------------------------------------------------------|----------|-----------------------------------------------------------------------------|
20+
| **Path** | The path to the code to test. | Yes | `${{ github.workspace }}` |
21+
| **Settings** | The type of tests to run: `Module`, `SourceCode`, or `Custom`. | No | `Custom` |
22+
| **SettingsFilePath**| If `Custom` is selected, the path to the settings file. | No | `${{ github.workspace }}/.github/linters/.powershell-psscriptanalyzer.psd1` |
2623

2724
## Outputs
2825

29-
| Output | Description | Value |
30-
|---------|---------------------------------------|--------------------------------------------|
31-
| passed | Indicates if the tests passed. | `${{ steps.test.outputs.Passed }}` |
26+
| Output | Description | Value |
27+
|----------|--------------------------------|------------------------------------|
28+
| `passed` | Indicates if the tests passed. | `${{ steps.test.outputs.Passed }}` |
3229

33-
## Files Overview
30+
## How It Works
3431

35-
- **action.yml**
36-
Describes the action inputs, outputs, and run steps. The action uses a
37-
composite run steps approach with two main steps:
38-
1. **Get test paths:** Uses a script to resolve paths and settings.
39-
2. **Invoke-Pester:** Runs Pester tests against PSScriptAnalyzer.
32+
1. **Set a Path**
33+
Choose a path for your code to test into the `Path` input. This can be a
34+
directory or a file.
4035

41-
- **scripts/main.ps1**
42-
Determines the correct settings file path based on the test type. It
43-
supports testing a module, source code, or using a custom settings file.
36+
2. **Choose settings**
37+
Choose the type of tests to run by setting the `Settings` input. The options
38+
are `Module`, `SourceCode`, or `Custom`. The default is `Custom`.
4439

45-
- **scripts/tests/PSScriptAnalyzer/**
46-
Contains Pester tests that run PSScriptAnalyzer using the provided settings
47-
file. The tests check for issues reported by PSScriptAnalyzer based on rule
48-
configuration.
40+
The predefined settings:
41+
- [`Module`](./scripts/tests/PSScriptAnalyzer/Module.Settings.psd1): Analyzes a module following PSModule standards.
42+
- [`SourceCode`](./scripts/tests/PSScriptAnalyzer/SourceCode.Settings.psd1): Analyzes the source code following PSModule standards.
4943

50-
## How It Works
44+
You can also create a custom settings file to customize the analysis. The
45+
settings file is a hashtable that defines the rules to include, exclude, or
46+
customize. The settings file is in the format of a `.psd1` file.
5147

52-
1. **Path Resolution:**
53-
The action reads inputs and determines the code path, test path, and the
54-
settings file path. For custom settings, it uses the file at:
55-
```powershell
56-
.github/linters/.powershell-psscriptanalyzer.psd1
57-
```
58-
Otherwise, it uses a default settings file from the test folder.
48+
For more info on how to create a settings file, see the [Settings Documentation](./Settings.md) file.
5949

60-
2. **Pester Testing:**
61-
The tests import the settings file and use `Invoke-ScriptAnalyzer` to scan
50+
3. **Run the Action**
51+
The tests import the settings file and use `Invoke-ScriptAnalyzer` to analyze
6252
the code. Each rule is evaluated, and if a rule violation is found, the test
6353
will fail for that rule. Rules that are marked to be skipped (via exclusions
6454
in the settings file) are automatically skipped in the test.
6555

66-
3. **Automation:**
67-
Designed for CI/CD, this action integrates with GitHub Actions, Azure Pipelines,
68-
and other systems. The settings file customizes analysis, letting you control
69-
rule inclusion, severity filtering, and custom rule paths.
56+
To be clear; the action follows the settings file to determine which rules to skip.
57+
58+
4. **View the Results**
59+
The action outputs the results of the tests. If the tests pass, the action
60+
will return a `passed` output with a value of `true`. If the tests fail, the
61+
action will return a `passed` output with a value of `false`.
62+
63+
The action also outputs the results of the tests to the console.
7064

7165
## Example Workflow
7266

@@ -81,23 +75,21 @@ jobs:
8175
lint:
8276
runs-on: ubuntu-latest
8377
steps:
84-
- uses: actions/checkout@v2
78+
- name: Checkout code
79+
uses: actions/checkout@v2
80+
8581
- name: Invoke PSScriptAnalyzer
8682
uses: PSModule/Invoke-ScriptAnalyzer@v1
8783
with:
8884
Path: ${{ github.workspace }}
89-
Settings: Custom
90-
SettingsFilePath: ${{ github.workspace }}/.github/linters/.powershell-psscriptanalyzer.psd1
85+
Settings: SourceCode
9186
```
9287
93-
## Appendix: Settings File Documentation
94-
95-
For detailed documentation on the format of the settings file, see the
96-
[Settings File Documentation](./SettingsFileDocumentation.md) file.
97-
9888
## References and Links
9989
10090
- [PSScriptAnalyzer Documentation](https://learn.microsoft.com/powershell/module/psscriptanalyzer/)
101-
- [GitHub Super-Linter](https://github.com/github/super-linter)
91+
- [PSScriptAnalyzer Module Overview](https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/overview?view=ps-modules)
92+
- [PSScriptAnalyzer Rules](https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/rules/readme?view=ps-modules)
10293
- [PSScriptAnalyzer GitHub Repository](https://github.com/PowerShell/PSScriptAnalyzer)
10394
- [Custom Rules in PSScriptAnalyzer](https://docs.microsoft.com/powershell/scripting/developer/hosting/psscriptanalyzer-extensibility)
95+
- [GitHub Super-Linter](https://github.com/github/super-linter)

Settings.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# PSScriptAnalyzer Settings File Format Documentation
2+
3+
This document describes the format and usage of the hashtable-based settings file
4+
for PSScriptAnalyzer. The file is used by the GitHub action to customize analysis.
5+
6+
## Basic Setup
7+
8+
The file is a PowerShell data file (.psd1) that returns a hashtable. For example:
9+
```powershell
10+
@{
11+
Severity = @('Error','Warning')
12+
ExcludeRules = @('PSAvoidUsingWriteHost')
13+
}
14+
```
15+
This example sets the severity filter and excludes a specific rule.
16+
17+
## Key Configuration Options
18+
19+
- **IncludeRules**
20+
A list of rules to run. Wildcards (e.g. `PSAvoid*`) are supported.
21+
22+
- **ExcludeRules**
23+
A list of rules to skip. Excludes take precedence over include lists.
24+
25+
- **Severity**
26+
Filters output by severity. Allowed values include `Error`, `Warning`, and `Information`.
27+
28+
- **IncludeDefaultRules**
29+
A Boolean switch to include default rules when using custom rules.
30+
31+
- **CustomRulePath**
32+
One or more paths to custom rule modules or scripts. These extend PSScriptAnalyzer.
33+
34+
- **RecurseCustomRulePath**
35+
Boolean to search subdirectories of the custom rule path(s) for more rule files.
36+
37+
- **Rules**
38+
A nested hashtable for rule-specific settings. Use it to pass parameters to rules.
39+
40+
```powershell
41+
Rules = @{
42+
PSAvoidUsingCmdletAliases = @{
43+
Enabled = $true
44+
Whitelist = @('ls','gc')
45+
}
46+
}
47+
```
48+
49+
## Configuring Custom Rules
50+
51+
Custom rules are implemented in modules (.psm1) or scripts. They must export
52+
functions that return DiagnosticRecord objects. Specify their location using
53+
`CustomRulePath`. Use `IncludeDefaultRules = $true` if you want to run both
54+
default and custom rules.
55+
56+
For example:
57+
```powershell
58+
@{
59+
CustomRulePath = @('.\Modules\MyCustomRules.psm1')
60+
IncludeDefaultRules = $true
61+
IncludeRules = @('PSUseApprovedVerbs', 'Measure-*')
62+
}
63+
```
64+
65+
## Rule Execution and Skip Logic
66+
67+
The action evaluates each rule using several configurable settings to determine whether it should be executed or skipped.
68+
The evaluation is performed in the following order:
69+
70+
1. **Exclude Rules**
71+
- If the rule's name is present in the **`ExcludeRules`** list, the rule is skipped immediately, regardless of other settings.
72+
73+
2. **Include Rules**
74+
- If an **`IncludeRules`** list is provided, the rule must be part of this list. If the rule's name is *not* in the list, it is skipped.
75+
76+
3. **Severity Filtering**
77+
- If a **`Severity`** list is specified, the rule's severity must be included in that list. If the rule's severity is not part of the allowed
78+
values, the rule is skipped.
79+
80+
4. **Rule-Specific Configuration**
81+
- If a specific configuration exists for the rule under the **Rules** key, and its `Enable` property is set to false, the rule is skipped.
82+
83+
To see what rules are skipped and why, check the logs for the action. There is a log group inside the test that contains the rules that were skipped.

SettingsFileDocumentation.md

Lines changed: 0 additions & 116 deletions
This file was deleted.

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ runs:
3838
Script: ${{ github.action_path }}/scripts/main.ps1
3939

4040
- name: Invoke-Pester
41-
uses: PSModule/Invoke-Pester@fix
41+
uses: PSModule/Invoke-Pester@v2
4242
id: test
4343
env:
4444
Settings: ${{ fromJson(steps.paths.outputs.result).Settings }}

0 commit comments

Comments
 (0)