From 053a756f70bdef888c11b5e8756e25e6b2af5877 Mon Sep 17 00:00:00 2001 From: Costin Zaharia Date: Fri, 17 May 2024 14:16:54 +0200 Subject: [PATCH] Update UTs to reflect the latest changes from dotnet 8.0.5 --- .../Metrics/CSharpExecutableLinesMetricTest.cs | 10 +++++----- .../Rules/Utilities/MetricsAnalyzerTest.cs | 6 +++--- .../Utilities/SymbolReferenceAnalyzerTest.cs | 4 ++-- .../Utilities/MetricsAnalyzer/Razor.razor | 16 ++++++++-------- .../Common/SourceGeneratorProviderTest.cs | 7 +++---- .../TestCases/Dummy.SecondaryLocation.razor | 4 ++-- .../TestCases/DummyExpressions.razor | 8 ++++---- .../Common/SourceGeneratorProvider.cs | 2 +- 8 files changed, 28 insertions(+), 29 deletions(-) diff --git a/analyzers/tests/SonarAnalyzer.Test/Metrics/CSharpExecutableLinesMetricTest.cs b/analyzers/tests/SonarAnalyzer.Test/Metrics/CSharpExecutableLinesMetricTest.cs index 11ea1a5796b..b012a527cf2 100644 --- a/analyzers/tests/SonarAnalyzer.Test/Metrics/CSharpExecutableLinesMetricTest.cs +++ b/analyzers/tests/SonarAnalyzer.Test/Metrics/CSharpExecutableLinesMetricTest.cs @@ -893,14 +893,14 @@ public void GetLineNumbers_Razor_FieldReference() => @page "/razor" @using TestCases -

Current count: @currentCount

+

Current count: @currentCount

