From e17551f8e93fe63933a26c0aceff211b9e908f67 Mon Sep 17 00:00:00 2001 From: Eddy Nakamura Date: Wed, 13 Jan 2021 17:01:58 -0300 Subject: [PATCH] Accepting relative URLs (#2235) * Accepting relative url * creating static to reuse object * Adding test to extension --- src/Sarif/ExtensionMethods.cs | 9 ++++++- src/Sarif/FileRegionsCache.cs | 11 ++++---- src/Sarif/RuleUtilities.cs | 5 ++-- src/Test.UnitTests.Sarif/ExtensionsTests.cs | 29 +++++++++++++++++++++ 4 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/Sarif/ExtensionMethods.cs b/src/Sarif/ExtensionMethods.cs index 7dddc2554..86ebe1023 100644 --- a/src/Sarif/ExtensionMethods.cs +++ b/src/Sarif/ExtensionMethods.cs @@ -173,12 +173,19 @@ public static string GetFileName(this Uri uri) { if (!uri.IsAbsoluteUri) { - throw new InvalidOperationException(); + const string baseUri = "https://example.com"; + var newAbsoluteUri = new Uri(new Uri(baseUri), uri.OriginalString); + return Path.GetFileName(newAbsoluteUri.LocalPath); } return Path.GetFileName(uri.LocalPath); } + public static string GetFilePath(this Uri uri) + { + return uri.IsAbsoluteUri ? uri.LocalPath : uri.OriginalString; + } + public static string FormatForVisualStudio(this Region region) { if (region == null) diff --git a/src/Sarif/FileRegionsCache.cs b/src/Sarif/FileRegionsCache.cs index 1e045a4bb..58c2a5d75 100644 --- a/src/Sarif/FileRegionsCache.cs +++ b/src/Sarif/FileRegionsCache.cs @@ -313,18 +313,19 @@ private void PopulateCharLength(NewLineIndex lineIndex, Region region) private NewLineIndex GetNewLineIndex(Uri uri, string fileText = null) { - NewLineIndex newLineIndex = null; + string path = uri.GetFilePath(); - if (!_cache.ContainsKey(uri.LocalPath) && fileText != null) + NewLineIndex newLineIndex; + if (!_cache.ContainsKey(path) && fileText != null) { newLineIndex = new NewLineIndex(fileText); - _cache[uri.LocalPath] = - new Tuple(item1: uri.LocalPath, item2: newLineIndex); + _cache[path] = + new Tuple(item1: path, item2: newLineIndex); } else { - Tuple entry = _cache[uri.LocalPath]; + Tuple entry = _cache[path]; newLineIndex = entry.Item2; } diff --git a/src/Sarif/RuleUtilities.cs b/src/Sarif/RuleUtilities.cs index 4120dc61d..1425e07b0 100644 --- a/src/Sarif/RuleUtilities.cs +++ b/src/Sarif/RuleUtilities.cs @@ -60,8 +60,7 @@ public static Result BuildResult(FailureLevel level, ResultKind kind, IAnalysisC Kind = kind }; - string targetPath = context.TargetUri?.LocalPath; - if (targetPath != null) + if (context.TargetUri != null) { result.Locations = new List { new Sarif.Location { @@ -69,7 +68,7 @@ public static Result BuildResult(FailureLevel level, ResultKind kind, IAnalysisC { ArtifactLocation = new ArtifactLocation { - Uri = new Uri(targetPath) + Uri = context.TargetUri }, Region = region } diff --git a/src/Test.UnitTests.Sarif/ExtensionsTests.cs b/src/Test.UnitTests.Sarif/ExtensionsTests.cs index c0aadc8ea..a6d2aab3a 100644 --- a/src/Test.UnitTests.Sarif/ExtensionsTests.cs +++ b/src/Test.UnitTests.Sarif/ExtensionsTests.cs @@ -1,7 +1,9 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using System; using System.Collections.Generic; +using System.Text; using System.Xml; using System.Xml.Linq; @@ -410,5 +412,32 @@ public void Extensions_ExtractsFirstSentenceProperly(string input, string expect string actual = ExtensionMethods.GetFirstSentence(input); actual.Should().Be(expected); } + + [Fact] + public void Extensions_GetFileName() + { + var tuples = new List> + { + new Tuple(new Uri("file.a", UriKind.Relative), "file.a"), + new Tuple(new Uri("\\file.a", UriKind.Relative), "file.a"), + new Tuple(new Uri("/file.a", UriKind.Relative), "file.a"), + new Tuple(new Uri("file.a?some-query-string", UriKind.Relative), "file.a"), + new Tuple(new Uri("https://github.com/microsoft/sarif-sdk/NuGet.Config", UriKind.Absolute), "NuGet.Config"), + new Tuple(new Uri("https://github.com/microsoft/sarif-sdk/NuGet.Config?some-query-string", UriKind.Absolute), "NuGet.Config"), + }; + + var sb = new StringBuilder(); + foreach (Tuple tuple in tuples) + { + string fileName = tuple.Item1.GetFileName(); + + if (!fileName.Equals(tuple.Item2)) + { + sb.Append($"{fileName} should be equal {tuple.Item2};"); + } + } + + sb.Length.Should().Be(0); + } } }