Răsfoiți Sursa

better error handling

Charlie Kindel 2 ani în urmă
părinte
comite
ccb8405041

+ 8 - 2
Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs

@@ -1528,10 +1528,16 @@ namespace Terminal.Gui {
 				powershell.Start ();
 				powershell.WaitForExit ();
 				if (!powershell.DoubleWaitForExit ()) {
-					var timeoutError = $@"Process timed out. Command line: bash {powershell.StartInfo.Arguments}.
+					var timeoutError = $@"Process timed out. Command line: {powershell.StartInfo.FileName} {powershell.StartInfo.Arguments}.
 							Output: {powershell.StandardOutput.ReadToEnd ()}
 							Error: {powershell.StandardError.ReadToEnd ()}";
-					throw new Exception (timeoutError);
+					throw new TimeoutException (timeoutError);
+				}
+				if (powershell.ExitCode > 0) {
+					var setClipboardError = $@"Set-Clipboard failed. Command line: {powershell.StartInfo.FileName} {powershell.StartInfo.Arguments}.
+							Output: {powershell.StandardOutput.ReadToEnd ()}
+							Error: {powershell.StandardError.ReadToEnd ()}";
+					throw new System.InvalidOperationException (setClipboardError);
 				}
 				if (Application.Driver is CursesDriver) {
 					Curses.raw ();

+ 2 - 0
Terminal.Gui/Core/Clipboard/Clipboard.cs

@@ -29,6 +29,8 @@ namespace Terminal.Gui {
 						Application.Driver.Clipboard.SetClipboardData (value.ToString ());
 					}
 					contents = value;
+				} catch (NotSupportedException e) {
+					throw e;
 				} catch (Exception) {
 					contents = value;
 				}

+ 9 - 19
UnitTests/ClipboardTests.cs

@@ -1,40 +1,36 @@
 using System.Diagnostics;
 using System.Runtime.InteropServices;
 using Xunit;
+using Xunit.Abstractions;
 
 namespace Terminal.Gui.ConsoleDrivers {
 	public class ClipboardTests {
-		[Fact]
+
+		[Fact, AutoInitShutdown (useFakeClipboard: false)]
 		public void Contents_Gets_Sets ()
 		{
-			Application.Init (new FakeDriver (useFakeClipboard: false), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
 			var clipText = "This is a clipboard unit test.";
 			Clipboard.Contents = clipText;
 
 			Application.Iteration += () => Application.RequestStop ();
-
 			Application.Run ();
 
 			Assert.Equal (clipText, Clipboard.Contents);
-			Application.Shutdown ();
 		}
 
-		[Fact]
+		[Fact, AutoInitShutdown (useFakeClipboard: false)]
 		public void IsSupported_Get ()
 		{
-			Application.Init (new FakeDriver (useFakeClipboard: false), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
 			if (Clipboard.IsSupported) {
 				Assert.True (Clipboard.IsSupported);
 			} else {
 				Assert.False (Clipboard.IsSupported);
 			}
-			Application.Shutdown ();
 		}
 
-		[Fact]
+		[Fact, AutoInitShutdown (useFakeClipboard: false)]
 		public void TryGetClipboardData_Gets_From_OS_Clipboard ()
 		{
-			Application.Init (new FakeDriver (useFakeClipboard: false), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
 			var clipText = "Trying to get from the OS clipboard.";
 			Clipboard.Contents = clipText;
 
@@ -49,13 +45,11 @@ namespace Terminal.Gui.ConsoleDrivers {
 				Assert.False (Clipboard.TryGetClipboardData (out string result));
 				Assert.NotEqual (clipText, result);
 			}
-			Application.Shutdown ();
 		}
 
-		[Fact]
+		[Fact, AutoInitShutdown (useFakeClipboard: false)]
 		public void TrySetClipboardData_Sets_The_OS_Clipboard ()
 		{
-			Application.Init (new FakeDriver (useFakeClipboard: false), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
 			var clipText = "Trying to set the OS clipboard.";
 			if (Clipboard.IsSupported) {
 				Assert.True (Clipboard.TrySetClipboardData (clipText));
@@ -72,13 +66,11 @@ namespace Terminal.Gui.ConsoleDrivers {
 			} else {
 				Assert.NotEqual (clipText, Clipboard.Contents);
 			}
-			Application.Shutdown ();
 		}
 
-		[Fact]
+		[Fact, AutoInitShutdown (useFakeClipboard: false)]
 		public void Contents_Gets_From_OS_Clipboard ()
 		{
-			Application.Init (new FakeDriver (useFakeClipboard: false), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
 			var clipText = "This is a clipboard unit test to get clipboard from OS.";
 			var exit = false;
 			var getClipText = "";
@@ -193,13 +185,11 @@ namespace Terminal.Gui.ConsoleDrivers {
 			if (!exit) {
 				Assert.Equal (clipText, getClipText);
 			}
-			Application.Shutdown ();
 		}
 
-		[Fact]
+		[Fact, AutoInitShutdown (useFakeClipboard: false)]
 		public void Contents_Sets_The_OS_Clipboard ()
 		{
-			Application.Init (new FakeDriver (useFakeClipboard: false), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
 			var clipText = "This is a clipboard unit test to set the OS clipboard.";
 			var clipReadText = "";
 			var exit = false;
@@ -279,7 +269,7 @@ namespace Terminal.Gui.ConsoleDrivers {
 			if (!exit) {
 				Assert.Equal (clipText, clipReadText);
 			}
-			Application.Shutdown ();
+
 		}
 
 		bool Is_WSL_Platform ()

+ 26 - 9
UnitTests/TestHelpers.cs

@@ -10,31 +10,48 @@ using Rune = System.Rune;
 using Attribute = Terminal.Gui.Attribute;
 using System.Text.RegularExpressions;
 using System.Reflection;
+using System.Diagnostics;
 
 
 // This class enables test functions annotated with the [AutoInitShutdown] attribute to 
-// automatically call Application.Init before called and Application.Shutdown after
+// automatically call Application.Init at start of the test and Application.Shutdown after the
+// test exits. 
 // 
 // This is necessary because a) Application is a singleton and Init/Shutdown must be called
-// as a pair, and b) all unit test functions should be atomic.
+// as a pair, and b) all unit test functions should be atomic..
 [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
 public class AutoInitShutdownAttribute : Xunit.Sdk.BeforeAfterTestAttribute {
 
+	public AutoInitShutdownAttribute (bool autoInit = true, bool autoShutdown = true, bool useFakeClipboard = true)
+	{
+		this.AutoInit = autoInit;
+		this.AutoShutdown = autoShutdown;
+		this.UseFakeClipboard = useFakeClipboard;
+	}
+
 	static bool _init = false;
+
+	public bool AutoInit { get; }
+	public bool AutoShutdown { get; }
+	public bool UseFakeClipboard { get; }
+
 	public override void Before (MethodInfo methodUnderTest)
 	{
-		if (_init) {
-			throw new InvalidOperationException ("After did not run.");
+		if (AutoShutdown && _init) {
+			throw new InvalidOperationException ("After did not run when AutoShutdown was specified.");
+		}
+		if (AutoInit) {
+			Application.Init (new FakeDriver (useFakeClipboard: UseFakeClipboard), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
+			_init = true;
 		}
-
-		Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
-		_init = true;
 	}
 
 	public override void After (MethodInfo methodUnderTest)
 	{
-		Application.Shutdown ();
-		_init = false;
+		if (AutoShutdown) {
+			Application.Shutdown ();
+			_init = false;
+		}
 	}
 }