Commit 376eafe
authored
Fix flaky CanRegisterAndInvokeCommandWithScriptBlock on PS 5.1 / net462 (#2307)
* Initial plan
* Fix CanRegisterAndInvokeCommandWithScriptBlock test reliability on PS 5.1
The test was asserting on $global:extensionValue after invoking a script
block command via Invoke-Command. In Windows PowerShell 5.1 / net462, the
global variable set inside the script block is not reliably visible in
subsequent command invocations, causing the test to fail intermittently.
Replace the global-variable assertion with a scope-independent check:
change the script block to Write-Output 10 and verify only that registration
completes successfully (via the CommandAdded event) and invocation completes
without error. The behavior being tested (script-block-backed commands can
be registered and invoked) is still fully covered."
* Restore value assertion in CanRegisterAndInvokeCommandWithScriptBlock
Instead of passing ScriptBlock.Create() from C#, register the command
using AddScript so the script block is created in PowerShell's own
session state. ScriptBlock.Create() binds to the C# module context,
causing $global: writes via Invoke-Command to land in an isolated scope
on PS 5.1, making the value invisible to subsequent commands.
With the script block created in PS session state, the $global:extensionValue
assertion is reliable across all supported frameworks and is restored.
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>1 parent 37dc288 commit 376eafe
1 file changed
Lines changed: 8 additions & 7 deletions
Lines changed: 8 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
96 | 99 | | |
97 | 100 | | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
102 | 104 | | |
103 | 105 | | |
104 | 106 | | |
105 | 107 | | |
106 | 108 | | |
107 | 109 | | |
108 | 110 | | |
109 | | - | |
110 | | - | |
| 111 | + | |
111 | 112 | | |
112 | | - | |
| 113 | + | |
113 | 114 | | |
114 | 115 | | |
115 | 116 | | |
| |||
0 commit comments