Skip to content

Commit

Permalink
Fixed netdriver running in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tig committed Aug 12, 2023
1 parent b71d533 commit 3dc19c9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
22 changes: 12 additions & 10 deletions Terminal.Gui/ConsoleDrivers/NetDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -572,14 +572,16 @@ public override void End ()
}

StopReportingMouseMoves ();
Console.ResetColor ();

//Disable alternative screen buffer.
Console.Out.Write (EscSeqUtils.CSI_RestoreCursorAndActivateAltBufferWithBackscroll);
if (!_runningUnitTests) {
Console.ResetColor ();

//Set cursor key to cursor.
Console.Out.Write (EscSeqUtils.CSI_ShowCursor);
//Disable alternative screen buffer.
Console.Out.Write (EscSeqUtils.CSI_RestoreCursorAndActivateAltBufferWithBackscroll);

//Set cursor key to cursor.
Console.Out.Write (EscSeqUtils.CSI_ShowCursor);
}
Console.Out.Close ();
}

Expand Down Expand Up @@ -608,13 +610,13 @@ public override void Init (Action terminalResized)

TerminalResized = terminalResized;

//Enable alternative screen buffer.
Console.Out.Write (EscSeqUtils.CSI_SaveCursorAndActivateAltBufferNoBackscroll);
try {
//Enable alternative screen buffer.
Console.Out.Write (EscSeqUtils.CSI_SaveCursorAndActivateAltBufferNoBackscroll);

//Set cursor key to application.
Console.Out.Write (EscSeqUtils.CSI_HideCursor);
//Set cursor key to application.
Console.Out.Write (EscSeqUtils.CSI_HideCursor);

try {
Console.TreatControlCAsInput = true;
Cols = Console.WindowWidth;
Rows = Console.WindowHeight;
Expand Down
8 changes: 6 additions & 2 deletions Terminal.Gui/ConsoleDrivers/WindowsDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ private void ChangeWin (Size e)
Cols = e.Width;
Rows = e.Height;

if (WinConsole != null) {
if (!_runningUnitTests) {
var newSize = WinConsole.SetConsoleWindow (
(short)Math.Max (w, 16), (short)Math.Max (e.Height, 0));

Expand Down Expand Up @@ -1447,6 +1447,8 @@ public override bool IsRuneSupported (Rune rune)
return base.IsRuneSupported (rune) && rune.IsBmp;
}

bool _runningUnitTests = false;

public override void Init (Action terminalResized)
{
TerminalResized = terminalResized;
Expand All @@ -1472,7 +1474,9 @@ public override void Init (Action terminalResized)
Console.Out.Write (EscSeqUtils.CSI_SaveCursorAndActivateAltBufferNoBackscroll);
}
} catch (Win32Exception e) {
// Likely running unit tests. Set WinConsole to null so we can test it elsewhere.
// We are being run in an environment that does not support a console
// such as a unit test, or a pipe.
_runningUnitTests = true;
Debug.WriteLine ($"Likely running unit tests. Setting WinConsole to null so we can test it elsewhere. Exception: {e}");
WinConsole = null;
}
Expand Down
2 changes: 1 addition & 1 deletion UnitTests/ConsoleDrivers/ColorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void SetColors_Changes_Colors (Type driverType)
{
var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
Application.Init (driver);
// driver.Init (() => { });

Assert.Equal (ConsoleColor.Gray, Console.ForegroundColor);
Assert.Equal (ConsoleColor.Black, Console.BackgroundColor);

Expand Down

0 comments on commit 3dc19c9

Please sign in to comment.