Skip to content

Commit

Permalink
TestFramework: Move framework files (#8568)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-mikula-sonarsource committed Jan 25, 2024
1 parent 4972c6d commit 9ed994e
Show file tree
Hide file tree
Showing 50 changed files with 118 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SonarAnalyzer.Test\SonarAnalyzer.Test.csproj" />
<ProjectReference Include="..\SonarAnalyzer.TestFramework\SonarAnalyzer.TestFramework.csproj" />
<ProjectReference Include="..\..\src\SonarAnalyzer.CSharp\SonarAnalyzer.CSharp.csproj">
<Aliases>global,csharp</Aliases>
</ProjectReference>
<ProjectReference Include="..\..\src\SonarAnalyzer.VisualBasic\SonarAnalyzer.VisualBasic.csproj">
<Aliases>global,vbnet</Aliases>
</ProjectReference>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public TestSetup(string testCase, SonarDiagnosticAnalyzer analyzer, IEnumerable<
Analyzer = analyzer;
additionalReferences = additionalReferences
.Concat(MetadataReferenceFacade.SystemComponentModelPrimitives)
.Concat(NetStandardMetadataReference.Netstandard)
.Concat(MetadataReferenceFacade.NetStandard)
.Concat(MetadataReferenceFacade.SystemData);
Builder = new VerifierBuilder().AddAnalyzer(() => analyzer).AddPaths(Path).AddReferences(additionalReferences);
}
Expand Down
14 changes: 10 additions & 4 deletions analyzers/tests/SonarAnalyzer.Test/Common/RuleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using SonarAnalyzer.Rules.CSharp;
using SonarAnalyzer.SymbolicExecution.Roslyn.RuleChecks.CSharp;
using SonarAnalyzer.Test.Helpers;
using SonarAnalyzer.Test.PackagingTests;

namespace SonarAnalyzer.Test.Common
{
Expand Down Expand Up @@ -150,7 +151,7 @@ public void Verify_ConcurrentExecutionIsExplicitlyDisabled(string value)
.Where(RuleFinder.IsParameterized)
.Select(type => (DiagnosticAnalyzer)Activator.CreateInstance(type))
.SelectMany(analyzer => analyzer.SupportedDiagnostics)
.Where(analyzer => !TestHelper.IsSecurityHotspot(analyzer))
.Where(analyzer => !IsSecurityHotspot(analyzer))
.ToList()
.ForEach(diagnostic => diagnostic.IsEnabledByDefault.Should().BeFalse());

Expand All @@ -172,7 +173,7 @@ public void AllRulesEnabledByDefault_ContainSonarWayCustomTag()
var descriptors = RuleFinder.RuleAnalyzerTypes.SelectMany(SupportedDiagnostics)
// Security hotspots are enabled by default, but they will report issues only
// when their ID is contained in SonarLint.xml
.Where(descriptor => !TestHelper.IsSecurityHotspot(descriptor));
.Where(descriptor => !IsSecurityHotspot(descriptor));

foreach (var descriptor in descriptors)
{
Expand Down Expand Up @@ -228,7 +229,7 @@ public void AllRules_SonarWayTagPresenceMatchesIsEnabledByDefault()

foreach (var diagnostic in RuleFinder.RuleAnalyzerTypes.SelectMany(SupportedDiagnostics))
{
if (TestHelper.IsSecurityHotspot(diagnostic))
if (IsSecurityHotspot(diagnostic))
{
// Security hotspots are enabled by default, but they will report issues only when their ID is contained in SonarLint.xml
// DiagnosticDescriptorFactory adds WellKnownDiagnosticTags.NotConfigurable to prevent rule supression and deactivation.
Expand Down Expand Up @@ -339,7 +340,6 @@ public void UnchangedFiles_ReportDiagnosticIfNonGeneratedBasedRule(string unchan
UnchangedFiles_Verify(builder, unchangedFileName, expectEmptyResults);
}

[AssertionMethod]
private static void AllRulesAreConfigurable(AnalyzerLanguage language)
{
foreach (var diagnostic in SupportedDiagnostics(language))
Expand Down Expand Up @@ -403,6 +403,12 @@ private static void VerifyNoExceptionThrown(string path, DiagnosticAnalyzer[] di
DiagnosticVerifier.VerifyNoExceptionThrown(diagnostics);
}

private static bool IsSecurityHotspot(DiagnosticDescriptor diagnostic)
{
var type = RuleTypeMappingCS.Rules.GetValueOrDefault(diagnostic.Id) ?? RuleTypeMappingVB.Rules.GetValueOrDefault(diagnostic.Id);
return type == "SECURITY_HOTSPOT";
}

[DiagnosticAnalyzer(LanguageNames.CSharp)]
private class ConcurrentExecutionReader : SonarDiagnosticAnalyzer
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

using SonarAnalyzer.Rules.CSharp;
using SonarAnalyzer.Test.PackagingTests;
using SonarAnalyzer.Test.Rules;

namespace SonarAnalyzer.Test.Common
Expand Down Expand Up @@ -65,7 +66,13 @@ private static void VerifyNoIssueReported(AnalyzerLanguage language, ImmutableAr
.Where(IsSecurityHotspot);

private static bool IsSecurityHotspot(DiagnosticAnalyzer analyzer) =>
analyzer.SupportedDiagnostics.Any(TestHelper.IsSecurityHotspot);
analyzer.SupportedDiagnostics.Any(IsSecurityHotspot);

private static bool IsSecurityHotspot(DiagnosticDescriptor diagnostic)
{
var type = RuleTypeMappingCS.Rules.GetValueOrDefault(diagnostic.Id) ?? RuleTypeMappingVB.Rules.GetValueOrDefault(diagnostic.Id);
return type == "SECURITY_HOTSPOT";
}

private static string GetTestCaseFileName(string analyzerName) =>
analyzerName switch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public class Foo
[Microsoft.AspNetCore.Mvc.NonActionAttribute]
public void PublicNonAction() { }
}";
var compilation = TestHelper.CompileCS(code, NetStandardMetadataReference.Netstandard.Union(NuGetMetadataReference.MicrosoftAspNetCoreMvcCore(aspNetMvcVersion)).ToArray())
var compilation = TestHelper.CompileCS(code, MetadataReferenceFacade.NetStandard.Union(NuGetMetadataReference.MicrosoftAspNetCoreMvcCore(aspNetMvcVersion)).ToArray())
.Model
.Compilation;
var publicFoo = GetMethodSymbol(compilation, "PublicFoo");
Expand All @@ -126,7 +126,7 @@ public class Foo : Microsoft.AspNetCore.Mvc.ControllerBase
{
public void PublicFoo() { }
}";
var compilation = TestHelper.CompileCS(code, NetStandardMetadataReference.Netstandard.Union(NuGetMetadataReference.MicrosoftAspNetCoreMvcCore(aspNetMvcVersion)).ToArray())
var compilation = TestHelper.CompileCS(code, MetadataReferenceFacade.NetStandard.Union(NuGetMetadataReference.MicrosoftAspNetCoreMvcCore(aspNetMvcVersion)).ToArray())
.Model
.Compilation;
var publicFoo = GetMethodSymbol(compilation, "PublicFoo");
Expand All @@ -145,7 +145,7 @@ public class Foo : Microsoft.AspNetCore.Mvc.ControllerBase
{
public Foo() { }
}";
var (tree, semanticModel) = TestHelper.CompileCS(code, NetStandardMetadataReference.Netstandard.Union(NuGetMetadataReference.MicrosoftAspNetCoreMvcCore(aspNetMvcVersion)).ToArray());
var (tree, semanticModel) = TestHelper.CompileCS(code, MetadataReferenceFacade.NetStandard.Union(NuGetMetadataReference.MicrosoftAspNetCoreMvcCore(aspNetMvcVersion)).ToArray());
var methodSymbol = semanticModel.GetDeclaredSymbol(tree.Single<ConstructorDeclarationSyntax>()) as IMethodSymbol;
methodSymbol.IsControllerMethod().Should().Be(false);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public class RuleTypeTest
public void DetectRuleTypeChanges_VB() =>
DetectTypeChanges(vbnet::SonarAnalyzer.RuleCatalog.Rules, RuleTypeMappingVB.Rules, LanguageNames.VisualBasic, nameof(RuleTypeMappingVB));

[AssertionMethod]
private static void DetectTypeChanges(Dictionary<string, RuleDescriptor> rules, IImmutableDictionary<string, string> expectedTypes, string language, string expectedTypesName)
{
var rulesWithUnmatchingType = Enumerable.Range(1, 10000)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class AssertionArgsShouldBePassedInCorrectOrderTest
public void AssertionArgsShouldBePassedInCorrectOrder_XUnit(string testFwkVersion) =>
builder.AddPaths("AssertionArgsShouldBePassedInCorrectOrder.Xunit.cs")
.AddReferences(NuGetMetadataReference.XunitFramework(testFwkVersion)
.Concat(NetStandardMetadataReference.Netstandard))
.Concat(MetadataReferenceFacade.NetStandard))
.Verify();

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class CertificateValidationCheckTest
private static readonly VerifierBuilder WithReferences = new VerifierBuilder()
.AddReferences(MetadataReferenceFacade.SystemNetHttp)
.AddReferences(MetadataReferenceFacade.SystemSecurityCryptography)
.AddReferences(NetStandardMetadataReference.Netstandard);
.AddReferences(MetadataReferenceFacade.NetStandard);
private readonly VerifierBuilder builderCS = WithReferences.AddAnalyzer(() => new CS.CertificateValidationCheck());
private readonly VerifierBuilder builderVB = WithReferences.AddAnalyzer(() => new VB.CertificateValidationCheck());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public class DeliveringDebugFeaturesInProductionTest

internal static IEnumerable<MetadataReference> AdditionalReferencesForAspNetCore2 =>
Enumerable.Empty<MetadataReference>()
.Concat(NetStandardMetadataReference.Netstandard)
.Concat(MetadataReferenceFacade.NetStandard)
.Concat(NuGetMetadataReference.MicrosoftAspNetCoreDiagnostics(Constants.DotNetCore220Version))
.Concat(NuGetMetadataReference.MicrosoftAspNetCoreDiagnosticsEntityFrameworkCore(Constants.DotNetCore220Version))
.Concat(NuGetMetadataReference.MicrosoftAspNetCoreHttpAbstractions(Constants.DotNetCore220Version))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ public class ExecutingSqlQueriesTest
.Verify();

internal static IEnumerable<MetadataReference> GetReferencesNet46(string sqlServerCeVersion) =>
NetStandardMetadataReference.Netstandard
.Concat(FrameworkMetadataReference.SystemData)
.Concat(FrameworkMetadataReference.SystemDataOracleClient)
.Concat(NuGetMetadataReference.SystemDataSqlServerCe(sqlServerCeVersion))
.Concat(NuGetMetadataReference.MySqlData("8.0.22"))
.Concat(NuGetMetadataReference.MicrosoftDataSqliteCore())
.Concat(NuGetMetadataReference.SystemDataSQLiteCore());
MetadataReferenceFacade.NetStandard
.Concat(FrameworkMetadataReference.SystemData)
.Concat(FrameworkMetadataReference.SystemDataOracleClient)
.Concat(NuGetMetadataReference.SystemDataSqlServerCe(sqlServerCeVersion))
.Concat(NuGetMetadataReference.MySqlData("8.0.22"))
.Concat(NuGetMetadataReference.MicrosoftDataSqliteCore())
.Concat(NuGetMetadataReference.SystemDataSQLiteCore());

#else

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public class RestrictDeserializedTypesTest
[TestMethod]
public void RestrictDeserializedTypesFormatters_Sonar()
{
using var _ = new AssertIgnoreScope(); // EnsureStackState fails an assertion in this test file
sonar.AddPaths("RestrictDeserializedTypes.cs")
.WithOptions(ParseOptionsHelper.FromCSharp8)
.Verify();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public class SkipMigration : Migration
.ExecutionTime().Should().BeLessOrEqualTo(30.Seconds());

private static ImmutableArray<MetadataReference> EntityFrameworkCoreReferences(string entityFrameworkVersion) =>
NetStandardMetadataReference.Netstandard
MetadataReferenceFacade.NetStandard
.Concat(NuGetMetadataReference.MicrosoftEntityFrameworkCoreSqlServer(entityFrameworkVersion))
.Concat(NuGetMetadataReference.MicrosoftEntityFrameworkCoreRelational(entityFrameworkVersion))
.ToImmutableArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void CreateMessage_NoEncoding_SetsEmptyString(bool isTestProject)
CreateBuilder(ProjectType.Product, fileName)
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.VerifyUtilityAnalyzer<FileMetadataInfo>(messages =>
messages.Select(x => Path.GetFileName(x.FilePath)).Should().BeEquivalentTo(expectedFiles));
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
3 changes: 3 additions & 0 deletions analyzers/tests/SonarAnalyzer.Test/SonarAnalyzer.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<IsPackable>false</IsPackable>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<!-- FIXME: This should be likely moved to MetadataReferences -->
<!--
This allows, on AzureDevops builds, to restore the NuGet packages which are not targeting .Net Standard or a compatible version by
usign as fallbacks .Net Framwework 4.0 client profile (net40-client) and Portable profile (portable-net45+win8+wp8+wpa81).
Expand All @@ -17,6 +18,7 @@
<AssetTargetFallback>$(AssetTargetFallback);net40-client;portable-net45+win8+wp8+wpa81;</AssetTargetFallback>
</PropertyGroup>

<!-- FIXME: We should be able to remove this now -->
<ItemGroup Condition="'$(TargetFramework)' != 'net48'">
<!-- Class AspNetCoreMetadataReference needs this FrameworkReference to generate metadata references for ASP.NET Core related test cases. -->
<FrameworkReference Include="Microsoft.AspNetCore.App" />
Expand All @@ -25,6 +27,7 @@
</ItemGroup>

<ItemGroup>
<!-- FIXME: Review what's still needed and what is not -->
<PackageReference Include="altcover" Version="8.6.95" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="FluentAssertions.Analyzers" Version="0.23.0">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

namespace SonarAnalyzer.Test
{
internal static class AnalysisScaffolding
public static class AnalysisScaffolding
{
public static SonarAnalysisContext CreateSonarAnalysisContext() =>
new(Mock.Of<RoslynAnalysisContext>(), ImmutableArray<DiagnosticDescriptor>.Empty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace SonarAnalyzer.Test
{
internal static class Constants
public static class Constants
{
public const string NuGetLatestVersion = "LATEST";
public const string DotNet7Preview = "7.0.0-rc.1.22426.10";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/

using System.Globalization;
using System.Threading;

namespace SonarAnalyzer.Test.Helpers
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,9 @@ public void SetVariable(string name, string value)
Environment.SetEnvironmentVariable(name, value, EnvironmentVariableTarget.Process);
}

#region IDispose implementation

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

private void Dispose(bool disposing)
{
if (disposing && originalValues != null)
if (originalValues != null)
{
foreach (var kvp in originalValues)
{
Expand All @@ -72,7 +64,5 @@ private void Dispose(bool disposing)
originalValues = null;
}
}

#endregion IDispose implementation
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace SonarAnalyzer.Test
{
internal class FixAllDiagnosticProvider : FixAllContext.DiagnosticProvider
public class FixAllDiagnosticProvider : FixAllContext.DiagnosticProvider
{
private readonly IEnumerable<Diagnostic> diagnostics;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@
*/

using System.IO;
using System.Security.Cryptography.Xml;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using SonarAnalyzer.CFG;
using SonarAnalyzer.CFG.Roslyn;
using SonarAnalyzer.Extensions;
using SonarAnalyzer.Test.PackagingTests;
using StyleCop.Analyzers.Lightup;
using CS = Microsoft.CodeAnalysis.CSharp;
using VB = Microsoft.CodeAnalysis.VisualBasic;

namespace SonarAnalyzer.Test
{
internal static class TestHelper
public static class TestHelper
{
public static (SyntaxTree Tree, SemanticModel Model) CompileIgnoreErrorsCS(string snippet, params MetadataReference[] additionalReferences) =>
Compile(snippet, true, AnalyzerLanguage.CSharp, additionalReferences);
Expand Down Expand Up @@ -113,12 +113,6 @@ End Sub
: node.RawKind == (int)VB.SyntaxKind.FunctionBlock || node.RawKind == (int)VB.SyntaxKind.SubBlock;
}

public static bool IsSecurityHotspot(DiagnosticDescriptor diagnostic)
{
var type = RuleTypeMappingCS.Rules.GetValueOrDefault(diagnostic.Id) ?? RuleTypeMappingVB.Rules.GetValueOrDefault(diagnostic.Id);
return type == "SECURITY_HOTSPOT";
}

public static IEnumerable<MetadataReference> ProjectTypeReference(ProjectType projectType) =>
projectType == ProjectType.Test
? NuGetMetadataReference.MSTestTestFrameworkV1 // Any reference to detect a test project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

namespace SonarAnalyzer.Test.TestFramework
{
internal readonly struct ProjectBuilder
public readonly struct ProjectBuilder
{
private readonly Lazy<SolutionBuilder> solution;
private readonly Project project;
Expand Down Expand Up @@ -57,7 +57,7 @@ public ProjectBuilder AddReferences(IEnumerable<MetadataReference> references)
}
if (references.Any(x => x.Display.Contains("\\netstandard")))
{
references = references.Concat(NetStandardMetadataReference.Netstandard);
references = references.Concat(MetadataReferenceFacade.NetStandard);
}
var existingReferences = project.MetadataReferences.ToHashSet();
return FromProject(project.AddMetadataReferences(references.Distinct().Where(x => !existingReferences.Contains(x))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@

namespace SonarAnalyzer.Test.TestFramework
{
/// <summary>
/// Helper class compiles snippets of code on the fly.
/// </summary>
internal class SnippetCompiler
public class SnippetCompiler
{
public Compilation Compilation { get; }
public SyntaxTree SyntaxTree { get; }
Expand Down
Loading

0 comments on commit 9ed994e

Please sign in to comment.