Skip to content
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
48 changes: 9 additions & 39 deletions src/System.Management.Automation/engine/NativeCommandProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,29 +221,6 @@ private string Path
}
}

/// <summary>
/// Gets true if Path is Console Application.
/// </summary>
private bool IsConsoleApplication => !IsWindowsApplication;

/// <summary>
/// Gets true if Path is Windows Application.
/// </summary>
private bool IsWindowsApplication
{
get
{
if (!_isWindowsApplication.HasValue)
{
_isWindowsApplication = CheckIfWindowsApplication(Path);
}

return _isWindowsApplication.Value;
}
}

private bool? _isWindowsApplication;

#endregion ctor/native command properties

#region parameter binder
Expand Down Expand Up @@ -423,7 +400,8 @@ private void InitNativeProcess()

_startPosition = new Host.Coordinates();

CalculateIORedirection(out redirectOutput, out redirectError, out redirectInput);
bool isWindowsApplication = IsWindowsApplication(this.Path);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These names are so similar that it can be confused. Can we keep the old name for the method?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old name with the Check makes it slightly harder to understand reading the code as it's not as clear what true/false means with Check vs Is. The alternative is to have the local variable be isConsoleApplication to differentiate I guess.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use bool windowsApplication without Bulgarian prefix.
If no I agreed with isWindowsApplication.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this code currently is only used in one place, I think the casing diff is ok and prefer the is making it a bit more readable

CalculateIORedirection(isWindowsApplication, out redirectOutput, out redirectError, out redirectInput);

// Find out if it's the only command in the pipeline.
bool soloCommand = this.Command.MyInvocation.PipelineLength == 1;
Expand Down Expand Up @@ -498,7 +476,8 @@ private void InitNativeProcess()
bool notDone = true;
if (!string.IsNullOrEmpty(executable))
{
if (CheckIfConsoleApplication(executable))
isWindowsApplication = IsWindowsApplication(executable);
if (!isWindowsApplication)
{
// Allocate a console if there isn't one attached already...
ConsoleVisibility.AllocateHiddenConsole();
Expand Down Expand Up @@ -554,7 +533,7 @@ private void InitNativeProcess()
_isRunningInBackground = true;
if (startInfo.UseShellExecute == false)
{
_isRunningInBackground = IsWindowsApplication;
_isRunningInBackground = isWindowsApplication;
}
}

Expand Down Expand Up @@ -952,23 +931,13 @@ private static void KillChildProcesses(int parentId, ProcessWithParentId[] curre

#region checkForConsoleApplication

/// <summary>
/// Return true if the passed in process is a console process.
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
private static bool CheckIfConsoleApplication(string fileName)
{
return !CheckIfWindowsApplication(fileName);
}

/// <summary>
/// Check if the passed in process is a windows application.
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
[ArchitectureSensitive]
private static bool CheckIfWindowsApplication(string fileName)
private static bool IsWindowsApplication(string fileName)
{
#if UNIX
return false;
Expand Down Expand Up @@ -1227,10 +1196,11 @@ private bool IsDownstreamOutDefault(Pipe downstreamPipe)
/// <summary>
/// This method calculates if input and output of the process are redirected.
/// </summary>
/// <param name="isWindowsApplication"></param>
/// <param name="redirectOutput"></param>
/// <param name="redirectError"></param>
/// <param name="redirectInput"></param>
private void CalculateIORedirection(out bool redirectOutput, out bool redirectError, out bool redirectInput)
private void CalculateIORedirection(bool isWindowsApplication, out bool redirectOutput, out bool redirectError, out bool redirectInput)
{
redirectInput = this.Command.MyInvocation.ExpectingInput;
redirectOutput = true;
Expand Down Expand Up @@ -1301,7 +1271,7 @@ private void CalculateIORedirection(out bool redirectOutput, out bool redirectEr
redirectOutput = true;
redirectError = true;
}
else if (Platform.IsWindowsDesktop && IsConsoleApplication)
else if (Platform.IsWindowsDesktop && !isWindowsApplication)
{
// On Windows desktops, if the command to run is a console application,
// then allocate a console if there isn't one attached already...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ Describe "Native Command Processor" -tags "Feature" {
It '$ErrorActionPreference does not apply to redirected stderr output' -Skip:(!$EnabledExperimentalFeatures.Contains('PSNotApplyErrorActionToStderr')) {
pwsh -noprofile -command '$ErrorActionPreference = ''Stop''; testexe -stderr stop 2>$null; ''hello''; $error; $?' | Should -BeExactly 'hello','True'
}

It 'Can start an elevated associated process correctly' -Skip:(
!$IsWindows -or (!(Test-Path (Join-Path -Path $env:windir -ChildPath 'system32' -AdditionalChildPath 'diskmgmt.msc')))
) {
# test bug https://github.com/PowerShell/PowerShell/issues/13744 where console is blocked
diskmgmt.msc
Wait-UntilTrue -sb { (Get-Process mmc).Count -gt 0 } -TimeoutInMilliseconds 5000 -IntervalInMilliseconds 1000 | Should -BeTrue
Get-Process mmc | Stop-Process
}
}

Describe "Open a text file with NativeCommandProcessor" -tags @("Feature", "RequireAdminOnWindows") {
Expand Down