Skip to content

Commit

Permalink
Cleanup resource test and migrated references on UTs
Browse files Browse the repository at this point in the history
  • Loading branch information
CristianAmbrosini committed Mar 9, 2023
1 parent af63f18 commit b8aabb3
Show file tree
Hide file tree
Showing 19 changed files with 49 additions and 179 deletions.
Expand Up @@ -37,7 +37,7 @@ public class ParameterLoaderTest
public void SetParameterValues_WhenNoSonarLintIsGiven_DoesNotPopulateParameters(string filePath)
{
// Arrange
var compilation = CreateCompilationWithOption(filePath, SourceText.From(File.ReadAllText("ResourceTests\\SonarLint.xml")));
var compilation = CreateCompilationWithOption(filePath, SourceText.From(File.ReadAllText("ResourceTests\\SonarLintXml\\All_properties_small_template\\SonarLint.xml")));
var analyzer = new ExpressionComplexity(); // Cannot use mock because we use reflection to find properties.

// Act
Expand All @@ -53,7 +53,7 @@ public void SetParameterValues_WhenNoSonarLintIsGiven_DoesNotPopulateParameters(
public void SetParameterValues_WhenGivenValidSonarLintFilePath_PopulatesProperties(string filePath)
{
// Arrange
var compilation = CreateCompilationWithOption(filePath, SourceText.From(File.ReadAllText("ResourceTests\\SonarLint.xml")));
var compilation = CreateCompilationWithOption(filePath, SourceText.From(File.ReadAllText("ResourceTests\\SonarLintXml\\All_properties_small_template\\SonarLint.xml")));
var analyzer = new ExpressionComplexity(); // Cannot use mock because we use reflection to find properties.

// Act
Expand All @@ -67,7 +67,7 @@ public void SetParameterValues_WhenGivenValidSonarLintFilePath_PopulatesProperti
public void SetParameterValues_WhenGivenSonarLintFileHasIntParameterType_PopulatesProperties()
{
// Arrange
var compilation = CreateCompilationWithOption("ResourceTests\\SonarLint.xml");
var compilation = CreateCompilationWithOption("ResourceTests\\SonarLintXml\\All_properties_small_template\\SonarLint.xml");
var analyzer = new ExpressionComplexity(); // Cannot use mock because we use reflection to find properties.

// Act
Expand All @@ -81,7 +81,7 @@ public void SetParameterValues_WhenGivenSonarLintFileHasIntParameterType_Populat
public void SetParameterValues_WhenGivenSonarLintFileHasStringParameterType_OnlyOneParameter_PopulatesProperty()
{
// Arrange
var compilation = CreateCompilationWithOption("ResourceTests\\RuleWithStringParameter\\SonarLint.xml");
var compilation = CreateCompilationWithOption("ResourceTests\\SonarLintXml\\RuleWithStringParameter\\SonarLint.xml");
var analyzer = new EnumNameShouldFollowRegex(); // Cannot use mock because we use reflection to find properties.

// Act
Expand All @@ -95,7 +95,7 @@ public void SetParameterValues_WhenGivenSonarLintFileHasStringParameterType_Only
public void SetParameterValues_WhenGivenSonarLintFileHasBooleanParameterType_OnlyOneParameter_PopulatesProperty()
{
// Arrange
var compilation = CreateCompilationWithOption("ResourceTests\\RuleWithBooleanParameter\\SonarLint.xml");
var compilation = CreateCompilationWithOption("ResourceTests\\SonarLintXml\\RuleWithBooleanParameter\\SonarLint.xml");
var analyzer = new CheckFileLicense(); // Cannot use mock because we use reflection to find properties.

// Act
Expand All @@ -109,7 +109,7 @@ public void SetParameterValues_WhenGivenSonarLintFileHasBooleanParameterType_Onl
public void SetParameterValues_WhenGivenValidSonarLintFileAndDoesNotContainAnalyzerParameters_DoesNotPopulateProperties()
{
// Arrange
var compilation = CreateCompilationWithOption("ResourceTests\\SonarLint.xml");
var compilation = CreateCompilationWithOption("ResourceTests\\SonarLintXml\\All_properties_small_template\\SonarLint.xml");
var analyzer = new LineLength(); // Cannot use mock because we use reflection to find properties.

// Act
Expand Down Expand Up @@ -180,7 +180,7 @@ public void SetParameterValues_WithMalformedXml_DoesNotPopulateProperties(string
public void SetParameterValues_WithWrongPropertyType_StringInsteadOfInt_DoesNotPopulateProperties()
{
// Arrange
var compilation = CreateCompilationWithOption("ResourceTests\\StringInsteadOfInt\\SonarLint.xml");
var compilation = CreateCompilationWithOption("ResourceTests\\SonarLintXml\\StringInsteadOfInt\\SonarLint.xml");
var analyzer = new ExpressionComplexity(); // Cannot use mock because we use reflection to find properties.

// Act
Expand All @@ -194,7 +194,7 @@ public void SetParameterValues_WithWrongPropertyType_StringInsteadOfInt_DoesNotP
public void SetParameterValues_WithWrongPropertyType_StringInsteadOfBoolean_DoesNotPopulateProperties()
{
// Arrange
var compilation = CreateCompilationWithOption("ResourceTests\\StringInsteadOfBoolean\\SonarLint.xml");
var compilation = CreateCompilationWithOption("ResourceTests\\SonarLintXml\\StringInsteadOfBoolean\\SonarLint.xml");
var analyzer = new CheckFileLicense(); // Cannot use mock because we use reflection to find properties.

// Act
Expand Down
22 changes: 11 additions & 11 deletions analyzers/tests/SonarAnalyzer.UnitTest/Helpers/SonarLintXmlTest.cs
Expand Up @@ -58,20 +58,20 @@ private static void AssertRules(List<SonarLintXmlRule> rules)
rules.Should().HaveCount(4);
rules.Where(x => x.Parameters.Any()).Should().HaveCount(2);

rules[0].Key.Should().BeEquivalentTo("S0001");
rules[0].Key.Should().BeEquivalentTo("S2225");
rules[0].Parameters.Should().BeEmpty();
rules[1].Key.Should().BeEquivalentTo("S0002");
rules[1].Parameters.Should().BeEmpty();

rules[2].Key.Should().BeEquivalentTo("S0003");
rules[2].Parameters.Should().HaveCount(2);
AssertKeyValuePair(rules[2].Parameters[0], "format", "^([A-Z]{1,3}[a-z0-9]+)*([A-Z]{2})?$");
AssertKeyValuePair(rules[2].Parameters[1], "flagsAttributeFormat", "^([A-Z]{1,3}[a-z0-9]+)*([A-Z]{2})?s$");
rules[1].Key.Should().BeEquivalentTo("S2342");
rules[1].Parameters.Should().HaveCount(2);
AssertKeyValuePair(rules[1].Parameters[0], "format", "^([A-Z]{1,3}[a-z0-9]+)*([A-Z]{2})?$");
AssertKeyValuePair(rules[1].Parameters[1], "flagsAttributeFormat", "^([A-Z]{1,3}[a-z0-9]+)*([A-Z]{2})?s$");

rules[3].Key.Should().BeEquivalentTo("S0004");
rules[3].Parameters.Should().HaveCount(2);
AssertKeyValuePair(rules[3].Parameters[0], "threshold", "15");
AssertKeyValuePair(rules[3].Parameters[1], "propertyThreshold", "3");
rules[2].Key.Should().BeEquivalentTo("S2346");
rules[2].Parameters.Should().BeEmpty();

rules[3].Key.Should().BeEquivalentTo("S1067");
rules[3].Parameters.Should().HaveCount(1);
AssertKeyValuePair(rules[3].Parameters[0], "max", "1");
}

private static void AssertKeyValuePair(SonarLintXmlKeyValuePair pair, string expectedKey, string expectedValue)
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

30 changes: 0 additions & 30 deletions analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLint.xml

This file was deleted.

Expand Up @@ -44,13 +44,10 @@
</Settings>
<Rules>
<Rule>
<Key>S0001</Key>
<Key>S2225</Key>
</Rule>
<Rule>
<Key>S0002</Key>
</Rule>
<Rule>
<Key>S0003</Key>
<Key>S2342</Key>
<Parameters>
<Parameter>
<Key>format</Key>
Expand All @@ -63,15 +60,14 @@
</Parameters>
</Rule>
<Rule>
<Key>S0004</Key>
<Key>S2346</Key>
</Rule>
<Rule>
<Key>S1067</Key>
<Parameters>
<Parameter>
<Key>threshold</Key>
<Value>15</Value>
</Parameter>
<Parameter>
<Key>propertyThreshold</Key>
<Value>3</Value>
<Key>max</Key>
<Value>1</Value>
</Parameter>
</Parameters>
</Rule>
Expand Down

This file was deleted.

Expand Up @@ -34,6 +34,7 @@ public class UtilityAnalyzerBaseTest
{
private const string DefaultSonarProjectConfig = @"ResourceTests\SonarProjectConfig\Path_Windows\SonarProjectConfig.xml";
private const string DefaultProjectOutFolderPath = @"ResourceTests\ProjectOutFolderPath.txt";
public TestContext TestContext { get; set; }

[DataTestMethod]
[DataRow(LanguageNames.CSharp, DefaultProjectOutFolderPath, @"path\output-cs")]
Expand All @@ -43,7 +44,7 @@ public class UtilityAnalyzerBaseTest
public void ReadConfig_OutPath(string language, string additionalPath, string expectedOutPath)
{
// We do not test what is read from the SonarLint file, but we need it
var utilityAnalyzer = new TestUtilityAnalyzer(language, @"ResourceTests\SonarLint.xml", additionalPath);
var utilityAnalyzer = new TestUtilityAnalyzer(language, @"ResourceTests\SonarLintXml\All_properties_cs\SonarLint.xml", additionalPath);

utilityAnalyzer.TestOutPath.Should().Be(expectedOutPath);
utilityAnalyzer.TestIsAnalyzerEnabled.Should().BeTrue();
Expand All @@ -55,35 +56,37 @@ public void ReadConfig_OutPath(string language, string additionalPath, string ex
public void ReadConfig_OutPath_FromSonarProjectConfig_HasPriority(string firstFile, string secondFile)
{
// We do not test what is read from the SonarLint file, but we need it
var utilityAnalyzer = new TestUtilityAnalyzer(LanguageNames.CSharp, @"ResourceTests\SonarLint.xml", firstFile, secondFile);
var utilityAnalyzer = new TestUtilityAnalyzer(LanguageNames.CSharp, @"ResourceTests\SonarLintXml\All_properties_cs\SonarLint.xml", firstFile, secondFile);

utilityAnalyzer.TestOutPath.Should().Be(@"C:\foo\bar\.sonarqube\out\0\output-cs");
utilityAnalyzer.TestIsAnalyzerEnabled.Should().BeTrue();
}

[DataTestMethod]
[DataRow(LanguageNames.CSharp, @"ResourceTests\AnalyzeGeneratedTrue\SonarLint.xml", true)]
[DataRow(LanguageNames.CSharp, @"ResourceTests\AnalyzeGeneratedFalse\SonarLint.xml", false)]
[DataRow(LanguageNames.VisualBasic, @"ResourceTests\AnalyzeGeneratedTrueVbnet\SonarLint.xml", true)]
[DataRow(LanguageNames.VisualBasic, @"ResourceTests\AnalyzeGeneratedFalseVbnet\SonarLint.xml", false)]
public void ReadsSettings_AnalyzeGenerated(string language, string sonarLintXmlPath, bool expectedAnalyzeGeneratedCodeValue)
[DataRow(LanguageNames.CSharp, true)]
[DataRow(LanguageNames.CSharp, false)]
[DataRow(LanguageNames.VisualBasic, true)]
[DataRow(LanguageNames.VisualBasic, false)]
public void ReadsSettings_AnalyzeGenerated(string language, bool analyzeGenerated)
{
var sonarLintXmlPath = AnalysisScaffolding.CreateSonarLintXml(TestContext, language: language, analyzeGeneratedCode: analyzeGenerated);
var utilityAnalyzer = new TestUtilityAnalyzer(language, sonarLintXmlPath, DefaultSonarProjectConfig);

utilityAnalyzer.TestAnalyzeGeneratedCode.Should().Be(expectedAnalyzeGeneratedCodeValue);
utilityAnalyzer.TestAnalyzeGeneratedCode.Should().Be(analyzeGenerated);
utilityAnalyzer.TestIsAnalyzerEnabled.Should().BeTrue();
}

[DataTestMethod]
[DataRow(LanguageNames.CSharp, @"ResourceTests\IgnoreHeaderCommentsTrueCSharp\SonarLint.xml", true)]
[DataRow(LanguageNames.CSharp, @"ResourceTests\IgnoreHeaderCommentsFalseCSharp\SonarLint.xml", false)]
[DataRow(LanguageNames.VisualBasic, @"ResourceTests\IgnoreHeaderCommentsTrueVbnet\SonarLint.xml", true)]
[DataRow(LanguageNames.VisualBasic, @"ResourceTests\IgnoreHeaderCommentsFalseVbnet\SonarLint.xml", false)]
public void ReadsSettings_IgnoreHeaderComments(string language, string sonarLintXmlPath, bool expectedIgnoreHeaderComments)
[DataRow(LanguageNames.CSharp, true)]
[DataRow(LanguageNames.CSharp, false)]
[DataRow(LanguageNames.VisualBasic, true)]
[DataRow(LanguageNames.VisualBasic, false)]
public void ReadsSettings_IgnoreHeaderComments(string language, bool ignoreHeaderComments)
{
var sonarLintXmlPath = AnalysisScaffolding.CreateSonarLintXml(TestContext, language: language, ignoreHeaderComments: ignoreHeaderComments);
var utilityAnalyzer = new TestUtilityAnalyzer(language, sonarLintXmlPath, DefaultSonarProjectConfig);

utilityAnalyzer.TestIgnoreHeaderComments.Should().Be(expectedIgnoreHeaderComments);
utilityAnalyzer.TestIgnoreHeaderComments.Should().Be(ignoreHeaderComments);
utilityAnalyzer.TestIsAnalyzerEnabled.Should().BeTrue();
}

Expand All @@ -96,7 +99,7 @@ public void NoSonarLintXml_AnalyzerNotEnabled()

[TestMethod]
public void NoOutputPath_AnalyzerNotEnabled() =>
new TestUtilityAnalyzer(LanguageNames.CSharp, @"ResourceTests\AnalyzeGeneratedTrue\SonarLint.xml").TestIsAnalyzerEnabled.Should().BeFalse();
new TestUtilityAnalyzer(LanguageNames.CSharp, AnalysisScaffolding.CreateSonarLintXml(TestContext, analyzeGeneratedCode: true)).TestIsAnalyzerEnabled.Should().BeFalse();

[TestMethod]
public void GetTextRange()
Expand Down
Expand Up @@ -86,18 +86,20 @@ public static string CreateSonarProjectConfigWithFilesToAnalyze(TestContext cont
TestContext context,
string language = LanguageNames.CSharp,
bool analyzeGeneratedCode = false,
bool ignoreHeaderComments = false,
string[] exclusions = null,
string[] inclusions = null,
string[] globalExclusions = null,
string[] testExclusions = null,
string[] testInclusions = null,
string[] globalTestExclusions = null,
List<SonarLintXmlRule> rulesParameters = null) =>
TestHelper.WriteFile(context, "SonarLint.xml", GenerateSonarLintXmlContent(language, analyzeGeneratedCode, exclusions, inclusions, globalExclusions, testExclusions, testInclusions, globalTestExclusions, rulesParameters));
TestHelper.WriteFile(context, "SonarLint.xml", GenerateSonarLintXmlContent(language, analyzeGeneratedCode, ignoreHeaderComments, exclusions, inclusions, globalExclusions, testExclusions, testInclusions, globalTestExclusions, rulesParameters));

public static string GenerateSonarLintXmlContent(
string language = LanguageNames.CSharp,
bool analyzeGeneratedCode = false,
bool ignoreHeaderComments = false,
string[] exclusions = null,
string[] inclusions = null,
string[] globalExclusions = null,
Expand All @@ -110,6 +112,7 @@ public static string CreateSonarProjectConfigWithFilesToAnalyze(TestContext cont
new XElement("AnalysisInput",
new XElement("Settings",
CreateSetting($"sonar.{(language == LanguageNames.CSharp ? "cs" : "vbnet")}.analyzeGeneratedCode", analyzeGeneratedCode.ToString()),
CreateSetting($"sonar.{(language == LanguageNames.CSharp ? "cs" : "vbnet")}.ignoreHeaderComments", ignoreHeaderComments.ToString()),
CreateSetting("sonar.exclusions", ConcatenateStringArray(exclusions)),
CreateSetting("sonar.inclusions", ConcatenateStringArray(inclusions)),
CreateSetting("sonar.global.exclusions", ConcatenateStringArray(globalExclusions)),
Expand Down

0 comments on commit b8aabb3

Please sign in to comment.