Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
georgii-borovinskikh-sonarsource committed Nov 29, 2023
1 parent fe5d47f commit 313ebfb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
10 changes: 5 additions & 5 deletions src/ConnectedMode.UnitTests/IssueMatcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void IsMatch_MatchesBasedOnAllParameters(string serverRuleId, int? server
var issueToMatch = CreateIssueToMatch("CorrectRuleId", 1, "CorrectHash");
var serverIssue = CreateServerIssue(serverRuleId, serverIssueLine, serverHash);

CreateTestSubject().IsGoodMatch(issueToMatch, serverIssue).Should().Be(expectedResult);
CreateTestSubject().IsLikelyMatch(issueToMatch, serverIssue).Should().Be(expectedResult);
}

[DataTestMethod]
Expand All @@ -71,7 +71,7 @@ public void IsMatch_FileLevelIssue(string serverRuleId, int? serverIssueLine,
var issueToMatch = CreateIssueToMatch("CorrectRuleId", null, null);
var serverIssue = CreateServerIssue(serverRuleId, serverIssueLine, serverHash);

CreateTestSubject().IsGoodMatch(issueToMatch, serverIssue);
CreateTestSubject().IsLikelyMatch(issueToMatch, serverIssue).Should().Be(expectedResult);
}

[TestMethod]
Expand Down Expand Up @@ -103,7 +103,7 @@ public void IsMatch_CheckFileComparisons(string serverFilePath, string localFile

var serverIssue = CreateServerIssue("111", 0, "hash", filePath: serverFilePath);

CreateTestSubject().IsGoodMatch(issueToMatch, serverIssue).Should().Be(expected);
CreateTestSubject().IsLikelyMatch(issueToMatch, serverIssue).Should().Be(expected);
}

[TestMethod]
Expand All @@ -116,7 +116,7 @@ public void FindMatchOrDefault_FindsFirstMatch()

var correctServerIssue = CreateServerIssue(ruleId, startLine, null, serverPath);

CreateTestSubject().GetFirstMatchFromSameFileOrNull(issueToMatch, new[]
CreateTestSubject().GetFirstLikelyMatchFromSameFileOrNull(issueToMatch, new[]
{
CreateServerIssue("222", startLine, null, serverPath),
CreateServerIssue(ruleId, 111, null, serverPath),
Expand All @@ -130,7 +130,7 @@ public void FindMatchOrDefault_NoServerIssues_ReturnsNull()
{
var issueToMatch = CreateIssueToMatch("1", 1, "1");

CreateTestSubject().GetFirstMatchFromSameFileOrNull(issueToMatch, Array.Empty<SonarQubeIssue>()).Should().BeNull();
CreateTestSubject().GetFirstLikelyMatchFromSameFileOrNull(issueToMatch, Array.Empty<SonarQubeIssue>()).Should().BeNull();
}

private IssueMatcher CreateTestSubject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void MatchExists_MultipleServerIssues(int indexOfServerIssue)
var hasMatch = indexOfServerIssue != -1;
if (hasMatch)
{
issueMatcherMock.Setup(x => x.IsGoodMatch(issueToMatch, sonarQubeIssues[indexOfServerIssue])).Returns(true);
issueMatcherMock.Setup(x => x.IsLikelyMatch(issueToMatch, sonarQubeIssues[indexOfServerIssue])).Returns(true);
}

ConfigureServerIssues(sonarQubeIssues);
Expand All @@ -106,7 +106,7 @@ public void MatchExists_ResultDependsOnSuppressionState(string serverRuleId, int
var issueToMatch = CreateIssueToMatch();
ConfigureServerIssues(CreateServerIssue(isSuppressed));
issueMatcherMock
.Setup(x => x.IsGoodMatch(It.IsAny<IFilterableIssue>(), It.IsAny<SonarQubeIssue>()))
.Setup(x => x.IsLikelyMatch(It.IsAny<IFilterableIssue>(), It.IsAny<SonarQubeIssue>()))
.Returns(true);

// Act and assert
Expand Down
28 changes: 18 additions & 10 deletions src/ConnectedMode/IssueMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,35 @@ namespace SonarLint.VisualStudio.ConnectedMode
{
internal interface IIssueMatcher
{
bool IsGoodMatch(IFilterableIssue issue, SonarQubeIssue serverIssue);
/// <summary>
/// This method attempts to match <paramref name="issue"/> with <paramref name="serverIssue"/>.
/// There's a possibility of False Positive matches: in case false tail-match of file paths, as the project root is not taken into account,
/// or when line number matches but line hash doesn't.
/// </summary>
/// <param name="issue">Local issue</param>
/// <param name="serverIssue">Server issue</param>
/// <returns>The best possible match based on rule id, file name (tail matched to the server path), line number and line hash (not checked when line numbers match)</returns>
bool IsLikelyMatch(IFilterableIssue issue, SonarQubeIssue serverIssue);

SonarQubeIssue GetFirstMatchFromSameFileOrNull(IFilterableIssue issue, IEnumerable<SonarQubeIssue> serverIssuesFromSameFile);
/// <summary>
/// Returns the first matching issue. Note: for this method to work correctly, all <paramref name="serverIssuesFromSameFile"/> need to be from the same server file
/// </summary>
/// <param name="issue">Local issue</param>
/// <param name="serverIssuesFromSameFile">List of server issues from the same file</param>
/// <returns>A matching server issue, if present in the <paramref name="serverIssuesFromSameFile"/> list, or null</returns>
SonarQubeIssue GetFirstLikelyMatchFromSameFileOrNull(IFilterableIssue issue, IEnumerable<SonarQubeIssue> serverIssuesFromSameFile);
}

[Export(typeof(IIssueMatcher))]
[PartCreationPolicy(CreationPolicy.Shared)]
internal class IssueMatcher : IIssueMatcher
{
public bool IsGoodMatch(IFilterableIssue issue, SonarQubeIssue serverIssue)
public bool IsLikelyMatch(IFilterableIssue issue, SonarQubeIssue serverIssue)
{
return IsMatch(issue, serverIssue, true);
}

/// <summary>
/// Returns the first matching issue. Note: for this method to work correctly, all <paramref name="serverIssuesFromSameFile"/> need to be from the same server file
/// </summary>
/// <param name="issue">Local issue</param>
/// <param name="serverIssuesFromSameFile">List of server issues from the same file</param>
/// <returns>A matching server issue, if present in the <paramref name="serverIssuesFromSameFile"/> list, or null</returns>
public SonarQubeIssue GetFirstMatchFromSameFileOrNull(IFilterableIssue issue, IEnumerable<SonarQubeIssue> serverIssuesFromSameFile)
public SonarQubeIssue GetFirstLikelyMatchFromSameFileOrNull(IFilterableIssue issue, IEnumerable<SonarQubeIssue> serverIssuesFromSameFile)
{
return serverIssuesFromSameFile?.FirstOrDefault(serverIssue => IsMatch(issue, serverIssue, false));
}
Expand Down
2 changes: 1 addition & 1 deletion src/ConnectedMode/Suppressions/SuppressedIssueMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public bool SuppressionExists(IFilterableIssue issue)
var serverIssues = serverIssuesStore.Get();

// Try to find an issue with the same ID and either the same line number or same line hash
return serverIssues.Any(s => s.IsResolved && issueMatcher.IsGoodMatch(issue, s));
return serverIssues.Any(s => s.IsResolved && issueMatcher.IsLikelyMatch(issue, s));
}
}
}

0 comments on commit 313ebfb

Please sign in to comment.