Skip to content

Commit 5ddc688

Browse files
Add regression tests for F5 and F8 saving to history (#1914)
This tests both the PowerShell history and the PSReadLine history via a stub, since we can't load PSReadLine itself in these tests.
1 parent 889018f commit 5ddc688

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

src/PowerShellEditorServices/Services/PowerShell/Console/ReadLineProvider.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal class ReadLineProvider : IReadLineProvider
1616

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

19-
public IReadLine ReadLine { get; private set; }
19+
public IReadLine ReadLine { get; internal set; }
2020

2121
public void OverrideReadLine(IReadLine readLine)
2222
{

src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ internal class PsesInternalHost : PSHost, IHostSupportsInteractiveSession, IRuns
5959

6060
private readonly CancellationContext _cancellationContext;
6161

62-
private readonly ReadLineProvider _readLineProvider;
62+
internal readonly ReadLineProvider _readLineProvider;
6363

6464
private readonly Thread _pipelineThread;
6565

test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs

+56
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Microsoft.PowerShell.EditorServices.Handlers;
1414
using Microsoft.PowerShell.EditorServices.Services;
1515
using Microsoft.PowerShell.EditorServices.Services.DebugAdapter;
16+
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Console;
1617
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
1718
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
1819
using Microsoft.PowerShell.EditorServices.Test;
@@ -22,6 +23,15 @@
2223

2324
namespace PowerShellEditorServices.Test.Debugging
2425
{
26+
internal class TestReadLine : IReadLine
27+
{
28+
public List<string> history = new();
29+
30+
public string ReadLine(CancellationToken cancellationToken) => "";
31+
32+
public void AddToHistory(string historyEntry) => history.Add(historyEntry);
33+
}
34+
2535
[Trait("Category", "DebugService")]
2636
public class DebugServiceTests : IDisposable
2737
{
@@ -32,13 +42,15 @@ public class DebugServiceTests : IDisposable
3242
private readonly WorkspaceService workspace;
3343
private readonly ScriptFile debugScriptFile;
3444
private readonly ScriptFile variableScriptFile;
45+
private readonly TestReadLine testReadLine = new();
3546

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

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

573+
[Fact]
574+
public async Task RecordsF5CommandInPowerShellHistory()
575+
{
576+
ConfigurationDoneHandler configurationDoneHandler = new(
577+
NullLoggerFactory.Instance, null, debugService, null, null, psesHost, workspace, null, psesHost);
578+
await configurationDoneHandler.LaunchScriptAsync(debugScriptFile.FilePath).ConfigureAwait(true);
579+
580+
IReadOnlyList<string> historyResult = await psesHost.ExecutePSCommandAsync<string>(
581+
new PSCommand().AddScript("(Get-History).CommandLine"),
582+
CancellationToken.None).ConfigureAwait(true);
583+
584+
// Check the PowerShell history
585+
Assert.Single(historyResult);
586+
Assert.Equal(". \"" + debugScriptFile.FilePath + "\"", historyResult[0]);
587+
588+
// Check the stubbed PSReadLine history
589+
Assert.Single(testReadLine.history);
590+
Assert.Equal(". \"" + debugScriptFile.FilePath + "\"", testReadLine.history[0]);
591+
}
592+
593+
[Fact]
594+
public async Task RecordsF8CommandInHistory()
595+
{
596+
const string script = "Write-Output Hello";
597+
EvaluateHandler evaluateHandler = new(psesHost);
598+
EvaluateResponseBody evaluateResponseBody = await evaluateHandler.Handle(
599+
new EvaluateRequestArguments { Expression = script, Context = "repl" },
600+
CancellationToken.None).ConfigureAwait(true);
601+
// TODO: Right now this response is hard-coded, maybe it should change?
602+
Assert.Equal("", evaluateResponseBody.Result);
603+
604+
IReadOnlyList<string> historyResult = await psesHost.ExecutePSCommandAsync<string>(
605+
new PSCommand().AddScript("(Get-History).CommandLine"),
606+
CancellationToken.None).ConfigureAwait(true);
607+
608+
// Check the PowerShell history
609+
Assert.Single(historyResult);
610+
Assert.Equal(script, historyResult[0]);
611+
612+
// Check the stubbed PSReadLine history
613+
Assert.Single(testReadLine.history);
614+
Assert.Equal(script, testReadLine.history[0]);
615+
}
616+
561617
[Fact]
562618
public async Task DebuggerVariableStringDisplaysCorrectly()
563619
{

0 commit comments

Comments
 (0)