Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 81 additions & 9 deletions src/tests/Module/PSModule/PSModule.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,38 @@ param(
[string] $Path
)

BeforeAll {
$moduleName = Split-Path -Path (Split-Path -Path $Path -Parent) -Leaf
$moduleManifestPath = Join-Path -Path $Path -ChildPath "$moduleName.psd1"
Write-Verbose "Module Manifest Path: [$moduleManifestPath]"
# Discovery-phase variables — must be set at script scope (outside BeforeAll) so that
# Pester's -Skip and -ForEach parameters can reference them during test discovery.
$moduleName = Split-Path -Path (Split-Path -Path $Path -Parent) -Leaf
$moduleManifestPath = Join-Path -Path $Path -ChildPath "$moduleName.psd1"
$moduleRootPath = Join-Path -Path $Path -ChildPath "$moduleName.psm1"

# Discover public classes and enums from the compiled module source.
# The class exporter region is injected by Build-PSModule when classes/public contains types.
$moduleContent = if (Test-Path -Path $moduleRootPath) { Get-Content -Path $moduleRootPath -Raw } else { '' }
$hasClassExporter = $moduleContent -match '#region\s+Class exporter'

# Extract expected class and enum names from the class exporter block.
$expectedClassNames = @()
$expectedEnumNames = @()
if ($hasClassExporter) {
# Match $ExportableClasses = @( ... ) block
if ($moduleContent -match '\$ExportableClasses\s*=\s*@\(([\s\S]*?)\)') {
$expectedClassNames = @([regex]::Matches($Matches[1], '\[([^\]]+)\]') | ForEach-Object { $_.Groups[1].Value })
}
# Match $ExportableEnums = @( ... ) block
if ($moduleContent -match '\$ExportableEnums\s*=\s*@\(([\s\S]*?)\)') {
$expectedEnumNames = @([regex]::Matches($Matches[1], '\[([^\]]+)\]') | ForEach-Object { $_.Groups[1].Value })
}
}
Write-Host "Has class exporter: $hasClassExporter"
Write-Host "Expected classes: $($expectedClassNames -join ', ')"
Write-Host "Expected enums: $($expectedEnumNames -join ', ')"

Describe 'PSModule - Module tests' {
Comment thread
MariusStorhaug marked this conversation as resolved.
Outdated
Context 'Module' {
It 'The module should be importable' {
{ Import-Module -Name $moduleName } | Should -Not -Throw
{ Import-Module -Name $moduleManifestPath -Force } | Should -Not -Throw
}
Comment thread
MariusStorhaug marked this conversation as resolved.
}

Expand All @@ -36,9 +58,59 @@ Describe 'PSModule - Module tests' {
$result | Should -Not -Be $null
Write-Host "$($result | Format-List | Out-String)"
}
# It 'has a valid license URL' {}
# It 'has a valid project URL' {}
# It 'has a valid icon URL' {}
# It 'has a valid help URL' {}
}

Context 'Framework - IsWindows compatibility shim' {
BeforeAll {
Import-Module -Name $moduleManifestPath -Force
$script:moduleRef = Get-Module -Name $moduleName
}
Comment thread
MariusStorhaug marked this conversation as resolved.
Outdated
It 'Should have $IsWindows defined in the module scope' {
# The framework injects "$IsWindows = $true" for PowerShell 5.1 (Desktop edition).
# On PS 7+ (Core), $IsWindows is a built-in automatic variable.
# The variable is set inside the module scope and is not exported, so we must check from within the module.
$isWindowsDefined = & $script:moduleRef { Get-Variable -Name 'IsWindows' -ErrorAction SilentlyContinue }
$isWindowsDefined | Should -Not -BeNullOrEmpty -Because 'the framework injects a compatibility shim for PS 5.1'
}
Comment thread
MariusStorhaug marked this conversation as resolved.
Outdated
}

Context 'Framework - Type accelerator registration' -Skip:(-not $hasClassExporter) {
BeforeAll {
Import-Module -Name $moduleManifestPath -Force
}
It 'Should register public enum [<_>] as a type accelerator' -ForEach $expectedEnumNames {
Comment thread
MariusStorhaug marked this conversation as resolved.
$registered = [psobject].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Get
$registered.Keys | Should -Contain $_ -Because 'the framework registers public enums as type accelerators'
}

It 'Should register public class [<_>] as a type accelerator' -ForEach $expectedClassNames {
$registered = [psobject].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Get
$registered.Keys | Should -Contain $_ -Because 'the framework registers public classes as type accelerators'
}
}

Context 'Framework - Module OnRemove cleanup' -Skip:(-not $hasClassExporter) {
BeforeAll {
Import-Module -Name $moduleManifestPath -Force
}
It 'Should clean up type accelerators when the module is removed' {
# Capture type names before removal
$typeNames = @(@($expectedEnumNames) + @($expectedClassNames) | Where-Object { $_ })
$typeNames | Should -Not -BeNullOrEmpty -Because 'there should be types to verify cleanup for'

try {
# Remove the module to trigger the OnRemove hook
Remove-Module -Name $moduleName -Force

# Verify type accelerators are cleaned up
$typeAccelerators = [psobject].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Get
foreach ($typeName in $typeNames) {
$typeAccelerators.Keys | Should -Not -Contain $typeName -Because "the OnRemove hook should remove type accelerator [$typeName]"
}
} finally {
# Re-import the module for any subsequent tests
Import-Module -Name $moduleManifestPath -Force
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidLongLines', '', Justification = 'Contains long links.')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSAvoidAssignmentToAutomaticVariable', 'IsWindows',
Justification = 'IsWindows does not exist in PS5.1'
)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSUseDeclaredVarsMoreThanAssignments', 'IsWindows',
Justification = 'IsWindows does not exist in PS5.1'
)]
[CmdletBinding()]
param()

$scriptName = $MyInvocation.MyCommand.Name
Write-Debug "[$scriptName] Importing module"

#region - IsWindows compatibility shim
if ($PSEdition -eq 'Desktop') {
$IsWindows = $true
}
#endregion - IsWindows compatibility shim

#region - Data import
Write-Debug "[$scriptName] - [data] - Processing folder"
$dataFolder = (Join-Path $PSScriptRoot 'data')
Expand Down Expand Up @@ -190,7 +204,7 @@ Write-Debug "[$scriptName] - [/private] - Processing folder"
#region - From /private/Get-InternalPSModule.ps1
Write-Debug "[$scriptName] - [/private/Get-InternalPSModule.ps1] - Importing"

Function Get-InternalPSModule {
function Get-InternalPSModule {
<#
.SYNOPSIS
Performs tests on a module.
Expand All @@ -214,7 +228,7 @@ Write-Debug "[$scriptName] - [/private/Get-InternalPSModule.ps1] - Done"
#region - From /private/Set-InternalPSModule.ps1
Write-Debug "[$scriptName] - [/private/Set-InternalPSModule.ps1] - Importing"

Function Set-InternalPSModule {
function Set-InternalPSModule {
<#
.SYNOPSIS
Performs tests on a module.
Expand Down Expand Up @@ -377,6 +391,48 @@ Write-Verbose '------------------------------'
Write-Debug "[$scriptName] - [/finally.ps1] - Done"
#endregion - From /finally.ps1

#region Class exporter
# Get the internal TypeAccelerators class to use its static methods.
$TypeAcceleratorsClass = [psobject].Assembly.GetType(
'System.Management.Automation.TypeAccelerators'
)
Comment thread
MariusStorhaug marked this conversation as resolved.
# Ensure none of the types would clobber an existing type accelerator.
# If a type accelerator with the same name exists, throw an exception.
$ExistingTypeAccelerators = $TypeAcceleratorsClass::Get
# Define the types to export with type accelerators.
Comment thread
MariusStorhaug marked this conversation as resolved.
$ExportableEnums = @(
)
$ExportableEnums | ForEach-Object { Write-Verbose "Exporting enum '$($_.FullName)'." }
foreach ($Type in $ExportableEnums) {
if ($Type.FullName -in $ExistingTypeAccelerators.Keys) {
Write-Verbose "Enum already exists [$($Type.FullName)]. Skipping."
} else {
Write-Verbose "Importing enum '$Type'."
$TypeAcceleratorsClass::Add($Type.FullName, $Type)
}
Comment thread
MariusStorhaug marked this conversation as resolved.
}
$ExportableClasses = @(
[Book]
[BookList]
)
$ExportableClasses | ForEach-Object { Write-Verbose "Exporting class '$($_.FullName)'." }
foreach ($Type in $ExportableClasses) {
if ($Type.FullName -in $ExistingTypeAccelerators.Keys) {
Write-Verbose "Class already exists [$($Type.FullName)]. Skipping."
} else {
Write-Verbose "Importing class '$Type'."
$TypeAcceleratorsClass::Add($Type.FullName, $Type)
}
}

# Remove type accelerators when the module is removed.
$MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = {
foreach ($Type in ($ExportableEnums + $ExportableClasses)) {
$null = $TypeAcceleratorsClass::Remove($Type.FullName)
}
Comment thread
MariusStorhaug marked this conversation as resolved.
}.GetNewClosure()
#endregion Class exporter

$exports = @{
Cmdlet = ''
Alias = '*'
Expand Down
Loading