Skip to content

Commit

Permalink
[System.Console] Fix manual test (dotnet#39460)
Browse files Browse the repository at this point in the history
* do not report '\n' as Ctrl+Enter on unix

* add macos instructions

* add windows test instructions
  • Loading branch information
eiriktsarpalis authored Aug 6, 2020
1 parent a778b7e commit ff47b4b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
5 changes: 0 additions & 5 deletions src/libraries/System.Console/src/System/IO/StdInReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,6 @@ internal ConsoleKey GetKeyFromCharValue(char x, out bool isShift, out bool isCtr
case '\r':
return ConsoleKey.Enter;

case '\n':
// Windows compatibility; LF is Ctrl+Enter
isCtrl = true;
return ConsoleKey.Enter;

case (char)(0x1B):
return ConsoleKey.Escape;

Expand Down
42 changes: 32 additions & 10 deletions src/libraries/System.Console/tests/ManualTests/ManualTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Xunit;

Expand Down Expand Up @@ -67,17 +69,9 @@ public static void ReadKey()
}

[ConditionalTheory(nameof(ManualTestsEnabled))]
[InlineData('\x01', ConsoleKey.A, ConsoleModifiers.Control)]
[InlineData('\x01', ConsoleKey.A, ConsoleModifiers.Control | ConsoleModifiers.Alt)]
[InlineData('\r', ConsoleKey.Enter, (ConsoleModifiers)0)]
[InlineData('\n', ConsoleKey.Enter, ConsoleModifiers.Control)]
public static void ReadKey_KeyChords(char keyChar, ConsoleKey key, ConsoleModifiers modifiers)
[MemberData(nameof(GetKeyChords))]
public static void ReadKey_KeyChords(ConsoleKeyInfo expected)
{
var expected = new ConsoleKeyInfo(keyChar, key,
control: modifiers.HasFlag(ConsoleModifiers.Control),
alt: modifiers.HasFlag(ConsoleModifiers.Alt),
shift: modifiers.HasFlag(ConsoleModifiers.Shift));

Console.Write($"Please type key chord {RenderKeyChord(expected)}: ");
var actual = Console.ReadKey(intercept: true);
Console.WriteLine();
Expand All @@ -96,6 +90,34 @@ static string RenderKeyChord(ConsoleKeyInfo key)
}
}

public static IEnumerable<object[]> GetKeyChords()
{
yield return MkConsoleKeyInfo('\x01', ConsoleKey.A, ConsoleModifiers.Control);
yield return MkConsoleKeyInfo('\x01', ConsoleKey.A, ConsoleModifiers.Control | ConsoleModifiers.Alt);
yield return MkConsoleKeyInfo('\r', ConsoleKey.Enter, (ConsoleModifiers)0);

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// windows will report '\n' as 'Ctrl+Enter', which is typically not picked up by Unix terminals
yield return MkConsoleKeyInfo('\n', ConsoleKey.Enter, ConsoleModifiers.Control);
}
else
{
yield return MkConsoleKeyInfo('\n', ConsoleKey.J, ConsoleModifiers.Control);
}

static object[] MkConsoleKeyInfo (char keyChar, ConsoleKey consoleKey, ConsoleModifiers modifiers)
{
return new object[]
{
new ConsoleKeyInfo(keyChar, consoleKey,
control: modifiers.HasFlag(ConsoleModifiers.Control),
alt: modifiers.HasFlag(ConsoleModifiers.Alt),
shift: modifiers.HasFlag(ConsoleModifiers.Shift))
};
}
}

[ConditionalFact(nameof(ManualTestsEnabled))]
public static void OpenStandardInput()
{
Expand Down
17 changes: 17 additions & 0 deletions src/libraries/System.Console/tests/ManualTests/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,20 @@ To run the suite, follow these steps:
2. Using a terminal, navigate to the current folder.
3. Enable manual testing by defining the `MANUAL_TESTS` environment variable (e.g. on bash `export MANUAL_TESTS=true`).
4. Run `dotnet test` and follow the instructions in the command prompt.

## Instructions for Windows testers

VsTest on Windows redirects console input, so in order to properly execute the manual tests,
`xunit-console` must be invoked directly. To do this first run

```
> dotnet build -t:Test
```

And then copy and execute the commands logged under the `To repro directly:` section of the output logs.

## Instructions for MacOS testers

By default, Alt-Key does not work on the MacOS terminal.
Before running the tests, navigate to `Terminal > Preferences > Settings > Keyboard`
and check "Use option as meta key" at the bottom.

0 comments on commit ff47b4b

Please sign in to comment.