Skip to content

Commit cb811a7

Browse files
fix: address review - import by manifest path, self-contained contexts, try/finally in OnRemove
1 parent 1021f29 commit cb811a7

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

src/tests/Module/PSModule/PSModule.Tests.ps1

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ BeforeAll {
4545
Describe 'PSModule - Module tests' {
4646
Context 'Module' {
4747
It 'The module should be importable' {
48-
{ Import-Module -Name $moduleName -Force } | Should -Not -Throw
48+
{ Import-Module -Name $moduleManifestPath -Force } | Should -Not -Throw
4949
}
5050
}
5151

@@ -63,16 +63,22 @@ Describe 'PSModule - Module tests' {
6363
}
6464

6565
Context 'Framework - IsWindows compatibility shim' {
66+
BeforeAll {
67+
$script:moduleRef = Import-Module -Name $moduleManifestPath -Force -PassThru
68+
}
6669
It 'Should have $IsWindows defined in the module scope' {
6770
# The framework injects "$IsWindows = $true" for PowerShell 5.1 (Desktop edition).
6871
# On PS 7+ (Core), $IsWindows is a built-in automatic variable.
6972
# The variable is set inside the module scope and is not exported, so we must check from within the module.
70-
$isWindowsDefined = & (Get-Module $moduleName) { Get-Variable -Name 'IsWindows' -ErrorAction SilentlyContinue }
73+
$isWindowsDefined = & $script:moduleRef { Get-Variable -Name 'IsWindows' -ErrorAction SilentlyContinue }
7174
$isWindowsDefined | Should -Not -BeNullOrEmpty -Because 'the framework injects a compatibility shim for PS 5.1'
7275
}
7376
}
7477

7578
Context 'Framework - Type accelerator registration' -Skip:(-not $hasClassExporter) {
79+
BeforeAll {
80+
Import-Module -Name $moduleManifestPath -Force
81+
}
7682
It 'Should register public enum [<_>] as a type accelerator' -ForEach $expectedEnumNames {
7783
$registered = [psobject].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Get
7884
$registered.Keys | Should -Contain $_ -Because 'the framework registers public enums as type accelerators'
@@ -85,22 +91,27 @@ Describe 'PSModule - Module tests' {
8591
}
8692

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

93-
# Remove the module to trigger the OnRemove hook
94-
Remove-Module -Name $moduleName -Force
102+
try {
103+
# Remove the module to trigger the OnRemove hook
104+
Remove-Module -Name $moduleName -Force
95105

96-
# Verify type accelerators are cleaned up
97-
$typeAccelerators = [psobject].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Get
98-
foreach ($typeName in $typeNames) {
99-
$typeAccelerators.Keys | Should -Not -Contain $typeName -Because "the OnRemove hook should remove type accelerator [$typeName]"
106+
# Verify type accelerators are cleaned up
107+
$typeAccelerators = [psobject].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Get
108+
foreach ($typeName in $typeNames) {
109+
$typeAccelerators.Keys | Should -Not -Contain $typeName -Because "the OnRemove hook should remove type accelerator [$typeName]"
110+
}
111+
} finally {
112+
# Re-import the module for any subsequent tests
113+
Import-Module -Name $moduleManifestPath -Force
100114
}
101-
102-
# Re-import the module for any subsequent tests
103-
Import-Module -Name $moduleName -Force
104115
}
105116
}
106117
}

tests/outputTestRepo/outputs/module/PSModuleTest/PSModuleTest.psm1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ Write-Debug "[$scriptName] - [/private] - Processing folder"
204204
#region - From /private/Get-InternalPSModule.ps1
205205
Write-Debug "[$scriptName] - [/private/Get-InternalPSModule.ps1] - Importing"
206206

207-
Function Get-InternalPSModule {
207+
function Get-InternalPSModule {
208208
<#
209209
.SYNOPSIS
210210
Performs tests on a module.
@@ -228,7 +228,7 @@ Write-Debug "[$scriptName] - [/private/Get-InternalPSModule.ps1] - Done"
228228
#region - From /private/Set-InternalPSModule.ps1
229229
Write-Debug "[$scriptName] - [/private/Set-InternalPSModule.ps1] - Importing"
230230

231-
Function Set-InternalPSModule {
231+
function Set-InternalPSModule {
232232
<#
233233
.SYNOPSIS
234234
Performs tests on a module.
@@ -402,7 +402,7 @@ $ExistingTypeAccelerators = $TypeAcceleratorsClass::Get
402402
# Define the types to export with type accelerators.
403403
$ExportableEnums = @(
404404
)
405-
$ExportableEnums | Foreach-Object { Write-Verbose "Exporting enum '$($_.FullName)'." }
405+
$ExportableEnums | ForEach-Object { Write-Verbose "Exporting enum '$($_.FullName)'." }
406406
foreach ($Type in $ExportableEnums) {
407407
if ($Type.FullName -in $ExistingTypeAccelerators.Keys) {
408408
Write-Verbose "Enum already exists [$($Type.FullName)]. Skipping."
@@ -415,7 +415,7 @@ $ExportableClasses = @(
415415
[Book]
416416
[BookList]
417417
)
418-
$ExportableClasses | Foreach-Object { Write-Verbose "Exporting class '$($_.FullName)'." }
418+
$ExportableClasses | ForEach-Object { Write-Verbose "Exporting class '$($_.FullName)'." }
419419
foreach ($Type in $ExportableClasses) {
420420
if ($Type.FullName -in $ExistingTypeAccelerators.Keys) {
421421
Write-Verbose "Class already exists [$($Type.FullName)]. Skipping."

0 commit comments

Comments
 (0)