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

UTs: Cleanup ResourceTest folder #6891

Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
618f9bb
SonarLint.xml file read base logic and tests
CristianAmbrosini Mar 1, 2023
84f9749
Removing relativeRootFromSonarLintXml and deprecated 'Files' element
CristianAmbrosini Mar 1, 2023
9ccaccf
Replaced Lazy initialization with null-coalescing assignment operator
CristianAmbrosini Mar 1, 2023
eb39dd8
Removed empty list initializers
CristianAmbrosini Mar 2, 2023
c2465fd
Addressed PR review comments
CristianAmbrosini Mar 2, 2023
7ff26e1
Add SonarLintXml unit tests
CristianAmbrosini Mar 3, 2023
69d65bc
Replaced tabs with spaces
CristianAmbrosini Mar 3, 2023
0b7c818
Add test cases
CristianAmbrosini Mar 3, 2023
07e7627
Bump temporarily UTs ToStringCallCount
CristianAmbrosini Mar 6, 2023
e2cb421
Small merge fix
CristianAmbrosini Mar 6, 2023
d2bb4ca
Fix rebase issues 2
CristianAmbrosini Mar 6, 2023
92cbf8a
Fix UTs
CristianAmbrosini Mar 6, 2023
1ab49e5
IgnoreAttribute for parametrized test.
CristianAmbrosini Mar 6, 2023
d13ad03
Addressed PR comments
CristianAmbrosini Mar 7, 2023
5ba46e6
Second round of comments
CristianAmbrosini Mar 8, 2023
9dde001
Migrated ShouldAnalyzeFile test case to ShouldAnalyzeTree
CristianAmbrosini Mar 8, 2023
dd2a57d
Fix rebase
CristianAmbrosini Mar 9, 2023
70b1ac7
Add test cases
CristianAmbrosini Mar 3, 2023
8659eec
Addressed PR comments
CristianAmbrosini Mar 7, 2023
9e01a32
Second round of comments
CristianAmbrosini Mar 8, 2023
042e33e
Migrated ShouldAnalyzeFile test case to ShouldAnalyzeTree
CristianAmbrosini Mar 8, 2023
f4b6239
Cleanup resource test and migrated references on UTs
CristianAmbrosini Mar 9, 2023
902989a
Reduced the size of the ResourceTest\SonarLint.xml files
CristianAmbrosini Mar 10, 2023
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 @@ -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")));
cristian-ambrosini-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
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