Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accepting relative URLs #2235

Merged
merged 8 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

@michaelcfanning michaelcfanning Jan 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetFileName [](start = 28, length = 11)

make sure this works with a path like \blah.txt and blah.txt (no prefixed slash). #Closed

}

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);
}
}
}