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 @@ -13,7 +13,7 @@ namespace Microsoft.PowerShell.Commands
/// <summary>
/// implementation for the out-string command
/// </summary>
[Cmdlet(VerbsData.Out, "String", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113368", RemotingCapability = RemotingCapability.None)]
[Cmdlet(VerbsData.Out, "String", DefaultParameterSetName = "NoNewLineFormatting", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113368", RemotingCapability = RemotingCapability.None)]
[OutputType(typeof(string))]
public class OutStringCommand : FrontEndCommandBase
{
Expand All @@ -24,7 +24,7 @@ public class OutStringCommand : FrontEndCommandBase
/// FALSE: accumulate all the data, then write a single string
/// TRUE: write one line at the time
/// </summary>
[Parameter]
[Parameter(ParameterSetName="StreamFormatting")]
public SwitchParameter Stream
{
get { return _stream; }
Expand All @@ -45,6 +45,19 @@ public int Width
}

private Nullable<int> _width = null;

/// <summary>
/// False to add a newline to the end of the output string, true if not.
/// </summary>
[Parameter(ParameterSetName="NoNewLineFormatting")]
public SwitchParameter NoNewline
{
get { return _noNewLine; }
set { _noNewLine = value;}
}

private bool _noNewLine = false;

#endregion

/// <summary>
Expand Down Expand Up @@ -122,9 +135,20 @@ private LineOutput InstantiateLineOutputInterface()
private void OnWriteLine(string s)
{
if (_stream)
{
this.WriteObject(s);
}
else
_buffer.AppendLine(s);
{
if (_noNewLine)
{
_buffer.Append(s);
}
else
{
_buffer.AppendLine(s);
}
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,19 @@ Describe "Out-String" -Tags "CI" {

$streamoutputlength | Should BeLessThan $nonstreamoutputlength
}

It "Should not print a newline when the nonewline switch is used" {
$testArray = "a", "b"
$testArray | Out-String -NoNewLine | Should Be "ab"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add the test for default parameter set:
$testArray | Out-String | Should Be "ab"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If i am not wrong, this is the test for the default parameter set wherein newlines are added to the output. Is my understanding correct?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks! Yes, correct.

Closed.

}

It "Should preserve embedded newline when the nonewline switch is used" {
$testArray = "a$nl", "b"
$testArray | Out-String -NoNewLine | Should Be "a${nl}b"
}

It "Should throw error when NoNewLine and Stream are used together" {
$testArray = "a", "b"
{ $testArray | Out-String -NoNewLine -Stream }| ShouldBeErrorId "AmbiguousParameterSet,Microsoft.PowerShell.Commands.OutStringCommand"
}
}