-@currentCount +@currentCount @code { private int currentCount = 0; } -""", RazorFile, 4, 6); +""", RazorFile); [TestMethod] public void GetLineNumbers_Razor_MethodReferenceAndCall() => @@ -927,13 +927,13 @@ private string ShowAmount() [TestMethod] public void GetLineNumbers_Razor_PropertyReference() => AssertLineNumbersOfExecutableLinesRazor(""" -@IncrementAmount +@IncrementAmount @code { [Parameter] public int IncrementAmount { get; set; } = 1; } -""", RazorFile, 1); +""", RazorFile); [TestMethod] public void GetLineNumbers_Razor_Html() => diff --git a/analyzers/tests/SonarAnalyzer.Test/Rules/Utilities/MetricsAnalyzerTest.cs b/analyzers/tests/SonarAnalyzer.Test/Rules/Utilities/MetricsAnalyzerTest.cs index 0ce55cf1517..ce73bd8f2e6 100644 --- a/analyzers/tests/SonarAnalyzer.Test/Rules/Utilities/MetricsAnalyzerTest.cs +++ b/analyzers/tests/SonarAnalyzer.Test/Rules/Utilities/MetricsAnalyzerTest.cs @@ -69,14 +69,14 @@ public void VerifyMetrics_Razor() => var metrics = messages.Single(x => x.FilePath.EndsWith(RazorFileName)); metrics.ClassCount.Should().Be(1); - metrics.CodeLine.Should().BeEquivalentTo(new[] { 3, 5, 8, 10, 13, 15, 16, 17, 19, 22, 23, 24, 26, 28, 29, 32, 33, 34, 36, 37, 39, 40, 43 }); + metrics.CodeLine.Should().BeEquivalentTo(new[] { 2, 1, 3, 5, 8, 10, 13, 15, 16, 17, 19, 22, 23, 24, 26, 28, 29, 32, 33, 34, 36, 37, 39, 40, 43 }); metrics.CognitiveComplexity.Should().Be(3); metrics.Complexity.Should().Be(4); - metrics.ExecutableLines.Should().BeEquivalentTo(new[] { 3, 5, 13, 15, 17, 24, 28, 29, 32, 36, 39, 43 }); + metrics.ExecutableLines.Should().BeEquivalentTo(new[] { 13, 15, 28, 29, 32, 36 }); metrics.FunctionCount.Should().Be(1); metrics.NoSonarComment.Should().BeEmpty(); metrics.NonBlankComment.Should().BeEquivalentTo(new[] { 7, 8, 10, 15, 21, 22, 23, 28, 29, 32, 33, 36, 37, 38 }); - metrics.StatementCount.Should().Be(13); + metrics.StatementCount.Should().Be(6); }); [TestMethod] diff --git a/analyzers/tests/SonarAnalyzer.Test/Rules/Utilities/SymbolReferenceAnalyzerTest.cs b/analyzers/tests/SonarAnalyzer.Test/Rules/Utilities/SymbolReferenceAnalyzerTest.cs index fe7bfffc438..605706ca30a 100644 --- a/analyzers/tests/SonarAnalyzer.Test/Rules/Utilities/SymbolReferenceAnalyzerTest.cs +++ b/analyzers/tests/SonarAnalyzer.Test/Rules/Utilities/SymbolReferenceAnalyzerTest.cs @@ -244,11 +244,11 @@ public void Verify_Razor() => orderedSymbols[1].FilePath.Should().EndWith("RazorComponent.razor"); // RazorComponent.razor // https://github.com/SonarSource/sonar-dotnet/issues/8417 - // Net8 SDK: Declaration (1,0) - (1,17) Reference (1,6) - (1,23) <- Overlapping + // before dotnet 8.0.5 SDK: Declaration (1,0) - (1,17) Reference (1,6) - (1,23) <- Overlapping // Declaration of TSomeVeryLongName is placed starting at index 0 (ignoring "@typeparam ") // Reference "where TSomeVeryLongName" is placed starting at index 6 (ignoring "@typeparam TSomeVeryLongName ") VerifyReferences(orderedSymbols[1].Reference, 1, 1, 1); - orderedSymbols[1].Reference.Single().Reference.Single().Should().BeEquivalentTo(new TextRange { StartLine = 1, EndLine = 1, StartOffset = 6, EndOffset = 23 }); + orderedSymbols[1].Reference.Single().Reference.Single().Should().BeEquivalentTo(new TextRange { StartLine = 1, EndLine = 1, StartOffset = 35, EndOffset = 52 }); }); [DataTestMethod] diff --git a/analyzers/tests/SonarAnalyzer.Test/TestCases/Utilities/MetricsAnalyzer/Razor.razor b/analyzers/tests/SonarAnalyzer.Test/TestCases/Utilities/MetricsAnalyzer/Razor.razor index 9ed16bf496a..1562e7b94da 100644 --- a/analyzers/tests/SonarAnalyzer.Test/TestCases/Utilities/MetricsAnalyzer/Razor.razor +++ b/analyzers/tests/SonarAnalyzer.Test/TestCases/Utilities/MetricsAnalyzer/Razor.razor @@ -1,8 +1,8 @@ -@page "/razor" // FN: line of code, comment (location mapping issue) -@namespace RazorNamespace // FN: line of code, comment (location mapping issue) -

Current count: @currentCount

+@page "/razor" +@namespace RazorNamespace +

Current count: @currentCount

-@currentCount +@currentCount @code { // FN: line of code (location mapping issue) private int currentCount = 0; // code line @@ -14,14 +14,14 @@ @for (var i = 0; i < 5; i++) // code line { - Value: @i + Value: @i test } @{ // FN: code line (location mapping issue) void RenderName(string name) // code line { // code line -

Name: @name

+

Name: @name

Name: Hardcoded

} @@ -37,10 +37,10 @@ { // code line // comment @:Name: @i // code line, FN: comment -} +} - @currentCount + @currentCount diff --git a/analyzers/tests/SonarAnalyzer.TestFramework.Test/Common/SourceGeneratorProviderTest.cs b/analyzers/tests/SonarAnalyzer.TestFramework.Test/Common/SourceGeneratorProviderTest.cs index 7762f604e60..5ce84f3bad5 100644 --- a/analyzers/tests/SonarAnalyzer.TestFramework.Test/Common/SourceGeneratorProviderTest.cs +++ b/analyzers/tests/SonarAnalyzer.TestFramework.Test/Common/SourceGeneratorProviderTest.cs @@ -26,12 +26,11 @@ namespace SonarAnalyzer.TestFramework.Test.Common; public class SourceGeneratorProviderTest { private static AnalyzerFileReference RazorSourceGenerator => - SourceGeneratorProvider.SourceGenerators.Single(x => x.FullPath.EndsWith("Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll")); - + SourceGeneratorProvider.SourceGenerators.Single(x => x.FullPath.EndsWith("Microsoft.CodeAnalysis.Razor.Compiler.dll")); [TestMethod] public void SourceGenerators_ContainsRazorSourceGenerator() => SourceGeneratorProvider.SourceGenerators.Should() - .Contain(x => x.FullPath.EndsWith(Path.Combine("Sdks", "Microsoft.NET.Sdk.Razor", "source-generators", "Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll"))); + .Contain(x => x.FullPath.EndsWith(Path.Combine("Sdks", "Microsoft.NET.Sdk.Razor", "source-generators", "Microsoft.CodeAnalysis.Razor.Compiler.dll"))); [TestMethod] public void RazorSourceGenerator_ExistsLocally() => @@ -39,7 +38,7 @@ public void RazorSourceGenerator_ExistsLocally() => [TestMethod] public void RazorSourceGenerator_LoadsCorrectAssembly() => - RazorSourceGenerator.GetAssembly().GetName().Name.Should().Be("Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators"); + RazorSourceGenerator.GetAssembly().GetName().Name.Should().Be("Microsoft.CodeAnalysis.Razor.Compiler"); [TestMethod] public void LatestSdkFolder_ReturnsAssemblyMajor() diff --git a/analyzers/tests/SonarAnalyzer.TestFramework.Test/TestCases/Dummy.SecondaryLocation.razor b/analyzers/tests/SonarAnalyzer.TestFramework.Test/TestCases/Dummy.SecondaryLocation.razor index 79df9be0503..fcc06936447 100644 --- a/analyzers/tests/SonarAnalyzer.TestFramework.Test/TestCases/Dummy.SecondaryLocation.razor +++ b/analyzers/tests/SonarAnalyzer.TestFramework.Test/TestCases/Dummy.SecondaryLocation.razor @@ -1,7 +1,7 @@

The solution to all problems is: @(RaiseHere(21))

@* wrong location *@ -@* ^^^^^^^^^ *@ -@* ^^ Secondary@-1 *@ +@* ^^^^^^^^^ *@ +@* ^^ Secondary@-1 *@ @code { diff --git a/analyzers/tests/SonarAnalyzer.TestFramework.Test/TestCases/DummyExpressions.razor b/analyzers/tests/SonarAnalyzer.TestFramework.Test/TestCases/DummyExpressions.razor index f2b1fbfbf14..3594e315b09 100644 --- a/analyzers/tests/SonarAnalyzer.TestFramework.Test/TestCases/DummyExpressions.razor +++ b/analyzers/tests/SonarAnalyzer.TestFramework.Test/TestCases/DummyExpressions.razor @@ -1,8 +1,8 @@ 

Explicit expression: @(RaiseHere())

@* Noncompliant, wrong location*@ -@* ^^^^^^^^^ *@ +@* ^^^^^^^^^ *@

Implicit expression: @RaiseHere()

@* Noncompliant, wrong location *@ -@* ^^^^^^^^^ *@ +@* ^^^^^^^^^ *@

Multi-statement block: @{ var result = RaiseHere(); }

@* ^^^^^^^^^ *@ @@ -17,7 +17,7 @@ @* ^^^^^^^^^ *@

Lambda expression: @((Func)(() => RaiseHere()))()

@* Noncompliant, wrong location *@ -@* ^^^^^^^^^ *@ +@* ^^^^^^^^^ *@

Nested multi-statement block: @{ var result2 = RaiseHere(); var result3 = RaiseHere(); }

@* ^^^^^^^^^ *@ @@ -25,7 +25,7 @@

Nested control structures: @if(RaiseHere() == 42) { @(RaiseHere() + 1) }

@* second issue has wrong location *@ @* ^^^^^^^^^ *@ -@* ^^^^^^^^^@-1 *@ +@* ^^^^^^^^^@-1 *@ @code { diff --git a/analyzers/tests/SonarAnalyzer.TestFramework/Common/SourceGeneratorProvider.cs b/analyzers/tests/SonarAnalyzer.TestFramework/Common/SourceGeneratorProvider.cs index a6aed6ed4d3..226f82e8534 100644 --- a/analyzers/tests/SonarAnalyzer.TestFramework/Common/SourceGeneratorProvider.cs +++ b/analyzers/tests/SonarAnalyzer.TestFramework/Common/SourceGeneratorProvider.cs @@ -26,7 +26,7 @@ namespace SonarAnalyzer.TestFramework.Common; public static class SourceGeneratorProvider { private static readonly string RazorSourceGeneratorPath = - Path.Combine(LatestSdkFolder(), "Sdks", "Microsoft.NET.Sdk.Razor", "source-generators", "Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll"); + Path.Combine(LatestSdkFolder(), "Sdks", "Microsoft.NET.Sdk.Razor", "source-generators", "Microsoft.CodeAnalysis.Razor.Compiler.dll"); public static AnalyzerFileReference[] SourceGenerators { get; } = [