Console.Unix: fix terminal settings during Console.KeyAvailable (#42371)
authorTom Deseyn <tom.deseyn@gmail.com>
Thu, 17 Sep 2020 14:20:22 +0000 (16:20 +0200)
committerGitHub <noreply@github.com>
Thu, 17 Sep 2020 14:20:22 +0000 (15:20 +0100)
* Console.Unix: fix terminal settings during Console.KeyAvailable

Arguments passed to configure the terminal are in the wrong order.
This causes Enter keys to be returned as ConsoleKey.J instead of
ConsoleKey.Enter.

* Add test

src/libraries/Native/Unix/System.Native/pal_console.c
src/libraries/System.Console/tests/ManualTests/ManualTests.cs

index fa967ac..877c105 100644 (file)
@@ -378,7 +378,7 @@ void SystemNative_GetControlCharacters(
 
 int32_t SystemNative_StdinReady()
 {
-    SystemNative_InitializeConsoleBeforeRead(1, 0, 0);
+    SystemNative_InitializeConsoleBeforeRead(/* convertCrToNl */ 0, /* minChars */ 1, /* decisecondsTimeout */ 0);
     struct pollfd fd = { .fd = STDIN_FILENO, .events = POLLIN };
     int rv = poll(&fd, 1, 0) > 0 ? 1 : 0;
     SystemNative_UninitializeConsoleAfterRead();
index 5928b7a..0c6b1cf 100644 (file)
@@ -89,6 +89,28 @@ namespace System
             AssertUserExpectedResults("\"console\" correctly not echoed as you typed it");
         }
 
+        [ConditionalFact(nameof(ManualTestsEnabled))]
+        public static void EnterKeyIsEnterAfterKeyAvailableCheck()
+        {
+            Console.WriteLine("Please hold down the 'Enter' key for some time. You shouldn't see new lines appear:");
+            int keysRead = 0;
+            while (keysRead < 50)
+            {
+                if (Console.KeyAvailable)
+                {
+                    ConsoleKeyInfo keyInfo = Console.ReadKey(true);
+                    Assert.Equal(ConsoleKey.Enter, keyInfo.Key);
+                    keysRead++;
+                }
+            }
+            while (Console.KeyAvailable)
+            {
+                ConsoleKeyInfo keyInfo = Console.ReadKey(true);
+                Assert.Equal(ConsoleKey.Enter, keyInfo.Key);
+            }
+            AssertUserExpectedResults("no empty newlines appear");
+        }
+
         [ConditionalTheory(nameof(ManualTestsEnabled))]
         [MemberData(nameof(GetKeyChords))]
         public static void ReadKey_KeyChords(ConsoleKeyInfo expected)