Skip to content

Commit

Permalink
TestFramework: Move test files (#8576)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-mikula-sonarsource committed Jan 25, 2024
1 parent 9ed994e commit e1aadba
Show file tree
Hide file tree
Showing 66 changed files with 357 additions and 260 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@
[assembly: AssemblyDescription("")]

[assembly: InternalsVisibleTo("SonarAnalyzer.Test" + Signing.InternalsVisibleToPublicKey)]
[assembly: InternalsVisibleTo("SonarAnalyzer.TestFramework.Test" + Signing.InternalsVisibleToPublicKey)]
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@
[assembly: AssemblyProduct("SonarAnalyzer")]
[assembly: AssemblyDescription("")]
[assembly: InternalsVisibleTo("SonarAnalyzer.Test" + Signing.InternalsVisibleToPublicKey)]
[assembly: InternalsVisibleTo("SonarAnalyzer.TestFramework.Test" + Signing.InternalsVisibleToPublicKey)]
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/

using SonarAnalyzer.AnalysisContext;
using SonarAnalyzer.Test.TestFramework.Tests;

using CS = Microsoft.CodeAnalysis.CSharp;
using VB = Microsoft.CodeAnalysis.VisualBasic;
Expand Down Expand Up @@ -96,4 +95,37 @@ public void Properties_ArePropagated()
Imports System ' Noncompliant
""")
.Verify();

internal abstract class TestAnalyzer : SonarDiagnosticAnalyzer
{
public static readonly DiagnosticDescriptor Rule = AnalysisScaffolding.CreateDescriptorMain("SDummy");
protected abstract GeneratedCodeRecognizer GeneratedCodeRecognizer { get; }
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
}

[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal class TestAnalyzerCS : TestAnalyzer
{
private readonly Action<SonarAnalysisContext, GeneratedCodeRecognizer> initializeAction;
protected override GeneratedCodeRecognizer GeneratedCodeRecognizer => CSharpGeneratedCodeRecognizer.Instance;

public TestAnalyzerCS(Action<SonarAnalysisContext, GeneratedCodeRecognizer> action) =>
initializeAction = action;

protected override void Initialize(SonarAnalysisContext context) =>
initializeAction(context, GeneratedCodeRecognizer);
}

[DiagnosticAnalyzer(LanguageNames.VisualBasic)]
internal class TestAnalyzerVB : TestAnalyzer
{
private readonly Action<SonarAnalysisContext, GeneratedCodeRecognizer> initializeAction;
protected override GeneratedCodeRecognizer GeneratedCodeRecognizer => VisualBasicGeneratedCodeRecognizer.Instance;

public TestAnalyzerVB(Action<SonarAnalysisContext, GeneratedCodeRecognizer> action) =>
initializeAction = action;

protected override void Initialize(SonarAnalysisContext context) =>
initializeAction(context, GeneratedCodeRecognizer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private static Compilation GetRawCompilation(IEnumerable<MetadataReference> theR
}

private static string CreateMockPath(string mockName) =>
Path.Combine(Paths.TestProjectRoot, "../FrameworkMocks/lib/", mockName);
Path.Combine(Paths.TestsRoot, "FrameworkMocks/lib/", mockName);

private static IEnumerable<MetadataReference> GetAdditionalReferences() =>
MetadataReferenceFacade.MsCorLib.Concat(MetadataReferenceFacade.SystemComponentModelComposition);
Expand Down
7 changes: 0 additions & 7 deletions analyzers/tests/SonarAnalyzer.Test/NuGet.config

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ public void DisablingRequestValidation_CS_WebConfig_SubFolders(string rootDirect
additionalFilePath: AnalysisScaffolding.CreateSonarProjectConfigWithFilesToAnalyze(TestContext, filesToAnalyze.ToArray())).ToList();
allDiagnostics.Should().NotBeEmpty();
var rootWebConfig = Path.Combine(rootDirectory, WebConfig);
VerifyResults(rootWebConfig, allDiagnostics, languageVersion);
DiagnosticVerifier.VerifyFile(rootWebConfig, allDiagnostics, languageVersion);
foreach (var subFolder in subFolders)
{
var subFolderWebConfig = Path.Combine(rootDirectory, subFolder, WebConfig);
VerifyResults(subFolderWebConfig, allDiagnostics, languageVersion);
DiagnosticVerifier.VerifyFile(subFolderWebConfig, allDiagnostics, languageVersion);
}
}

Expand Down Expand Up @@ -159,14 +159,5 @@ public void DisablingRequestValidation_VB_WebConfig()
webConfigPath,
AnalysisScaffolding.CreateSonarProjectConfigWithFilesToAnalyze(TestContext, webConfigPath));
}

// Verifies the results for the given web.config file path.
private static void VerifyResults(string webConfigPath, IList<Diagnostic> allDiagnostics, string languageVersion)
{
var actualIssues = allDiagnostics.Where(d => d.Location.GetLineSpan().Path.EndsWith(webConfigPath)).ToArray();
var fileNameSourceText = new DiagnosticVerifier.File(webConfigPath);
var expectedIssueLocations = fileNameSourceText.ToExpectedIssueLocations();
DiagnosticVerifier.CompareActualToExpected(languageVersion, actualIssues, new[] { expectedIssueLocations }, false);
}
}
}
3 changes: 0 additions & 3 deletions analyzers/tests/SonarAnalyzer.Test/SonarAnalyzer.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@
<Content Include="TestResources\**\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Include="TestFramework\Razor\EmptyProject\**\*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,57 +29,44 @@ public class ProjectBuilderTest
[TestMethod]
public void AddDocument_ValidExtension()
{
EmptyCS.AddDocument("TestCases\\VariableUnused.cs").FindDocument("VariableUnused.cs").Should().NotBeNull();
EmptyVB.AddDocument("TestCases\\VariableUnused.vb").FindDocument("VariableUnused.vb").Should().NotBeNull();
EmptyCS.AddDocument(@"TestCases\ProjectBuilder.AddDocument.cs").FindDocument("ProjectBuilder.AddDocument.cs").Should().NotBeNull();
EmptyVB.AddDocument(@"TestCases\ProjectBuilder.AddDocument.vb").FindDocument("ProjectBuilder.AddDocument.vb").Should().NotBeNull();
}

[TestMethod]
public void AddDocument_MismatchingExtension()
{
Action f;

f = () => EmptyCS.AddDocument("TestCases\\VariableUnused.vb");
f.Should().Throw<ArgumentException>();

f = () => EmptyVB.AddDocument("TestCases\\VariableUnused.cs");
f.Should().Throw<ArgumentException>();
EmptyCS.Invoking(x => x.AddDocument(@"TestCases\ProjectBuilder.AddDocument.vb")).Should().Throw<ArgumentException>();
EmptyVB.Invoking(x => x.AddDocument(@"TestCases\ProjectBuilder.AddDocument.cs")).Should().Throw<ArgumentException>();
}

[TestMethod]
public void AddDocument_InvalidExtension()
{
Action f;

f = () => EmptyCS.AddDocument("TestCases\\VariableUnused.unknown");
f.Should().Throw<ArgumentException>();

f = () => EmptyVB.AddDocument("TestCases\\VariableUnused.unknown");
f.Should().Throw<ArgumentException>();
EmptyCS.Invoking(x => x.AddDocument(@"TestCases\ProjectBuilder.AddDocument.unknown")).Should().Throw<ArgumentException>();
EmptyVB.Invoking(x => x.AddDocument(@"TestCases\ProjectBuilder.AddDocument.unknown")).Should().Throw<ArgumentException>();
}

[TestMethod]
public void AddDocument_SupportsRazorFiles()
{
EmptyCS.AddDocument("TestCases\\UnusedPrivateMember.razor").FindDocument("UnusedPrivateMember.razor").Should().NotBeNull();
EmptyVB.AddDocument("TestCases\\UnusedPrivateMember.razor").FindDocument("UnusedPrivateMember.razor").Should().NotBeNull();
EmptyCS.AddDocument(@"TestCases\ProjectBuilder.AddDocument.razor").FindDocument("ProjectBuilder.AddDocument.razor").Should().NotBeNull();
EmptyVB.AddDocument(@"TestCases\ProjectBuilder.AddDocument.razor").FindDocument("ProjectBuilder.AddDocument.razor").Should().NotBeNull();
}

[TestMethod]
public void AddDocument_CsharpSupportsCshtmlFiles() =>
EmptyCS.AddDocument("TestCases\\UnusedPrivateMember.cshtml").FindDocument("UnusedPrivateMember.cshtml").Should().NotBeNull();
EmptyCS.AddDocument(@"TestCases\ProjectBuilder.AddDocument.cshtml").FindDocument("ProjectBuilder.AddDocument.cshtml").Should().NotBeNull();

[TestMethod]
public void AddDocument_VbnetDoesntSupportCshtmlFiles() =>
new Action(() => EmptyVB.AddDocument("TestCases\\UnusedPrivateMember.cshtml").FindDocument("UnusedPrivateMember.cshtml").Should().NotBeNull())
.Should().Throw<ArgumentException>();
EmptyVB.Invoking(x => x.AddDocument(@"TestCases\ProjectBuilder.AddDocument.cshtml").FindDocument("ProjectBuilder.AddDocument.cshtml")).Should().Throw<ArgumentException>();

[TestMethod]
public void AddDocument_CsharpDoesntSupportVbnetFiles() =>
new Action(() => EmptyCS.AddDocument("TestCases\\UnusedPrivateMember.vbhtml").FindDocument("UnusedPrivateMember.vbhtml").Should().NotBeNull())
.Should().Throw<ArgumentException>();
EmptyCS.Invoking(x => x.AddDocument(@"TestCases\ProjectBuilder.AddDocument.vbhtml").FindDocument("ProjectBuilder.AddDocument.vbhtml").Should().NotBeNull()).Should().Throw<ArgumentException>();

[TestMethod]
public void AddDocument_VbnetDoesntSupportVbnetFiles() =>
new Action(() => EmptyVB.AddDocument("TestCases\\UnusedPrivateMember.vbhtml").FindDocument("UnusedPrivateMember.vbhtml").Should().NotBeNull())
.Should().Throw<ArgumentException>();
EmptyVB.Invoking(x => x.AddDocument(@"TestCases\ProjectBuilder.AddDocument.vbhtml").FindDocument("ProjectBuilder.AddDocument.vbhtml").Should().NotBeNull()).Should().Throw<ArgumentException>();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<!-- 01/2024: Using net8.0-windows breaks Verifier.CompileRazor -->
<TargetFramework>net7.0-windows</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand All @@ -18,11 +19,28 @@

<ItemGroup>
<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>

<ItemGroup>
<Using Include="FluentAssertions" />
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
<Using Include="SonarAnalyzer.Common" />
<Using Include="SonarAnalyzer.Helpers" />
<Using Include="SonarAnalyzer.Test.MetadataReferences" />
<Using Include="SonarAnalyzer.Test.TestFramework" />
</ItemGroup>

<ItemGroup>
<Compile Remove="TestCases\**\*" />
<None Include="TestCases\**\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

// Used for ProjectBuilder assertions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

' Used for ProjectBuilder assertions
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<OutputType>Library</OutputType>
</PropertyGroup>
<ItemGroup>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@using System.Net.Http
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.Web.Virtualization
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public class Sample
{
private int value = 42;
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private class TestDuplicateLocationRule : SonarDiagnosticAnalyzer
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(rule);

protected override void Initialize(SonarAnalysisContext context) =>
context.RegisterNodeAction(CSharpGeneratedCodeRecognizer.Instance, c =>
context.RegisterNodeAction(TestGeneratedCodeRecognizer.Instance, c =>
{
// Duplicate issues from different analyzer versions, see https://github.com/SonarSource/sonar-dotnet/issues/1109
c.ReportIssue(Diagnostic.Create(rule, c.Context.Node.GetLocation()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,8 @@ public void Verify_ParseOptions()
public void Verify_BasePath()
{
DummyCS.AddPaths("Nonexistent.cs").Invoking(x => x.Verify()).Should().Throw<FileNotFoundException>("This file should not exist in TestCases directory.");
DummyCS.AddPaths("ArrayCovariance.cs").Invoking(x => x.Verify()).Should().Throw<UnexpectedDiagnosticException>("File should be found in TestCases directory.");
DummyCS.WithBasePath("TestFramework").AddPaths("Verifier.BasePath.cs").Invoking(x => x.Verify()).Should().NotThrow();
DummyCS.AddPaths("Verifier.BasePathAssertFails.cs").Invoking(x => x.Verify()).Should().Throw<UnexpectedDiagnosticException>("File should be found in TestCases directory.");
DummyCS.WithBasePath("Verifier").AddPaths("Verifier.BasePath.cs").Invoking(x => x.Verify()).Should().NotThrow();
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"version": 1,
"dependencies": {
"net8.0-windows7.0": {
"net7.0-windows7.0": {
"altcover": {
"type": "Direct",
"requested": "[8.6.95, )",
Expand Down Expand Up @@ -459,6 +459,14 @@
"System.Collections.Immutable": "[1.1.37, )"
}
},
"sonaranalyzer.csharp": {
"type": "Project",
"dependencies": {
"Microsoft.CodeAnalysis.CSharp.Workspaces": "[1.3.2, )",
"SonarAnalyzer": "[1.0.0, )",
"System.Collections.Immutable": "[1.1.37, )"
}
},
"sonaranalyzer.testframework": {
"type": "Project",
"dependencies": {
Expand All @@ -473,6 +481,14 @@
"NuGet.Protocol": "[6.8.0, )",
"SonarAnalyzer": "[1.0.0, )"
}
},
"sonaranalyzer.visualbasic": {
"type": "Project",
"dependencies": {
"Microsoft.CodeAnalysis.VisualBasic.Workspaces": "[1.3.2, )",
"SonarAnalyzer": "[1.0.0, )",
"System.Collections.Immutable": "[1.1.37, )"
}
}
}
}
Expand Down
Loading

0 comments on commit e1aadba

Please sign in to comment.