Skip to content

Fix $PSDefaultParameterValues leak causing tests to skip unexpectedly#26602

Merged
iSazonov merged 3 commits intomasterfrom
copilot/fix-skipped-tests-on-linux
Dec 12, 2025
Merged

Fix $PSDefaultParameterValues leak causing tests to skip unexpectedly#26602
iSazonov merged 3 commits intomasterfrom
copilot/fix-skipped-tests-on-linux

Conversation

Copy link
Contributor

Copilot AI commented Dec 11, 2025

PR Summary

Fix #26601

Six test files were leaking $PSDefaultParameterValues["It:Skip"] settings to subsequent tests, causing widespread test skipping on Linux. The leaks occurred through two different patterns: using .Remove() instead of properly restoring state, and imbalanced Push-DefaultParameterValueStack/Pop-DefaultParameterValueStack calls.

PR Context

When test files modify $PSDefaultParameterValues["It:Skip"] and don't properly restore the original state, the modification persists in the PowerShell session and affects all subsequent test files. This caused tests that should run on Linux to be skipped, particularly affecting resource validation tests like CimCmdletsResources.Tests.ps1.

Two patterns of leakage were identified:

Pattern 1: Using .Remove() (4 files fixed)

Files using .Remove("It:Skip") which doesn't restore the original state:

  • test/powershell/Modules/Microsoft.PowerShell.Security/AclCmdlets.Tests.ps1
  • test/powershell/engine/Remoting/SessionOption.Tests.ps1
  • test/powershell/engine/ETS/CimAdapter.Tests.ps1
  • test/powershell/Modules/Microsoft.PowerShell.Management/Rename-Computer.Tests.ps1

Before (leaked):

BeforeAll {
    $PSDefaultParameterValues["It:Skip"] = -not $IsWindows
}
AfterAll {
    $PSDefaultParameterValues.Remove("It:Skip")  # Insufficient
}

After (isolated):

BeforeAll {
    $originalDefaultParameterValues = $PSDefaultParameterValues.Clone()
    $PSDefaultParameterValues["It:Skip"] = -not $IsWindows
}
AfterAll {
    $global:PSDefaultParameterValues = $originalDefaultParameterValues
}

Pattern 2: Imbalanced Push/Pop (2 files fixed)

Files with imbalanced Push-DefaultParameterValueStack/Pop-DefaultParameterValueStack calls:

  • test/powershell/engine/Remoting/RemoteSession.Basic.Tests.ps1 - Had unconditional Pop without corresponding Push
  • test/powershell/engine/Security/FileSignature.Tests.ps1 - Missing Pop when test was skipped

RemoteSession.Basic.Tests.ps1 Before:

AfterAll {
    if ($skipTest) {
        Pop-DefaultParameterValueStack
        return
    }
    Pop-DefaultParameterValueStack  # This Pop had no matching Push!
}

After:

AfterAll {
    if ($skipTest) {
        Pop-DefaultParameterValueStack
    }
}

FileSignature.Tests.ps1 Before:

BeforeAll {
    if ($shouldSkip) {
        Push-DefaultParameterValueStack @{ "it:skip" = $shouldSkip }
        return
    }
}
AfterAll {
    if ($shouldSkip) {
        return  # Missing Pop!
    }
}

After:

AfterAll {
    if ($shouldSkip) {
        Pop-DefaultParameterValueStack
        return
    }
}

The RemoteSession.Basic.Tests.ps1 leak was particularly impactful as it runs before ResourceValidation tests in alphabetical order (engine/Remoting/ before engine/ResourceValidation/), causing CimCmdletsResources.Tests.ps1 and other subsequent tests to be skipped.

PR Checklist

Original prompt

This section details on the original issue you should resolve

<issue_title>Large amount of tests are skipped on Linux even if some of them should run on Linux</issue_title>
<issue_description>### Test Bug

See the "Linux Unelevated Others" links below:

https://github.com/PowerShell/PowerShell/actions/runs/20116374515/job/57726954938?pr=26565#step:3:3712
https://github.com/PowerShell/PowerShell/actions/runs/20111102340/job/57708654522?pr=26565#step:3:3704

Lots of tests are skipped on Linux except for some that have explicit -Skip:(...) used in the tests.
It looks like a $PSDefaultParameterValues[It:Skip] setting in a test was not reverted after the test and thus the setting gets leaked to other tests. We need to find out where that happens.
</issue_description>

Comments on the Issue (you are @copilot in this section)

@iSazonov In addition to the fact that we have to restore `$PSDefaultParameterValues[It:Skip]` in the AfterAll clause, an alternative with Push-DefaultParameterValueStack Pop-DefaultParameterValueStack methods is used in the tests. This may also be the cause of this issue.

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

