Skip to content

Commit

Permalink
SLVS-1436 Implement review feedback:
Browse files Browse the repository at this point in the history
- remove check if the filename contains reserved char for performance reason
- remove ? char from reserved list, because it is not supported in file names in windows and it also increases performance
- use string builder to improve performance
- add more unit tests
  • Loading branch information
gabriela-trutan-sonarsource committed Sep 4, 2024
1 parent e792185 commit 097b069
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
41 changes: 41 additions & 0 deletions src/SLCore.UnitTests/Common/Models/FileUriTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,47 @@ public void LocalPath_ReturnsCorrectPath()
new FileUri(filePath).LocalPath.Should().Be(filePath);
}

[TestMethod]
[DataRow("[", "%5B")]
[DataRow("]", "%5D")]
[DataRow("#", "%2523")]
[DataRow("@", "%40")]
[DataRow(" ", "%20")]
[DataRow("`", "%60")]
public void LocalPath_UnescapesEncodesCharacters(string reservedChar, string expectedEncoding)
{
var expectedFilePath = @$"C:\filewithRfc3986ReservedChar{reservedChar}.cs";
var encodedFilePath = @$"file:///C:/filewithRfc3986ReservedChar{expectedEncoding}.cs";

new FileUri(encodedFilePath).LocalPath.Should().Be(expectedFilePath);
}

[TestMethod]
[DataRow("[")]
[DataRow("]")]
[DataRow("@")]
[DataRow(" ")]
[DataRow("`")]
public void LocalPath_PathDoesNotHaveEncodedChars_ReturnsCorrectLocalPath(string reservedChar)
{
var notEncodedFilePath = @$"file:///C:/filewithRfc3986ReservedChar{reservedChar}.cs";
var expectedFilePath = @$"C:\filewithRfc3986ReservedChar{reservedChar}.cs";

new FileUri(notEncodedFilePath).LocalPath.Should().Be(expectedFilePath);
}

/// <summary>
/// The # character as the beginning of a fragment, so <see cref="Uri.LocalPath"/> will return the path without the fragment.
/// </summary>
[TestMethod]
public void LocalPath_PathWithHashCharacter_ReturnsLocalPathWithoutHash()
{
var notEncodedFilePath = @$"file:///C:/filewithRfc3986ReservedChar#.cs";
var expectedFilePath = @$"C:\filewithRfc3986ReservedChar";

new FileUri(notEncodedFilePath).LocalPath.Should().Be(expectedFilePath);
}

[TestMethod]
public void SerializeDeserializeToEqualObject()
{
Expand Down
15 changes: 7 additions & 8 deletions src/SLCore/Common/Models/FileUri.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@

using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text;
using SonarLint.VisualStudio.SLCore.Protocol;

namespace SonarLint.VisualStudio.SLCore.Common.Models;
Expand All @@ -30,7 +29,7 @@ namespace SonarLint.VisualStudio.SLCore.Common.Models;
public sealed class FileUri
{
private readonly Uri uri;
private static readonly char[] Rfc3986ReservedCharsToEncoding = ['?', '#', '[', ']', '@'];
private static readonly char[] Rfc3986ReservedCharsToEncode = ['#', '[', ']', '@'];

public FileUri(string uriString)
{
Expand All @@ -56,11 +55,11 @@ public override string ToString()
/// <returns></returns>
private static string EscapeRfc3986ReservedCharacters(string stringToEscape)
{
var charsToEscape = Rfc3986ReservedCharsToEncoding.Where(stringToEscape.Contains).ToList();

return !charsToEscape.Any()
? stringToEscape
: charsToEscape.Aggregate(stringToEscape, (current, charToEscape) => current.Replace(charToEscape.ToString(), Uri.HexEscape(charToEscape)));
var stringBuilderToEscape = new StringBuilder(stringToEscape);
return Rfc3986ReservedCharsToEncode.Aggregate(stringBuilderToEscape,
(current, charToEscape) => current.Replace(charToEscape.ToString(), Uri.HexEscape(charToEscape)))
.ToString();
}

[ExcludeFromCodeCoverage]
Expand Down

0 comments on commit 097b069

Please sign in to comment.