Skip to content

Commit

Permalink
cover case of duplicate keys in sonarlint xml
Browse files Browse the repository at this point in the history
  • Loading branch information
mary-georgiou-sonarsource committed Mar 21, 2023
1 parent 0424e30 commit 00c54dc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class SonarLintXmlReader
public SonarLintXmlReader(SourceText sonarLintXmlText)
{
var sonarLintXml = ParseContent(sonarLintXmlText);
var settings = sonarLintXml.Settings?.ToDictionary(x => x.Key, x => x.Value) ?? new Dictionary<string, string>();
var settings = sonarLintXml.Settings?.GroupBy(x => x.Key).ToDictionary(x => x.Key, x => x.First().Value) ?? new Dictionary<string, string>();
Exclusions = ReadArray("sonar.exclusions");
Inclusions = ReadArray("sonar.inclusions");
GlobalExclusions = ReadArray("sonar.global.exclusions");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ public void SonarLintXmlReader_PropertiesCSharpTrueVBNetFalse_ExpectedValues()
sut.AnalyzeGeneratedCode(LanguageNames.VisualBasic).Should().BeFalse();
}

[TestMethod]
public void SonarLintXmlReader_DuplicatedProperties_DoesNotFail()
{
try
{
_ = CreateSonarLintXmlReader("ResourceTests\\SonarLintXml\\Duplicated_Properties\\SonarLint.xml");
}
catch (ArgumentException ex)
{
if (ex.Message.Contains("An item with the same key has already been added."))
{
Assert.Fail("Expected no exception, but got: " + ex.Message);
}
}
}

[DataTestMethod]
[DataRow("")]
[DataRow("this is not an xml")]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<AnalysisInput>
<Settings>
<Setting>
<Key>sonar.cs.ignoreHeaderComments</Key>
<Value>true</Value>
</Setting>
<Setting>
<Key>sonar.cs.ignoreHeaderComments</Key>
<Value>true</Value>
</Setting>
<Setting>
<Key>sonar.cs.analyzeGeneratedCode</Key>
<Value>true</Value>
</Setting>
<Setting>
<Key>sonar.cs.analyzeGeneratedCode</Key>
<Value>false</Value>
</Setting>
<Setting>
<Key>sonar.exclusions</Key>
<Value>Fake/Exclusions/**/*,Fake/Exclusions/Second*/**/*</Value>
</Setting>
<Setting>
<Key>sonar.exclusions</Key>
<Value>Fake/Inclusions/**/*</Value>
</Setting>
</Settings>
</AnalysisInput>

0 comments on commit 00c54dc

Please sign in to comment.