Skip to content
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

Add regression tests for F5 and F8 saving to history #1914

Merged
merged 1 commit into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
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 @@ -16,7 +16,7 @@ internal class ReadLineProvider : IReadLineProvider

public ReadLineProvider(ILoggerFactory loggerFactory) => _logger = loggerFactory.CreateLogger<ReadLineProvider>();

public IReadLine ReadLine { get; private set; }
public IReadLine ReadLine { get; internal set; }

public void OverrideReadLine(IReadLine readLine)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ internal class PsesInternalHost : PSHost, IHostSupportsInteractiveSession, IRuns

private readonly CancellationContext _cancellationContext;

private readonly ReadLineProvider _readLineProvider;
internal readonly ReadLineProvider _readLineProvider;

private readonly Thread _pipelineThread;

Expand Down
56 changes: 56 additions & 0 deletions test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.PowerShell.EditorServices.Handlers;
using Microsoft.PowerShell.EditorServices.Services;
using Microsoft.PowerShell.EditorServices.Services.DebugAdapter;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Console;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
using Microsoft.PowerShell.EditorServices.Test;
Expand All @@ -22,6 +23,15 @@

namespace PowerShellEditorServices.Test.Debugging
{
internal class TestReadLine : IReadLine
{
public List<string> history = new();

public string ReadLine(CancellationToken cancellationToken) => "";

public void AddToHistory(string historyEntry) => history.Add(historyEntry);
}

[Trait("Category", "DebugService")]
public class DebugServiceTests : IDisposable
{
Expand All @@ -32,13 +42,15 @@ public class DebugServiceTests : IDisposable
private readonly WorkspaceService workspace;
private readonly ScriptFile debugScriptFile;
private readonly ScriptFile variableScriptFile;
private readonly TestReadLine testReadLine = new();

public DebugServiceTests()
{
psesHost = PsesHostFactory.Create(NullLoggerFactory.Instance);
// This is required for remote debugging, but we call it here to end up in the same
// state as the usual startup path.
psesHost.DebugContext.EnableDebugMode();
psesHost._readLineProvider.ReadLine = testReadLine;

breakpointService = new BreakpointService(
NullLoggerFactory.Instance,
Expand Down Expand Up @@ -558,6 +570,50 @@ await debugService.SetCommandBreakpointsAsync(
Assert.Equal("\". $args[0]\"", myInvocationLine.ValueString);
}

[Fact]
public async Task RecordsF5CommandInPowerShellHistory()
{
ConfigurationDoneHandler configurationDoneHandler = new(
NullLoggerFactory.Instance, null, debugService, null, null, psesHost, workspace, null, psesHost);
await configurationDoneHandler.LaunchScriptAsync(debugScriptFile.FilePath).ConfigureAwait(true);

IReadOnlyList<string> historyResult = await psesHost.ExecutePSCommandAsync<string>(
new PSCommand().AddScript("(Get-History).CommandLine"),
CancellationToken.None).ConfigureAwait(true);

// Check the PowerShell history
Assert.Single(historyResult);
Assert.Equal(". \"" + debugScriptFile.FilePath + "\"", historyResult[0]);

// Check the stubbed PSReadLine history
Assert.Single(testReadLine.history);
Assert.Equal(". \"" + debugScriptFile.FilePath + "\"", testReadLine.history[0]);
}

[Fact]
public async Task RecordsF8CommandInHistory()
{
const string script = "Write-Output Hello";
EvaluateHandler evaluateHandler = new(psesHost);
EvaluateResponseBody evaluateResponseBody = await evaluateHandler.Handle(
new EvaluateRequestArguments { Expression = script, Context = "repl" },
CancellationToken.None).ConfigureAwait(true);
// TODO: Right now this response is hard-coded, maybe it should change?
Assert.Equal("", evaluateResponseBody.Result);

IReadOnlyList<string> historyResult = await psesHost.ExecutePSCommandAsync<string>(
new PSCommand().AddScript("(Get-History).CommandLine"),
CancellationToken.None).ConfigureAwait(true);

// Check the PowerShell history
Assert.Single(historyResult);
Assert.Equal(script, historyResult[0]);

// Check the stubbed PSReadLine history
Assert.Single(testReadLine.history);
Assert.Equal(script, testReadLine.history[0]);
}

[Fact]
public async Task DebuggerVariableStringDisplaysCorrectly()
{
Expand Down