Merge pull request #62 from SergeyTeplyakov/uriFormatException

More robust absolute path computation for the enhanced stack traces.
This commit is contained in:
Ben Adams 2018-02-22 05:48:35 +00:00 committed by GitHub
commit 4867d50668
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 16 deletions

View File

@ -1,9 +1,10 @@
// Copyright (c) Ben A Adams. All rights reserved. // Copyright (c) Ben A Adams. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Generic.Enumerable; using System.Collections.Generic.Enumerable;
using System.IO;
using System.Text; using System.Text;
namespace System.Diagnostics namespace System.Diagnostics
@ -104,21 +105,9 @@ namespace System.Diagnostics
var filePath = frame.GetFileName(); var filePath = frame.GetFileName();
if (!string.IsNullOrEmpty(filePath)) if (!string.IsNullOrEmpty(filePath))
{ {
try sb.Append(" in ");
{ sb.Append(TryGetFullPath(filePath));
sb.Append(" in ");
var uri = new Uri(filePath);
if (uri.IsFile)
{
sb.Append(System.IO.Path.GetFullPath(filePath));
}
else
{
sb.Append(uri);
}
}
catch
{ }
} }
var lineNo = frame.GetFileLineNumber(); var lineNo = frame.GetFileLineNumber();
@ -133,5 +122,27 @@ namespace System.Diagnostics
EnumerableIList<EnhancedStackFrame> GetEnumerator() => EnumerableIList.Create(_frames); EnumerableIList<EnhancedStackFrame> GetEnumerator() => EnumerableIList.Create(_frames);
IEnumerator<EnhancedStackFrame> IEnumerable<EnhancedStackFrame>.GetEnumerator() => _frames.GetEnumerator(); IEnumerator<EnhancedStackFrame> IEnumerable<EnhancedStackFrame>.GetEnumerator() => _frames.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => _frames.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => _frames.GetEnumerator();
/// <summary>
/// Tries to convert a given <paramref name="filePath"/> to a full path.
/// Returns original value if the conversion isn't possible or a given path is relative.
/// </summary>
public static string TryGetFullPath(string filePath)
{
try
{
var uri = new Uri(filePath);
if (uri.IsFile)
{
return uri.AbsolutePath;
}
return uri.ToString();
}
catch (ArgumentException) { }
catch (UriFormatException) { }
return filePath;
}
} }
} }

View File

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Xunit;
namespace Ben.Demystifier.Test
{
public class EnhancedStackTraceTests
{
[Theory]
[InlineData(@"file://Sources\MySolution\Foo.cs", @"/MySolution/Foo.cs")]
[InlineData(@"d:\Public\Src\Foo.cs", @"d:/Public/Src/Foo.cs")]
// To be deterministic, the C# compiler can take a /pathmap command line option.
// This option force the compiler to emit the same bits even when their built from the
// differrent locations.
// The binaries built with the pathmap usually don't have an absolute path,
// but have some prefix like \.\.
// This test case makes sure that EhancedStackTrace can deal with such kind of paths.
[InlineData(@"\.\Public\Src\Foo.cs", @"/./Public/Src/Foo.cs")]
public void RelativePathIsConvertedToAnAbsolutePath(string original, string expected)
{
var converted = EnhancedStackTrace.TryGetFullPath(original);
Assert.Equal(expected, NormalizePath(converted));
}
// Used in tests to avoid platform-specific issues.
private static string NormalizePath(string path)
=> path.Replace("\\", "/");
}
}