- AclCmdlets.Tests.ps1: Clone and restore original values
- SessionOption.Tests.ps1: Clone and restore original values
- CimAdapter.Tests.ps1: Clone and restore original values
- Rename-Computer.Tests.ps1: Clone and restore original values

All files now properly save the original $PSDefaultParameterValues before modification
and restore it to $global:PSDefaultParameterValues in their cleanup blocks, preventing
the It:Skip setting from leaking to other tests.

Co-authored-by: iSazonov <22290914+iSazonov@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix issue with skipped tests on Linux environments Fix $PSDefaultParameterValues leak causing tests to skip unexpectedly Dec 11, 2025
Copilot AI requested a review from iSazonov December 11, 2025 06:05
@iSazonov
Copy link
Collaborator

iSazonov commented Dec 11, 2025

@copilot The issue is not still fixed. See log https://github.com/PowerShell/PowerShell/actions/runs/20123556684/job/57749053868?pr=26602 - all tests starting with "Describing Resources strings in Microsoft.Management.Infrastructure.CimCmdlets (was -ResGen used with Start-PSBuild)" are still skipped. So a root of the issue is in a test before the test.

GitHub
PowerShell for every system! Contribute to PowerShell/PowerShell development by creating an account on GitHub.

- RemoteSession.Basic.Tests.ps1: Remove unconditional Pop that was executed when test wasn't skipped
- FileSignature.Tests.ps1: Add missing Pop when test is skipped

These tests use Push-DefaultParameterValueStack in BeforeAll when skipping,
but were not properly calling Pop-DefaultParameterValueStack in AfterAll,
causing the skip setting to leak to subsequent tests like CimCmdletsResources.Tests.ps1.

Co-authored-by: iSazonov <22290914+iSazonov@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 11, 2025

@copilot The issue is not still fixed. See log https://github.com/PowerShell/PowerShell/actions/runs/20123556684/job/57749053868?pr=26602 - all tests starting with "Describing Resources strings in Mic...

Found and fixed the root cause. Two additional test files had Push-DefaultParameterValueStack/Pop-DefaultParameterValueStack imbalance:

  1. RemoteSession.Basic.Tests.ps1 - Had an extra unconditional Pop that was popping a value pushed by a previous test
  2. FileSignature.Tests.ps1 - Missing Pop in AfterAll when test was skipped

The RemoteSession.Basic.Tests.ps1 file runs before CimCmdletsResources.Tests.ps1 (alphabetically under engine/Remoting vs engine/ResourceValidation), so its leak was causing the CimCmdlets tests to be skipped.

Fixed in commit 2665e2e.

GitHub
PowerShell for every system! Contribute to PowerShell/PowerShell development by creating an account on GitHub.

@daxian-dbw daxian-dbw added the CL-Test Indicates that a PR should be marked as a test change in the Change Log label Dec 11, 2025
@daxian-dbw daxian-dbw marked this pull request as ready for review December 11, 2025 22:48
Copilot AI review requested due to automatic review settings December 11, 2025 22:48
Copy link
Member

@daxian-dbw daxian-dbw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes LGTM!

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes test isolation issues where six test files were leaking $PSDefaultParameterValues["It:Skip"] settings to subsequent tests, causing widespread test skipping on Linux. The fixes address two distinct patterns: using .Remove() which doesn't restore the original state (4 files), and imbalanced Push-DefaultParameterValueStack/Pop-DefaultParameterValueStack calls (2 files).

Key changes:

  • Replace .Remove() pattern with proper state cloning and restoration in 4 test files
  • Fix imbalanced Push/Pop calls in 2 test files to ensure proper cleanup even when tests are skipped

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
test/powershell/engine/Security/FileSignature.Tests.ps1 Adds missing Pop-DefaultParameterValueStack call in AfterAll when test is skipped
test/powershell/engine/Remoting/SessionOption.Tests.ps1 Replaces .Remove() with clone/restore pattern to properly restore state
test/powershell/engine/Remoting/RemoteSession.Basic.Tests.ps1 Removes unconditional Pop that had no matching Push, fixing stack balance
test/powershell/engine/ETS/CimAdapter.Tests.ps1 Replaces .Remove() with clone/restore pattern to properly restore state
test/powershell/Modules/Microsoft.PowerShell.Security/AclCmdlets.Tests.ps1 Replaces .Remove() with clone/restore pattern to properly restore state
test/powershell/Modules/Microsoft.PowerShell.Management/Rename-Computer.Tests.ps1 Replaces .Remove() with clone/restore pattern to properly restore state

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


