Skip to content

Re-enable ExtensionCommandTests.cs #1657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Re-enable ExtensionCommandTests.cs
These tests actually did not require the language server nor the service
provider, we just had to manually initialize the extension service
itself.
  • Loading branch information
andyleejordan committed Jan 7, 2022
commit 17b9c45afe8bc978c6eec0d4d3e8f646419d4c70
153 changes: 71 additions & 82 deletions test/PowerShellEditorServices.Test/Extensions/ExtensionCommandTests.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,48 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.PowerShell.EditorServices.Extensions;
using Microsoft.PowerShell.EditorServices.Extensions.Services;
using Microsoft.PowerShell.EditorServices.Services;
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
using Microsoft.PowerShell.EditorServices.Test.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.PowerShell.EditorServices.Extensions;
using Microsoft.PowerShell.EditorServices.Extensions.Services;
using Microsoft.PowerShell.EditorServices.Services.Extension;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
using Microsoft.PowerShell.EditorServices.Test.Shared;
using Xunit;

namespace Microsoft.PowerShell.EditorServices.Test.Extensions
{
// TODO:
// These tests require being able to instantiate a language server and use the service provider.
// Re-enable them when we have mocked out more infrastructure for testing.

/*
[Trait("Category", "Extensions")]
public class ExtensionCommandTests : IDisposable
{
private readonly PowerShellContextService _powershellContextService;

private readonly IExtensionCommandService _extensionCommandService;
private readonly PsesInternalHost psesHost;

private readonly ExtensionService _extensionService;
private readonly ExtensionCommandService extensionCommandService;

public ExtensionCommandTests()
{
_powershellContextService = PowerShellContextFactory.Create(NullLogger.Instance);
_extensionCommandService = EditorObject.Instance.GetExtensionServiceProvider().ExtensionCommands;
psesHost = PsesHostFactory.Create(NullLoggerFactory.Instance);
ExtensionService extensionService = new(
languageServer: null,
serviceProvider: null,
editorOperations: null,
executionService: psesHost);
extensionService.InitializeAsync().Wait();
extensionCommandService = new(extensionService);
}

public void Dispose()
{
psesHost.StopAsync().Wait();
GC.SuppressFinalize(this);
}

[Trait("Category", "Extensions")]
[Fact]
public async Task CanRegisterAndInvokeCommandWithCmdletName()
{
Expand All @@ -48,34 +55,30 @@ public async Task CanRegisterAndInvokeCommandWithCmdletName()
BufferRange.None);

EditorCommand commandAdded = null;
_extensionCommandService.CommandAdded += (object sender, EditorCommand command) =>
{
commandAdded = command;
};

string commandName = "test.function";
string commandDisplayName = "Function extension";
extensionCommandService.CommandAdded += (object _, EditorCommand command) => commandAdded = command;

const string commandName = "test.function";
const string commandDisplayName = "Function extension";

await _powershellContextService.ExecuteScriptStringAsync(
TestUtilities.NormalizeNewlines($@"
function Invoke-Extension {{ $global:testValue = 5 }}
Register-EditorCommand -Name {commandName} -DisplayName ""{commandDisplayName}"" -Function Invoke-Extension"));
await psesHost.ExecutePSCommandAsync(
new PSCommand().AddScript(
"function Invoke-Extension { $global:extensionValue = 5 }; " +
$"Register-EditorCommand -Name {commandName} -DisplayName \"{commandDisplayName}\" -Function Invoke-Extension"),
CancellationToken.None).ConfigureAwait(true);

Assert.NotNull(commandAdded);
Assert.Equal(commandAdded.Name, commandName);
Assert.Equal(commandAdded.DisplayName, commandDisplayName);

// Invoke the command
await _extensionCommandService.InvokeCommandAsync(commandName, editorContext);
await extensionCommandService.InvokeCommandAsync(commandName, editorContext).ConfigureAwait(true);

// Assert the expected value
PSCommand psCommand = new PSCommand().AddScript("$global:extensionValue");
IEnumerable<int> results = await _powershellContextService.ExecuteCommandAsync<int>(psCommand);
IEnumerable<int> results = await psesHost.ExecutePSCommandAsync<int>(psCommand, CancellationToken.None).ConfigureAwait(true);
Assert.Equal(5, results.FirstOrDefault());
}

[Trait("Category", "Extensions")]
[Fact]
public async Task CanRegisterAndInvokeCommandWithScriptBlock()
{
Expand All @@ -87,62 +90,56 @@ public async Task CanRegisterAndInvokeCommandWithScriptBlock()
new BufferPosition(line: 1, column: 1),
BufferRange.None);


EditorCommand commandAdded = null;
_extensionCommandService.CommandAdded += (object sender, EditorCommand command) =>
{
commandAdded = command;
};
extensionCommandService.CommandAdded += (object _, EditorCommand command) => commandAdded = command;

const string commandName = "test.scriptblock";
const string commandDisplayName = "ScriptBlock extension";

string commandName = "test.scriptblock";
string commandDisplayName = "ScriptBlock extension";

await _powershellContextService.ExecuteCommandAsync(new PSCommand()
.AddCommand("Register-EditorCommand")
.AddParameter("Name", commandName)
.AddParameter("DisplayName", commandDisplayName)
.AddParameter("ScriptBlock", ScriptBlock.Create("$global:extensionValue = 10")));
await psesHost.ExecutePSCommandAsync(
new PSCommand()
.AddCommand("Register-EditorCommand")
.AddParameter("Name", commandName)
.AddParameter("DisplayName", commandDisplayName)
.AddParameter("ScriptBlock", ScriptBlock.Create("$global:extensionValue = 10")),
CancellationToken.None).ConfigureAwait(true);

Assert.NotNull(commandAdded);
Assert.Equal(commandName, commandAdded.Name);
Assert.Equal(commandDisplayName, commandAdded.DisplayName);

// Invoke the command
await _extensionCommandService.InvokeCommandAsync("test.scriptblock", editorContext);
await extensionCommandService.InvokeCommandAsync("test.scriptblock", editorContext).ConfigureAwait(true);

// Assert the expected value
PSCommand psCommand = new PSCommand().AddScript("$global:extensionValue");
IEnumerable<int> results = await _powershellContextService.ExecuteCommandAsync<int>(psCommand);
IEnumerable<int> results = await psesHost.ExecutePSCommandAsync<int>(psCommand, CancellationToken.None).ConfigureAwait(true);
Assert.Equal(10, results.FirstOrDefault());
}

[Trait("Category", "Extensions")]
[Fact]
public async Task CanUpdateRegisteredCommand()
{
EditorCommand updatedCommand = null;
_extensionCommandService.CommandUpdated += (object sender, EditorCommand command) =>
{
updatedCommand = command;
};
extensionCommandService.CommandUpdated += (object _, EditorCommand command) => updatedCommand = command;

string commandName = "test.function";
string commandDisplayName = "Updated function extension";
const string commandName = "test.function";
const string commandDisplayName = "Updated function extension";

// Register a command and then update it
await _powershellContextService.ExecuteScriptStringAsync(TestUtilities.NormalizeNewlines(
"function Invoke-Extension { Write-Output \"Extension output!\" }\n" +
$"Register-EditorCommand -Name \"{commandName}\" -DisplayName \"Old function extension\" -Function \"Invoke-Extension\"\n" +
$"Register-EditorCommand -Name \"{commandName}\" -DisplayName \"{commandDisplayName}\" -Function \"Invoke-Extension\""));
await psesHost.ExecutePSCommandAsync(
new PSCommand().AddScript(
"function Invoke-Extension { Write-Output \"Extension output!\" }; " +
$"Register-EditorCommand -Name {commandName} -DisplayName \"Old function extension\" -Function Invoke-Extension; " +
$"Register-EditorCommand -Name {commandName} -DisplayName \"{commandDisplayName}\" -Function Invoke-Extension"),
CancellationToken.None).ConfigureAwait(true);

// Wait for the add and update events
Assert.NotNull(updatedCommand);
Assert.Equal(commandName, updatedCommand.Name);
Assert.Equal(commandDisplayName, updatedCommand.DisplayName);
}

[Trait("Category", "Extensions")]
[Fact]
public async Task CanUnregisterCommand()
{
Expand All @@ -154,41 +151,33 @@ public async Task CanUnregisterCommand()
new BufferPosition(line: 1, column: 1),
BufferRange.None);

string commandName = "test.scriptblock";
string commandDisplayName = "ScriptBlock extension";
const string commandName = "test.scriptblock";
const string commandDisplayName = "ScriptBlock extension";

EditorCommand removedCommand = null;
_extensionCommandService.CommandRemoved += (object sender, EditorCommand command) =>
{
removedCommand = command;
};
extensionCommandService.CommandRemoved += (object _, EditorCommand command) => removedCommand = command;

// Add the command and wait for the add event
await _powershellContextService.ExecuteCommandAsync(new PSCommand()
.AddCommand("Register-EditorCommand")
.AddParameter("Name", commandName)
.AddParameter("DisplayName", commandDisplayName)
.AddParameter("ScriptBlock", ScriptBlock.Create("Write-Output \"Extension output!\"")));
await psesHost.ExecutePSCommandAsync(
new PSCommand()
.AddCommand("Register-EditorCommand")
.AddParameter("Name", commandName)
.AddParameter("DisplayName", commandDisplayName)
.AddParameter("ScriptBlock", ScriptBlock.Create("Write-Output \"Extension output!\"")),
CancellationToken.None).ConfigureAwait(true);

// Remove the command and wait for the remove event
await _powershellContextService.ExecuteCommandAsync(new PSCommand()
.AddCommand("Unregister-EditorCommand")
.AddParameter("Name", commandName));
await psesHost.ExecutePSCommandAsync(
new PSCommand().AddCommand("Unregister-EditorCommand").AddParameter("Name", commandName),
CancellationToken.None).ConfigureAwait(true);

Assert.NotNull(removedCommand);
Assert.Equal(commandName, removedCommand.Name);
Assert.Equal(commandDisplayName, removedCommand.DisplayName);

// Ensure that the command has been unregistered
await Assert.ThrowsAsync<KeyNotFoundException>(() =>
_extensionCommandService.InvokeCommandAsync("test.scriptblock", editorContext));
}

public void Dispose()
{
_powershellContextService.Dispose();
await Assert.ThrowsAsync<KeyNotFoundException>(
() => extensionCommandService.InvokeCommandAsync("test.scriptblock", editorContext)).ConfigureAwait(true);
}
}
*/
}

Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public async Task CanQueueParallelPSCommands()
[Fact]
public async Task CanCancelExecutionWithToken()
{
_ = await Assert.ThrowsAsync<TaskCanceledException>(() =>
await Assert.ThrowsAsync<TaskCanceledException>(() =>
{
return psesHost.ExecutePSCommandAsync(
new PSCommand().AddScript("Start-Sleep 10"),
Expand All @@ -103,7 +103,7 @@ public async Task CanCancelExecutionWithMethod()
// Wait until our task has started.
Thread.Sleep(2000);
psesHost.CancelCurrentTask();
_ = await Assert.ThrowsAsync<TaskCanceledException>(() => executeTask).ConfigureAwait(true);
await Assert.ThrowsAsync<TaskCanceledException>(() => executeTask).ConfigureAwait(true);
Assert.True(executeTask.IsCanceled);
}

Expand Down