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

sonarReport.ProjectName can be not unique #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -7,7 +7,9 @@
"filePath": "{sourceRoot}/Resources/ReSharperTest/Test1/line-ending.js",
"message": "Statement termination doesn't match code style",
"textRange": {
"endColumn": 36,
"endLine": 2,
"startColumn": 35,
"startLine": 2
}
},
Expand Down
Expand Up @@ -12,7 +12,9 @@
"resultFile": {
"uri": "{sourceRoot}/Resources/ReSharperTest/Test1/line-ending.js",
"region": {
"endColumn": 37,
"endLine": 2,
"startColumn": 36,
"startLine": 2
}
}
Expand Down
28 changes: 14 additions & 14 deletions src/dotnet-reqube.tests/SonarConverterTest.cs
Expand Up @@ -20,7 +20,7 @@ public class SonarConverterTest

[Fact]
public void SonarConverter_WithInvalidProjectPath_ShouldFail()
{
{
var options = new Options
{
Input = "Resources/ReSharperTestReport.xml",
Expand All @@ -36,7 +36,7 @@ public void SonarConverter_WithInvalidProjectPath_ShouldFail()

[Fact]
public void SonarConverter_WithValidProjectPathButNoIssues_ShouldBeSkipped()
{
{
var options = new Options
{
Input = "Resources/ReSharperTestReport.xml",
Expand All @@ -52,7 +52,7 @@ public void SonarConverter_WithValidProjectPathButNoIssues_ShouldBeSkipped()

mockLogger.Verify(
l => l.Information(
It.Is<string>(m => m == "Project Resources/ReSharperTest/Test1/Test1.csproj contains no issues.")),
It.Is<string>(m => m == "Project Resources/ReSharperTest/Test1/Test1.csproj contains no issues.")),
Times.Once());
}

Expand Down Expand Up @@ -274,7 +274,7 @@ public void ConvertToAbsolutePaths_ShouldConvertAllRelativePaths(string solution
Assert.Equal(Path.Combine(solutionDir, "path2"), report.Issues[0].Issue[1].File);
Assert.Equal(Path.Combine(solutionDir, "path3"), report.Issues[1].Issue[0].File);
}

[Fact]
public void RemoveExcludedRules_ShouldFilterRulesCorrectly()
{
Expand Down Expand Up @@ -306,7 +306,7 @@ public void RemoveExcludedRules_ShouldFilterRulesCorrectly()
AssertEqualIssueTypes(reSharperReport, new[] { "CheckNamespace" }, new[] { "UnusedType.Global" });

// test message filtering
reSharperReport = ParseReSharperReport(reportXml);
reSharperReport = ParseReSharperReport(reportXml);
SonarConverter.RemoveExcludedRules(
reSharperReport,
"RedundantUsingDirective|UnusedParameter.Global##Parameter 'geocodeResolution'.*|UnusedVariable");
Expand All @@ -325,7 +325,7 @@ public void RemoveExcludedRules_ShouldFilterRulesCorrectly()

AssertEqualIssueTypes(
reSharperReport,
new[] { "CheckNamespace" },
new[] { "CheckNamespace" },
new[] { "UnusedType.Global" });

// test message filtering with full match
Expand Down Expand Up @@ -362,8 +362,8 @@ public void SonarConverter_ForRoslynOutput_ShouldCallAddReSharperAnalysisPathsWi
};

var sonarConverter = GetSonarConverter(
new TestReportGenerator {
SonarReports = new List<ISonarReport> {
new TestReportGenerator {
SonarReports = new List<ISonarReport> {
new SonarRoslynReport
{
ProjectName = "Test1"
Expand All @@ -372,7 +372,7 @@ public void SonarConverter_ForRoslynOutput_ShouldCallAddReSharperAnalysisPathsWi
{
ProjectName = "Test2"
}
}
}
},
options);

Expand All @@ -388,14 +388,14 @@ public void SonarConverter_ForRoslynOutput_ShouldCallAddReSharperAnalysisPathsWi

metaDataWriterMock.Verify(
x => x.AddReSharperAnalysisPaths(
It.Is<IDictionary<string, string>>(
map =>
map["Test1"] == Path.GetFullPath("Resources/ReSharperTest/Test1/report.json") &&
map["Test2"] == Path.GetFullPath("Resources/ReSharperTest/Test2/src/report.json") &&
It.Is<List<KeyValuePair<string, string>>>(
map =>
map.Single(x=> x.Key == "Test1").Value == Path.GetFullPath("Resources/ReSharperTest/Test1/report.json") &&
map.Single(x=> x.Key == "Test2").Value == Path.GetFullPath("Resources/ReSharperTest/Test2/src/report.json") &&
map.Count == 2
)), Times.Once);
}

private void RunGenericSonarConverterTest(string[] reportFiles, Options options)
{
foreach (var reportFile in reportFiles)
Expand Down
4 changes: 2 additions & 2 deletions src/dotnet-reqube.tests/SonarMetaDataWriterTest.cs
Expand Up @@ -26,11 +26,11 @@ public void AddReSharperAnalysisPaths_ShouldModifyProjectInfoFilesCorrectly()
CreateSonarAndProjectStructure();

var sonarMetaDataWriter = new SonarRoslynMetaDataWriter(SonarDir);
var reportPathsByProject = new Dictionary<string, string>();
var reportPathsByProject = new List<KeyValuePair<string, string>>();

for (int i = 0; i < 5; i++)
{
reportPathsByProject.Add($"test{i}", Path.Combine(ProjectDir, $"test{i}.ReSharper.RoslynCA.json"));
reportPathsByProject.Add(new KeyValuePair<string, string>($"test{i}", Path.Combine(ProjectDir, $"test{i}.ReSharper.RoslynCA.json")));
}

sonarMetaDataWriter.AddReSharperAnalysisPaths(reportPathsByProject);
Expand Down
2 changes: 1 addition & 1 deletion src/dotnet-reqube/ISonarMetaDataWriter.cs
Expand Up @@ -4,6 +4,6 @@ namespace ReQube
{
public interface ISonarMetaDataWriter
{
void AddReSharperAnalysisPaths(IDictionary<string, string> reportPathsByProject);
void AddReSharperAnalysisPaths(List<KeyValuePair<string, string>> reportPathsByProject);
}
}
4 changes: 2 additions & 2 deletions src/dotnet-reqube/SonarConverter.cs
Expand Up @@ -24,7 +24,7 @@ public class SonarConverter : ISonarConverter
= new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };

private readonly Options _options;
private readonly IDictionary<string, string> _reportPathsByProject = new Dictionary<string, string>();
private readonly List<KeyValuePair<string, string>> _reportPathsByProject = new List<KeyValuePair<string, string>>();

public SonarReportGeneratorFactory SonarReportGeneratorFactory { get; set; }
= new SonarReportGeneratorFactory();
Expand Down Expand Up @@ -267,7 +267,7 @@ private void WriteReport(string filePath, ISonarReport sonarReport)

if (sonarReport.ProjectName != null)
{
_reportPathsByProject.Add(sonarReport.ProjectName, Path.GetFullPath(filePath));
_reportPathsByProject.Add(new KeyValuePair<string, string>(sonarReport.ProjectName, Path.GetFullPath(filePath)));
}
}

Expand Down
31 changes: 16 additions & 15 deletions src/dotnet-reqube/SonarRoslynMetaDataWriter.cs
Expand Up @@ -12,14 +12,14 @@ public class SonarRoslynMetaDataWriter : ISonarMetaDataWriter
{
private readonly DirectoryInfo _sonarDirInfo;

private ILogger Logger { get; } = LoggerFactory.GetLogger();
private ILogger Logger { get; } = LoggerFactory.GetLogger();

public SonarRoslynMetaDataWriter(string sonarDir)
{
_sonarDirInfo = new DirectoryInfo(sonarDir);
{
_sonarDirInfo = new DirectoryInfo(sonarDir);
}

public void AddReSharperAnalysisPaths(IDictionary<string, string> reportPathsByProject)
public void AddReSharperAnalysisPaths(List<KeyValuePair<string, string>> reportPathsByProject)
{
var projectInfoFiles = _sonarDirInfo.GetFiles(
"ProjectInfo.xml",
Expand All @@ -35,14 +35,15 @@ public void AddReSharperAnalysisPaths(IDictionary<string, string> reportPathsByP
}

private void AddReSharperAnalysisPaths(
FileInfo projectInfoFile, IDictionary<string, string> reportPathsByProject)
FileInfo projectInfoFile, List<KeyValuePair<string, string>> reportPathsByProject)
{
var projectInfo = XElement.Load(projectInfoFile.FullName);
var ns = projectInfo.GetDefaultNamespace();

var projectPath = projectInfo.RequiredElement(ns + "FullPath").Value;
reportPathsByProject.TryGetValue(
Path.GetFileNameWithoutExtension(projectPath), out var reSharperRoslynFile);
var reSharperRoslynFile = reportPathsByProject
.FirstOrDefault(x => x.Value == Path.GetFileNameWithoutExtension(projectPath))
Copy link
Owner

@olsh olsh Aug 11, 2022

Choose a reason for hiding this comment

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

This is still unsafe, since there may be null

.Key;

if (reSharperRoslynFile == null || !File.Exists(reSharperRoslynFile))
{
Expand All @@ -57,16 +58,16 @@ public void AddReSharperAnalysisPaths(IDictionary<string, string> reportPathsByP
var reportFilePathNameAttribute = $"sonar.{sonarLanguage}.roslyn.reportFilePath";
var projectOutPathNameAttribute = $"sonar.{sonarLanguage}.analyzer.projectOutPath";

var reportFilePathProperty =
var reportFilePathProperty =
analysisSettings
.Elements()
.FirstOrDefault(p => p.Attribute("Name")?.Value == reportFilePathNameAttribute);
.Elements()
.FirstOrDefault(p => p.Attribute("Name")?.Value == reportFilePathNameAttribute);

if (reportFilePathProperty != null)
{
var reportFilePaths =
var reportFilePaths =
reportFilePathProperty.Value
.Split("|").Select(x => x.Trim()).Where(x => !string.IsNullOrEmpty(x)).ToHashSet();
.Split("|").Select(x => x.Trim()).Where(x => !string.IsNullOrEmpty(x)).ToHashSet();
reportFilePaths.Add(reSharperRoslynFile);
reportFilePathProperty.Value = string.Join("|", reportFilePaths);
} else
Expand All @@ -78,8 +79,8 @@ public void AddReSharperAnalysisPaths(IDictionary<string, string> reportPathsByP

var projectOutPathProperty =
analysisSettings
.Elements()
.FirstOrDefault(p => p.Attribute("Name")?.Value == projectOutPathNameAttribute);
.Elements()
.FirstOrDefault(p => p.Attribute("Name")?.Value == projectOutPathNameAttribute);

if (projectOutPathProperty != null)
{
Expand All @@ -97,4 +98,4 @@ public void AddReSharperAnalysisPaths(IDictionary<string, string> reportPathsByP
$"{reSharperRoslynFile} is successfully added to {projectInfoFile.FullName}.");
}
}
}
}