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

* Make AllowedType more flexible

* Add test cases for passing empty/null strings to IsAllowed
This commit is contained in:
Thomas Nind
2023-05-20 18:59:35 +01:00
committed by GitHub
parent ecf6d4f340
commit d0107e6026
2 changed files with 40 additions and 1 deletions

View File

@@ -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));
}
}

View File

@@ -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)]