Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Console.Unix: fix OpenStandardInput Stream sometimes throwing for Reads. #62153

Merged
merged 3 commits into from
Dec 1, 2021
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
4 changes: 3 additions & 1 deletion src/libraries/System.Console/src/System/IO/StdInReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,14 @@ public int ReadLine(Span<byte> buffer)
int charsUsedTotal = 0;
foreach (ReadOnlyMemory<char> chunk in _readLineSB.GetChunks())
{
Debug.Assert(!buffer.IsEmpty);

encoder.Convert(chunk.Span, buffer, flush: false, out int charsUsed, out int bytesUsed, out bool completed);
buffer = buffer.Slice(bytesUsed);
bytesUsedTotal += bytesUsed;
charsUsedTotal += charsUsed;

if (charsUsed == 0)
if (!completed || buffer.IsEmpty)
{
break;
}
Expand Down
18 changes: 18 additions & 0 deletions src/libraries/System.Console/tests/ManualTests/ManualTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Threading.Tasks;
using System.IO;
using System.Text;
using Xunit;

namespace System
Expand Down Expand Up @@ -45,6 +46,23 @@ public static void ReadLineFromOpenStandardInput()
AssertUserExpectedResults("the characters you typed properly echoed as you typed");
}

[ConditionalFact(nameof(ManualTestsEnabled))]
public static void ReadFromOpenStandardInput()
{
// The implementation in StdInReader uses a StringBuilder for caching. We want this builder to use
// multiple chunks. So the expectedLine is longer than 16 characters (StringBuilder.DefaultCapacity).
string expectedLine = $"This is a test for ReadFromOpenStandardInput.";
Assert.True(expectedLine.Length > new StringBuilder().Capacity);
Console.WriteLine($"Please type the sentence (without the quotes): \"{expectedLine}\"");
using Stream inputStream = Console.OpenStandardInput();
for (int i = 0; i < expectedLine.Length; i++)
{
Assert.Equal((byte)expectedLine[i], inputStream.ReadByte());
}
Assert.Equal((byte)'\n', inputStream.ReadByte());
AssertUserExpectedResults("the characters you typed properly echoed as you typed");
}

[ConditionalFact(nameof(ManualTestsEnabled))]
public static void ConsoleReadSupportsBackspace()
{
Expand Down