Skip to content

Commit

Permalink
🩹 [Patch]: Test module manifest before import (#33)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
MariusStorhaug authored Mar 25, 2024
1 parent 89eb966 commit f03f932
Show file tree
Hide file tree
Showing 24 changed files with 36 additions and 573 deletions.
9 changes: 2 additions & 7 deletions .github/workflows/Action-Test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,10 @@ jobs:
matrix:
shell: [pwsh]
os: [ubuntu-latest, macos-latest, windows-latest]
path: [tests/src, tests/outputs/modules]
include:
- shell: powershell
os: windows-latest
path: tests/src
- shell: powershell
os: windows-latest
path: tests/outputs/modules
name: Action-Test - [${{ matrix.os }}@${{ matrix.shell }}] - [${{ matrix.path }}]
name: Action-Test - [${{ matrix.os }}@${{ matrix.shell }}]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout repo
Expand All @@ -38,5 +33,5 @@ jobs:
GITHUB_TOKEN: ${{ github.token }}
with:
Name: PSModuleTest
Path: ${{ matrix.path }}
Path: tests/outputs/modules
Shell: ${{ matrix.shell }}
28 changes: 0 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,6 @@ 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:
Name: PSModule # Needed if the repo is not named the same as the module
Path: src
RunModuleTests: false

```
</details>

<details>
<summary>Workflow suggestion - after module is built</summary>
Expand Down
65 changes: 34 additions & 31 deletions scripts/helpers/Test-PSModule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ function Test-PSModule {

$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

#region Get test kit versions
Start-LogGroup 'Get test kit versions'
$PSSAModule = Get-PSResource -Name PSScriptAnalyzer | Sort-Object Version -Descending | Select-Object -First 1
Expand Down Expand Up @@ -64,34 +81,34 @@ function Test-PSModule {
#endregion

#region Add test - Specific - $moduleName
$moduleTestsPath = Join-Path $env:GITHUB_WORKSPACE 'tests'
if ((Test-Path -Path $moduleTestsPath) -and $RunModuleTests) {
Start-LogGroup "Add test - Specific - $moduleName"
$containerParams = @{
Path = $moduleTestsPath
Data = @{
Path = $Path
if ($RunModuleTests) {
$moduleTestsPath = Join-Path $env:GITHUB_WORKSPACE 'tests'
if (Test-Path -Path $moduleTestsPath) {
Start-LogGroup "Add test - Specific - $moduleName"
$containerParams = @{
Path = $moduleTestsPath
Data = @{
Path = $Path
}
}
}
Write-Verbose 'ContainerParams:'
Write-Verbose "$($containerParams | ConvertTo-Json)"
$containers += New-PesterContainer @containerParams
Stop-LogGroup
} else {
if (-not $RunModuleTests) {
Write-Warning "⚠️ Module tests are disabled - [$moduleName]"
Write-Verbose 'ContainerParams:'
Write-Verbose "$($containerParams | ConvertTo-Json)"
$containers += New-PesterContainer @containerParams
Stop-LogGroup
} 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) {
Start-LogGroup "Importing module: $moduleName"
Add-PSModulePath -Path (Split-Path $Path -Parent)
Get-Module -Name $moduleName -ListAvailable | Remove-Module -Force -Verbose:$false
Import-Module -Name $moduleName -Force
Get-Module -Name $moduleName -ListAvailable | Remove-Module -Force
Import-Module -Name $moduleName -Force -RequiredVersion 999.0.0 -Global
Stop-LogGroup
}
#endregion
Expand Down Expand Up @@ -141,19 +158,5 @@ function Test-PSModule {
Stop-LogGroup
#endregion

#region Test Module Manifest
Start-LogGroup 'Test Module Manifest'
$moduleManifestPath = Join-Path -Path $Path -ChildPath "$moduleName.psd1"
if (Test-Path -Path $moduleManifestPath) {
try {
Test-ModuleManifest -Path $moduleManifestPath
} catch {
Write-Warning "⚠️ Test-ModuleManifest failed: $moduleManifestPath"
throw $_.Exception.Message
}
} else {
Write-Warning "⚠️ Module manifest not found: $moduleManifestPath"
}

$results
}
4 changes: 0 additions & 4 deletions tests/src/PSModuleTest/PSModuleTest.psd1

This file was deleted.

73 changes: 0 additions & 73 deletions tests/src/PSModuleTest/PSModuleTest.psm1

This file was deleted.

Binary file removed tests/src/PSModuleTest/assemblies/LsonLib.dll
Binary file not shown.
132 changes: 0 additions & 132 deletions tests/src/PSModuleTest/classes/Book.ps1

This file was deleted.

3 changes: 0 additions & 3 deletions tests/src/PSModuleTest/data/Config.psd1

This file was deleted.

3 changes: 0 additions & 3 deletions tests/src/PSModuleTest/data/Settings.psd1

This file was deleted.

3 changes: 0 additions & 3 deletions tests/src/PSModuleTest/finally.ps1

This file was deleted.

Loading

0 comments on commit f03f932

Please sign in to comment.