|
1 | 1 | # Test-PSModule |
2 | 2 |
|
3 | | -Test PowerShell modules with Pester and PSScriptAnalyzer. |
| 3 | +Tests PowerShell module repos using PSModule framework rules. |
4 | 4 |
|
5 | | -This GitHub Action is a part of the [PSModule framework](https://github.com/PSModule). It is recommended to use the [Process-PSModule workflow](https://github.com/PSModule/Process-PSModule) to automate the whole process of managing the PowerShell module. |
| 5 | +This GitHub Action is a part of the [PSModule framework](https://github.com/PSModule). It is recommended to use the |
| 6 | +[Process-PSModule workflow](https://github.com/PSModule/Process-PSModule) to automate the whole process of managing the PowerShell module. |
6 | 7 |
|
7 | 8 | ## Specifications and practices |
8 | 9 |
|
9 | 10 | Test-PSModule enables: |
10 | 11 |
|
11 | | -- [Test-Driven Development](https://testdriven.io/test-driven-development/) using [Pester](https://pester.dev) and [PSScriptAnalyzer](https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/overview?view=ps-modules) |
| 12 | +- [Test-Driven Development](https://testdriven.io/test-driven-development/) using [Pester](https://pester.dev) via [Invoke-Pester](https://github.com/PSModule/Invoke-Pester). |
12 | 13 |
|
13 | 14 | ## How it works |
14 | 15 |
|
15 | | -The action runs the following the Pester test framework: |
16 | | -- [PSScriptAnalyzer tests](https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/rules/readme?view=ps-modules) |
17 | | -- [PSModule framework tests](#psmodule-tests) |
18 | | -- If `Settings` is set to `Module`: |
19 | | - - The module manifest is tested using [Test-ModuleManifest](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/test-modulemanifest). |
20 | | - - The module is imported. |
21 | | - - Custom module tests from the `tests` directory in the module repository are run. |
22 | | - - CodeCoverage is calculated. |
23 | | -- If `Settings` is set to `SourceCode`: |
24 | | - - The source code is tested with: |
25 | | - - `PSScriptAnalyzer` for best practices, using custom settings. |
26 | | - - `PSModule.SourceCode` for other PSModule standards. |
27 | | -- The action returns a `passed` output that is `true` if all tests pass, else `false`. |
28 | | -- The following reports are calculated and uploaded as artifacts: |
29 | | - - Test suite results. |
30 | | - - Code coverage results. |
| 16 | +- The action runs test on the module repository based on `Settings`: |
| 17 | + - `SourceCode` - Tests source code style and standards based on PSModule framework rules. |
| 18 | + - `Module` - Tests the module build module style and standards based on PSModule framework rules. |
| 19 | + - The module is imported in its own context to avoid conflicts with other modules. |
| 20 | +- The action returns the test results as action [outputs](#outputs). |
| 21 | +- The following reports are calculated and uploaded as artifacts. This is done to support the action being run in matrix jobs. |
| 22 | + - Test suite results. In [Process-PSModule](https://github.com/PSModule/Process-PSModule) this is evaluated in a later job by [Get-PesterTestResults](https://github.com/PSModule/Get-PesterTestResults) |
| 23 | + - Code coverage results. In [Process-PSModule](https://github.com/PSModule/Process-PSModule) this is evaluated in a later job by [Get-PesterCodeCoverage](https://github.com/PSModule/Get-PesterCodeCoverage) |
31 | 24 |
|
32 | 25 | The action fails if any of the tests fail or it fails to run the tests. |
33 | 26 | This is mitigated by the `continue-on-error` option in the workflow. |
34 | 27 |
|
35 | 28 | ## How to use it |
36 | 29 |
|
| 30 | +It is recommended to use the [Process-PSModule workflow](https://github.com/PSModule/Process-PSModule) to automate the whole process of managing the PowerShell module. |
| 31 | + |
37 | 32 | To use the action, create a new file in the `.github/workflows` directory of the module repository and add the following content. |
38 | 33 | <details> |
39 | 34 | <summary>Workflow suggestion - before module is built</summary> |
@@ -146,23 +141,42 @@ jobs: |
146 | 141 | | `TestDrive_Enabled` | Enable `TestDrive`. | | |
147 | 142 | | `TestRegistry_Enabled` | Enable `TestRegistry`. | | |
148 | 143 |
|
149 | | - |
150 | 144 | ### Outputs |
151 | 145 |
|
152 | | -| Name | Description | Possible values | |
153 | | -| ---- | ----------- | --------------- | |
154 | | -| `passed` | If the tests passed. | `true`, `false` | |
| 146 | +| Output | Description | |
| 147 | +|---------------|--------------------------------------| |
| 148 | +| `Outcome` | Outcome of the test run. | |
| 149 | +| `Conclusion` | Conclusion status of test execution. | |
| 150 | +| `Executed` | Indicates if tests were executed. | |
| 151 | +| `Result` | Overall result (`Passed`, `Failed`). | |
| 152 | +| `FailedCount` | Number of failed tests. | |
| 153 | +| `PassedCount` | Number of passed tests. | |
| 154 | +| `TotalCount` | Total tests executed. | |
155 | 155 |
|
156 | 156 | ## PSModule tests |
157 | 157 |
|
158 | | -The [PSModule framework tests](https://github.com/PSModule/Test-PSModule/blob/main/scripts/tests/PSModule/PSModule.Tests.ps1) verifies the following coding practices that the framework enforces: |
| 158 | +### SourceCode tests |
| 159 | + |
| 160 | +The [PSModule - SourceCode tests](./scripts/tests/SourceCode/PSModule/PSModule.Tests.ps1) verifies the following coding practices that the framework enforces: |
| 161 | + |
| 162 | +| ID | Category | Description | |
| 163 | +|---------------------|---------------------|--------------------------------------------------------------------------------------------| |
| 164 | +| NumberOfProcessors | General | Should use `[System.Environment]::ProcessorCount` instead of `$env:NUMBER_OF_PROCESSORS`. | |
| 165 | +| Verbose | General | Should not contain `-Verbose` unless it is explicitly disabled with `:$false`. | |
| 166 | +| OutNull | General | Should use `$null = ...` instead of piping output to `Out-Null`. | |
| 167 | +| NoTernary | General | Should not use ternary operations to maintain compatibility with PowerShell 5.1 and below. | |
| 168 | +| LowercaseKeywords | General | All PowerShell keywords should be written in lowercase. | |
| 169 | +| FunctionCount | Functions (Generic) | Each script file should contain exactly one function or filter. | |
| 170 | +| FunctionName | Functions (Generic) | Script filenames should match the name of the function or filter they contain. | |
| 171 | +| CmdletBinding | Functions (Generic) | Functions should include the `[CmdletBinding()]` attribute. | |
| 172 | +| ParamBlock | Functions (Generic) | Functions should have a parameter block (`param()`). | |
| 173 | +| FunctionTest | Functions (Public) | All public functions/filters should have corresponding tests. | |
159 | 174 |
|
160 | | -- Script filename and function/filter name should match. |
| 175 | +### Module tests |
161 | 176 |
|
162 | | -## Tools |
| 177 | +The [PSModule - Module tests](./scripts/tests/Module/PSModule/PSModule.Tests.ps1) verifies the following coding practices that the framework enforces: |
163 | 178 |
|
164 | | -- Pester | [Docs](https://www.pester.dev) | [GitHub](https://github.com/Pester/Pester) | [PS Gallery](https://www.powershellgallery.com/packages/Pester/) |
165 | | -- PSScriptAnalyzer [Docs](https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/overview?view=ps-modules) | [GitHub](https://github.com/PowerShell/PSScriptAnalyzer) | [PS Gallery](https://www.powershellgallery.com/packages/PSScriptAnalyzer/) |
166 | | -- PSResourceGet | [Docs](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.psresourceget/?view=powershellget-3.x) | [GitHub](https://github.com/PowerShell/PSResourceGet) | [PS Gallery](https://www.powershellgallery.com/packages/Microsoft.PowerShell.PSResourceGet/) |
167 | | -- [Test-ModuleManifest | Microsoft Learn](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/test-modulemanifest) |
168 | | -- [PowerShellGet | Microsoft Learn](https://learn.microsoft.com/en-us/powershell/module/PowerShellGet/test-scriptfileinfo) |
| 179 | +| Name | Description | |
| 180 | +| ------ | ----------- | |
| 181 | +| Module Manifest exists | Verifies that a module manifest file is present. | |
| 182 | +| Module Manifest is valid | Verifies that the module manifest file is valid. | |
0 commit comments