Browse Source

daignosing unit test fail in action

Tigger Kindel 2 years ago
parent
commit
fe23b564b3

+ 11 - 0
Terminal.Gui/ConsoleDrivers/ConsoleDriver.cs

@@ -21,6 +21,17 @@ namespace Terminal.Gui;
 /// - <see cref="FakeConsole"/> for unit testing.
 /// </remarks>
 public abstract class ConsoleDriver {
+	/// <summary>
+	/// Set this to true in any unit tests that attempt to test drivers other than FakeDriver.
+	/// <code>
+	///  public ColorTests ()
+	///  {
+	///    ConsoleDriver.RunningUnitTests = true;
+	///  }
+	/// </code>
+	/// </summary>
+	internal static bool RunningUnitTests { get; set; }
+
 	/// <summary>
 	/// Prepare the driver and set the key and mouse events handlers.
 	/// </summary>

+ 18 - 23
Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs

@@ -14,7 +14,6 @@ namespace Terminal.Gui;
 /// This is the Curses driver for the gui.cs/Terminal framework.
 /// </summary>
 internal class CursesDriver : ConsoleDriver {
-	bool _runningUnitTests = false;
 
 	public override int Cols => Curses.Cols;
 	public override int Rows => Curses.Lines;
@@ -30,7 +29,7 @@ internal class CursesDriver : ConsoleDriver {
 	{
 		base.Move (col, row);
 
-		if (_runningUnitTests) {
+		if (RunningUnitTests) {
 			return;
 		}
 
@@ -57,7 +56,7 @@ internal class CursesDriver : ConsoleDriver {
 
 	private void ProcessWinChange ()
 	{
-		if (!_runningUnitTests && Curses.CheckWinChange ()) {
+		if (!RunningUnitTests && Curses.CheckWinChange ()) {
 			ClearContents ();
 			TerminalResized?.Invoke ();
 		}
@@ -91,7 +90,7 @@ internal class CursesDriver : ConsoleDriver {
 	/// </remarks>
 	public override Attribute MakeColor (Color fore, Color back)
 	{
-		if (!_runningUnitTests) {
+		if (!RunningUnitTests) {
 			return MakeColor (ColorToCursesColorNumber (fore), ColorToCursesColorNumber (back));
 		} else {
 			return new Attribute (
@@ -198,7 +197,7 @@ internal class CursesDriver : ConsoleDriver {
 	{
 		EnsureCursorVisibility ();
 
-		if (!_runningUnitTests && Col >= 0 && Col < Cols && Row >= 0 && Row < Rows) {
+		if (!RunningUnitTests && Col >= 0 && Col < Cols && Row >= 0 && Row < Rows) {
 			Curses.move (Row, Col);
 		}
 	}
@@ -208,7 +207,7 @@ internal class CursesDriver : ConsoleDriver {
 		StopReportingMouseMoves ();
 		SetCursorVisibility (CursorVisibility.Default);
 
-		if (_runningUnitTests) {
+		if (RunningUnitTests) {
 			return;
 		}
 		// throws away any typeahead that has been typed by
@@ -230,7 +229,7 @@ internal class CursesDriver : ConsoleDriver {
 				if (Contents [row, col].IsDirty == false) {
 					continue;
 				}
-				if (_runningUnitTests) {
+				if (RunningUnitTests) {
 					// In unit tests, we don't want to actually write to the screen.
 					continue;
 				}
@@ -256,7 +255,7 @@ internal class CursesDriver : ConsoleDriver {
 			}
 		}
 
-		if (!_runningUnitTests) {
+		if (!RunningUnitTests) {
 			Curses.move (Row, Col);
 			_window.wrefresh ();
 		}
@@ -598,7 +597,7 @@ internal class CursesDriver : ConsoleDriver {
 
 	public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<KeyEvent> keyDownHandler, Action<KeyEvent> keyUpHandler, Action<MouseEvent> mouseHandler)
 	{
-		if (!_runningUnitTests) {
+		if (!RunningUnitTests) {
 			// Note: Curses doesn't support keydown/up events and thus any passed keyDown/UpHandlers will never be called
 			Curses.timeout (0);
 		}
@@ -619,16 +618,11 @@ internal class CursesDriver : ConsoleDriver {
 
 	public override void Init (Action terminalResized)
 	{
-		try {
+		if (!RunningUnitTests) {
+
 			_window = Curses.initscr ();
 			Curses.set_escdelay (10);
-		} catch (Exception e) {
-			_window = null;
-			_runningUnitTests = true;
-			throw new InvalidProgramException ($"Curses failed to initialize. Assuming Unit Tests. The exception is: {e.Message}");
-		}
 
-		if (!_runningUnitTests) {
 			// Ensures that all procedures are performed at some previous closing.
 			Curses.doupdate ();
 
@@ -682,11 +676,12 @@ internal class CursesDriver : ConsoleDriver {
 			}
 		}
 
-		if (!_runningUnitTests) {
+		ClearContents ();
+		StartReportingMouseMoves ();
+
+		if (!RunningUnitTests) {
 			Curses.CheckWinChange ();
-			ClearContents ();
 			Curses.refresh ();
-			StartReportingMouseMoves ();
 		}
 	}
 
@@ -707,7 +702,7 @@ internal class CursesDriver : ConsoleDriver {
 	public override void Suspend ()
 	{
 		StopReportingMouseMoves ();
-		if (!_runningUnitTests) {
+		if (!RunningUnitTests) {
 			Platform.Suspend ();
 			Curses.Window.Standard.redrawwin ();
 			Curses.refresh ();
@@ -717,14 +712,14 @@ internal class CursesDriver : ConsoleDriver {
 
 	public void StartReportingMouseMoves ()
 	{
-		if (!_runningUnitTests) {
+		if (!RunningUnitTests) {
 			Console.Out.Write (EscSeqUtils.CSI_EnableMouseEvents);
 		}
 	}
 
 	public void StopReportingMouseMoves ()
 	{
-		if (!_runningUnitTests) {
+		if (!RunningUnitTests) {
 			Console.Out.Write (EscSeqUtils.CSI_DisableMouseEvents);
 		}
 	}
@@ -749,7 +744,7 @@ internal class CursesDriver : ConsoleDriver {
 			return false;
 		}
 
-		if (!_runningUnitTests) {
+		if (!RunningUnitTests) {
 			Curses.curs_set (((int)visibility >> 16) & 0x000000FF);
 		}
 

+ 3 - 9
Terminal.Gui/ConsoleDrivers/CursesDriver/UnixMainLoop.cs

@@ -86,13 +86,11 @@ namespace Terminal.Gui {
 		MainLoop mainLoop;
 		bool winChanged;
 
-		bool _runningUnitTests = false;
-
 		public Action WinChanged;
 
 		void IMainLoopDriver.Wakeup ()
 		{
-			if (!_runningUnitTests) {
+			if (!ConsoleDriver.RunningUnitTests) {
 				write (wakeupPipes [1], ignore, (IntPtr)1);
 			}
 		}
@@ -107,7 +105,7 @@ namespace Terminal.Gui {
 					return true;
 				});
 			} catch (DllNotFoundException e) {
-				_runningUnitTests = true;
+				throw new NotSupportedException ("liblibc not found", e);
 			}
 		}
 
@@ -119,7 +117,7 @@ namespace Terminal.Gui {
 		/// </remarks>
 		public void RemoveWatch (object token)
 		{
-			if (!_runningUnitTests) {
+			if (!ConsoleDriver.RunningUnitTests) {
 				var watch = token as Watch;
 				if (watch == null)
 					return;
@@ -224,9 +222,5 @@ namespace Terminal.Gui {
 				}
 			}
 		}
-		public void TearDown ()
-		{
-			//throw new NotImplementedException ();
-		}
 	}
 }

+ 16 - 15
Terminal.Gui/ConsoleDrivers/NetDriver.cs

@@ -556,8 +556,6 @@ internal class NetDriver : ConsoleDriver {
 	const int COLOR_BRIGHT_CYAN = 96;
 	const int COLOR_BRIGHT_WHITE = 97;
 
-	bool _runningUnitTests = false;
-
 	public override bool SupportsTrueColor => Environment.OSVersion.Platform == PlatformID.Unix || (IsWinPlatform && Environment.OSVersion.Version.Build >= 14931);
 
 	public NetWinVTConsole NetWinConsole { get; private set; }
@@ -573,7 +571,7 @@ internal class NetDriver : ConsoleDriver {
 
 		StopReportingMouseMoves ();
 
-		if (!_runningUnitTests) {
+		if (!RunningUnitTests) {
 			Console.ResetColor ();
 
 			//Disable alternative screen buffer.
@@ -581,8 +579,8 @@ internal class NetDriver : ConsoleDriver {
 
 			//Set cursor key to cursor.
 			Console.Out.Write (EscSeqUtils.CSI_ShowCursor);
+			Console.Out.Close ();
 		}
-		Console.Out.Close ();
 	}
 
 	public override void Init (Action terminalResized)
@@ -610,10 +608,10 @@ internal class NetDriver : ConsoleDriver {
 
 		TerminalResized = terminalResized;
 
-		try {
-			// In unit tests, this will throw
-			Console.TreatControlCAsInput = true;
 
+		if (!RunningUnitTests) {
+			Console.TreatControlCAsInput = true;
+			
 			Cols = Console.WindowWidth;
 			Rows = Console.WindowHeight;
 
@@ -623,10 +621,9 @@ internal class NetDriver : ConsoleDriver {
 			//Set cursor key to application.
 			Console.Out.Write (EscSeqUtils.CSI_HideCursor);
 
-		} catch (IOException) {
+		} else {
 			// We are being run in an environment that does not support a console
 			// such as a unit test, or a pipe.
-			_runningUnitTests = true;
 			Cols = 80;
 			Rows = 24;
 		}
@@ -678,7 +675,7 @@ internal class NetDriver : ConsoleDriver {
 
 	public override void UpdateScreen ()
 	{
-		if (_runningUnitTests || _winSizeChanging || Console.WindowHeight < 1 || Contents.Length != Rows * Cols || Rows != Console.WindowHeight) {
+		if (RunningUnitTests || _winSizeChanging || Console.WindowHeight < 1 || Contents.Length != Rows * Cols || Rows != Console.WindowHeight) {
 			return;
 		}
 
@@ -869,7 +866,7 @@ internal class NetDriver : ConsoleDriver {
 	public override bool SetCursorVisibility (CursorVisibility visibility)
 	{
 		_cachedCursorVisibility = visibility;
-		var isVisible = _runningUnitTests ? visibility == CursorVisibility.Default : Console.CursorVisible = visibility == CursorVisibility.Default;
+		var isVisible = RunningUnitTests ? visibility == CursorVisibility.Default : Console.CursorVisible = visibility == CursorVisibility.Default;
 		//Console.Out.Write (isVisible ? EscSeqUtils.CSI_ShowCursor : EscSeqUtils.CSI_HideCursor);
 		return isVisible;
 	}
@@ -892,7 +889,7 @@ internal class NetDriver : ConsoleDriver {
 
 	void SetWindowPosition (int col, int row)
 	{
-		if (!_runningUnitTests) {
+		if (!RunningUnitTests) {
 			Top = Console.WindowTop;
 			Left = Console.WindowLeft;
 		} else {
@@ -900,18 +897,22 @@ internal class NetDriver : ConsoleDriver {
 			Left = col;
 		}
 	}
-	
+
 	#endregion
 
 
 	public void StartReportingMouseMoves ()
 	{
-		Console.Out.Write (EscSeqUtils.CSI_EnableMouseEvents);
+		if (!RunningUnitTests) {
+			Console.Out.Write (EscSeqUtils.CSI_EnableMouseEvents);
+		}
 	}
 
 	public void StopReportingMouseMoves ()
 	{
-		Console.Out.Write (EscSeqUtils.CSI_DisableMouseEvents);
+		if (!RunningUnitTests) {
+			Console.Out.Write (EscSeqUtils.CSI_DisableMouseEvents);
+		}
 	}
 
 	ConsoleKeyInfo FromVKPacketToKConsoleKeyInfo (ConsoleKeyInfo consoleKeyInfo)

+ 5 - 8
Terminal.Gui/ConsoleDrivers/WindowsDriver.cs

@@ -777,7 +777,7 @@ internal class WindowsDriver : ConsoleDriver {
 
 	public WindowsConsole WinConsole { get; private set; }
 
-	public override bool SupportsTrueColor => _runningUnitTests || (_isWindowsTerminal && Environment.OSVersion.Version.Build >= 14931);
+	public override bool SupportsTrueColor => RunningUnitTests || (_isWindowsTerminal && Environment.OSVersion.Version.Build >= 14931);
 
 	public override bool Force16Colors {
 		get => base.Force16Colors;
@@ -834,7 +834,7 @@ internal class WindowsDriver : ConsoleDriver {
 		Cols = e.Width;
 		Rows = e.Height;
 
-		if (!_runningUnitTests) {
+		if (!RunningUnitTests) {
 			var newSize = WinConsole.SetConsoleWindow (
 				(short)Math.Max (w, 16), (short)Math.Max (e.Height, 0));
 
@@ -1447,16 +1447,14 @@ internal class WindowsDriver : ConsoleDriver {
 		return base.IsRuneSupported (rune) && rune.IsBmp;
 	}
 
-	bool _runningUnitTests = false;
-	
 	public override void Init (Action terminalResized)
 	{
 		TerminalResized = terminalResized;
 
-		if (Environment.OSVersion.Platform == PlatformID.Unix) {
-			_runningUnitTests = true;
+		if (RunningUnitTests) {
 			return;
 		}
+		
 		try {
 			if (WinConsole != null) {
 				var winSize = WinConsole.GetConsoleOutputWindow (out Point pos);
@@ -1480,7 +1478,6 @@ internal class WindowsDriver : ConsoleDriver {
 		} catch (Win32Exception e) {
 			// 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;
 		}
@@ -1712,7 +1709,7 @@ internal class WindowsDriver : ConsoleDriver {
 		WinConsole?.Cleanup ();
 		WinConsole = null;
 
-		if (!_runningUnitTests && _isWindowsTerminal) {
+		if (!RunningUnitTests && _isWindowsTerminal) {
 			// Disable alternative screen buffer.
 			Console.Out.Write (EscSeqUtils.CSI_RestoreAltBufferWithBackscroll);
 		}

+ 11 - 1
UnitTests/ConsoleDrivers/ColorTests.cs

@@ -5,7 +5,17 @@ using Xunit;
 using Console = Terminal.Gui.FakeConsole;
 
 namespace Terminal.Gui.DriverTests {
-	public class ColorTests {
+	public class ColorTests: IDisposable
+		{
+		public ColorTests ()
+		{
+			ConsoleDriver.RunningUnitTests = true;
+		}
+		
+		public void Dispose ()
+		{
+			// ... clean up test data from the database ...
+		}
 
 		[Theory]
 		[InlineData (typeof (FakeDriver))]