diff --git a/src/PowerShellEditorServices/Services/PowerShell/Console/ReadLineProvider.cs b/src/PowerShellEditorServices/Services/PowerShell/Console/ReadLineProvider.cs index 8b9a50312..be4b6d407 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Console/ReadLineProvider.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Console/ReadLineProvider.cs @@ -16,7 +16,7 @@ internal class ReadLineProvider : IReadLineProvider public ReadLineProvider(ILoggerFactory loggerFactory) => _logger = loggerFactory.CreateLogger(); - public IReadLine ReadLine { get; private set; } + public IReadLine ReadLine { get; internal set; } public void OverrideReadLine(IReadLine readLine) { diff --git a/src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs b/src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs index e1ee99345..5e9a84f3d 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs @@ -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; diff --git a/test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs b/test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs index 02a5a23bc..43101db9a 100644 --- a/test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs +++ b/test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs @@ -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; @@ -22,6 +23,15 @@ namespace PowerShellEditorServices.Test.Debugging { + internal class TestReadLine : IReadLine + { + public List history = new(); + + public string ReadLine(CancellationToken cancellationToken) => ""; + + public void AddToHistory(string historyEntry) => history.Add(historyEntry); + } + [Trait("Category", "DebugService")] public class DebugServiceTests : IDisposable { @@ -32,6 +42,7 @@ public class DebugServiceTests : IDisposable private readonly WorkspaceService workspace; private readonly ScriptFile debugScriptFile; private readonly ScriptFile variableScriptFile; + private readonly TestReadLine testReadLine = new(); public DebugServiceTests() { @@ -39,6 +50,7 @@ public DebugServiceTests() // 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, @@ -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 historyResult = await psesHost.ExecutePSCommandAsync( + 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 historyResult = await psesHost.ExecutePSCommandAsync( + 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() {