Skip to content

Commit

Permalink
Revert "Enable razor UTs in-memory compilation (#9190)" (#9214)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim-Pohlmann committed Apr 26, 2024
1 parent 97af801 commit 735b0a1
Show file tree
Hide file tree
Showing 56 changed files with 480 additions and 1,450 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ public async Task SonarCompilationStartAnalysisContext_RegisterSymbolStartAction
public void DisabledRules_ForRazor_DoNotRaise(string ruleId, string extension) =>
new VerifierBuilder()
.AddAnalyzer(() => new DummyAnalyzerWithLocation(ruleId, DiagnosticDescriptorFactory.MainSourceScopeTag))
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.AddSnippet(Snippet(extension), $"SomeFile.{extension}")
.VerifyNoIssueReported();

Expand All @@ -449,6 +450,7 @@ public void AllScopedRules_ForRazor_Raise(string extension)
var keyword = extension == "razor" ? "code" : "functions";
new VerifierBuilder()
.AddAnalyzer(() => new DummyAnalyzerWithLocation("DummyId", DiagnosticDescriptorFactory.TestSourceScopeTag, DiagnosticDescriptorFactory.MainSourceScopeTag))
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.AddSnippet($$"""
@{{keyword}}
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,21 @@ public class AvoidLambdaExpressionInLoopsInBlazorTest

[TestMethod]
public void AvoidLambdaExpressionInLoopsInBlazor_Blazor() =>
builder.AddPaths("AvoidLambdaExpressionInLoopsInBlazor.razor").Verify();
builder.AddPaths("AvoidLambdaExpressionInLoopsInBlazor.razor")
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.Verify();

[TestMethod]
public void AvoidLambdaExpressionInLoopsInBlazor_BlazorLoopsWithNoBody() =>
builder.AddPaths("AvoidLambdaExpressionInLoopsInBlazor.LoopsWithNoBody.razor").Verify();
builder.AddPaths("AvoidLambdaExpressionInLoopsInBlazor.LoopsWithNoBody.razor")
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.Verify();

[TestMethod]
public void AvoidLambdaExpressionInLoopsInBlazor_UsingRenderFragment() =>
builder.AddPaths("AvoidLambdaExpressionInLoopsInBlazor.RenderFragment.razor", "AvoidLambdaExpressionInLoopsInBlazor.RenderFragmentConsumer.razor").Verify();
builder.AddPaths("AvoidLambdaExpressionInLoopsInBlazor.RenderFragment.razor", "AvoidLambdaExpressionInLoopsInBlazor.RenderFragmentConsumer.razor")
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.Verify();

[TestMethod]
public void AvoidLambdaExpressionInLoopsInBlazor_CS() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,20 @@ public class BlazorQueryParameterRoutableComponentTest
[TestMethod]
public void BlazorQueryParameterRoutableComponent_Blazor() =>
builder.AddPaths("BlazorQueryParameterRoutableComponent.razor")
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.Verify();

[TestMethod]
public void BlazorQueryParameterRoutableComponent_BlazorNoRoute() =>
builder.AddPaths("BlazorQueryParameterRoutableComponent_NoRoute.razor")
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.Verify();

[TestMethod]
public void BlazorQueryParameterRoutableComponent_Partial() =>
builder.AddPaths("BlazorQueryParameterRoutableComponent_Partial.razor.cs",
"BlazorQueryParameterRoutableComponent_Partial.razor")
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.Verify();

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public class Derived: Base
}
""")
.AddPaths("CastShouldNotBeDuplicated.cshtml")
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.Verify();

#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public class CollectionPropertiesShouldBeReadOnlyTest

[TestMethod]
public void CollectionPropertiesShouldBeReadOnly_Razor() =>
builder.AddPaths("CollectionPropertiesShouldBeReadOnly.razor", "CollectionPropertiesShouldBeReadOnly.razor.cs").Verify();
builder.AddPaths("CollectionPropertiesShouldBeReadOnly.razor", "CollectionPropertiesShouldBeReadOnly.razor.cs")
.Verify();

#endif

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public void Method()
}
""",
"SomeRazorFile.razor")
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.Verify();

#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ public void Method()
}
""",
"SomeRazorFile.razor")
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.Verify();

