From 8d4b75606e66a5b5330cd3d0fb9af9ac2546ed56 Mon Sep 17 00:00:00 2001 From: Cristian Ambrosini Date: Wed, 1 Mar 2023 12:09:33 +0100 Subject: [PATCH 01/25] SonarLint.xml file read base logic and tests --- .../Helpers/SonarLintXmlReader.cs | 1 + .../All_properties_cs/SonarLint.xml | 44 +++++++++---------- .../All_properties_vbnet/SonarLint.xml | 44 +++++++++---------- 3 files changed, 45 insertions(+), 44 deletions(-) diff --git a/analyzers/src/SonarAnalyzer.Common/Helpers/SonarLintXmlReader.cs b/analyzers/src/SonarAnalyzer.Common/Helpers/SonarLintXmlReader.cs index 83fb3a13080..d2af3d106ab 100644 --- a/analyzers/src/SonarAnalyzer.Common/Helpers/SonarLintXmlReader.cs +++ b/analyzers/src/SonarAnalyzer.Common/Helpers/SonarLintXmlReader.cs @@ -82,6 +82,7 @@ private static SonarLintXml ParseContent(SourceText sonarLintXml) return SonarLintXml.Empty; } } +} private List ReadRuleParameters() => sonarLintXml is { Rules: { } rules } diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/All_properties_cs/SonarLint.xml b/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/All_properties_cs/SonarLint.xml index 54a1ed9e464..de395f6f809 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/All_properties_cs/SonarLint.xml +++ b/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/All_properties_cs/SonarLint.xml @@ -17,30 +17,30 @@ sonar.cs.roslyn.ignoreIssues false - - sonar.exclusions - Fake/Exclusions/**/*,Fake/Exclusions/Second*/**/* - - - sonar.inclusions - Fake/Inclusions/**/*,Fake/Inclusions/Second*/**/* - - - sonar.global.exclusions - Fake/GlobalExclusions/**/*,Fake/GlobalExclusions/Second*/**/* - - - sonar.test.exclusions - Fake/TestExclusions/**/*,Fake/TestExclusions/Second*/**/* - - - sonar.test.inclusions - Fake/TestInclusions/**/*,Fake/TestInclusions/Second*/**/* - - + + sonar.exclusions + Fake/Exclusions/**/*,Fake/Exclusions/Second*/**/* + + + sonar.inclusions + Fake/Inclusions/**/*,Fake/Inclusions/Second*/**/* + + + sonar.global.exclusions + Fake/GlobalExclusions/**/*,Fake/GlobalExclusions/Second*/**/* + + + sonar.test.exclusions + Fake/TestExclusions/**/*,Fake/TestExclusions/Second*/**/* + + + sonar.test.inclusions + Fake/TestInclusions/**/*,Fake/TestInclusions/Second*/**/* + + sonar.global.test.exclusions Fake/GlobalTestExclusions/**/*,Fake/GlobalTestExclusions/Second*/**/* - + diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/All_properties_vbnet/SonarLint.xml b/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/All_properties_vbnet/SonarLint.xml index 270c5c95dd8..9cc17640c7b 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/All_properties_vbnet/SonarLint.xml +++ b/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/All_properties_vbnet/SonarLint.xml @@ -17,30 +17,30 @@ sonar.vbnet.roslyn.ignoreIssues false - - sonar.exclusions - Fake/Exclusions/**/*,Fake/Exclusions/Second*/**/* - - - sonar.inclusions - Fake/Inclusions/**/*,Fake/Inclusions/Second*/**/* - - - sonar.global.exclusions - Fake/GlobalExclusions/**/*,Fake/GlobalExclusions/Second*/**/* - - - sonar.test.exclusions - Fake/TestExclusions/**/*,Fake/TestExclusions/Second*/**/* - - - sonar.test.inclusions - Fake/TestInclusions/**/*,Fake/TestInclusions/Second*/**/* - - + + sonar.exclusions + Fake/Exclusions/**/*,Fake/Exclusions/Second*/**/* + + + sonar.inclusions + Fake/Inclusions/**/*,Fake/Inclusions/Second*/**/* + + + sonar.global.exclusions + Fake/GlobalExclusions/**/*,Fake/GlobalExclusions/Second*/**/* + + + sonar.test.exclusions + Fake/TestExclusions/**/*,Fake/TestExclusions/Second*/**/* + + + sonar.test.inclusions + Fake/TestInclusions/**/*,Fake/TestInclusions/Second*/**/* + + sonar.global.test.exclusions Fake/GlobalTestExclusions/**/*,Fake/GlobalTestExclusions/Second*/**/* - + From 0a4d736a6ba5b53bfa6c4a992a451f6cc1296646 Mon Sep 17 00:00:00 2001 From: Cristian Ambrosini Date: Wed, 1 Mar 2023 12:10:06 +0100 Subject: [PATCH 02/25] Removing relativeRootFromSonarLintXml and deprecated 'Files' element --- analyzers/src/SonarAnalyzer.Common/Helpers/SonarLintXml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/analyzers/src/SonarAnalyzer.Common/Helpers/SonarLintXml.cs b/analyzers/src/SonarAnalyzer.Common/Helpers/SonarLintXml.cs index d699d960638..53f5f090144 100644 --- a/analyzers/src/SonarAnalyzer.Common/Helpers/SonarLintXml.cs +++ b/analyzers/src/SonarAnalyzer.Common/Helpers/SonarLintXml.cs @@ -49,7 +49,7 @@ public class SonarLintXmlRule [XmlArray("Parameters")] [XmlArrayItem("Parameter")] - public List Parameters { get; set; } + public List Parameters { get; set; } = new List(); } public class SonarLintXmlKeyValuePair From 191024e1c19baef0fd62e0c4802ae2f0e05131a8 Mon Sep 17 00:00:00 2001 From: Cristian Ambrosini Date: Wed, 1 Mar 2023 12:10:26 +0100 Subject: [PATCH 03/25] Replaced Lazy initialization with null-coalescing assignment operator --- .../src/SonarAnalyzer.Common/Helpers/SonarLintXmlReader.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/analyzers/src/SonarAnalyzer.Common/Helpers/SonarLintXmlReader.cs b/analyzers/src/SonarAnalyzer.Common/Helpers/SonarLintXmlReader.cs index d2af3d106ab..dc2daf8dfc1 100644 --- a/analyzers/src/SonarAnalyzer.Common/Helpers/SonarLintXmlReader.cs +++ b/analyzers/src/SonarAnalyzer.Common/Helpers/SonarLintXmlReader.cs @@ -77,7 +77,7 @@ private static SonarLintXml ParseContent(SourceText sonarLintXml) using var reader = XmlReader.Create(sr); return (SonarLintXml)serializer.Deserialize(reader); } - catch + catch (Exception) { return SonarLintXml.Empty; } From 9f75a7fad44e32d5ef914d334d28b71730def83e Mon Sep 17 00:00:00 2001 From: Cristian Ambrosini Date: Thu, 2 Mar 2023 09:30:12 +0100 Subject: [PATCH 04/25] Removed empty list initializers --- analyzers/src/SonarAnalyzer.Common/Helpers/SonarLintXml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/analyzers/src/SonarAnalyzer.Common/Helpers/SonarLintXml.cs b/analyzers/src/SonarAnalyzer.Common/Helpers/SonarLintXml.cs index 53f5f090144..d699d960638 100644 --- a/analyzers/src/SonarAnalyzer.Common/Helpers/SonarLintXml.cs +++ b/analyzers/src/SonarAnalyzer.Common/Helpers/SonarLintXml.cs @@ -49,7 +49,7 @@ public class SonarLintXmlRule [XmlArray("Parameters")] [XmlArrayItem("Parameter")] - public List Parameters { get; set; } = new List(); + public List Parameters { get; set; } } public class SonarLintXmlKeyValuePair From 072d3a14e2087bd51e3b2a798ac17a1fc1b5b38c Mon Sep 17 00:00:00 2001 From: Cristian Ambrosini Date: Thu, 2 Mar 2023 15:43:32 +0100 Subject: [PATCH 05/25] Addressed PR review comments --- .../Helpers/SonarLintXmlReaderTest.cs | 7 +++++++ .../SonarLintXml/Invalid_Xml/SonarLint.xml | 2 +- .../SonarLint.xml | 20 +++++++++---------- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/Helpers/SonarLintXmlReaderTest.cs b/analyzers/tests/SonarAnalyzer.UnitTest/Helpers/SonarLintXmlReaderTest.cs index 009508344c2..9cd1c12ad2e 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/Helpers/SonarLintXmlReaderTest.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/Helpers/SonarLintXmlReaderTest.cs @@ -110,6 +110,13 @@ private static void AssertArrayContent(string[] array, string folder) array[1].Should().BeEquivalentTo($"Fake/{folder}/Second*/**/*"); } + private static void AssertArrayContent(string[] array, string folder) + { + array.Should().HaveCount(2); + array[0].Should().BeEquivalentTo($"Fake/{folder}/**/*"); + array[1].Should().BeEquivalentTo($"Fake/{folder}/Second*/**/*"); + } + private static SonarLintXmlReader CreateSonarLintXmlReader(string relativePath, string language = LanguageNames.CSharp) => new(SourceText.From(File.ReadAllText(relativePath)), language); } diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/Invalid_Xml/SonarLint.xml b/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/Invalid_Xml/SonarLint.xml index 9a259cbeb32..318e9dd187d 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/Invalid_Xml/SonarLint.xml +++ b/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/Invalid_Xml/SonarLint.xml @@ -1,7 +1,7 @@ - Setting> + Setting> sonar.nothing nothing diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/Partially_missing_properties/SonarLint.xml b/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/Partially_missing_properties/SonarLint.xml index ed671af08b6..e74b7f74d4f 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/Partially_missing_properties/SonarLint.xml +++ b/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/Partially_missing_properties/SonarLint.xml @@ -1,22 +1,22 @@ - + sonar.nothing nothing - + sonar.cs.analyzeGeneratedCode true - - sonar.exclusions - Fake/Exclusions/**/*,Fake/Exclusions/Second*/**/* - - - sonar.inclusions - Fake/Inclusions/**/*,Fake/Inclusions/Second*/**/* - + + sonar.exclusions + Fake/Exclusions/**/*,Fake/Exclusions/Second*/**/* + + + sonar.inclusions + Fake/Inclusions/**/*,Fake/Inclusions/Second*/**/* + From a0f2261f6170d6e0336cb210d9f6a30cb97e4bcc Mon Sep 17 00:00:00 2001 From: Cristian Ambrosini Date: Fri, 3 Mar 2023 09:43:59 +0100 Subject: [PATCH 06/25] Replaced tabs with spaces --- .../SonarLintXml/Invalid_Xml/SonarLint.xml | 2 +- .../SonarLint.xml | 20 +++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/Invalid_Xml/SonarLint.xml b/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/Invalid_Xml/SonarLint.xml index 318e9dd187d..9a259cbeb32 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/Invalid_Xml/SonarLint.xml +++ b/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/Invalid_Xml/SonarLint.xml @@ -1,7 +1,7 @@ - Setting> + Setting> sonar.nothing nothing diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/Partially_missing_properties/SonarLint.xml b/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/Partially_missing_properties/SonarLint.xml index e74b7f74d4f..ed671af08b6 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/Partially_missing_properties/SonarLint.xml +++ b/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/Partially_missing_properties/SonarLint.xml @@ -1,22 +1,22 @@ - + sonar.nothing nothing - + sonar.cs.analyzeGeneratedCode true - - sonar.exclusions - Fake/Exclusions/**/*,Fake/Exclusions/Second*/**/* - - - sonar.inclusions - Fake/Inclusions/**/*,Fake/Inclusions/Second*/**/* - + + sonar.exclusions + Fake/Exclusions/**/*,Fake/Exclusions/Second*/**/* + + + sonar.inclusions + Fake/Inclusions/**/*,Fake/Inclusions/Second*/**/* + From 70e2ed6a92e127b3b2d08dc291f9721d73fc5885 Mon Sep 17 00:00:00 2001 From: Cristian Ambrosini Date: Fri, 3 Mar 2023 17:30:33 +0100 Subject: [PATCH 07/25] Add test cases --- ...alysisContextBaseTest.ShouldAnalyzeFile.cs | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs b/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs new file mode 100644 index 00000000000..ba868d01163 --- /dev/null +++ b/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs @@ -0,0 +1,99 @@ +/* + * SonarAnalyzer for .NET + * Copyright (C) 2015-2023 SonarSource SA + * mailto: contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +namespace SonarAnalyzer.UnitTest.AnalysisContext; + +public partial class SonarAnalysisContextBaseTest +{ + [DataTestMethod] + [DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, false)] + [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, true)] + [DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, true)] + [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, true)] + public void ShouldAnalyzeFile_Exclusions_ReturnExpected(string filePath, string[] exclusions, ProjectType projectType, bool shouldAnalyze) => + AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, exclusions: exclusions); + + [DataTestMethod] + [DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, false)] + [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, true)] + [DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, true)] + [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, true)] + public void ShouldAnalyzeFile_GlobalExclusions_ReturnExpected(string filePath, string[] globalExclusions, ProjectType projectType, bool shouldAnalyze) => + AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, globalExclusions: globalExclusions); + + [DataTestMethod] + [DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, true)] + [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, true)] + [DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, false)] + [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, true)] + public void ShouldAnalyzeFile_TestExclusions_ReturnExpected(string filePath, string[] testExclusions, ProjectType projectType, bool shouldAnalyze) => + AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, testExclusions: testExclusions); + + [DataTestMethod] + [DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, true)] + [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, true)] + [DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, false)] + [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, true)] + public void ShouldAnalyzeFile_GlobalTestExclusions_ReturnExpected(string filePath, string[] globalTestExclusions, ProjectType projectType, bool shouldAnalyze) => + AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, globalTestExclusions: globalTestExclusions); + + [DataTestMethod] + [DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, true)] + [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, false)] + [DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, true)] + [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, true)] + public void ShouldAnalyzeFile_Inclusions_ReturnExpected(string filePath, string[] inclusions, ProjectType projectType, bool shouldAnalyze) => + AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, inclusions: inclusions); + + [DataTestMethod] + [DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, true)] + [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, true)] + [DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, true)] + [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, false)] + public void ShouldAnalyzeFile_TestInclusions_ReturnExpected(string filePath, string[] testInclusions, ProjectType projectType, bool shouldAnalyze) => + AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, testInclusions: testInclusions); + + private void AssertShouldAnalyzeFile( + string filePath, + ProjectType projectType, + bool shouldAnalyze, + string[] exclusions = null, + string[] inclusions = null, + string[] globalExclusions = null, + string[] testExclusions = null, + string[] testInclusions = null, + string[] globalTestExclusions = null) + { + var compilation = new SnippetCompiler("// Nothing to see here", TestHelper.ProjectTypeReference(projectType)).SemanticModel.Compilation; + var sonarLintXml = AnalysisScaffolding.CreateSonarLintXml( + TestContext, + exclusions: exclusions, + inclusions: inclusions, + globalExclusions: globalExclusions, + testExclusions: testExclusions, + testInclusions: testInclusions, + globalTestExclusions: + globalTestExclusions); + var options = AnalysisScaffolding.CreateOptions(sonarLintXml); + var sut = CreateSut(compilation, options); + + sut.ShouldAnalyzeFile(filePath).Should().Be(shouldAnalyze); + } +} From 121fe615674e41ba5b7c7d445bfe362836957155 Mon Sep 17 00:00:00 2001 From: Cristian Ambrosini Date: Mon, 6 Mar 2023 09:31:11 +0100 Subject: [PATCH 08/25] Bump temporarily UTs ToStringCallCount --- .../SonarAnalysisContextBaseTest.ShouldAnalyzeTree.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeTree.cs b/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeTree.cs index c667b8898e2..42d2f017b9f 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeTree.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeTree.cs @@ -101,8 +101,8 @@ public void ShouldAnalyzeTree_GeneratedFile_ShouldAnalyzeGeneratedProvider_IsCac sut.ShouldAnalyzeTree(tree, CSharpGeneratedCodeRecognizer.Instance).Should().BeTrue(); // GetText should be called every time ShouldAnalyzeGenerated is called... - additionalText.Verify(x => x.GetText(It.IsAny()), Times.Exactly(3)); - sonarLintXml.ToStringCallCount.Should().Be(1); // ... but we should only try to read the file once + additionalText.Verify(x => x.GetText(It.IsAny()), Times.Exactly(6)); // TEMPORARILY BUMPED TO 6 + sonarLintXml.ToStringCallCount.Should().Be(2); // ... but we should only try to read the file once // TEMPORARILY BUMPED TO 2 } [DataTestMethod] @@ -115,7 +115,7 @@ public void ShouldAnalyzeTree_GeneratedFile_InvalidSonarLintXml(string fileName, var sut = CreateSut(compilation, CreateOptions(sonarLintXml)); // 1. Read -> no error - sut.ShouldAnalyzeTree(tree, CSharpGeneratedCodeRecognizer.Instance).Should().Be(expected); + sut.ShouldAnalyzeTree(tree, CSharpGeneratedCodeRecognizer.Instance).Should().Be(expected); sonarLintXml.ToStringCallCount.Should().Be(1); // should have attempted to read the file // 2. Read again to check that the load error doesn't prevent caching from working From 34f5e2affed0f89d09eeb4224f762f3aa16d8736 Mon Sep 17 00:00:00 2001 From: Cristian Ambrosini Date: Mon, 6 Mar 2023 10:28:50 +0100 Subject: [PATCH 09/25] Small merge fix --- .../src/SonarAnalyzer.Common/Helpers/SonarLintXmlReader.cs | 3 +-- .../Helpers/SonarLintXmlReaderTest.cs | 7 ------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/analyzers/src/SonarAnalyzer.Common/Helpers/SonarLintXmlReader.cs b/analyzers/src/SonarAnalyzer.Common/Helpers/SonarLintXmlReader.cs index dc2daf8dfc1..83fb3a13080 100644 --- a/analyzers/src/SonarAnalyzer.Common/Helpers/SonarLintXmlReader.cs +++ b/analyzers/src/SonarAnalyzer.Common/Helpers/SonarLintXmlReader.cs @@ -77,12 +77,11 @@ private static SonarLintXml ParseContent(SourceText sonarLintXml) using var reader = XmlReader.Create(sr); return (SonarLintXml)serializer.Deserialize(reader); } - catch (Exception) + catch { return SonarLintXml.Empty; } } -} private List ReadRuleParameters() => sonarLintXml is { Rules: { } rules } diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/Helpers/SonarLintXmlReaderTest.cs b/analyzers/tests/SonarAnalyzer.UnitTest/Helpers/SonarLintXmlReaderTest.cs index 9cd1c12ad2e..009508344c2 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/Helpers/SonarLintXmlReaderTest.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/Helpers/SonarLintXmlReaderTest.cs @@ -110,13 +110,6 @@ private static void AssertArrayContent(string[] array, string folder) array[1].Should().BeEquivalentTo($"Fake/{folder}/Second*/**/*"); } - private static void AssertArrayContent(string[] array, string folder) - { - array.Should().HaveCount(2); - array[0].Should().BeEquivalentTo($"Fake/{folder}/**/*"); - array[1].Should().BeEquivalentTo($"Fake/{folder}/Second*/**/*"); - } - private static SonarLintXmlReader CreateSonarLintXmlReader(string relativePath, string language = LanguageNames.CSharp) => new(SourceText.From(File.ReadAllText(relativePath)), language); } From b9fc324df8375a3d48a2e8202ef964723fff35f9 Mon Sep 17 00:00:00 2001 From: Cristian Ambrosini Date: Mon, 6 Mar 2023 10:34:47 +0100 Subject: [PATCH 10/25] Fix rebase issues 2 --- ...alysisContextBaseTest.ShouldAnalyzeTree.cs | 2 +- .../All_properties_cs/SonarLint.xml | 44 +++++++++---------- .../All_properties_vbnet/SonarLint.xml | 44 +++++++++---------- 3 files changed, 45 insertions(+), 45 deletions(-) diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeTree.cs b/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeTree.cs index 42d2f017b9f..9052cddfe3f 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeTree.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeTree.cs @@ -115,7 +115,7 @@ public void ShouldAnalyzeTree_GeneratedFile_InvalidSonarLintXml(string fileName, var sut = CreateSut(compilation, CreateOptions(sonarLintXml)); // 1. Read -> no error - sut.ShouldAnalyzeTree(tree, CSharpGeneratedCodeRecognizer.Instance).Should().Be(expected); + sut.ShouldAnalyzeTree(tree, CSharpGeneratedCodeRecognizer.Instance).Should().Be(expected); sonarLintXml.ToStringCallCount.Should().Be(1); // should have attempted to read the file // 2. Read again to check that the load error doesn't prevent caching from working diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/All_properties_cs/SonarLint.xml b/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/All_properties_cs/SonarLint.xml index de395f6f809..54a1ed9e464 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/All_properties_cs/SonarLint.xml +++ b/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/All_properties_cs/SonarLint.xml @@ -17,30 +17,30 @@ sonar.cs.roslyn.ignoreIssues false - - sonar.exclusions - Fake/Exclusions/**/*,Fake/Exclusions/Second*/**/* - - - sonar.inclusions - Fake/Inclusions/**/*,Fake/Inclusions/Second*/**/* - - - sonar.global.exclusions - Fake/GlobalExclusions/**/*,Fake/GlobalExclusions/Second*/**/* - - - sonar.test.exclusions - Fake/TestExclusions/**/*,Fake/TestExclusions/Second*/**/* - - - sonar.test.inclusions - Fake/TestInclusions/**/*,Fake/TestInclusions/Second*/**/* - - + + sonar.exclusions + Fake/Exclusions/**/*,Fake/Exclusions/Second*/**/* + + + sonar.inclusions + Fake/Inclusions/**/*,Fake/Inclusions/Second*/**/* + + + sonar.global.exclusions + Fake/GlobalExclusions/**/*,Fake/GlobalExclusions/Second*/**/* + + + sonar.test.exclusions + Fake/TestExclusions/**/*,Fake/TestExclusions/Second*/**/* + + + sonar.test.inclusions + Fake/TestInclusions/**/*,Fake/TestInclusions/Second*/**/* + + sonar.global.test.exclusions Fake/GlobalTestExclusions/**/*,Fake/GlobalTestExclusions/Second*/**/* - + diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/All_properties_vbnet/SonarLint.xml b/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/All_properties_vbnet/SonarLint.xml index 9cc17640c7b..270c5c95dd8 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/All_properties_vbnet/SonarLint.xml +++ b/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/All_properties_vbnet/SonarLint.xml @@ -17,30 +17,30 @@ sonar.vbnet.roslyn.ignoreIssues false - - sonar.exclusions - Fake/Exclusions/**/*,Fake/Exclusions/Second*/**/* - - - sonar.inclusions - Fake/Inclusions/**/*,Fake/Inclusions/Second*/**/* - - - sonar.global.exclusions - Fake/GlobalExclusions/**/*,Fake/GlobalExclusions/Second*/**/* - - - sonar.test.exclusions - Fake/TestExclusions/**/*,Fake/TestExclusions/Second*/**/* - - - sonar.test.inclusions - Fake/TestInclusions/**/*,Fake/TestInclusions/Second*/**/* - - + + sonar.exclusions + Fake/Exclusions/**/*,Fake/Exclusions/Second*/**/* + + + sonar.inclusions + Fake/Inclusions/**/*,Fake/Inclusions/Second*/**/* + + + sonar.global.exclusions + Fake/GlobalExclusions/**/*,Fake/GlobalExclusions/Second*/**/* + + + sonar.test.exclusions + Fake/TestExclusions/**/*,Fake/TestExclusions/Second*/**/* + + + sonar.test.inclusions + Fake/TestInclusions/**/*,Fake/TestInclusions/Second*/**/* + + sonar.global.test.exclusions Fake/GlobalTestExclusions/**/*,Fake/GlobalTestExclusions/Second*/**/* - + From 8967f014eb934db59a5d3ffdbf0692b1e3d7ee79 Mon Sep 17 00:00:00 2001 From: Cristian Ambrosini Date: Mon, 6 Mar 2023 11:18:32 +0100 Subject: [PATCH 11/25] Fix UTs --- .../SonarAnalysisContextBaseTest.ShouldAnalyzeTree.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeTree.cs b/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeTree.cs index 9052cddfe3f..81d34e5a24e 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeTree.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeTree.cs @@ -116,11 +116,11 @@ public void ShouldAnalyzeTree_GeneratedFile_InvalidSonarLintXml(string fileName, // 1. Read -> no error sut.ShouldAnalyzeTree(tree, CSharpGeneratedCodeRecognizer.Instance).Should().Be(expected); - sonarLintXml.ToStringCallCount.Should().Be(1); // should have attempted to read the file + sonarLintXml.ToStringCallCount.Should().Be(2); // should have attempted to read the file // TEMPORARILY BUMPED TO 2 // 2. Read again to check that the load error doesn't prevent caching from working sut.ShouldAnalyzeTree(tree, CSharpGeneratedCodeRecognizer.Instance).Should().Be(expected); - sonarLintXml.ToStringCallCount.Should().Be(1); // should not have attempted to read the file again + sonarLintXml.ToStringCallCount.Should().Be(2); // should not have attempted to read the file again // TEMPORARILY BUMPED TO 2 } [DataTestMethod] From 1b3090a15a0af8d159a92ec764990a321eef2d4b Mon Sep 17 00:00:00 2001 From: Cristian Ambrosini Date: Mon, 6 Mar 2023 12:10:58 +0100 Subject: [PATCH 12/25] IgnoreAttribute for parametrized test. --- .../SonarAnalysisContextBaseTest.ShouldAnalyzeTree.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeTree.cs b/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeTree.cs index 81d34e5a24e..0358e3a2d39 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeTree.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeTree.cs @@ -105,6 +105,7 @@ public void ShouldAnalyzeTree_GeneratedFile_ShouldAnalyzeGeneratedProvider_IsCac sonarLintXml.ToStringCallCount.Should().Be(2); // ... but we should only try to read the file once // TEMPORARILY BUMPED TO 2 } + [Ignore("Temporarely ignored until the new SonarLintXmlReader will be used to access all properties.")] [DataTestMethod] [DataRow(GeneratedFileName, false)] [DataRow(OtherFileName, true)] @@ -116,11 +117,11 @@ public void ShouldAnalyzeTree_GeneratedFile_InvalidSonarLintXml(string fileName, // 1. Read -> no error sut.ShouldAnalyzeTree(tree, CSharpGeneratedCodeRecognizer.Instance).Should().Be(expected); - sonarLintXml.ToStringCallCount.Should().Be(2); // should have attempted to read the file // TEMPORARILY BUMPED TO 2 + sonarLintXml.ToStringCallCount.Should().Be(1); // should have attempted to read the file // 2. Read again to check that the load error doesn't prevent caching from working sut.ShouldAnalyzeTree(tree, CSharpGeneratedCodeRecognizer.Instance).Should().Be(expected); - sonarLintXml.ToStringCallCount.Should().Be(2); // should not have attempted to read the file again // TEMPORARILY BUMPED TO 2 + sonarLintXml.ToStringCallCount.Should().Be(1); // should not have attempted to read the file again } [DataTestMethod] @@ -135,6 +136,7 @@ public void ShouldAnalyzeTree_GeneratedFile_AnalyzeGenerated_AnalyzeAllFiles(str sut.ShouldAnalyzeTree(tree, CSharpGeneratedCodeRecognizer.Instance).Should().BeTrue(); } + [Ignore("Temporarely ignored until the new SonarLintXmlReader will be used to access all properties.")] [DataTestMethod] [DataRow(GeneratedFileName, LanguageNames.CSharp, false)] [DataRow(OtherFileName, LanguageNames.CSharp, true)] From dad394fd15fb9d493eed4a7b6d6503f0bcd3f151 Mon Sep 17 00:00:00 2001 From: Cristian Ambrosini Date: Tue, 7 Mar 2023 12:32:00 +0100 Subject: [PATCH 13/25] Addressed PR comments --- .../SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs b/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs index ba868d01163..90c39be474e 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs @@ -70,6 +70,14 @@ public partial class SonarAnalysisContextBaseTest public void ShouldAnalyzeFile_TestInclusions_ReturnExpected(string filePath, string[] testInclusions, ProjectType projectType, bool shouldAnalyze) => AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, testInclusions: testInclusions); + [DataTestMethod] + [DataRow("Foo", new string[] { "Foo" }, new string[] { "Foo" }, ProjectType.Product, false)] + [DataRow("Foo", new string[] { "NotFoo" }, new string[] { "Foo" }, ProjectType.Product, false)] + [DataRow("Foo", new string[] { "Foo" }, new string[] { "NotFoo" }, ProjectType.Product, true)] + [DataRow("Foo", new string[] { "NotFoo" }, new string[] { "NotFoo" }, ProjectType.Product, false)] + public void ShouldAnalyzeFile_MixedInput_ReturnExpected(string filePath, string[] inclusions, string[] exclusions, ProjectType projectType, bool shouldAnalyze) => + AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, inclusions: inclusions, exclusions: exclusions); + private void AssertShouldAnalyzeFile( string filePath, ProjectType projectType, From f3048a79fbbd1a9f21078378dc3dab5ee210ec8e Mon Sep 17 00:00:00 2001 From: Cristian Ambrosini Date: Wed, 8 Mar 2023 09:33:30 +0100 Subject: [PATCH 14/25] Second round of comments --- .../SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs b/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs index 90c39be474e..7aad69f4d09 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs @@ -82,6 +82,7 @@ public partial class SonarAnalysisContextBaseTest string filePath, ProjectType projectType, bool shouldAnalyze, + string language = LanguageNames.CSharp, string[] exclusions = null, string[] inclusions = null, string[] globalExclusions = null, @@ -92,6 +93,7 @@ public partial class SonarAnalysisContextBaseTest var compilation = new SnippetCompiler("// Nothing to see here", TestHelper.ProjectTypeReference(projectType)).SemanticModel.Compilation; var sonarLintXml = AnalysisScaffolding.CreateSonarLintXml( TestContext, + language: language, exclusions: exclusions, inclusions: inclusions, globalExclusions: globalExclusions, @@ -102,6 +104,6 @@ public partial class SonarAnalysisContextBaseTest var options = AnalysisScaffolding.CreateOptions(sonarLintXml); var sut = CreateSut(compilation, options); - sut.ShouldAnalyzeFile(filePath).Should().Be(shouldAnalyze); + sut.ShouldAnalyzeFile(sut.SonarLintFile(), filePath).Should().Be(shouldAnalyze); } } From 6b6810957b5e22d2bdb2deb2d283fbb08d0725c7 Mon Sep 17 00:00:00 2001 From: Cristian Ambrosini Date: Wed, 8 Mar 2023 16:29:34 +0100 Subject: [PATCH 15/25] Migrated ShouldAnalyzeFile test case to ShouldAnalyzeTree --- ...alysisContextBaseTest.ShouldAnalyzeFile.cs | 109 ------------------ 1 file changed, 109 deletions(-) delete mode 100644 analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs b/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs deleted file mode 100644 index 7aad69f4d09..00000000000 --- a/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs +++ /dev/null @@ -1,109 +0,0 @@ -/* - * SonarAnalyzer for .NET - * Copyright (C) 2015-2023 SonarSource SA - * mailto: contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -namespace SonarAnalyzer.UnitTest.AnalysisContext; - -public partial class SonarAnalysisContextBaseTest -{ - [DataTestMethod] - [DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, false)] - [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, true)] - [DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, true)] - [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, true)] - public void ShouldAnalyzeFile_Exclusions_ReturnExpected(string filePath, string[] exclusions, ProjectType projectType, bool shouldAnalyze) => - AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, exclusions: exclusions); - - [DataTestMethod] - [DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, false)] - [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, true)] - [DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, true)] - [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, true)] - public void ShouldAnalyzeFile_GlobalExclusions_ReturnExpected(string filePath, string[] globalExclusions, ProjectType projectType, bool shouldAnalyze) => - AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, globalExclusions: globalExclusions); - - [DataTestMethod] - [DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, true)] - [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, true)] - [DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, false)] - [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, true)] - public void ShouldAnalyzeFile_TestExclusions_ReturnExpected(string filePath, string[] testExclusions, ProjectType projectType, bool shouldAnalyze) => - AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, testExclusions: testExclusions); - - [DataTestMethod] - [DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, true)] - [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, true)] - [DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, false)] - [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, true)] - public void ShouldAnalyzeFile_GlobalTestExclusions_ReturnExpected(string filePath, string[] globalTestExclusions, ProjectType projectType, bool shouldAnalyze) => - AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, globalTestExclusions: globalTestExclusions); - - [DataTestMethod] - [DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, true)] - [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, false)] - [DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, true)] - [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, true)] - public void ShouldAnalyzeFile_Inclusions_ReturnExpected(string filePath, string[] inclusions, ProjectType projectType, bool shouldAnalyze) => - AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, inclusions: inclusions); - - [DataTestMethod] - [DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, true)] - [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, true)] - [DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, true)] - [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, false)] - public void ShouldAnalyzeFile_TestInclusions_ReturnExpected(string filePath, string[] testInclusions, ProjectType projectType, bool shouldAnalyze) => - AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, testInclusions: testInclusions); - - [DataTestMethod] - [DataRow("Foo", new string[] { "Foo" }, new string[] { "Foo" }, ProjectType.Product, false)] - [DataRow("Foo", new string[] { "NotFoo" }, new string[] { "Foo" }, ProjectType.Product, false)] - [DataRow("Foo", new string[] { "Foo" }, new string[] { "NotFoo" }, ProjectType.Product, true)] - [DataRow("Foo", new string[] { "NotFoo" }, new string[] { "NotFoo" }, ProjectType.Product, false)] - public void ShouldAnalyzeFile_MixedInput_ReturnExpected(string filePath, string[] inclusions, string[] exclusions, ProjectType projectType, bool shouldAnalyze) => - AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, inclusions: inclusions, exclusions: exclusions); - - private void AssertShouldAnalyzeFile( - string filePath, - ProjectType projectType, - bool shouldAnalyze, - string language = LanguageNames.CSharp, - string[] exclusions = null, - string[] inclusions = null, - string[] globalExclusions = null, - string[] testExclusions = null, - string[] testInclusions = null, - string[] globalTestExclusions = null) - { - var compilation = new SnippetCompiler("// Nothing to see here", TestHelper.ProjectTypeReference(projectType)).SemanticModel.Compilation; - var sonarLintXml = AnalysisScaffolding.CreateSonarLintXml( - TestContext, - language: language, - exclusions: exclusions, - inclusions: inclusions, - globalExclusions: globalExclusions, - testExclusions: testExclusions, - testInclusions: testInclusions, - globalTestExclusions: - globalTestExclusions); - var options = AnalysisScaffolding.CreateOptions(sonarLintXml); - var sut = CreateSut(compilation, options); - - sut.ShouldAnalyzeFile(sut.SonarLintFile(), filePath).Should().Be(shouldAnalyze); - } -} From 6a51a15a27489ce5af55421d6f28a24439c67d82 Mon Sep 17 00:00:00 2001 From: Cristian Ambrosini Date: Thu, 9 Mar 2023 17:07:49 +0100 Subject: [PATCH 16/25] Fix rebase --- .../SonarAnalysisContextBaseTest.ShouldAnalyzeTree.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeTree.cs b/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeTree.cs index 0358e3a2d39..c667b8898e2 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeTree.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeTree.cs @@ -101,11 +101,10 @@ public void ShouldAnalyzeTree_GeneratedFile_ShouldAnalyzeGeneratedProvider_IsCac sut.ShouldAnalyzeTree(tree, CSharpGeneratedCodeRecognizer.Instance).Should().BeTrue(); // GetText should be called every time ShouldAnalyzeGenerated is called... - additionalText.Verify(x => x.GetText(It.IsAny()), Times.Exactly(6)); // TEMPORARILY BUMPED TO 6 - sonarLintXml.ToStringCallCount.Should().Be(2); // ... but we should only try to read the file once // TEMPORARILY BUMPED TO 2 + additionalText.Verify(x => x.GetText(It.IsAny()), Times.Exactly(3)); + sonarLintXml.ToStringCallCount.Should().Be(1); // ... but we should only try to read the file once } - [Ignore("Temporarely ignored until the new SonarLintXmlReader will be used to access all properties.")] [DataTestMethod] [DataRow(GeneratedFileName, false)] [DataRow(OtherFileName, true)] @@ -136,7 +135,6 @@ public void ShouldAnalyzeTree_GeneratedFile_AnalyzeGenerated_AnalyzeAllFiles(str sut.ShouldAnalyzeTree(tree, CSharpGeneratedCodeRecognizer.Instance).Should().BeTrue(); } - [Ignore("Temporarely ignored until the new SonarLintXmlReader will be used to access all properties.")] [DataTestMethod] [DataRow(GeneratedFileName, LanguageNames.CSharp, false)] [DataRow(OtherFileName, LanguageNames.CSharp, true)] From 46beb8c2f47fdf90ef4432b5693a69567c63af6d Mon Sep 17 00:00:00 2001 From: Cristian Ambrosini Date: Fri, 3 Mar 2023 17:30:33 +0100 Subject: [PATCH 17/25] Add test cases --- ...alysisContextBaseTest.ShouldAnalyzeFile.cs | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs b/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs new file mode 100644 index 00000000000..ba868d01163 --- /dev/null +++ b/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs @@ -0,0 +1,99 @@ +/* + * SonarAnalyzer for .NET + * Copyright (C) 2015-2023 SonarSource SA + * mailto: contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +namespace SonarAnalyzer.UnitTest.AnalysisContext; + +public partial class SonarAnalysisContextBaseTest +{ + [DataTestMethod] + [DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, false)] + [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, true)] + [DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, true)] + [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, true)] + public void ShouldAnalyzeFile_Exclusions_ReturnExpected(string filePath, string[] exclusions, ProjectType projectType, bool shouldAnalyze) => + AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, exclusions: exclusions); + + [DataTestMethod] + [DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, false)] + [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, true)] + [DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, true)] + [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, true)] + public void ShouldAnalyzeFile_GlobalExclusions_ReturnExpected(string filePath, string[] globalExclusions, ProjectType projectType, bool shouldAnalyze) => + AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, globalExclusions: globalExclusions); + + [DataTestMethod] + [DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, true)] + [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, true)] + [DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, false)] + [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, true)] + public void ShouldAnalyzeFile_TestExclusions_ReturnExpected(string filePath, string[] testExclusions, ProjectType projectType, bool shouldAnalyze) => + AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, testExclusions: testExclusions); + + [DataTestMethod] + [DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, true)] + [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, true)] + [DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, false)] + [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, true)] + public void ShouldAnalyzeFile_GlobalTestExclusions_ReturnExpected(string filePath, string[] globalTestExclusions, ProjectType projectType, bool shouldAnalyze) => + AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, globalTestExclusions: globalTestExclusions); + + [DataTestMethod] + [DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, true)] + [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, false)] + [DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, true)] + [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, true)] + public void ShouldAnalyzeFile_Inclusions_ReturnExpected(string filePath, string[] inclusions, ProjectType projectType, bool shouldAnalyze) => + AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, inclusions: inclusions); + + [DataTestMethod] + [DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, true)] + [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, true)] + [DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, true)] + [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, false)] + public void ShouldAnalyzeFile_TestInclusions_ReturnExpected(string filePath, string[] testInclusions, ProjectType projectType, bool shouldAnalyze) => + AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, testInclusions: testInclusions); + + private void AssertShouldAnalyzeFile( + string filePath, + ProjectType projectType, + bool shouldAnalyze, + string[] exclusions = null, + string[] inclusions = null, + string[] globalExclusions = null, + string[] testExclusions = null, + string[] testInclusions = null, + string[] globalTestExclusions = null) + { + var compilation = new SnippetCompiler("// Nothing to see here", TestHelper.ProjectTypeReference(projectType)).SemanticModel.Compilation; + var sonarLintXml = AnalysisScaffolding.CreateSonarLintXml( + TestContext, + exclusions: exclusions, + inclusions: inclusions, + globalExclusions: globalExclusions, + testExclusions: testExclusions, + testInclusions: testInclusions, + globalTestExclusions: + globalTestExclusions); + var options = AnalysisScaffolding.CreateOptions(sonarLintXml); + var sut = CreateSut(compilation, options); + + sut.ShouldAnalyzeFile(filePath).Should().Be(shouldAnalyze); + } +} From e42c37985f74c379208086857dc5041ef7e5a5b5 Mon Sep 17 00:00:00 2001 From: Cristian Ambrosini Date: Tue, 7 Mar 2023 12:32:00 +0100 Subject: [PATCH 18/25] Addressed PR comments --- .../SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs b/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs index ba868d01163..90c39be474e 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs @@ -70,6 +70,14 @@ public partial class SonarAnalysisContextBaseTest public void ShouldAnalyzeFile_TestInclusions_ReturnExpected(string filePath, string[] testInclusions, ProjectType projectType, bool shouldAnalyze) => AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, testInclusions: testInclusions); + [DataTestMethod] + [DataRow("Foo", new string[] { "Foo" }, new string[] { "Foo" }, ProjectType.Product, false)] + [DataRow("Foo", new string[] { "NotFoo" }, new string[] { "Foo" }, ProjectType.Product, false)] + [DataRow("Foo", new string[] { "Foo" }, new string[] { "NotFoo" }, ProjectType.Product, true)] + [DataRow("Foo", new string[] { "NotFoo" }, new string[] { "NotFoo" }, ProjectType.Product, false)] + public void ShouldAnalyzeFile_MixedInput_ReturnExpected(string filePath, string[] inclusions, string[] exclusions, ProjectType projectType, bool shouldAnalyze) => + AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, inclusions: inclusions, exclusions: exclusions); + private void AssertShouldAnalyzeFile( string filePath, ProjectType projectType, From 6d4aa6b6222f8ec40babc87a6ff3c785114948ee Mon Sep 17 00:00:00 2001 From: Cristian Ambrosini Date: Wed, 8 Mar 2023 09:33:30 +0100 Subject: [PATCH 19/25] Second round of comments --- .../SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs b/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs index 90c39be474e..7aad69f4d09 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs @@ -82,6 +82,7 @@ public partial class SonarAnalysisContextBaseTest string filePath, ProjectType projectType, bool shouldAnalyze, + string language = LanguageNames.CSharp, string[] exclusions = null, string[] inclusions = null, string[] globalExclusions = null, @@ -92,6 +93,7 @@ public partial class SonarAnalysisContextBaseTest var compilation = new SnippetCompiler("// Nothing to see here", TestHelper.ProjectTypeReference(projectType)).SemanticModel.Compilation; var sonarLintXml = AnalysisScaffolding.CreateSonarLintXml( TestContext, + language: language, exclusions: exclusions, inclusions: inclusions, globalExclusions: globalExclusions, @@ -102,6 +104,6 @@ public partial class SonarAnalysisContextBaseTest var options = AnalysisScaffolding.CreateOptions(sonarLintXml); var sut = CreateSut(compilation, options); - sut.ShouldAnalyzeFile(filePath).Should().Be(shouldAnalyze); + sut.ShouldAnalyzeFile(sut.SonarLintFile(), filePath).Should().Be(shouldAnalyze); } } From 7a4f0bbb63d1757c0c70d1922f0642a49cde65c0 Mon Sep 17 00:00:00 2001 From: Cristian Ambrosini Date: Wed, 8 Mar 2023 16:29:34 +0100 Subject: [PATCH 20/25] Migrated ShouldAnalyzeFile test case to ShouldAnalyzeTree --- ...alysisContextBaseTest.ShouldAnalyzeFile.cs | 109 ------------------ 1 file changed, 109 deletions(-) delete mode 100644 analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs b/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs deleted file mode 100644 index 7aad69f4d09..00000000000 --- a/analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarAnalysisContextBaseTest.ShouldAnalyzeFile.cs +++ /dev/null @@ -1,109 +0,0 @@ -/* - * SonarAnalyzer for .NET - * Copyright (C) 2015-2023 SonarSource SA - * mailto: contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -namespace SonarAnalyzer.UnitTest.AnalysisContext; - -public partial class SonarAnalysisContextBaseTest -{ - [DataTestMethod] - [DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, false)] - [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, true)] - [DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, true)] - [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, true)] - public void ShouldAnalyzeFile_Exclusions_ReturnExpected(string filePath, string[] exclusions, ProjectType projectType, bool shouldAnalyze) => - AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, exclusions: exclusions); - - [DataTestMethod] - [DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, false)] - [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, true)] - [DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, true)] - [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, true)] - public void ShouldAnalyzeFile_GlobalExclusions_ReturnExpected(string filePath, string[] globalExclusions, ProjectType projectType, bool shouldAnalyze) => - AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, globalExclusions: globalExclusions); - - [DataTestMethod] - [DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, true)] - [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, true)] - [DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, false)] - [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, true)] - public void ShouldAnalyzeFile_TestExclusions_ReturnExpected(string filePath, string[] testExclusions, ProjectType projectType, bool shouldAnalyze) => - AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, testExclusions: testExclusions); - - [DataTestMethod] - [DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, true)] - [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, true)] - [DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, false)] - [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, true)] - public void ShouldAnalyzeFile_GlobalTestExclusions_ReturnExpected(string filePath, string[] globalTestExclusions, ProjectType projectType, bool shouldAnalyze) => - AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, globalTestExclusions: globalTestExclusions); - - [DataTestMethod] - [DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, true)] - [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, false)] - [DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, true)] - [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, true)] - public void ShouldAnalyzeFile_Inclusions_ReturnExpected(string filePath, string[] inclusions, ProjectType projectType, bool shouldAnalyze) => - AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, inclusions: inclusions); - - [DataTestMethod] - [DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, true)] - [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, true)] - [DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, true)] - [DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, false)] - public void ShouldAnalyzeFile_TestInclusions_ReturnExpected(string filePath, string[] testInclusions, ProjectType projectType, bool shouldAnalyze) => - AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, testInclusions: testInclusions); - - [DataTestMethod] - [DataRow("Foo", new string[] { "Foo" }, new string[] { "Foo" }, ProjectType.Product, false)] - [DataRow("Foo", new string[] { "NotFoo" }, new string[] { "Foo" }, ProjectType.Product, false)] - [DataRow("Foo", new string[] { "Foo" }, new string[] { "NotFoo" }, ProjectType.Product, true)] - [DataRow("Foo", new string[] { "NotFoo" }, new string[] { "NotFoo" }, ProjectType.Product, false)] - public void ShouldAnalyzeFile_MixedInput_ReturnExpected(string filePath, string[] inclusions, string[] exclusions, ProjectType projectType, bool shouldAnalyze) => - AssertShouldAnalyzeFile(filePath, projectType, shouldAnalyze, inclusions: inclusions, exclusions: exclusions); - - private void AssertShouldAnalyzeFile( - string filePath, - ProjectType projectType, - bool shouldAnalyze, - string language = LanguageNames.CSharp, - string[] exclusions = null, - string[] inclusions = null, - string[] globalExclusions = null, - string[] testExclusions = null, - string[] testInclusions = null, - string[] globalTestExclusions = null) - { - var compilation = new SnippetCompiler("// Nothing to see here", TestHelper.ProjectTypeReference(projectType)).SemanticModel.Compilation; - var sonarLintXml = AnalysisScaffolding.CreateSonarLintXml( - TestContext, - language: language, - exclusions: exclusions, - inclusions: inclusions, - globalExclusions: globalExclusions, - testExclusions: testExclusions, - testInclusions: testInclusions, - globalTestExclusions: - globalTestExclusions); - var options = AnalysisScaffolding.CreateOptions(sonarLintXml); - var sut = CreateSut(compilation, options); - - sut.ShouldAnalyzeFile(sut.SonarLintFile(), filePath).Should().Be(shouldAnalyze); - } -} From 98c10052e3dc44979a3f5b7906d4d8b481b591b0 Mon Sep 17 00:00:00 2001 From: Cristian Ambrosini Date: Thu, 9 Mar 2023 15:39:25 +0100 Subject: [PATCH 21/25] Cleanup resource test and migrated references on UTs --- .../sources/AnalyzeGenerated.CS/SonarLint.xml | 30 ------------------- .../sources/SkipGenerated.CS/SonarLint.xml | 30 ------------------- 2 files changed, 60 deletions(-) delete mode 100644 analyzers/its/sources/AnalyzeGenerated.CS/SonarLint.xml delete mode 100644 analyzers/its/sources/SkipGenerated.CS/SonarLint.xml diff --git a/analyzers/its/sources/AnalyzeGenerated.CS/SonarLint.xml b/analyzers/its/sources/AnalyzeGenerated.CS/SonarLint.xml deleted file mode 100644 index 15dbb76706c..00000000000 --- a/analyzers/its/sources/AnalyzeGenerated.CS/SonarLint.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - - sonar.cs.ignoreHeaderComments - true - - - sonar.cs.analyzeGeneratedCode - true - - - sonar.cs.file.suffixes - .cs - - - - - S1067 - - - max - 1 - - - - - - - diff --git a/analyzers/its/sources/SkipGenerated.CS/SonarLint.xml b/analyzers/its/sources/SkipGenerated.CS/SonarLint.xml deleted file mode 100644 index b99df9c60f6..00000000000 --- a/analyzers/its/sources/SkipGenerated.CS/SonarLint.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - - sonar.cs.ignoreHeaderComments - true - - - sonar.cs.analyzeGeneratedCode - false - - - sonar.cs.file.suffixes - .cs - - - - - S1067 - - - max - 1 - - - - - - - From 8fc422be94848f91b081f9369099126475f2213e Mon Sep 17 00:00:00 2001 From: Cristian Ambrosini Date: Fri, 10 Mar 2023 17:25:33 +0100 Subject: [PATCH 22/25] More cleanup, removed in-memory files --- .../Helpers/ParameterLoaderTest.cs | 40 ++++++++++++++++--- .../RuleWithBooleanParameter/SonarLint.xml | 30 -------------- .../RuleWithStringParameter/SonarLint.xml | 30 -------------- .../StringInsteadOfBoolean/SonarLint.xml | 30 -------------- .../StringInsteadOfInt/SonarLint.xml | 30 -------------- 5 files changed, 34 insertions(+), 126 deletions(-) delete mode 100644 analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/RuleWithBooleanParameter/SonarLint.xml delete mode 100644 analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/RuleWithStringParameter/SonarLint.xml delete mode 100644 analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/StringInsteadOfBoolean/SonarLint.xml delete mode 100644 analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/StringInsteadOfInt/SonarLint.xml diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/Helpers/ParameterLoaderTest.cs b/analyzers/tests/SonarAnalyzer.UnitTest/Helpers/ParameterLoaderTest.cs index 5ed1026cd73..10e8fac8a00 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/Helpers/ParameterLoaderTest.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/Helpers/ParameterLoaderTest.cs @@ -81,28 +81,32 @@ public void SetParameterValues_WhenGivenSonarLintFileHasIntParameterType_Populat public void SetParameterValues_WhenGivenSonarLintFileHasStringParameterType_OnlyOneParameter_PopulatesProperty() { // Arrange - var compilation = CreateCompilationWithOption("ResourceTests\\SonarLintXml\\RuleWithStringParameter\\SonarLint.xml"); + var parameterValue = "1"; + var filePath = GenerateSonarLintWithParametrizedRule("S2342", "flagsAttributeFormat", parameterValue); + var compilation = CreateCompilationWithOption(filePath); var analyzer = new EnumNameShouldFollowRegex(); // Cannot use mock because we use reflection to find properties. // Act ParameterLoader.SetParameterValues(analyzer, compilation.SonarLintFile()); // Assert - analyzer.FlagsEnumNamePattern.Should().Be("1"); // value from XML file + analyzer.FlagsEnumNamePattern.Should().Be(parameterValue); // value from XML file } [TestMethod] public void SetParameterValues_WhenGivenSonarLintFileHasBooleanParameterType_OnlyOneParameter_PopulatesProperty() { // Arrange - var compilation = CreateCompilationWithOption("ResourceTests\\SonarLintXml\\RuleWithBooleanParameter\\SonarLint.xml"); + var parameterValue = true; + var filePath = GenerateSonarLintWithParametrizedRule("S1451", "isRegularExpression", parameterValue.ToString()); + var compilation = CreateCompilationWithOption(filePath); var analyzer = new CheckFileLicense(); // Cannot use mock because we use reflection to find properties. // Act ParameterLoader.SetParameterValues(analyzer, compilation.SonarLintFile()); // Assert - analyzer.IsRegularExpression.Should().BeTrue(); // value from XML file + analyzer.IsRegularExpression.Should().Be(parameterValue); // value from XML file } [TestMethod] @@ -180,7 +184,9 @@ public void SetParameterValues_WithMalformedXml_DoesNotPopulateProperties(string public void SetParameterValues_WithWrongPropertyType_StringInsteadOfInt_DoesNotPopulateProperties() { // Arrange - var compilation = CreateCompilationWithOption("ResourceTests\\SonarLintXml\\StringInsteadOfInt\\SonarLint.xml"); + var parameterValue = "fooBar"; + var filePath = GenerateSonarLintWithParametrizedRule("S1067", "max", parameterValue); + var compilation = CreateCompilationWithOption(filePath); var analyzer = new ExpressionComplexity(); // Cannot use mock because we use reflection to find properties. // Act @@ -194,7 +200,9 @@ public void SetParameterValues_WithWrongPropertyType_StringInsteadOfInt_DoesNotP public void SetParameterValues_WithWrongPropertyType_StringInsteadOfBoolean_DoesNotPopulateProperties() { // Arrange - var compilation = CreateCompilationWithOption("ResourceTests\\SonarLintXml\\StringInsteadOfBoolean\\SonarLint.xml"); + var parameterValue = "fooBar"; + var filePath = GenerateSonarLintWithParametrizedRule("S1451", "isRegularExpression", parameterValue); + var compilation = CreateCompilationWithOption(filePath); var analyzer = new CheckFileLicense(); // Cannot use mock because we use reflection to find properties. // Act @@ -213,5 +221,25 @@ private static SonarCompilationReportingContext CreateCompilationWithOption(stri var compilationContext = new CompilationAnalysisContext(compilation, options, _ => { }, _ => true, default); return new(AnalysisScaffolding.CreateSonarAnalysisContext(), compilationContext); } + private string GenerateSonarLintWithParametrizedRule(string ruleId, string key, string value) + { + var ruleParameters = new List() + { + new SonarLintXmlRule() + { + Key = ruleId, + Parameters = new List() + { + new SonarLintXmlKeyValuePair() + { + Key = key, + Value = value + } + } + } + }; + var sonarLintXml = AnalysisScaffolding.GenerateSonarLintXmlContent(rulesParameters: ruleParameters); + return TestHelper.WriteFile(TestContext, "SonarLint.xml", sonarLintXml); + } } } diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/RuleWithBooleanParameter/SonarLint.xml b/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/RuleWithBooleanParameter/SonarLint.xml deleted file mode 100644 index 4170e60a90a..00000000000 --- a/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/RuleWithBooleanParameter/SonarLint.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - - sonar.cs.ignoreHeaderComments - true - - - sonar.cs.analyzeGeneratedCode - false - - - sonar.cs.file.suffixes - .cs - - - - - S1451 - - - isRegularExpression - true - - - - - - - diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/RuleWithStringParameter/SonarLint.xml b/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/RuleWithStringParameter/SonarLint.xml deleted file mode 100644 index d45ac7addb5..00000000000 --- a/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/RuleWithStringParameter/SonarLint.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - - sonar.cs.ignoreHeaderComments - true - - - sonar.cs.analyzeGeneratedCode - false - - - sonar.cs.file.suffixes - .cs - - - - - S2342 - - - flagsAttributeFormat - 1 - - - - - - - diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/StringInsteadOfBoolean/SonarLint.xml b/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/StringInsteadOfBoolean/SonarLint.xml deleted file mode 100644 index 913b8886a2d..00000000000 --- a/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/StringInsteadOfBoolean/SonarLint.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - - sonar.cs.ignoreHeaderComments - true - - - sonar.cs.analyzeGeneratedCode - false - - - sonar.cs.file.suffixes - .cs - - - - - S1451 - - - isRegularExpression - fooBar - - - - - - - diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/StringInsteadOfInt/SonarLint.xml b/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/StringInsteadOfInt/SonarLint.xml deleted file mode 100644 index 4596b44be4b..00000000000 --- a/analyzers/tests/SonarAnalyzer.UnitTest/ResourceTests/SonarLintXml/StringInsteadOfInt/SonarLint.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - - sonar.cs.ignoreHeaderComments - true - - - sonar.cs.analyzeGeneratedCode - false - - - sonar.cs.file.suffixes - .cs - - - - - S1067 - - - max - fooBar - - - - - - - From c688e8c1d50b9b2b868281a6841f84dddf95304e Mon Sep 17 00:00:00 2001 From: Cristian Ambrosini Date: Mon, 13 Mar 2023 08:59:01 +0100 Subject: [PATCH 23/25] Use CreateSonarLintXml instiead of GenerateSonarLintXmlContent --- .../SonarAnalyzer.UnitTest/Helpers/ParameterLoaderTest.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/Helpers/ParameterLoaderTest.cs b/analyzers/tests/SonarAnalyzer.UnitTest/Helpers/ParameterLoaderTest.cs index 10e8fac8a00..92ab7ccbbd2 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/Helpers/ParameterLoaderTest.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/Helpers/ParameterLoaderTest.cs @@ -238,8 +238,7 @@ private string GenerateSonarLintWithParametrizedRule(string ruleId, string key, } } }; - var sonarLintXml = AnalysisScaffolding.GenerateSonarLintXmlContent(rulesParameters: ruleParameters); - return TestHelper.WriteFile(TestContext, "SonarLint.xml", sonarLintXml); + return AnalysisScaffolding.CreateSonarLintXml(TestContext, rulesParameters: ruleParameters); } } } From b343f8acd50cb77c7235da82f6d8263f6146dfae Mon Sep 17 00:00:00 2001 From: Cristian Ambrosini Date: Mon, 13 Mar 2023 11:46:22 +0100 Subject: [PATCH 24/25] UTs methods renaming --- .../Helpers/ParameterLoaderTest.cs | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/Helpers/ParameterLoaderTest.cs b/analyzers/tests/SonarAnalyzer.UnitTest/Helpers/ParameterLoaderTest.cs index 92ab7ccbbd2..d9c047c1d40 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/Helpers/ParameterLoaderTest.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/Helpers/ParameterLoaderTest.cs @@ -34,7 +34,7 @@ public class ParameterLoaderTest [TestMethod] [DataRow("path//aSonarLint.xml")] // different name [DataRow("path//SonarLint.xmla")] // different extension - public void SetParameterValues_WhenNoSonarLintIsGiven_DoesNotPopulateParameters(string filePath) + public void SetParameterValues_WithInvalidSonarLintPath_DoesNotPopulateParameters(string filePath) { // Arrange var compilation = CreateCompilationWithOption(filePath, SourceText.From(File.ReadAllText("ResourceTests\\SonarLintXml\\All_properties_cs\\SonarLint.xml"))); @@ -50,7 +50,7 @@ public void SetParameterValues_WhenNoSonarLintIsGiven_DoesNotPopulateParameters( [TestMethod] [DataRow("a/SonarLint.xml")] // unix path [DataRow("a\\SonarLint.xml")] - public void SetParameterValues_WhenGivenValidSonarLintFilePath_PopulatesProperties(string filePath) + public void SetParameterValues_WithValidSonarLintPath_PopulatesProperties(string filePath) { // Arrange var compilation = CreateCompilationWithOption(filePath, SourceText.From(File.ReadAllText("ResourceTests\\SonarLintXml\\All_properties_cs\\SonarLint.xml"))); @@ -64,7 +64,7 @@ public void SetParameterValues_WhenGivenValidSonarLintFilePath_PopulatesProperti } [TestMethod] - public void SetParameterValues_WhenGivenSonarLintFileHasIntParameterType_PopulatesProperties() + public void SetParameterValues_SonarLintFileWithIntParameterType_PopulatesProperties() { // Arrange var compilation = CreateCompilationWithOption("ResourceTests\\SonarLintXml\\All_properties_cs\\SonarLint.xml"); @@ -78,11 +78,11 @@ public void SetParameterValues_WhenGivenSonarLintFileHasIntParameterType_Populat } [TestMethod] - public void SetParameterValues_WhenGivenSonarLintFileHasStringParameterType_OnlyOneParameter_PopulatesProperty() + public void SetParameterValues_SonarLintFileWithStringParameterType_PopulatesProperty() { // Arrange var parameterValue = "1"; - var filePath = GenerateSonarLintWithParametrizedRule("S2342", "flagsAttributeFormat", parameterValue); + var filePath = GenerateSonarLintXmlWithParametrizedRule("S2342", "flagsAttributeFormat", parameterValue); var compilation = CreateCompilationWithOption(filePath); var analyzer = new EnumNameShouldFollowRegex(); // Cannot use mock because we use reflection to find properties. @@ -94,11 +94,11 @@ public void SetParameterValues_WhenGivenSonarLintFileHasStringParameterType_Only } [TestMethod] - public void SetParameterValues_WhenGivenSonarLintFileHasBooleanParameterType_OnlyOneParameter_PopulatesProperty() + public void SetParameterValues_SonarLintFileWithBooleanParameterType_PopulatesProperty() { // Arrange var parameterValue = true; - var filePath = GenerateSonarLintWithParametrizedRule("S1451", "isRegularExpression", parameterValue.ToString()); + var filePath = GenerateSonarLintXmlWithParametrizedRule("S1451", "isRegularExpression", parameterValue.ToString()); var compilation = CreateCompilationWithOption(filePath); var analyzer = new CheckFileLicense(); // Cannot use mock because we use reflection to find properties. @@ -110,7 +110,7 @@ public void SetParameterValues_WhenGivenSonarLintFileHasBooleanParameterType_Onl } [TestMethod] - public void SetParameterValues_WhenGivenValidSonarLintFileAndDoesNotContainAnalyzerParameters_DoesNotPopulateProperties() + public void SetParameterValues_SonarLintFileWithoutRuleParameters_DoesNotPopulateProperties() { // Arrange var compilation = CreateCompilationWithOption("ResourceTests\\SonarLintXml\\All_properties_cs\\SonarLint.xml"); @@ -181,11 +181,11 @@ public void SetParameterValues_WithMalformedXml_DoesNotPopulateProperties(string } [TestMethod] - public void SetParameterValues_WithWrongPropertyType_StringInsteadOfInt_DoesNotPopulateProperties() + public void SetParameterValues_SonarLintFileWithStringInsteadOfIntParameterType_PopulatesProperty() { // Arrange var parameterValue = "fooBar"; - var filePath = GenerateSonarLintWithParametrizedRule("S1067", "max", parameterValue); + var filePath = GenerateSonarLintXmlWithParametrizedRule("S1067", "max", parameterValue); var compilation = CreateCompilationWithOption(filePath); var analyzer = new ExpressionComplexity(); // Cannot use mock because we use reflection to find properties. @@ -197,11 +197,11 @@ public void SetParameterValues_WithWrongPropertyType_StringInsteadOfInt_DoesNotP } [TestMethod] - public void SetParameterValues_WithWrongPropertyType_StringInsteadOfBoolean_DoesNotPopulateProperties() + public void SetParameterValues_SonarLintFileWithStringInsteadOfBooleanParameterType_PopulatesProperty() { // Arrange var parameterValue = "fooBar"; - var filePath = GenerateSonarLintWithParametrizedRule("S1451", "isRegularExpression", parameterValue); + var filePath = GenerateSonarLintXmlWithParametrizedRule("S1451", "isRegularExpression", parameterValue); var compilation = CreateCompilationWithOption(filePath); var analyzer = new CheckFileLicense(); // Cannot use mock because we use reflection to find properties. @@ -221,7 +221,7 @@ private static SonarCompilationReportingContext CreateCompilationWithOption(stri var compilationContext = new CompilationAnalysisContext(compilation, options, _ => { }, _ => true, default); return new(AnalysisScaffolding.CreateSonarAnalysisContext(), compilationContext); } - private string GenerateSonarLintWithParametrizedRule(string ruleId, string key, string value) + private string GenerateSonarLintXmlWithParametrizedRule(string ruleId, string key, string value) { var ruleParameters = new List() { From f0cc86a6bc3482b54cc611bd362cb01f763f1267 Mon Sep 17 00:00:00 2001 From: Cristian Ambrosini Date: Mon, 13 Mar 2023 12:19:56 +0100 Subject: [PATCH 25/25] Add newline --- .../tests/SonarAnalyzer.UnitTest/Helpers/ParameterLoaderTest.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/Helpers/ParameterLoaderTest.cs b/analyzers/tests/SonarAnalyzer.UnitTest/Helpers/ParameterLoaderTest.cs index d9c047c1d40..fb9a2ff026c 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/Helpers/ParameterLoaderTest.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/Helpers/ParameterLoaderTest.cs @@ -221,6 +221,7 @@ private static SonarCompilationReportingContext CreateCompilationWithOption(stri var compilationContext = new CompilationAnalysisContext(compilation, options, _ => { }, _ => true, default); return new(AnalysisScaffolding.CreateSonarAnalysisContext(), compilationContext); } + private string GenerateSonarLintXmlWithParametrizedRule(string ruleId, string key, string value) { var ruleParameters = new List()