Skip to content
Closed
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
14 changes: 10 additions & 4 deletions src/System.Management.Automation/engine/NativeCommandProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1594,15 +1594,19 @@ private void CalculateIORedirection(bool isWindowsApplication, out bool redirect
// $powershell.AddScript('ipconfig.exe')
// $powershell.AddCommand('Out-Default')
// $powershell.Invoke())
// we should not count it as a redirection.
if (IsDownstreamOutDefault(this.commandRuntime.OutputPipe))
// we should not count it as a redirection. Unless the native command has its stdout redirected
// for example:
// cmd.exe /c "echo test" > somefile.log
// in that case we want to keep output redirection even though Out-Default is the only
// downstream command.
if (IsDownstreamOutDefault(this.commandRuntime.OutputPipe) && StdOutDestination is null)
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we didn't have tests covering the stdout redirection scenarios. Can you please add tests?

{
redirectOutput = false;
}
}

// See if the error output stream has been redirected, either through an explicit 2> foo.txt or
// my merging error into output through 2>&1.
// by merging error into output through 2>&1.
if (CommandRuntime.ErrorMergeTo != MshCommandRuntime.MergeDataStream.Output)
{
// If the error output pipe is the default outputter, for example, calling the native command from command-line host,
Expand All @@ -1612,7 +1616,9 @@ private void CalculateIORedirection(bool isWindowsApplication, out bool redirect
// $powershell.AddScript('ipconfig.exe')
// $powershell.AddCommand('Out-Default')
// $powershell.Invoke())
// we should not count that as a redirection.
// we should not count that as a redirection. We do not need to worry
// about StdOutDestination here as if error is redirected then it's assumed
// to be text based and Out-File will be added to the pipeline instead.
if (IsDownstreamOutDefault(this.commandRuntime.ErrorOutputPipe))
{
redirectError = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,13 @@ Describe 'Native command byte piping tests' -Tags 'CI' {
($pipe)?.Dispose()
}
}

It 'Bytes are retained when redirecting to a file' {
testexe -writebytes FF > $TestDrive/content.bin | Should -BeNullOrEmpty
Get-Content -LiteralPath $TestDrive/content.bin -AsByteStream | Should -Be 0xFFuy
}

It 'Redirecting to $null should emit no output' {
testexe -writebytes FF > $null | Should -BeNullOrEmpty
}
}