Skip to content

Commit f03f932

Browse files
🩹 [Patch]: Test module manifest before import (#33)
## Description - Test module manifest before attempting to import. - Import version 999.0.0 as built from `Build-PSModule`. ## 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
1 parent 89eb966 commit f03f932

24 files changed

+36
-573
lines changed

.github/workflows/Action-Test.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,10 @@ jobs:
1313
matrix:
1414
shell: [pwsh]
1515
os: [ubuntu-latest, macos-latest, windows-latest]
16-
path: [tests/src, tests/outputs/modules]
1716
include:
1817
- shell: powershell
1918
os: windows-latest
20-
path: tests/src
21-
- shell: powershell
22-
os: windows-latest
23-
path: tests/outputs/modules
24-
name: Action-Test - [${{ matrix.os }}@${{ matrix.shell }}] - [${{ matrix.path }}]
19+
name: Action-Test - [${{ matrix.os }}@${{ matrix.shell }}]
2520
runs-on: ${{ matrix.os }}
2621
steps:
2722
- name: Checkout repo
@@ -38,5 +33,5 @@ jobs:
3833
GITHUB_TOKEN: ${{ github.token }}
3934
with:
4035
Name: PSModuleTest
41-
Path: ${{ matrix.path }}
36+
Path: tests/outputs/modules
4237
Shell: ${{ matrix.shell }}

README.md

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,6 @@ The action fails if any of the tests fail or it fails to run the tests.
2424
## How to use it
2525

2626
To use the action, create a new file in the `.github/workflows` directory of the module repository and add the following content.
27-
<details>
28-
<summary>Workflow suggestion - before module is built</summary>
29-
30-
```yaml
31-
name: Test-PSModule
32-
33-
on: [push]
34-
35-
jobs:
36-
Test-PSModule:
37-
name: Test-PSModule
38-
runs-on: ubuntu-latest
39-
steps:
40-
- name: Checkout repo
41-
uses: actions/checkout@v4
42-
43-
- name: Initialize environment
44-
uses: PSModule/Initialize-PSModule@main
45-
46-
- name: Test-PSModule
47-
uses: PSModule/Test-PSModule@main
48-
with:
49-
Name: PSModule # Needed if the repo is not named the same as the module
50-
Path: src
51-
RunModuleTests: false
52-
53-
```
54-
</details>
5527

5628
<details>
5729
<summary>Workflow suggestion - after module is built</summary>

scripts/helpers/Test-PSModule.ps1

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@ function Test-PSModule {
1919

2020
$moduleName = Split-Path -Path $Path -Leaf
2121

22+
#region Test Module Manifest
23+
Start-LogGroup 'Test Module Manifest'
24+
$moduleManifestPath = Join-Path -Path $Path -ChildPath "$moduleName.psd1"
25+
if (Test-Path -Path $moduleManifestPath) {
26+
try {
27+
$status = Test-ModuleManifest -Path $moduleManifestPath
28+
} catch {
29+
Write-Warning "⚠️ Test-ModuleManifest failed: $moduleManifestPath"
30+
throw $_.Exception.Message
31+
}
32+
Write-Verbose ($status | Format-List | Out-String) -Verbose
33+
} else {
34+
Write-Warning "⚠️ Module manifest not found: $moduleManifestPath"
35+
}
36+
Stop-LogGroup
37+
#endregion
38+
2239
#region Get test kit versions
2340
Start-LogGroup 'Get test kit versions'
2441
$PSSAModule = Get-PSResource -Name PSScriptAnalyzer | Sort-Object Version -Descending | Select-Object -First 1
@@ -64,34 +81,34 @@ function Test-PSModule {
6481
#endregion
6582

6683
#region Add test - Specific - $moduleName
67-
$moduleTestsPath = Join-Path $env:GITHUB_WORKSPACE 'tests'
68-
if ((Test-Path -Path $moduleTestsPath) -and $RunModuleTests) {
69-
Start-LogGroup "Add test - Specific - $moduleName"
70-
$containerParams = @{
71-
Path = $moduleTestsPath
72-
Data = @{
73-
Path = $Path
84+
if ($RunModuleTests) {
85+
$moduleTestsPath = Join-Path $env:GITHUB_WORKSPACE 'tests'
86+
if (Test-Path -Path $moduleTestsPath) {
87+
Start-LogGroup "Add test - Specific - $moduleName"
88+
$containerParams = @{
89+
Path = $moduleTestsPath
90+
Data = @{
91+
Path = $Path
92+
}
7493
}
75-
}
76-
Write-Verbose 'ContainerParams:'
77-
Write-Verbose "$($containerParams | ConvertTo-Json)"
78-
$containers += New-PesterContainer @containerParams
79-
Stop-LogGroup
80-
} else {
81-
if (-not $RunModuleTests) {
82-
Write-Warning "⚠️ Module tests are disabled - [$moduleName]"
94+
Write-Verbose 'ContainerParams:'
95+
Write-Verbose "$($containerParams | ConvertTo-Json)"
96+
$containers += New-PesterContainer @containerParams
97+
Stop-LogGroup
8398
} else {
8499
Write-Warning "⚠️ No tests found - [$moduleTestsPath]"
85100
}
101+
} else {
102+
Write-Warning "⚠️ Module tests are disabled - [$moduleName]"
86103
}
87104
#endregion
88105

89106
#region Import module
90107
if ((Test-Path -Path $moduleTestsPath) -and $RunModuleTests) {
91108
Start-LogGroup "Importing module: $moduleName"
92109
Add-PSModulePath -Path (Split-Path $Path -Parent)
93-
Get-Module -Name $moduleName -ListAvailable | Remove-Module -Force -Verbose:$false
94-
Import-Module -Name $moduleName -Force
110+
Get-Module -Name $moduleName -ListAvailable | Remove-Module -Force
111+
Import-Module -Name $moduleName -Force -RequiredVersion 999.0.0 -Global
95112
Stop-LogGroup
96113
}
97114
#endregion
@@ -141,19 +158,5 @@ function Test-PSModule {
141158
Stop-LogGroup
142159
#endregion
143160

144-
#region Test Module Manifest
145-
Start-LogGroup 'Test Module Manifest'
146-
$moduleManifestPath = Join-Path -Path $Path -ChildPath "$moduleName.psd1"
147-
if (Test-Path -Path $moduleManifestPath) {
148-
try {
149-
Test-ModuleManifest -Path $moduleManifestPath
150-
} catch {
151-
Write-Warning "⚠️ Test-ModuleManifest failed: $moduleManifestPath"
152-
throw $_.Exception.Message
153-
}
154-
} else {
155-
Write-Warning "⚠️ Module manifest not found: $moduleManifestPath"
156-
}
157-
158161
$results
159162
}

tests/src/PSModuleTest/PSModuleTest.psd1

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

tests/src/PSModuleTest/PSModuleTest.psm1

Lines changed: 0 additions & 73 deletions
This file was deleted.
-42.5 KB
Binary file not shown.

tests/src/PSModuleTest/classes/Book.ps1

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

tests/src/PSModuleTest/data/Config.psd1

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

tests/src/PSModuleTest/data/Settings.psd1

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

tests/src/PSModuleTest/finally.ps1

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

0 commit comments

Comments
 (0)