Skip to content

Commit e1aadba

Browse files
TestFramework: Move test files (#8576)
1 parent 9ed994e commit e1aadba

File tree

66 files changed

+357
-260
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+357
-260
lines changed

analyzers/src/SonarAnalyzer.CSharp/Properties/AssemblyInfo.cs

+1
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@
2727
[assembly: AssemblyDescription("")]
2828

2929
[assembly: InternalsVisibleTo("SonarAnalyzer.Test" + Signing.InternalsVisibleToPublicKey)]
30+
[assembly: InternalsVisibleTo("SonarAnalyzer.TestFramework.Test" + Signing.InternalsVisibleToPublicKey)]

analyzers/src/SonarAnalyzer.VisualBasic/Properties/AssemblyInfo.cs

+1
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@
2626
[assembly: AssemblyProduct("SonarAnalyzer")]
2727
[assembly: AssemblyDescription("")]
2828
[assembly: InternalsVisibleTo("SonarAnalyzer.Test" + Signing.InternalsVisibleToPublicKey)]
29+
[assembly: InternalsVisibleTo("SonarAnalyzer.TestFramework.Test" + Signing.InternalsVisibleToPublicKey)]

analyzers/tests/SonarAnalyzer.Test/AnalysisContext/SonarSemanticModelReportingContextTest.cs

+33-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
*/
2020

2121
using SonarAnalyzer.AnalysisContext;
22-
using SonarAnalyzer.Test.TestFramework.Tests;
2322

2423
using CS = Microsoft.CodeAnalysis.CSharp;
2524
using VB = Microsoft.CodeAnalysis.VisualBasic;
@@ -96,4 +95,37 @@ public void RegistrationIsExecuted_SonarCompilationStartAnalysisContext_VB() =>
9695
Imports System ' Noncompliant
9796
""")
9897
.Verify();
98+
99+
internal abstract class TestAnalyzer : SonarDiagnosticAnalyzer
100+
{
101+
public static readonly DiagnosticDescriptor Rule = AnalysisScaffolding.CreateDescriptorMain("SDummy");
102+
protected abstract GeneratedCodeRecognizer GeneratedCodeRecognizer { get; }
103+
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
104+
}
105+
106+
[DiagnosticAnalyzer(LanguageNames.CSharp)]
107+
internal class TestAnalyzerCS : TestAnalyzer
108+
{
109+
private readonly Action<SonarAnalysisContext, GeneratedCodeRecognizer> initializeAction;
110+
protected override GeneratedCodeRecognizer GeneratedCodeRecognizer => CSharpGeneratedCodeRecognizer.Instance;
111+
112+
public TestAnalyzerCS(Action<SonarAnalysisContext, GeneratedCodeRecognizer> action) =>
113+
initializeAction = action;
114+
115+
protected override void Initialize(SonarAnalysisContext context) =>
116+
initializeAction(context, GeneratedCodeRecognizer);
117+
}
118+
119+
[DiagnosticAnalyzer(LanguageNames.VisualBasic)]
120+
internal class TestAnalyzerVB : TestAnalyzer
121+
{
122+
private readonly Action<SonarAnalysisContext, GeneratedCodeRecognizer> initializeAction;
123+
protected override GeneratedCodeRecognizer GeneratedCodeRecognizer => VisualBasicGeneratedCodeRecognizer.Instance;
124+
125+
public TestAnalyzerVB(Action<SonarAnalysisContext, GeneratedCodeRecognizer> action) =>
126+
initializeAction = action;
127+
128+
protected override void Initialize(SonarAnalysisContext context) =>
129+
initializeAction(context, GeneratedCodeRecognizer);
130+
}
99131
}

analyzers/tests/SonarAnalyzer.Test/Helpers/NetFrameworkVersionProviderTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private static Compilation GetRawCompilation(IEnumerable<MetadataReference> theR
120120
}
121121

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

125125
private static IEnumerable<MetadataReference> GetAdditionalReferences() =>
126126
MetadataReferenceFacade.MsCorLib.Concat(MetadataReferenceFacade.SystemComponentModelComposition);

analyzers/tests/SonarAnalyzer.Test/NuGet.config

-7
This file was deleted.

analyzers/tests/SonarAnalyzer.Test/Rules/Hotspots/DisablingRequestValidationTest.cs

+2-11
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ public void DisablingRequestValidation_CS_WebConfig_SubFolders(string rootDirect
103103
additionalFilePath: AnalysisScaffolding.CreateSonarProjectConfigWithFilesToAnalyze(TestContext, filesToAnalyze.ToArray())).ToList();
104104
allDiagnostics.Should().NotBeEmpty();
105105
var rootWebConfig = Path.Combine(rootDirectory, WebConfig);
106-
VerifyResults(rootWebConfig, allDiagnostics, languageVersion);
106+
DiagnosticVerifier.VerifyFile(rootWebConfig, allDiagnostics, languageVersion);
107107
foreach (var subFolder in subFolders)
108108
{
109109
var subFolderWebConfig = Path.Combine(rootDirectory, subFolder, WebConfig);
110-
VerifyResults(subFolderWebConfig, allDiagnostics, languageVersion);
110+
DiagnosticVerifier.VerifyFile(subFolderWebConfig, allDiagnostics, languageVersion);
111111
}
112112
}
113113

@@ -159,14 +159,5 @@ public void DisablingRequestValidation_VB_WebConfig()
159159
webConfigPath,
160160
AnalysisScaffolding.CreateSonarProjectConfigWithFilesToAnalyze(TestContext, webConfigPath));
161161
}
162-
163-
// Verifies the results for the given web.config file path.
164-
private static void VerifyResults(string webConfigPath, IList<Diagnostic> allDiagnostics, string languageVersion)
165-
{
166-
var actualIssues = allDiagnostics.Where(d => d.Location.GetLineSpan().Path.EndsWith(webConfigPath)).ToArray();
167-
var fileNameSourceText = new DiagnosticVerifier.File(webConfigPath);
168-
var expectedIssueLocations = fileNameSourceText.ToExpectedIssueLocations();
169-
DiagnosticVerifier.CompareActualToExpected(languageVersion, actualIssues, new[] { expectedIssueLocations }, false);
170-
}
171162
}
172163
}

analyzers/tests/SonarAnalyzer.Test/SonarAnalyzer.Test.csproj

-3
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@
5858
<Content Include="TestResources\**\*">
5959
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
6060
</Content>
61-
<None Include="TestFramework\Razor\EmptyProject\**\*">
62-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
63-
</None>
6461
</ItemGroup>
6562

6663
<ItemGroup>

analyzers/tests/SonarAnalyzer.Test/TestFramework/Tests/TestAnalyzer.cs

-56
This file was deleted.

analyzers/tests/SonarAnalyzer.Test/TestFramework/Tests/ProjectBuilderTest.cs analyzers/tests/SonarAnalyzer.TestFramework.Test/Compilation/ProjectBuilderTest.cs

+12-25
Original file line numberDiff line numberDiff line change
@@ -29,57 +29,44 @@ public class ProjectBuilderTest
2929
[TestMethod]
3030
public void AddDocument_ValidExtension()
3131
{
32-
EmptyCS.AddDocument("TestCases\\VariableUnused.cs").FindDocument("VariableUnused.cs").Should().NotBeNull();
33-
EmptyVB.AddDocument("TestCases\\VariableUnused.vb").FindDocument("VariableUnused.vb").Should().NotBeNull();
32+
EmptyCS.AddDocument(@"TestCases\ProjectBuilder.AddDocument.cs").FindDocument("ProjectBuilder.AddDocument.cs").Should().NotBeNull();
33+
EmptyVB.AddDocument(@"TestCases\ProjectBuilder.AddDocument.vb").FindDocument("ProjectBuilder.AddDocument.vb").Should().NotBeNull();
3434
}
3535

3636
[TestMethod]
3737
public void AddDocument_MismatchingExtension()
3838
{
39-
Action f;
40-
41-
f = () => EmptyCS.AddDocument("TestCases\\VariableUnused.vb");
42-
f.Should().Throw<ArgumentException>();
43-
44-
f = () => EmptyVB.AddDocument("TestCases\\VariableUnused.cs");
45-
f.Should().Throw<ArgumentException>();
39+
EmptyCS.Invoking(x => x.AddDocument(@"TestCases\ProjectBuilder.AddDocument.vb")).Should().Throw<ArgumentException>();
40+
EmptyVB.Invoking(x => x.AddDocument(@"TestCases\ProjectBuilder.AddDocument.cs")).Should().Throw<ArgumentException>();
4641
}
4742

4843
[TestMethod]
4944
public void AddDocument_InvalidExtension()
5045
{
51-
Action f;
52-
53-
f = () => EmptyCS.AddDocument("TestCases\\VariableUnused.unknown");
54-
f.Should().Throw<ArgumentException>();
55-
56-
f = () => EmptyVB.AddDocument("TestCases\\VariableUnused.unknown");
57-
f.Should().Throw<ArgumentException>();
46+
EmptyCS.Invoking(x => x.AddDocument(@"TestCases\ProjectBuilder.AddDocument.unknown")).Should().Throw<ArgumentException>();
47+
EmptyVB.Invoking(x => x.AddDocument(@"TestCases\ProjectBuilder.AddDocument.unknown")).Should().Throw<ArgumentException>();
5848
}
5949

6050
[TestMethod]
6151
public void AddDocument_SupportsRazorFiles()
6252
{
63-
EmptyCS.AddDocument("TestCases\\UnusedPrivateMember.razor").FindDocument("UnusedPrivateMember.razor").Should().NotBeNull();
64-
EmptyVB.AddDocument("TestCases\\UnusedPrivateMember.razor").FindDocument("UnusedPrivateMember.razor").Should().NotBeNull();
53+
EmptyCS.AddDocument(@"TestCases\ProjectBuilder.AddDocument.razor").FindDocument("ProjectBuilder.AddDocument.razor").Should().NotBeNull();
54+
EmptyVB.AddDocument(@"TestCases\ProjectBuilder.AddDocument.razor").FindDocument("ProjectBuilder.AddDocument.razor").Should().NotBeNull();
6555
}
6656

6757
[TestMethod]
6858
public void AddDocument_CsharpSupportsCshtmlFiles() =>
69-
EmptyCS.AddDocument("TestCases\\UnusedPrivateMember.cshtml").FindDocument("UnusedPrivateMember.cshtml").Should().NotBeNull();
59+
EmptyCS.AddDocument(@"TestCases\ProjectBuilder.AddDocument.cshtml").FindDocument("ProjectBuilder.AddDocument.cshtml").Should().NotBeNull();
7060

7161
[TestMethod]
7262
public void AddDocument_VbnetDoesntSupportCshtmlFiles() =>
73-
new Action(() => EmptyVB.AddDocument("TestCases\\UnusedPrivateMember.cshtml").FindDocument("UnusedPrivateMember.cshtml").Should().NotBeNull())
74-
.Should().Throw<ArgumentException>();
63+
EmptyVB.Invoking(x => x.AddDocument(@"TestCases\ProjectBuilder.AddDocument.cshtml").FindDocument("ProjectBuilder.AddDocument.cshtml")).Should().Throw<ArgumentException>();
7564

7665
[TestMethod]
7766
public void AddDocument_CsharpDoesntSupportVbnetFiles() =>
78-
new Action(() => EmptyCS.AddDocument("TestCases\\UnusedPrivateMember.vbhtml").FindDocument("UnusedPrivateMember.vbhtml").Should().NotBeNull())
79-
.Should().Throw<ArgumentException>();
67+
EmptyCS.Invoking(x => x.AddDocument(@"TestCases\ProjectBuilder.AddDocument.vbhtml").FindDocument("ProjectBuilder.AddDocument.vbhtml").Should().NotBeNull()).Should().Throw<ArgumentException>();
8068

8169
[TestMethod]
8270
public void AddDocument_VbnetDoesntSupportVbnetFiles() =>
83-
new Action(() => EmptyVB.AddDocument("TestCases\\UnusedPrivateMember.vbhtml").FindDocument("UnusedPrivateMember.vbhtml").Should().NotBeNull())
84-
.Should().Throw<ArgumentException>();
71+
EmptyVB.Invoking(x => x.AddDocument(@"TestCases\ProjectBuilder.AddDocument.vbhtml").FindDocument("ProjectBuilder.AddDocument.vbhtml").Should().NotBeNull()).Should().Throw<ArgumentException>();
8572
}

analyzers/tests/SonarAnalyzer.TestFramework.Test/SonarAnalyzer.TestFramework.Test.csproj

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0-windows</TargetFramework>
4+
<!-- 01/2024: Using net8.0-windows breaks Verifier.CompileRazor -->
5+
<TargetFramework>net7.0-windows</TargetFramework>
56
</PropertyGroup>
67

78
<ItemGroup>
@@ -18,11 +19,28 @@
1819

1920
<ItemGroup>
2021
<ProjectReference Include="..\SonarAnalyzer.TestFramework\SonarAnalyzer.TestFramework.csproj" />
22+
<ProjectReference Include="..\..\src\SonarAnalyzer.CSharp\SonarAnalyzer.CSharp.csproj">
23+
<Aliases>global,csharp</Aliases>
24+
</ProjectReference>
25+
<ProjectReference Include="..\..\src\SonarAnalyzer.VisualBasic\SonarAnalyzer.VisualBasic.csproj">
26+
<Aliases>global,vbnet</Aliases>
27+
</ProjectReference>
2128
</ItemGroup>
2229

2330
<ItemGroup>
2431
<Using Include="FluentAssertions" />
2532
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
33+
<Using Include="SonarAnalyzer.Common" />
34+
<Using Include="SonarAnalyzer.Helpers" />
35+
<Using Include="SonarAnalyzer.Test.MetadataReferences" />
36+
<Using Include="SonarAnalyzer.Test.TestFramework" />
37+
</ItemGroup>
38+
39+
<ItemGroup>
40+
<Compile Remove="TestCases\**\*" />
41+
<None Include="TestCases\**\*">
42+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
43+
</None>
2644
</ItemGroup>
2745

2846
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+

2+
// Used for ProjectBuilder assertions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
' Used for ProjectBuilder assertions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
<PropertyGroup>
3+
<TargetFramework>net7.0</TargetFramework>
4+
<LangVersion>latest</LangVersion>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<OutputType>Library</OutputType>
8+
</PropertyGroup>
9+
<ItemGroup>
10+
</ItemGroup>
11+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@using System.Net.Http
2+
@using Microsoft.AspNetCore.Authorization
3+
@using Microsoft.AspNetCore.Components.Authorization
4+
@using Microsoft.AspNetCore.Components.Forms
5+
@using Microsoft.AspNetCore.Components.Routing
6+
@using Microsoft.AspNetCore.Components.Web
7+
@using Microsoft.AspNetCore.Components.Web.Virtualization
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public class Sample
2+
{
3+
private int value = 42;
4+
}

analyzers/tests/SonarAnalyzer.Test/TestFramework/Tests/CodeFixProviderTest.cs analyzers/tests/SonarAnalyzer.TestFramework.Test/Verification/CodeFixProviderTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private class TestDuplicateLocationRule : SonarDiagnosticAnalyzer
4949
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(rule);
5050

5151
protected override void Initialize(SonarAnalysisContext context) =>
52-
context.RegisterNodeAction(CSharpGeneratedCodeRecognizer.Instance, c =>
52+
context.RegisterNodeAction(TestGeneratedCodeRecognizer.Instance, c =>
5353
{
5454
// Duplicate issues from different analyzer versions, see https://github.com/SonarSource/sonar-dotnet/issues/1109
5555
c.ReportIssue(Diagnostic.Create(rule, c.Context.Node.GetLocation()));

analyzers/tests/SonarAnalyzer.Test/TestFramework/Tests/VerifierTest.cs analyzers/tests/SonarAnalyzer.TestFramework.Test/Verification/VerifierTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,8 @@ public void Verify_ParseOptions()
432432
public void Verify_BasePath()
433433
{
434434
DummyCS.AddPaths("Nonexistent.cs").Invoking(x => x.Verify()).Should().Throw<FileNotFoundException>("This file should not exist in TestCases directory.");
435-
DummyCS.AddPaths("ArrayCovariance.cs").Invoking(x => x.Verify()).Should().Throw<UnexpectedDiagnosticException>("File should be found in TestCases directory.");
436-
DummyCS.WithBasePath("TestFramework").AddPaths("Verifier.BasePath.cs").Invoking(x => x.Verify()).Should().NotThrow();
435+
DummyCS.AddPaths("Verifier.BasePathAssertFails.cs").Invoking(x => x.Verify()).Should().Throw<UnexpectedDiagnosticException>("File should be found in TestCases directory.");
436+
DummyCS.WithBasePath("Verifier").AddPaths("Verifier.BasePath.cs").Invoking(x => x.Verify()).Should().NotThrow();
437437
}
438438

439439
[TestMethod]

analyzers/tests/SonarAnalyzer.TestFramework.Test/packages.lock.json

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"version": 1,
33
"dependencies": {
4-
"net8.0-windows7.0": {
4+
"net7.0-windows7.0": {
55
"altcover": {
66
"type": "Direct",
77
"requested": "[8.6.95, )",
@@ -459,6 +459,14 @@
459459
"System.Collections.Immutable": "[1.1.37, )"
460460
}
461461
},
462+
"sonaranalyzer.csharp": {
463+
"type": "Project",
464+
"dependencies": {
465+
"Microsoft.CodeAnalysis.CSharp.Workspaces": "[1.3.2, )",
466+
"SonarAnalyzer": "[1.0.0, )",
467+
"System.Collections.Immutable": "[1.1.37, )"
468+
}
469+
},
462470
"sonaranalyzer.testframework": {
463471
"type": "Project",
464472
"dependencies": {
@@ -473,6 +481,14 @@
473481
"NuGet.Protocol": "[6.8.0, )",
474482
"SonarAnalyzer": "[1.0.0, )"
475483
}
484+
},
485+
"sonaranalyzer.visualbasic": {
486+
"type": "Project",
487+
"dependencies": {
488+
"Microsoft.CodeAnalysis.VisualBasic.Workspaces": "[1.3.2, )",
489+
"SonarAnalyzer": "[1.0.0, )",
490+
"System.Collections.Immutable": "[1.1.37, )"
491+
}
476492
}
477493
}
478494
}

0 commit comments

Comments
 (0)