-
-
Notifications
You must be signed in to change notification settings - Fork 473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pester Test Execution Fails When $WhatIfPreference is Enabled #2585
Comments
here is the content of Firewall.Tests.ps1: param(
[string[]]$FirewallRules = @()
)
Describe "Firewall Rule Validation" {
BeforeAll {
$script:rulesToTest = if ($FirewallRules.Count -gt 0) { $FirewallRules } else {
@(
"Rule1Name",
"Rule2Name",
"Rule3Name"
)
}
}
# If no rules are passed, use default rules
It "Should have required firewall rules" {
foreach ($ruleName in $script:rulesToTest) {
$firewallRule = Get-NetFirewallRule -DisplayName $ruleName -ErrorAction SilentlyContinue
$firewallRule | Should -Not -BeNullOrEmpty -Because "Firewall rule '$ruleName' should exist"
$firewallRule.Enabled | Should -Be $true -Because "Firewall rule '$ruleName' should be enabled"
}
}
# Optional: More specific rule validation
It "Should have correct rule configurations" {
foreach ($ruleName in $script:rulesToTest) {
$firewallRule = Get-NetFirewallRule -DisplayName $ruleName -ErrorAction SilentlyContinue
$firewallRule | Should -Not -BeNullOrEmpty -Because "Firewall rule '$ruleName' should exist"
if ($firewallRule) {
# Example additional checks
$firewallRule.Action | Should -Be "Allow" -Because "Rule '$ruleName' should allow traffic"
# Add more specific assertions as needed
}
}
}
} |
Cool that no-one saw that before. We should either detect that and throw, or pass the variable down skipping the pester stuff. Both solutions seem bad. |
I guess somewhere $PSCmdlet.ShouldProcess prevents execution of important initialization steps for registry mocking. If those important steps would be executed even if $WhatIfPreference is true, then the problem would be solved. Another possibility is to save the state of $WhatIfPreference before execution, setting it to $false and finally restoring, as in the example I provided |
Sorry I meant bad == hard to implement and track. And relatively expensive to restore the whatIf before every user scope executed script block. |
Then maybe remains throwing an exception with an explanation? |
Checklist
What is the issue?
Problem Description: When $WhatIfPreference is set to $true, Pester test execution fails with multiple side effects:
Specific Symptoms:
Root Cause: The $WhatIfPreference setting affects command execution globally, interfering with Pester's internal test mechanisms. Simply setting it to $false within a function's local scope does not consistently resolve the issue.
Expected Behavior
$WhatIfPreference should not cause exception
Steps To Reproduce
I use ipynb. I put the following into a cell:
Describe your environment
Pester version : 5.6.1 C:\Program Files\WindowsPowerShell\Modules\Pester\5.6.1\Pester.psm1
PowerShell version : 5.1.22621.4391
OS version : Microsoft Windows NT 10.0.22631.0
Possible Solution?
this approach works:
The text was updated successfully, but these errors were encountered: