Improve path/uri handling (#82)
* Improve file path special characters handling * More specific tests * Optimize EnhancedStackTrace.TryGetFullPath to prevent exceptions being thrown and the immediately re-caught
This commit is contained in:
parent
9b85871130
commit
8604340a0e
@ -129,19 +129,11 @@ namespace System.Diagnostics
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static string TryGetFullPath(string filePath)
|
public static string TryGetFullPath(string filePath)
|
||||||
{
|
{
|
||||||
try
|
if (Uri.TryCreate(filePath, UriKind.Absolute, out var uri) && uri.IsFile)
|
||||||
{
|
{
|
||||||
var uri = new Uri(filePath);
|
return Uri.UnescapeDataString(uri.AbsolutePath);
|
||||||
if (uri.IsFile)
|
|
||||||
{
|
|
||||||
return uri.AbsolutePath;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return uri.ToString();
|
|
||||||
}
|
|
||||||
catch (ArgumentException) { }
|
|
||||||
catch (UriFormatException) { }
|
|
||||||
|
|
||||||
return filePath;
|
return filePath;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,6 +21,21 @@ namespace Ben.Demystifier.Test
|
|||||||
Assert.Equal(expected, NormalizePath(converted));
|
Assert.Equal(expected, NormalizePath(converted));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(@"file://Sources\My 100%.Done+Solution\Foo`1.cs", @"/My 100%.Done+Solution/Foo`1.cs", false)]
|
||||||
|
[InlineData(@"d:\Public Files+50%.Done\Src\Foo`1.cs", @"d:/Public Files+50%.Done/Src/Foo`1.cs", false)]
|
||||||
|
[InlineData(@"\.\Public Files+50%.Done\Src\Foo`1.cs", @"/./Public Files+50%.Done/Src/Foo`1.cs", true)]
|
||||||
|
public void SpecialPathCharactersAreHandledCorrectly(string original, string expected, bool normalize)
|
||||||
|
{
|
||||||
|
var converted = EnhancedStackTrace.TryGetFullPath(original);
|
||||||
|
if (normalize)
|
||||||
|
{
|
||||||
|
converted = NormalizePath(converted);
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert.Equal(expected, converted);
|
||||||
|
}
|
||||||
|
|
||||||
// Used in tests to avoid platform-specific issues.
|
// Used in tests to avoid platform-specific issues.
|
||||||
private static string NormalizePath(string path)
|
private static string NormalizePath(string path)
|
||||||
=> path.Replace("\\", "/");
|
=> path.Replace("\\", "/");
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user