AfterAll {
if ($shouldSkip) {
Pop-DefaultParameterValueStack
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the root cause that results in tests get skipped on Linux.

@daxian-dbw
Copy link
Member

daxian-dbw commented Dec 11, 2025

From the copilot session:

I can see the issue! In AclCmdlets.Tests.ps1 at line 105-107, the AfterAll block only calls $PSDefaultParameterValues.Remove("It:Skip") but this is inside a Context block, not at the Describe level. When a Context ends, the change to $PSDefaultParameterValues persists to other Context blocks and even other Describe blocks in the same file and potentially across test files.

Practically, .Remove("It:Skip") should work. I think copilot believes it's possible that "It:skip" = true was already contained in $PSDefaultParameterValues before running $PSDefaultParameterValues["It:Skip"] = -not $IsWindows, and in that case, running .Remove("It:Skip") would not restore it to the original state.

I don't think the changes regarding .Remove("It:Skip") are really needed, because the root cause lies in the unbalanced push/pop-PSDefaultParameterValue in RemoteSession.Basic.Tests.ps1 and FileSignature.Tests.ps1‎. But maybe it's also good to be more consistent. I don't have a strong preference, so @iSazonov, if you want to revert those .Remove("It:Skip") changes, I'm fine too.


Comparison before and after this fix for "Linux Unelevated Others":

Before: https://github.com/PowerShell/PowerShell/actions/runs/20116374515/job/57726954938?pr=26565#step:3:5415

Tests completed in 204.39s
Tests Passed: 1139, Failed: 0, Skipped: 2469, Pending: 92, Inconclusive: 0

After: https://github.com/PowerShell/PowerShell/actions/runs/20124034564/job/57750282196?pr=26602#step:3:5428

Tests completed in 242.74s
Tests Passed: 2308, Failed: 0, Skipped: 1299, Pending: 92, Inconclusive: 0

1170 tests were incorrectly skipped due to the leaking It:Skip.

@iSazonov
Copy link
Collaborator

I don't have a strong preference, so @iSazonov, if you want to revert those .Remove("It:Skip") changes, I'm fine too.

I don't want :-)

@iSazonov iSazonov merged commit 2e7765e into master Dec 12, 2025
46 of 68 checks passed
@iSazonov iSazonov deleted the copilot/fix-skipped-tests-on-linux branch December 12, 2025 04:26
@iSazonov
Copy link
Collaborator

@daxian-dbw I believe it should be backported.

kilasuit pushed a commit to kilasuit/PowerShell that referenced this pull request Jan 2, 2026
…PowerShell#26602)

This fixes test isolation issues where six test files were leaking $PSDefaultParameterValues["It:Skip"] settings to subsequent tests, causing widespread test skipping on Linux. The fixes address two distinct patterns: using .Remove() which doesn't restore the original state (4 files), and imbalanced Push-DefaultParameterValueStack/Pop-DefaultParameterValueStack calls (2 files).
daxian-dbw pushed a commit to daxian-dbw/PowerShell that referenced this pull request Jan 20, 2026
…PowerShell#26602)

This fixes test isolation issues where six test files were leaking $PSDefaultParameterValues["It:Skip"] settings to subsequent tests, causing widespread test skipping on Linux. The fixes address two distinct patterns: using .Remove() which doesn't restore the original state (4 files), and imbalanced Push-DefaultParameterValueStack/Pop-DefaultParameterValueStack calls (2 files).
daxian-dbw pushed a commit to daxian-dbw/PowerShell that referenced this pull request Feb 13, 2026
…PowerShell#26602)

This fixes test isolation issues where six test files were leaking $PSDefaultParameterValues["It:Skip"] settings to subsequent tests, causing widespread test skipping on Linux. The fixes address two distinct patterns: using .Remove() which doesn't restore the original state (4 files), and imbalanced Push-DefaultParameterValueStack/Pop-DefaultParameterValueStack calls (2 files).
TravisEz13 pushed a commit to TravisEz13/PowerShell that referenced this pull request Feb 20, 2026
…PowerShell#26602)

This fixes test isolation issues where six test files were leaking $PSDefaultParameterValues["It:Skip"] settings to subsequent tests, causing widespread test skipping on Linux. The fixes address two distinct patterns: using .Remove() which doesn't restore the original state (4 files), and imbalanced Push-DefaultParameterValueStack/Pop-DefaultParameterValueStack calls (2 files).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Backport-7.4.x-Migrated Backport-7.5.x-Migrated Backport-7.6.x-Migrated CL-Test Indicates that a PR should be marked as a test change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Large amount of tests are skipped on Linux even if some of them should run on Linux

5 participants