Skip to content

Commit c35bc96

Browse files
committed
Merge pull request #148 from PowerShell/daviwil/native-app-output
Fix #147: Enable native console app output in REPL
2 parents 05d6351 + f58ca48 commit c35bc96

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/PowerShellEditorServices/Session/PowerShellContext.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace Microsoft.PowerShell.EditorServices
1919
{
2020
using System.Management.Automation;
2121
using System.Management.Automation.Runspaces;
22+
using System.Reflection;
2223

2324
/// <summary>
2425
/// Manages the lifetime and usage of a PowerShell session.
@@ -114,6 +115,23 @@ public PowerShellContext()
114115

115116
this.Initialize(runspace);
116117

118+
// Use reflection to execute ConsoleVisibility.AlwaysCaptureApplicationIO = true;
119+
Type consoleVisibilityType =
120+
Type.GetType(
121+
"System.Management.Automation.ConsoleVisibility, System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
122+
123+
if (consoleVisibilityType != null)
124+
{
125+
PropertyInfo propertyInfo =
126+
consoleVisibilityType.GetProperty(
127+
"AlwaysCaptureApplicationIO",
128+
BindingFlags.Static | BindingFlags.Public);
129+
130+
if (propertyInfo != null)
131+
{
132+
propertyInfo.SetValue(null, true);
133+
}
134+
}
117135
}
118136

119137
/// <summary>

test/PowerShellEditorServices.Test.Host/LanguageServerTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,30 @@ await requestContext.SendResult(
565565
await evaluateTask;
566566
}
567567

568+
[Fact]
569+
public async Task ServiceExecutesNativeCommandAndReceivesCommand()
570+
{
571+
OutputReader outputReader = new OutputReader(this.protocolClient);
572+
573+
// Execute the script but don't await the task yet because
574+
// the choice prompt will block execution from completing
575+
Task<EvaluateResponseBody> evaluateTask =
576+
this.SendRequest(
577+
EvaluateRequest.Type,
578+
new EvaluateRequestArguments
579+
{
580+
Expression = "cmd.exe /c 'echo Test Output'",
581+
Context = "repl"
582+
});
583+
584+
// Skip the command line and the following newline
585+
await outputReader.ReadLines(2);
586+
587+
// Wait for the selection to appear as output
588+
await evaluateTask;
589+
Assert.Equal("Test Output", await outputReader.ReadLine());
590+
}
591+
568592
private async Task SendOpenFileEvent(string filePath, bool waitForDiagnostics = true)
569593
{
570594
string fileContents = string.Join(Environment.NewLine, File.ReadAllLines(filePath));

0 commit comments

Comments
 (0)