#endif

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public void Method(int a, int b)
}
""",
"SomeRazorFile.razor")
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.Verify();

#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public void Method()
}
""",
"SomeRazorFile.razor")
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.Verify();

#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public int Method(int j)
}
""",
"SomeRazorFile.razor")
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.Verify();

#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class JSInvokableMethodsShouldBePublicTest
public void JSInvokableMethodsShouldBePublic_Razor() =>
builder
.AddPaths("JSInvokableMethodsShouldBePublic.razor", "JSInvokableMethodsShouldBePublic.razor.cs")
.WithOptions(ParseOptionsHelper.FromCSharp9)
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.Verify();

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class MethodOverloadOptionalParameterTest
}
""",
"SomeRazorFile.razor")
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.Verify();

#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ public class ParameterTypeShouldMatchRouteTypeConstraintTest

[TestMethod]
public void ParameterTypeShouldMatchRouteTypeConstraint_Blazor() =>
builder.AddPaths("ParameterTypeShouldMatchRouteTypeConstraint.razor").Verify();
builder.AddPaths("ParameterTypeShouldMatchRouteTypeConstraint.razor")
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.Verify();

[TestMethod]
public void ParameterTypeShouldMatchRouteTypeConstraint_Partial() =>
builder.AddPaths("ParameterTypeShouldMatchRouteTypeConstraint.Partial.razor", "ParameterTypeShouldMatchRouteTypeConstraint.Partial.razor.cs").Verify();
builder.AddPaths("ParameterTypeShouldMatchRouteTypeConstraint.Partial.razor", "ParameterTypeShouldMatchRouteTypeConstraint.Partial.razor.cs")
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.Verify();

[TestMethod]
public void ParameterTypeShouldMatchRouteTypeConstraint_CS() =>
Expand All @@ -48,7 +52,9 @@ public class ParameterTypeShouldMatchRouteTypeConstraintTest

[TestMethod]
public void ParameterTypeShouldMatchRouteTypeConstraint_Conversion() =>
builder.AddPaths("ParameterTypeShouldMatchRouteTypeConstraint.Conversion.razor").Verify();
builder.AddPaths("ParameterTypeShouldMatchRouteTypeConstraint.Conversion.razor")
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.Verify();
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public class SingleStatementPerLineTest
void DoSomething(bool flag) { if (flag) Console.WriteLine("Test"); } // Noncompliant
}
""",
"SomeRazorFile.razor").Verify();
"SomeRazorFile.razor")
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.Verify();

#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ public class PublicMethodArgumentsShouldBeCheckedForNullTest

[TestMethod]
public void PublicMethodArgumentsShouldBeCheckedForNull_Roslyn_Razor() =>
roslynCS.AddPaths("PublicMethodArgumentsShouldBeCheckedForNull.razor", "PublicMethodArgumentsShouldBeCheckedForNull_component.razor").Verify();
roslynCS.AddPaths("PublicMethodArgumentsShouldBeCheckedForNull.razor", "PublicMethodArgumentsShouldBeCheckedForNull_component.razor")
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.Verify();

#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@ public void Method()
}
""",
"SomeRazorFile.razor")
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.Verify();

#endif

}
}
22 changes: 15 additions & 7 deletions analyzers/tests/SonarAnalyzer.Test/Rules/UnnecessaryUsingsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,34 +53,42 @@ public class UnnecessaryUsingsTest
[DataRow("_viewimports.cshtml")]
[DataRow("_viEwiMpoRts.cshtml")]
public void UnnecessaryUsings_RazorViewImportsCshtmlFile_NoIssueReported(string fileName) =>
builder.AddSnippet(@"@using System.Text.Json;", fileName).VerifyNoIssueReported();
builder
.AddSnippet(@"@using System.Text.Json;", fileName)
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.VerifyNoIssueReported();

