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
65 changes: 65 additions & 0 deletions RunCommand.Test/LineOutputHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace ktsu.RunCommand.Test;
[TestClass]
public class LineOutputHandlerTests
{
private static readonly string[] ExpectedXY = ["x", "y"];
private static readonly string[] ExpectedEveryLineEndingKind = ["a", "b", "c", "d", ""];

[TestMethod]
public void HandleStandardOutputDataShouldProcessLinesCorrectly()
{
Expand Down Expand Up @@ -94,4 +97,66 @@ public void HandleStandardErrorDataShouldBufferIncompleteLines()
Assert.AreEqual(expectedLines.Length, index);
Assert.AreEqual("Incomplete", handler.errorBuffer);
}

[TestMethod]
public void HandleStandardOutputDataShouldTreatCrlfSplitAcrossChunksAsOneLineBreak()
{
// Arrange
List<string> lines = [];
LineOutputHandler handler = new(onStandardOutput: lines.Add);

// Act
handler.HandleStandardOutputData("x\r");
handler.HandleStandardOutputData("\ny\n");

// Assert
Assert.AreSequenceEqual(ExpectedXY, lines);
Assert.AreEqual(string.Empty, handler.outputBuffer);
}

[TestMethod]
public void HandleStandardErrorDataShouldTreatCrlfSplitAcrossChunksAsOneLineBreak()
{
// Arrange
List<string> lines = [];
LineOutputHandler handler = new(onStandardError: lines.Add);

// Act
handler.HandleStandardErrorData("x\r");
handler.HandleStandardErrorData("\ny\n");

// Assert
Assert.AreSequenceEqual(ExpectedXY, lines);
Assert.AreEqual(string.Empty, handler.errorBuffer);
}

[TestMethod]
public void HandleStandardOutputDataShouldHoldBackTrailingCarriageReturn()
{
// Arrange
List<string> lines = [];
LineOutputHandler handler = new(onStandardOutput: lines.Add);

// Act
handler.HandleStandardOutputData("x\r");

// Assert
Assert.IsEmpty(lines);
Assert.AreEqual("x\r", handler.outputBuffer);
}

[TestMethod]
public void HandleStandardOutputDataShouldSplitOnEveryLineEndingKind()
{
// Arrange
List<string> lines = [];
LineOutputHandler handler = new(onStandardOutput: lines.Add);

// Act
handler.HandleStandardOutputData("a\r\nb\rc\nd\r\n\r\ne");

// Assert
Assert.AreSequenceEqual(ExpectedEveryLineEndingKind, lines);
Assert.AreEqual("e", handler.outputBuffer);
}
}
45 changes: 38 additions & 7 deletions RunCommand/LineOutputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,48 @@ internal override void HandleStandardErrorData(string data)
/// <param name="data">The data to be processed.</param>
/// <param name="buffer">The buffer to store incomplete lines.</param>
/// <param name="onLineReceived">The action to be invoked for each complete line received.</param>
/// <remarks>
/// Line endings are recognised on the buffered text rather than on each chunk, so a CRLF split across two
/// reads is still one line break. A trailing CR stays in the buffer until the next chunk shows whether an LF follows.
/// </remarks>
private static void ProcessDataByLine(string data, ref string buffer, Action<string>? onLineReceived)
{
buffer += data.ReplaceLineEndings();
while (buffer.Contains(Environment.NewLine))
buffer += data;
int lineStart = 0;
int i = 0;
while (i < buffer.Length)
{
string[] split = buffer.Split(Environment.NewLine, 2);
buffer = split.Length == 1
? string.Empty
: split[1];
char c = buffer[i];
if (c == '\r')
{
if (i == buffer.Length - 1)
{
break;
}

onLineReceived?.Invoke(split[0]);
onLineReceived?.Invoke(buffer[lineStart..i]);
i += buffer[i + 1] == '\n' ? 2 : 1;
lineStart = i;
}
else if (IsLineBreak(c))
{
onLineReceived?.Invoke(buffer[lineStart..i]);
i++;
lineStart = i;
}
else
{
i++;
}
}

buffer = buffer[lineStart..];
}

/// <summary>
/// Determines whether a character other than CR ends a line, matching the set <c>string.ReplaceLineEndings</c> recognises.
/// </summary>
/// <param name="c">The character to test.</param>
/// <returns><see langword="true"/> if <paramref name="c"/> is LF, NEL, LS, PS or FF; otherwise <see langword="false"/>.</returns>
private static bool IsLineBreak(char c) => c is '\n' or '\u0085' or '\u2028' or '\u2029' or '\f';
}
Loading