Skip to content

Commit

Permalink
🩹 [Patch]: Add static code analysis use case test (#35)
Browse files Browse the repository at this point in the history
## Description

- Add static code analysis use case test

## Type of change

<!-- Use the check-boxes [x] on the options that are relevant. -->

- [ ] 📖 [Docs]
- [ ] 🪲 [Fix]
- [x] 🩹 [Patch]
- [ ] ⚠️ [Security fix]
- [ ] 🚀 [Feature]
- [ ] 🌟 [Breaking change]

## Checklist

<!-- Use the check-boxes [x] on the options that are relevant. -->

- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
  • Loading branch information
MariusStorhaug authored Mar 27, 2024
1 parent f03f932 commit d15f697
Show file tree
Hide file tree
Showing 29 changed files with 706 additions and 82 deletions.
41 changes: 35 additions & 6 deletions .github/workflows/Action-Test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,40 @@ jobs:
strategy:
fail-fast: false
matrix:
shell: [pwsh]
os: [ubuntu-latest, macos-latest, windows-latest]
include:
- shell: powershell
os: windows-latest
name: Action-Test - [${{ matrix.os }}@${{ matrix.shell }}]
- os: ubuntu-latest
shell: pwsh
path: tests/src
testtype: SourceCode
- os: ubuntu-latest
shell: pwsh
path: tests/outputs/modules
testtype: Module
- os: macos-latest
shell: pwsh
path: tests/src
testtype: sourcecode
- os: macos-latest
shell: pwsh
path: tests/outputs/modules
testtype: module
- os: windows-latest
shell: pwsh
path: tests/src
testtype: sourcecode
- os: windows-latest
shell: pwsh
path: tests/outputs/modules
testtype: module
- os: windows-latest
shell: powershell
path: tests/src
testtype: sourcecode
- os: windows-latest
shell: powershell
path: tests/outputs/modules
testtype: module
name: Action-Test - [${{ matrix.os }}@${{ matrix.shell }}] - [${{ matrix.path }}]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout repo
Expand All @@ -33,5 +61,6 @@ jobs:
GITHUB_TOKEN: ${{ github.token }}
with:
Name: PSModuleTest
Path: tests/outputs/modules
Path: ${{ matrix.path }}
Shell: ${{ matrix.shell }}
TestType: ${{ matrix.TestType }}
52 changes: 45 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This GitHub Action is a part of the [PSModule framework](https://github.com/PSMo

## Specifications and practices

Test-PSModule follows:
Test-PSModule enables:

- [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)

Expand All @@ -15,15 +15,53 @@ Test-PSModule follows:
The action runs the following the Pester test framework:
- [PSScriptAnalyzer tests](https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/rules/readme?view=ps-modules)
- [PSModule framework tests](#psmodule-tests)
- If `RunModuleTests` is set to `true`:
- Custom module tests from the `tests` directory in the module repository.
- Module manifest tests using [Test-ModuleManifest](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/test-modulemanifest)
- If `TestType` is set to `Module`:
- The module manifest is:
- tested using [Test-ModuleManifest](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/test-modulemanifest).
- temporarily altered to version `999.0.0` to avoid version conflicts when running for dependencies of the framework.
- The module is imported.
- Custom module tests from the `tests` directory in the module repository are run.
- CodeCoverage is calculated.
- The following reports are calculated and uploaded as artifacts:
- Test suite results.
- Code coverage results.
- If `TestType` is set to `SourceCode`:
- The source code is tested with:
- `PSScriptAnalyzer` for best practices, using custom settings.
- `PSModule.SourceCode` for other PSModule standards.

The action fails if any of the tests fail or it fails to run the tests.

## How to use it

To use the action, create a new file in the `.github/workflows` directory of the module repository and add the following content.
<details>
<summary>Workflow suggestion - before module is built</summary>

```yaml
name: Test-PSModule

on: [push]

jobs:
Test-PSModule:
name: Test-PSModule
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4

- name: Initialize environment
uses: PSModule/Initialize-PSModule@main

- name: Test-PSModule
uses: PSModule/Test-PSModule@main
with:
Path: src
TestType: SourceCode

```
</details>

<details>
<summary>Workflow suggestion - after module is built</summary>
Expand All @@ -47,8 +85,8 @@ jobs:
- name: Test-PSModule
uses: PSModule/Test-PSModule@main
with:
Name: PSModule
Path: outputs/modules
TestType: Module

```
</details>
Expand All @@ -59,9 +97,9 @@ jobs:

| Name | Description | Required | Default |
| ---- | ----------- | -------- | ------- |
| `Name` | The name of the module to test. The name of the repository is used if not specified. | `false` | |
| `Path` | The path to the module to test. | `true` | |
| `RunModuleTests` | Run the module tests. | `false` | `true` |
| `TestType` | The type of tests to run. Can be either `Module` or `SourceCode`. | `true` | |
| `Name` | The name of the module to test. The name of the repository is used if not specified. | `false` | |
| `Shell` | The shell to use for running the tests. | `false` | `pwsh` |

## PSModule tests
Expand Down
23 changes: 18 additions & 5 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ inputs:
Path:
description: The path to the module to test.
required: true
RunModuleTests:
description: Run the module tests.
required: false
default: 'true'
TestType:
description: The type of tests to run. Can be either 'Module' or 'SourceCode'.
required: true
Shell:
description: The shell to use for running the tests.
required: false
Expand All @@ -29,7 +28,21 @@ runs:
env:
GITHUB_ACTION_INPUT_Name: ${{ inputs.Name }}
GITHUB_ACTION_INPUT_Path: ${{ inputs.Path }}
GITHUB_ACTION_INPUT_RunModuleTests: ${{ inputs.RunModuleTests }}
GITHUB_ACTION_INPUT_TestType: ${{ inputs.TestType }}
run: |
# Test-PSModule
. "$env:GITHUB_ACTION_PATH\scripts\main.ps1" -Verbose
- name: Upload test results
uses: actions/upload-artifact@v4
if: ${{ inputs.TestType == 'Module' }}
with:
name: ${{ runner.os }}-${{ inputs.Shell }}-Test-Report
path: ${{ github.workspace }}/outputs/Test-Report.xml

- name: Upload code coverage report
uses: actions/upload-artifact@v4
if: ${{ inputs.TestType == 'Module' }}
with:
name: ${{ runner.os }}-${{ inputs.Shell }}-CodeCoverage-Report
path: ${{ github.workspace }}/outputs/CodeCoverage-Report.xml
96 changes: 59 additions & 37 deletions scripts/helpers/Test-PSModule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,19 @@ function Test-PSModule {

# Run module tests.
[Parameter()]
[switch] $RunModuleTests
[ValidateSet('SourceCode', 'Module')]
[string] $TestType = 'SourceCode'
)

$moduleName = Split-Path -Path $Path -Leaf

#region Test Module Manifest
Start-LogGroup 'Test Module Manifest'
$moduleManifestPath = Join-Path -Path $Path -ChildPath "$moduleName.psd1"
if (Test-Path -Path $moduleManifestPath) {
try {
$status = Test-ModuleManifest -Path $moduleManifestPath
} catch {
Write-Warning "⚠️ Test-ModuleManifest failed: $moduleManifestPath"
throw $_.Exception.Message
}
Write-Verbose ($status | Format-List | Out-String) -Verbose
} else {
Write-Warning "⚠️ Module manifest not found: $moduleManifestPath"
}
Stop-LogGroup
#endregion
$testSourceCode = $TestType -eq 'SourceCode'
$testModule = $TestType -eq 'Module'
$moduleTestsPath = Join-Path $env:GITHUB_WORKSPACE 'tests'

#region Get test kit versions
Start-LogGroup 'Get test kit versions'
$PSSAModule = Get-PSResource -Name PSScriptAnalyzer | Sort-Object Version -Descending | Select-Object -First 1
$pesterModule = Get-PSResource -Name Pester | Sort-Object Version -Descending | Select-Object -First 1
$PSSAModule = Get-PSResource -Name PSScriptAnalyzer -Verbose:$false | Sort-Object Version -Descending | Select-Object -First 1
$pesterModule = Get-PSResource -Name Pester -Verbose:$false | Sort-Object Version -Descending | Select-Object -First 1

Write-Verbose 'Testing with:'
Write-Verbose " PowerShell $($PSVersionTable.PSVersion.ToString())"
Expand All @@ -57,6 +44,7 @@ function Test-PSModule {
Data = @{
Path = $Path
SettingsFilePath = Join-Path $PSSATestsPath 'PSScriptAnalyzer.Tests.psd1'
Verbose = $true
}
}
Write-Verbose 'ContainerParams:'
Expand All @@ -67,11 +55,11 @@ function Test-PSModule {

#region Add test - Common - PSModule
Start-LogGroup 'Add test - Common - PSModule'
$PSModuleTestsPath = Join-Path -Path $env:GITHUB_ACTION_PATH -ChildPath 'scripts\tests\PSModule'
$containerParams = @{
Path = $PSModuleTestsPath
Path = Join-Path -Path $env:GITHUB_ACTION_PATH -ChildPath 'scripts\tests\PSModule\Common.Tests.ps1'
Data = @{
Path = $Path
Path = $Path
Verbose = $true
}
}
Write-Verbose 'ContainerParams:'
Expand All @@ -80,15 +68,49 @@ function Test-PSModule {
Stop-LogGroup
#endregion

#region Add test - Specific - $moduleName
if ($RunModuleTests) {
$moduleTestsPath = Join-Path $env:GITHUB_WORKSPACE 'tests'
#region Add test - Module - PSModule
if ($testModule) {
Start-LogGroup 'Add test - Module - PSModule'
$containerParams = @{
Path = Join-Path -Path $env:GITHUB_ACTION_PATH -ChildPath 'scripts\tests\PSModule\Module.Tests.ps1'
Data = @{
Path = $Path
Verbose = $true
}
}
Write-Verbose 'ContainerParams:'
Write-Verbose "$($containerParams | ConvertTo-Json)"
$containers += New-PesterContainer @containerParams
Stop-LogGroup
}
#endregion

#region Add test - SourceCode - PSModule
if ($testSourceCode) {
Start-LogGroup 'Add test - SourceCode - PSModule'
$containerParams = @{
Path = Join-Path -Path $env:GITHUB_ACTION_PATH -ChildPath 'scripts\tests\PSModule\SourceCode.Tests.ps1'
Data = @{
Path = $Path
Verbose = $true
}
}
Write-Verbose 'ContainerParams:'
Write-Verbose "$($containerParams | ConvertTo-Json)"
$containers += New-PesterContainer @containerParams
Stop-LogGroup
}
#endregion

#region Add test - Module - $moduleName
if ($testModule) {
if (Test-Path -Path $moduleTestsPath) {
Start-LogGroup "Add test - Specific - $moduleName"
Start-LogGroup "Add test - Module - $moduleName"
$containerParams = @{
Path = $moduleTestsPath
Data = @{
Path = $Path
Path = $Path
Verbose = $true
}
}
Write-Verbose 'ContainerParams:'
Expand All @@ -98,17 +120,17 @@ function Test-PSModule {
} else {
Write-Warning "⚠️ No tests found - [$moduleTestsPath]"
}
} else {
Write-Warning "⚠️ Module tests are disabled - [$moduleName]"
}
#endregion

#region Import module
if ((Test-Path -Path $moduleTestsPath) -and $RunModuleTests) {
if ((Test-Path -Path $moduleTestsPath) -and $testModule) {
Start-LogGroup "Importing module: $moduleName"
$moduleManifestPath = Join-Path -Path $Path -ChildPath "$moduleName.psd1"
Set-ModuleManifest -Path $moduleManifestPath -ModuleVersion '999.0.0'
Add-PSModulePath -Path (Split-Path $Path -Parent)
Get-Module -Name $moduleName -ListAvailable | Remove-Module -Force
Import-Module -Name $moduleName -Force -RequiredVersion 999.0.0 -Global
Import-Module -Name $moduleName -Force -RequiredVersion '999.0.0' -Global
Stop-LogGroup
}
#endregion
Expand All @@ -123,14 +145,14 @@ function Test-PSModule {
PassThru = $true
}
TestResult = @{
Enabled = $true
Enabled = $testModule
OutputFormat = 'NUnitXml'
OutputPath = '.\outputs\PSModuleTest.Results.xml'
TestSuiteName = 'PSModule Tests'
OutputPath = Join-Path -Path $env:GITHUB_WORKSPACE -ChildPath 'outputs\Test-Report.xml'
TestSuiteName = 'Unit tests'
}
CodeCoverage = @{
Enabled = $true
OutputPath = '.\outputs\CodeCoverage.xml'
Enabled = $testModule
OutputPath = Join-Path -Path $env:GITHUB_WORKSPACE -ChildPath 'outputs\CodeCoverage-Report.xml'
OutputFormat = 'JaCoCo'
OutputEncoding = 'UTF8'
CoveragePercentTarget = 75
Expand Down
7 changes: 3 additions & 4 deletions scripts/main.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ Write-Verbose "Code to test: [$codeToTest]"
if (-not (Test-Path -Path $codeToTest)) {
throw "Path [$codeToTest] does not exist."
}
$runModuleTests = $env:GITHUB_ACTION_INPUT_RunModuleTests -eq 'true'
Write-Verbose "Run module tests: [$runModuleTests]"
Write-Verbose "Test type to run: [$env:GITHUB_ACTION_INPUT_TestType]"

Stop-LogGroup

$params = @{
Path = $codeToTest
RunModuleTests = $runModuleTests
Path = $codeToTest
TestType = $env:GITHUB_ACTION_INPUT_TestType
}
$results = Test-PSModule @params

Expand Down
5 changes: 0 additions & 5 deletions scripts/tests/PSModule/CodingStyle.Tests.ps1

This file was deleted.

Loading

0 comments on commit d15f697

Please sign in to comment.