From ac808ca585517fc0636eb10d2e293fb3e7a12e58 Mon Sep 17 00:00:00 2001 From: Toni Solarin-Sodara Date: Sun, 9 Dec 2018 11:47:13 +0100 Subject: [PATCH] add method to retrieve document url from sourcelink debug information --- src/coverlet.core/Coverage.cs | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/coverlet.core/Coverage.cs b/src/coverlet.core/Coverage.cs index c3a6f9c..bb31765 100644 --- a/src/coverlet.core/Coverage.cs +++ b/src/coverlet.core/Coverage.cs @@ -248,5 +248,46 @@ namespace Coverlet.Core InstrumentationHelper.DeleteHitsFile(result.HitsFilePath); } } + + private string GetSourceLinkUrl(Dictionary sourceLinkDocuments, string document) + { + if (sourceLinkDocuments.TryGetValue(document, out string url)) + { + return url; + } + + var keyWithBestMatch = string.Empty; + var relativePathOfBestMatch = string.Empty; + + foreach (var sourceLinkDocument in sourceLinkDocuments) + { + string key = sourceLinkDocument.Key; + if (Path.GetFileName(key) != "*") continue; + + string relativePath = Path.GetRelativePath(Path.GetDirectoryName(key), Path.GetDirectoryName(document)); + + if (relativePath.Contains("..")) continue; + + if (relativePathOfBestMatch.Length == 0) + { + keyWithBestMatch = sourceLinkDocument.Key; + relativePathOfBestMatch = relativePath; + } + + if (relativePath.Length < relativePathOfBestMatch.Length) + { + keyWithBestMatch = sourceLinkDocument.Key; + relativePathOfBestMatch = relativePath; + } + } + + relativePathOfBestMatch = relativePathOfBestMatch == "." ? string.Empty : relativePathOfBestMatch; + + string replacement = Path.Combine(relativePathOfBestMatch, Path.GetFileName(document)); + replacement = replacement.Replace('\\', '/'); + + url = sourceLinkDocuments[keyWithBestMatch]; + return url.Replace("*", replacement); + } } }