diff --git a/test/PowerShellEditorServices.Test/Extensions/ExtensionCommandTests.cs b/test/PowerShellEditorServices.Test/Extensions/ExtensionCommandTests.cs index 70e35d3dd..11d7f61cd 100644 --- a/test/PowerShellEditorServices.Test/Extensions/ExtensionCommandTests.cs +++ b/test/PowerShellEditorServices.Test/Extensions/ExtensionCommandTests.cs @@ -93,12 +93,14 @@ public async Task CanRegisterAndInvokeCommandWithScriptBlock() const string commandName = "test.scriptblock"; const string commandDisplayName = "ScriptBlock extension"; + // Use AddScript so the script block is created in PowerShell's own session state + // rather than C#'s module context. ScriptBlock.Create() binds to the C# module, + // so $global: writes made via Invoke-Command land in an isolated scope on PS 5.1. await psesHost.ExecutePSCommandAsync( new PSCommand() - .AddCommand("Register-EditorCommand") - .AddParameter("Name", commandName) - .AddParameter("DisplayName", commandDisplayName) - .AddParameter("ScriptBlock", ScriptBlock.Create("$global:extensionValue = 10")), + .AddScript( + $"Register-EditorCommand -Name '{commandName}' -DisplayName \"{commandDisplayName}\" " + + "-ScriptBlock { $global:extensionValue = 10 }"), CancellationToken.None); Assert.NotNull(commandAdded); @@ -106,10 +108,9 @@ await psesHost.ExecutePSCommandAsync( Assert.Equal(commandDisplayName, commandAdded.DisplayName); // Invoke the command. - // TODO: What task was this cancelling? - await extensionCommandService.InvokeCommandAsync("test.scriptblock", editorContext); + await extensionCommandService.InvokeCommandAsync(commandName, editorContext); - // Assert the expected value + // Assert the expected value. PSCommand psCommand = new PSCommand().AddScript("$global:extensionValue"); IEnumerable results = await psesHost.ExecutePSCommandAsync(psCommand, CancellationToken.None); Assert.Equal(10, results.FirstOrDefault());