Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,24 @@ 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I do not know PowerShell well enough to say if this is true; but the test has been flaky, so if the test is now passing, perhaps that's enough?

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);
Assert.Equal(commandName, commandAdded.Name);
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<int> results = await psesHost.ExecutePSCommandAsync<int>(psCommand, CancellationToken.None);
Assert.Equal(10, results.FirstOrDefault());
Expand Down
Loading