Browse Source

Fixed netdriver running in unit tests

Tigger Kindel 2 years ago
parent
commit
3dc19c9243

+ 12 - 10
Terminal.Gui/ConsoleDrivers/NetDriver.cs

@@ -572,14 +572,16 @@ internal class NetDriver : ConsoleDriver {
 		}
 
 		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 ();
 	}
 
@@ -608,13 +610,13 @@ internal class NetDriver : ConsoleDriver {
 
 		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;

+ 6 - 2
Terminal.Gui/ConsoleDrivers/WindowsDriver.cs

@@ -834,7 +834,7 @@ internal class WindowsDriver : ConsoleDriver {
 		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));
 
@@ -1447,6 +1447,8 @@ internal class WindowsDriver : ConsoleDriver {
 		return base.IsRuneSupported (rune) && rune.IsBmp;
 	}
 
+	bool _runningUnitTests = false;
+	
 	public override void Init (Action terminalResized)
 	{
 		TerminalResized = terminalResized;
@@ -1472,7 +1474,9 @@ internal class WindowsDriver : ConsoleDriver {
 				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;
 		}

+ 1 - 1
UnitTests/ConsoleDrivers/ColorTests.cs

@@ -16,7 +16,7 @@ namespace Terminal.Gui.DriverTests {
 		{
 			var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
 			Application.Init (driver);
-//			driver.Init (() => { });
+
 			Assert.Equal (ConsoleColor.Gray, Console.ForegroundColor);
 			Assert.Equal (ConsoleColor.Black, Console.BackgroundColor);