Merge pull request #62 from SergeyTeplyakov/uriFormatException
More robust absolute path computation for the enhanced stack traces.
This commit is contained in:
commit
4867d50668
@ -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.
|
||||
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic.Enumerable;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace System.Diagnostics
|
||||
@ -103,22 +104,10 @@ namespace System.Diagnostics
|
||||
|
||||
var filePath = frame.GetFileName();
|
||||
if (!string.IsNullOrEmpty(filePath))
|
||||
{
|
||||
try
|
||||
{
|
||||
sb.Append(" in ");
|
||||
var uri = new Uri(filePath);
|
||||
if (uri.IsFile)
|
||||
{
|
||||
sb.Append(System.IO.Path.GetFullPath(filePath));
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.Append(uri);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
sb.Append(TryGetFullPath(filePath));
|
||||
|
||||
}
|
||||
|
||||
var lineNo = frame.GetFileLineNumber();
|
||||
@ -133,5 +122,27 @@ namespace System.Diagnostics
|
||||
EnumerableIList<EnhancedStackFrame> GetEnumerator() => EnumerableIList.Create(_frames);
|
||||
IEnumerator<EnhancedStackFrame> IEnumerable<EnhancedStackFrame>.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
31
test/Ben.Demystifier.Test/EnhancedStackTraceTests.cs
Normal file
31
test/Ben.Demystifier.Test/EnhancedStackTraceTests.cs
Normal 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("\\", "/");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user