Skip to content

Commit 376eafe

Browse files
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

File tree

test/PowerShellEditorServices.Test/Extensions/ExtensionCommandTests.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,24 @@ public async Task CanRegisterAndInvokeCommandWithScriptBlock()
9393
const string commandName = "test.scriptblock";
9494
const string commandDisplayName = "ScriptBlock extension";
9595

96+
// Use AddScript so the script block is created in PowerShell's own session state
97+
// rather than C#'s module context. ScriptBlock.Create() binds to the C# module,
98+
// so $global: writes made via Invoke-Command land in an isolated scope on PS 5.1.
9699
await psesHost.ExecutePSCommandAsync(
97100
new PSCommand()
98-
.AddCommand("Register-EditorCommand")
99-
.AddParameter("Name", commandName)
100-
.AddParameter("DisplayName", commandDisplayName)
101-
.AddParameter("ScriptBlock", ScriptBlock.Create("$global:extensionValue = 10")),
101+
.AddScript(
102+
$"Register-EditorCommand -Name '{commandName}' -DisplayName \"{commandDisplayName}\" " +
103+
"-ScriptBlock { $global:extensionValue = 10 }"),
102104
CancellationToken.None);
103105

104106
Assert.NotNull(commandAdded);
105107
Assert.Equal(commandName, commandAdded.Name);
106108
Assert.Equal(commandDisplayName, commandAdded.DisplayName);
107109

108110
// Invoke the command.
109-
// TODO: What task was this cancelling?
110-
await extensionCommandService.InvokeCommandAsync("test.scriptblock", editorContext);
111+
await extensionCommandService.InvokeCommandAsync(commandName, editorContext);
111112

112-
// Assert the expected value
113+
// Assert the expected value.
113114
PSCommand psCommand = new PSCommand().AddScript("$global:extensionValue");
114115
IEnumerable<int> results = await psesHost.ExecutePSCommandAsync<int>(psCommand, CancellationToken.None);
115116
Assert.Equal(10, results.FirstOrDefault());

0 commit comments

Comments
 (0)