[DataTestMethod]
[DataRow("_Imports.razor")]
[DataRow("_imports.razor")]
[DataRow("_iMpoRts.razor")]
public void UnnecessaryUsings_RazorImportsRazorFile_NoIssueReported(string fileName) =>
builder.AddSnippet(@"@using System.Text.Json;", fileName).VerifyNoIssueReported();
builder
.AddSnippet(@"@using System.Text.Json;", fileName)
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.VerifyNoIssueReported();

[DataTestMethod]
[DataRow("RandomFile_ViewImports.cshtml")]
[DataRow("RandomFile_Imports.cshtml")]
[DataRow("_Imports.cshtml")]
public void UnnecessaryUsings_RazorViewImportsSimilarCshtmlFile_IssuesReported(string fileName) =>
builder.AddReferences(NuGetMetadataReference.SystemTextJson("7.0.4"))
builder
.AddSnippet("@using System.Linq;", "_ViewImports.cshtml")
.AddSnippet(@"@using System.Text.Json; @* Noncompliant *@", fileName)
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.Verify();

[DataTestMethod]
[DataRow("RandomFile_ViewImports.razor")]
[DataRow("RandomFile_Imports.razor")]
[DataRow("_ViewImports.razor")]
public void UnnecessaryUsings_RazorViewImportsSimilarRazorFile_IssuesReported(string fileName) =>
builder.AddReferences(NuGetMetadataReference.SystemTextJson("7.0.4"))
.AddSnippet("@using System.Linq;", "_Imports.razor")
.AddSnippet(@"@using System.Text.Json; @* Noncompliant *@", fileName)
.Verify();
builder
.AddSnippet("@using System.Linq;", "_Imports.razor")
.AddSnippet(@"@using System.Text.Json; @* Noncompliant *@", fileName)
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.Verify();

