Make `Out-String` and `Out-File` keep string input unchanged by daxian-dbw · Pull Request #17455 · PowerShell/PowerShell · GitHub
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
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,13 @@ internal virtual void ExecuteBufferPlayBack(DoPlayBackCall playback) { }
/// </param>
internal abstract void WriteLine(string s);

/// <summary>
/// Write a line of string as raw text to the output device, with no change to the string.
/// For example, keeping VT escape sequences intact in it.
/// </summary>
/// <param name="s">The raw text to be written to the device.</param>
internal virtual void WriteRawText(string s) => WriteLine(s);

internal WriteStreamType WriteStream
{
get;
Expand Down Expand Up @@ -431,7 +438,7 @@ private void WriteLineInternal(string val, int cols)
/// Implementation of the ILineOutput interface accepting an instance of a
/// TextWriter abstract class.
/// </summary>
internal class TextWriterLineOutput : LineOutput
internal sealed class TextWriterLineOutput : LineOutput
{
#region ILineOutput methods

Expand Down Expand Up @@ -467,9 +474,17 @@ internal override int RowNumber
/// <param name="s"></param>
internal override void WriteLine(string s)
{
CheckStopProcessing();
WriteRawText(PSHostUserInterface.GetOutputString(s, isHost: false));
}

s = PSHostUserInterface.GetOutputString(s, isHost: false);
/// <summary>
/// Write a raw text by delegating to the writer underneath, with no change to the text.
/// For example, keeping VT escape sequences intact in it.
/// </summary>
/// <param name="s">The raw text to be written to the device.</param>
internal override void WriteRawText(string s)
{
CheckStopProcessing();

if (_suppressNewline)
{
Expand All @@ -480,6 +495,7 @@ internal override void WriteLine(string s)
_writer.WriteLine(s);
}
}

#endregion

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion test/powershell/engine/Formatting/ErrorView.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ Describe 'Tests for $ErrorView' -Tag CI {

It "Error shows for advanced function" {
# need to have it virtually interactive so that InvocationInfo.MyCommand is empty
$e = '[cmdletbinding()]param()$pscmdlet.writeerror([System.Management.Automation.ErrorRecord]::new(([System.NotImplementedException]::new("myTest")),"stub","notimplemented","command"))' | pwsh -noprofile -file - 2>&1 | Out-String
$e = '[cmdletbinding()]param()$pscmdlet.writeerror([System.Management.Automation.ErrorRecord]::new(([System.NotImplementedException]::new("myTest")),"stub","notimplemented","command"))' | pwsh -noprofile -file - 2>&1
$e = $e | Where-Object { $_ -is [System.Management.Automation.ErrorRecord] } | Out-String
$e | Should -Not -BeNullOrEmpty

# need to see if ANSI escape sequences are in the output as ANSI is disabled for CI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Describe 'OutputRendering tests' {
param($outputRendering, $ansi)

$PSStyle.OutputRendering = $outputRendering
$out = "$($PSStyle.Foreground.Green)hello" | Out-String
$out = [pscustomobject] @{ key = "$($PSStyle.Foreground.Green)hello" } | Out-String

if ($ansi) {
$out | Should -BeLike "*`e*" -Because ($out | Format-Hex | Out-String)
Expand Down
71 changes: 71 additions & 0 deletions test/powershell/engine/Formatting/PSStyle.Tests.ps1