فهرست منبع

Fixes #2643 - Make AllowedType more permissive in file matching (#2644)

* Make AllowedType more flexible

* Add test cases for passing empty/null strings to IsAllowed
Thomas Nind 2 سال پیش
والد
کامیت
d0107e6026
2فایلهای تغییر یافته به همراه40 افزوده شده و 1 حذف شده
  1. 9 1
      Terminal.Gui/FileServices/AllowedType.cs
  2. 31 0
      UnitTests/FileServices/FileDialogTests.cs

+ 9 - 1
Terminal.Gui/FileServices/AllowedType.cs

@@ -94,14 +94,22 @@ namespace Terminal.Gui {
 		/// <inheritdoc/>
 		public bool IsAllowed(string path)
 		{
+			if(string.IsNullOrWhiteSpace(path)) {
+				return false;
+			}
+
 			var extension = Path.GetExtension (path);
 
+			if(this.Extensions.Any(e=>path.EndsWith(e, StringComparison.InvariantCultureIgnoreCase))) {
+				return true;
+			}
+
 			// There is a requirement to have a particular extension and we have none
 			if (string.IsNullOrEmpty (extension)) {
 				return false;
 			}
 
-			return this.Extensions.Any (e => e.Equals (extension));
+			return this.Extensions.Any (e => e.Equals (extension, StringComparison.InvariantCultureIgnoreCase));
 		}
 	}
 	

+ 31 - 0
UnitTests/FileServices/FileDialogTests.cs

@@ -481,6 +481,37 @@ namespace Terminal.Gui.FileServicesTests {
 			};
 		}
 
+		[Theory]
+		[InlineData (".csv", null, false)]
+		[InlineData (".csv", "", false)]
+		[InlineData (".csv", "c:\\MyFile.csv", true)]
+		[InlineData (".csv", "c:\\MyFile.CSV", true)]
+		[InlineData (".csv", "c:\\MyFile.csv.bak", false)]
+		public void TestAllowedType_Basic(string allowed, string candidate, bool expected)
+		{
+			Assert.Equal (expected, new AllowedType ("Test", allowed).IsAllowed (candidate));
+		}
+
+		[Theory]
+		[InlineData ("Dockerfile", "c:\\temp\\Dockerfile", true)]
+		[InlineData ("Dockerfile", "Dockerfile", true)]
+		[InlineData ("Dockerfile", "someimg.Dockerfile", true)]
+		public void TestAllowedType_SpecificFile(string allowed, string candidate, bool expected)
+		{
+			Assert.Equal (expected, new AllowedType ("Test", allowed).IsAllowed (candidate));
+		}
+
+		[Theory]
+		[InlineData (".Designer.cs", "c:\\MyView.Designer.cs", true)]
+		[InlineData (".Designer.cs", "c:\\temp/MyView.Designer.cs", true)]
+		[InlineData(".Designer.cs","MyView.Designer.cs",true)]
+		[InlineData (".Designer.cs", "c:\\MyView.DESIGNER.CS", true)]
+		[InlineData (".Designer.cs", "MyView.cs", false)]
+		public void TestAllowedType_DoubleBarreled (string allowed, string candidate, bool expected)
+		{
+			Assert.Equal (expected, new AllowedType ("Test", allowed).IsAllowed (candidate));
+		}
+
 		[Theory, AutoInitShutdown]
 		[InlineData (true)]
 		[InlineData (false)]