Skip to content

Commit cf31ed4

Browse files
tablackburnclaude
andcommitted
test(WindowsRSAT): always stub install cmdlets for reliable mocking on PS 5.1
The previous BeforeAll only injected stub functions when Get-Command found no real Install-WindowsFeature / Add-WindowsCapability. On the Windows PowerShell 5.1 CI matrix entry, ServerManager auto-loads on Server 2022 and the real Install-WindowsFeature is present, so the stub was skipped. Pester then mocked the CDXML cmdlet, but the mock silently did not intercept calls from the script under test -- all four Server-dispatch tests reported "was called 0 times" while the rest passed. (PS 7 matrix entries are unaffected since ServerManager does not load on PowerShell Core, so the stub path was always taken.) PowerShell resolves functions before cmdlets in the same scope, so injecting the stub unconditionally lets it take precedence over the real cmdlet, and Pester reliably mocks the function across all matrix entries. Verified locally on Win 11 in both PowerShell 7 and Windows PowerShell 5.1: 12 passed, 0 failed, 0 skipped on both. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent adc3f6a commit cf31ed4

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

Tests/WindowsRSAT.Type.Tests.ps1

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@ BeforeAll {
1616

1717
$script:ScriptPath = Join-Path $env:BHProjectPath 'PSDepend/PSDependScripts/WindowsRSAT.ps1'
1818

19-
# Install-WindowsFeature ships only on Windows Server (ServerManager module),
20-
# and Add-WindowsCapability requires Windows. Inject stubs into the PSDepend
21-
# module scope so Mock has a command to attach to on hosts that don't ship
22-
# the real cmdlets (e.g. Windows client when testing the Server dispatch path).
19+
# Inject stub functions for the install-side cmdlets into the PSDepend
20+
# module scope so Pester's Mock attaches to a regular PowerShell function
21+
# rather than to the underlying CDXML/binary cmdlets. PowerShell resolves
22+
# functions before cmdlets in the same scope, so on hosts where the real
23+
# cmdlets exist (Windows Server PS 5.1 with ServerManager, Windows client
24+
# with DISM) the stub still wins. Mocking the real CDXML cmdlets has been
25+
# observed to silently not intercept on Windows PowerShell 5.1 -- the
26+
# stub-function approach makes mocking work consistently across PS 5.1,
27+
# PS 7, and all platforms.
2328
InModuleScope PSDepend {
24-
if (-not (Get-Command -Name Install-WindowsFeature -ErrorAction SilentlyContinue)) {
25-
function script:Install-WindowsFeature { [CmdletBinding()] param([string]$Name) }
26-
}
27-
if (-not (Get-Command -Name Add-WindowsCapability -ErrorAction SilentlyContinue)) {
28-
function script:Add-WindowsCapability { [CmdletBinding()] param([switch]$Online, [string]$Name) }
29-
}
29+
function script:Install-WindowsFeature { [CmdletBinding()] param([string]$Name) }
30+
function script:Add-WindowsCapability { [CmdletBinding()] param([switch]$Online, [string]$Name) }
3031
}
3132
}
3233

0 commit comments

Comments
 (0)