Skip to content

Commit

Permalink
Accepting relative URLs (#2235)
Browse files Browse the repository at this point in the history
* Accepting relative url

* creating static to reuse object

* Adding test to extension
  • Loading branch information
eddynaka authored Jan 13, 2021
1 parent f26267e commit e17551f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
9 changes: 8 additions & 1 deletion src/Sarif/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 6 additions & 5 deletions src/Sarif/FileRegionsCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, NewLineIndex>(item1: uri.LocalPath, item2: newLineIndex);
_cache[path] =
new Tuple<string, NewLineIndex>(item1: path, item2: newLineIndex);
}
else
{
Tuple<string, NewLineIndex> entry = _cache[uri.LocalPath];
Tuple<string, NewLineIndex> entry = _cache[path];

newLineIndex = entry.Item2;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Sarif/RuleUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,15 @@ 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<Location> {
new Sarif.Location {
PhysicalLocation = new PhysicalLocation
{
ArtifactLocation = new ArtifactLocation
{
Uri = new Uri(targetPath)
Uri = context.TargetUri
},
Region = region
}
Expand Down
29 changes: 29 additions & 0 deletions src/Test.UnitTests.Sarif/ExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<Tuple<Uri, string>>
{
new Tuple<Uri, string>(new Uri("file.a", UriKind.Relative), "file.a"),
new Tuple<Uri, string>(new Uri("\\file.a", UriKind.Relative), "file.a"),
new Tuple<Uri, string>(new Uri("/file.a", UriKind.Relative), "file.a"),
new Tuple<Uri, string>(new Uri("file.a?some-query-string", UriKind.Relative), "file.a"),
new Tuple<Uri, string>(new Uri("https://github.com/microsoft/sarif-sdk/NuGet.Config", UriKind.Absolute), "NuGet.Config"),
new Tuple<Uri, string>(new Uri("https://github.com/microsoft/sarif-sdk/NuGet.Config?some-query-string", UriKind.Absolute), "NuGet.Config"),
};

var sb = new StringBuilder();
foreach (Tuple<Uri, string> 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);
}
}
}

0 comments on commit e17551f

Please sign in to comment.