[DataTestMethod]
[DataRow("_ViewImports.cs")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public class CopyPasteTokenAnalyzerTest
[TestMethod]
public void Verify_Duplicated_CS_GlobalUsings() =>
CreateBuilder(ProjectType.Product, "Duplicated.CSharp10.cs")
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.VerifyUtilityAnalyzer<CopyPasteTokenInfo>(messages =>
{
messages.Should().ContainSingle();
Expand Down Expand Up @@ -121,6 +122,7 @@ public class CopyPasteTokenAnalyzerTest

private void Verify(string fileName, Action<IReadOnlyList<CopyPasteTokenInfo.Types.TokenInfo>> verifyTokenInfo) =>
CreateBuilder(ProjectType.Product, fileName)
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.VerifyUtilityAnalyzer<CopyPasteTokenInfo>(messages =>
{
messages.Should().ContainSingle();
Expand All @@ -136,9 +138,8 @@ public class CopyPasteTokenAnalyzerTest
[DataRow("Razor.cshtml")]
public void Verify_NoMetricsAreComputedForRazorFiles(string fileName) =>
CreateBuilder(ProjectType.Product, fileName)
.VerifyUtilityAnalyzer<CopyPasteTokenInfo>(messages => messages.Select(x => Path.GetFileName(x.FilePath))
.Should()
.BeEmpty());
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.VerifyUtilityAnalyzer<CopyPasteTokenInfo>(messages => messages.Select(x => Path.GetFileName(x.FilePath)).Should().BeEmpty());

#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,13 @@ public void CreateMessage_NoEncoding_SetsEmptyString(bool isTestProject)
#if NET

[DataTestMethod]
[DataRow("Razor.razor")]
[DataRow("Razor.cshtml")]
public void Verify_RazorFilesAreIgnored(string fileName) =>
[DataRow("Razor.razor", "EmptyProject.GlobalUsings.g.cs", ".NETCoreApp,Version=v7.0.AssemblyAttributes.cs", "EmptyProject.AssemblyInfo.cs")]
[DataRow("Razor.cshtml", "EmptyProject.GlobalUsings.g.cs", ".NETCoreApp,Version=v7.0.AssemblyAttributes.cs", "EmptyProject.AssemblyInfo.cs", "EmptyProject.RazorAssemblyInfo.cs")]
public void Verify_RazorFilesAreIgnored(string fileName, params string[] expectedFiles) =>
CreateBuilder(ProjectType.Product, fileName)
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.VerifyUtilityAnalyzer<FileMetadataInfo>(messages =>
messages.Select(x => Path.GetFileName(x.FilePath)).Should().BeEmpty()); // There are more files on some PCs: JSExports.g.cs, LibraryImports.g.cs, JSImports.g.cs
messages.Select(x => Path.GetFileName(x.FilePath)).Should().Contain(expectedFiles)); // There are more files on some PCs: JSExports.g.cs, LibraryImports.g.cs, JSImports.g.cs

#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ public void LogAutogenerated_CsHtml()
{
const string fileName = "Generated_cshtml.g.cs";
CreateBuilder(fileName)
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.VerifyUtilityAnalyzer<LogInfo>(logs => logs.Should().NotContain(x => x.Text.IndexOf(fileName, StringComparison.OrdinalIgnoreCase) != -1));
}

[TestMethod]
public void LogAutogenerated_Razor() =>
CreateBuilder("Generated_razor.g.cs")
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.VerifyUtilityAnalyzer<LogInfo>(logs => logs.Select(x => x.Text).Should().Contain($"File 'Utilities\\LogAnalyzer\\Generated_razor.g.cs' was recognized as razor generated"));

[DataTestMethod]
Expand All @@ -80,7 +82,9 @@ public void LogAutogenerated_CsHtml()
.VerifyUtilityAnalyzer<LogInfo>(x => x.Where(info => info.Text.Contains("generated")).Should().HaveCount(expectedGeneratedFiles));

private void Verify(string[] paths, Action<IReadOnlyList<LogInfo>> verifyProtobuf) =>
CreateBuilder(paths).VerifyUtilityAnalyzer(verifyProtobuf);
CreateBuilder(paths)
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.VerifyUtilityAnalyzer(verifyProtobuf);

private VerifierBuilder CreateBuilder(params string[] paths)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class MetricsAnalyzerTest
[DataTestMethod]
public void VerifyMetrics() =>
CreateBuilder(false, AllMetricsFileName)
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.VerifyUtilityAnalyzer<MetricsInfo>(messages =>
{
messages.Should().ContainSingle();
Expand All @@ -61,10 +62,11 @@ public class MetricsAnalyzerTest
[TestMethod]
public void VerifyMetrics_Razor() =>
CreateBuilder(false, RazorFileName, "Component.razor")
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.VerifyUtilityAnalyzer<MetricsInfo>(messages =>
{
var orderedMessages = messages.OrderBy(x => x.FilePath, StringComparer.InvariantCulture).ToArray();
orderedMessages.Select(x => Path.GetFileName(x.FilePath)).Should().BeEquivalentTo(RazorFileName, "Component.razor");
orderedMessages.Select(x => Path.GetFileName(x.FilePath)).Should().BeEquivalentTo("_Imports.razor", RazorFileName, "Component.razor");
var metrics = messages.Single(x => x.FilePath.EndsWith(RazorFileName));
Expand All @@ -82,13 +84,15 @@ public class MetricsAnalyzerTest
[TestMethod]
public void VerifyMetrics_CsHtml() =>
CreateBuilder(false, CsHtmlFileName)
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.VerifyUtilityAnalyzer<MetricsInfo>(messages =>
// There should be no metrics messages for the cshtml files.
messages.Select(x => Path.GetFileName(x.FilePath)).Should().BeEmpty());
messages.Select(x => Path.GetFileName(x.FilePath)).Should().BeEquivalentTo("_Imports.razor"));

[TestMethod]
public void VerifyMetrics_CSharp12() =>
CreateBuilder(false, CSharp12FileName)
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.VerifyUtilityAnalyzer<MetricsInfo>(messages =>
{
messages.Should().ContainSingle();
Expand Down

0 comments on commit 735b0a1

Please sign in to comment.