Skip to content

Commit fffbcdb

Browse files
Fix PSScriptAnalyzer warnings and test IsWindows from module scope
1 parent 1cd8c4e commit fffbcdb

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ BeforeAll {
3737
$expectedEnumNames = [regex]::Matches($Matches[1], '\[([^\]]+)\]') | ForEach-Object { $_.Groups[1].Value }
3838
}
3939
}
40-
$expectedTypeNames = @($expectedEnumNames) + @($expectedClassNames) | Where-Object { $_ }
4140
Write-Host "Has class exporter: $hasClassExporter"
4241
Write-Host "Expected classes: $($expectedClassNames -join ', ')"
4342
Write-Host "Expected enums: $($expectedEnumNames -join ', ')"
@@ -64,33 +63,31 @@ Describe 'PSModule - Module tests' {
6463
}
6564

6665
Context 'Framework - IsWindows compatibility shim' {
67-
It 'Should have $IsWindows defined' {
66+
It 'Should have $IsWindows defined in the module scope' {
6867
# The framework injects "$IsWindows = $true" for PowerShell 5.1 (Desktop edition).
6968
# On PS 7+ (Core), $IsWindows is a built-in automatic variable.
70-
# Either way, it must be defined after module import.
71-
$variable = Get-Variable -Name 'IsWindows' -ErrorAction SilentlyContinue
72-
$variable | Should -Not -BeNullOrEmpty -Because 'the framework injects a compatibility shim for PS 5.1'
69+
# 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 }
71+
$isWindowsDefined | Should -Not -BeNullOrEmpty -Because 'the framework injects a compatibility shim for PS 5.1'
7372
}
7473
}
7574

7675
Context 'Framework - Type accelerator registration' -Skip:(-not $hasClassExporter) {
77-
BeforeAll {
78-
$typeAccelerators = [psobject].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Get
79-
}
80-
8176
It 'Should register public enum [<_>] as a type accelerator' -ForEach $expectedEnumNames {
82-
$typeAccelerators.Keys | Should -Contain $_ -Because 'the framework registers public enums as type accelerators'
77+
$registered = [psobject].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Get
78+
$registered.Keys | Should -Contain $_ -Because 'the framework registers public enums as type accelerators'
8379
}
8480

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

9087
Context 'Framework - Module OnRemove cleanup' -Skip:(-not $hasClassExporter) {
9188
It 'Should clean up type accelerators when the module is removed' {
9289
# Capture type names before removal
93-
$typeNames = $expectedTypeNames.Clone()
90+
$typeNames = @(@($expectedEnumNames) + @($expectedClassNames) | Where-Object { $_ })
9491
$typeNames | Should -Not -BeNullOrEmpty -Because 'there should be types to verify cleanup for'
9592

9693
# Remove the module to trigger the OnRemove hook

0 commit comments

Comments
 (0)