diff options
author | Mateo Torres-Ruiz <mateoatr@users.noreply.github.com> | 2021-10-21 01:09:26 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-21 01:09:26 +0300 |
commit | bf7aaabc663fdae0cf6d363d49cb6187caed8134 (patch) | |
tree | 712cc82ad0cc3eee71b5c9f5741838d4ccd50980 /test | |
parent | 28ec75e84f7953b50cd21693ecf4b1051d605e90 (diff) |
Use all diagnostic analyzers when running analyzer's tests (#2327)
* Use all analyzers for each linker's test run
* Remove SimpleAnalyzerOptions from CSharpAnalyzerVerifier
Diffstat (limited to 'test')
8 files changed, 109 insertions, 98 deletions
diff --git a/test/ILLink.RoslynAnalyzer.Tests/RequiresAssemblyFilesAnalyzerTests.cs b/test/ILLink.RoslynAnalyzer.Tests/RequiresAssemblyFilesAnalyzerTests.cs index 10da5a5d9..a4bdc5be5 100644 --- a/test/ILLink.RoslynAnalyzer.Tests/RequiresAssemblyFilesAnalyzerTests.cs +++ b/test/ILLink.RoslynAnalyzer.Tests/RequiresAssemblyFilesAnalyzerTests.cs @@ -1147,7 +1147,7 @@ class AnotherImplementation : IRAF } } "; - var compilation = (await CSharpAnalyzerVerifier<RequiresAssemblyFilesAnalyzer>.GetCompilation (references)).EmitToImageReference (); + var compilation = (await TestCaseCompilation.GetCompilation (references)).EmitToImageReference (); await VerifyRequiresAssemblyFilesAnalyzer (src, additionalReferences: new[] { compilation }, // (4,14): warning IL3003: Interface member 'IRAF.Method()' with 'RequiresAssemblyFilesAttribute' has an implementation member 'Implementation.Method()' without 'RequiresAssemblyFilesAttribute'. Attributes must match across all interface implementations or overrides. @@ -1219,7 +1219,7 @@ class AnotherImplementation : IRAF } } "; - var compilation = (await CSharpAnalyzerVerifier<RequiresAssemblyFilesAnalyzer>.GetCompilation (references)).EmitToImageReference (); + var compilation = (await TestCaseCompilation.GetCompilation (references)).EmitToImageReference (); await VerifyRequiresAssemblyFilesAnalyzer (src, additionalReferences: new[] { compilation }, // (7,14): warning IL3003: Member 'Implementation.Method()' with 'RequiresAssemblyFilesAttribute' implements interface member 'IRAF.Method()' without 'RequiresAssemblyFilesAttribute'. Attributes must match across all interface implementations or overrides. diff --git a/test/ILLink.RoslynAnalyzer.Tests/RequiresUnreferencedCodeAnalyzerTests.cs b/test/ILLink.RoslynAnalyzer.Tests/RequiresUnreferencedCodeAnalyzerTests.cs index 74b21fc7e..9cb7d8995 100644 --- a/test/ILLink.RoslynAnalyzer.Tests/RequiresUnreferencedCodeAnalyzerTests.cs +++ b/test/ILLink.RoslynAnalyzer.Tests/RequiresUnreferencedCodeAnalyzerTests.cs @@ -795,7 +795,7 @@ class AnotherImplementation : IRAF } } "; - var compilation = (await CSharpAnalyzerVerifier<RequiresUnreferencedCodeAnalyzer>.GetCompilation (references)).EmitToImageReference (); + var compilation = (await TestCaseCompilation.GetCompilation (references)).EmitToImageReference (); await VerifyRequiresUnreferencedCodeAnalyzer (src, additionalReferences: new[] { compilation }, // (4,14): warning IL2046: Interface member 'IRAF.Method()' with 'RequiresUnreferencedCodeAttribute' has an implementation member 'Implementation.Method()' without 'RequiresUnreferencedCodeAttribute'. Attributes must match across all interface implementations or overrides. @@ -847,7 +847,7 @@ class AnotherImplementation : IRAF } } "; - var compilation = (await CSharpAnalyzerVerifier<RequiresUnreferencedCodeAnalyzer>.GetCompilation (references)).EmitToImageReference (); + var compilation = (await TestCaseCompilation.GetCompilation (references)).EmitToImageReference (); await VerifyRequiresUnreferencedCodeAnalyzer (src, additionalReferences: new[] { compilation }, // (7,14): warning IL2046: Member 'Implementation.Method()' with 'RequiresUnreferencedCodeAttribute' implements interface member 'IRAF.Method()' without 'RequiresUnreferencedCodeAttribute'. Attributes must match across all interface implementations or overrides. diff --git a/test/ILLink.RoslynAnalyzer.Tests/TestCaseCompilation.cs b/test/ILLink.RoslynAnalyzer.Tests/TestCaseCompilation.cs new file mode 100644 index 000000000..01acca13b --- /dev/null +++ b/test/ILLink.RoslynAnalyzer.Tests/TestCaseCompilation.cs @@ -0,0 +1,99 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.Diagnostics; + +namespace ILLink.RoslynAnalyzer.Tests +{ + internal class TestCaseCompilation + { + private static readonly ImmutableArray<DiagnosticAnalyzer> SupportedDiagnosticAnalyzers = + ImmutableArray.Create<DiagnosticAnalyzer> ( + new COMAnalyzer (), + new RequiresAssemblyFilesAnalyzer (), + new RequiresUnreferencedCodeAnalyzer ()); + + public static Task<(CompilationWithAnalyzers Compilation, SemanticModel SemanticModel)> CreateCompilation ( + string src, + (string, string)[]? globalAnalyzerOptions = null, + IEnumerable<MetadataReference>? additionalReferences = null, + IEnumerable<SyntaxTree>? additionalSources = null) + => CreateCompilation (CSharpSyntaxTree.ParseText (src), globalAnalyzerOptions, additionalReferences, additionalSources); + + public static async Task<(CompilationWithAnalyzers Compilation, SemanticModel SemanticModel)> CreateCompilation ( + SyntaxTree src, + (string, string)[]? globalAnalyzerOptions = null, + IEnumerable<MetadataReference>? additionalReferences = null, + IEnumerable<SyntaxTree>? additionalSources = null) + { + var mdRef = MetadataReference.CreateFromFile (typeof (Mono.Linker.Tests.Cases.Expectations.Metadata.BaseMetadataAttribute).Assembly.Location); + additionalReferences ??= Array.Empty<MetadataReference> (); + var sources = new List<SyntaxTree> () { src }; + sources.AddRange (additionalSources ?? Array.Empty<SyntaxTree> ()); + var comp = CSharpCompilation.Create ( + assemblyName: Guid.NewGuid ().ToString ("N"), + syntaxTrees: sources, + references: (await TestCaseUtils.GetNet6References ()).Add (mdRef).AddRange (additionalReferences), + new CSharpCompilationOptions (OutputKind.DynamicallyLinkedLibrary)); + + var analyzerOptions = new AnalyzerOptions ( + ImmutableArray<AdditionalText>.Empty, + new SimpleAnalyzerOptions (globalAnalyzerOptions)); + + var compWithAnalyzerOptions = new CompilationWithAnalyzersOptions ( + analyzerOptions, + (_1, _2, _3) => { }, + concurrentAnalysis: true, + logAnalyzerExecutionTime: false); + + return (new CompilationWithAnalyzers (comp, SupportedDiagnosticAnalyzers, compWithAnalyzerOptions), comp.GetSemanticModel (src)); + } + + public static async Task<Compilation> GetCompilation (string source, IEnumerable<MetadataReference>? additionalReferences = null, IEnumerable<SyntaxTree>? additionalSources = null) + => (await CreateCompilation (source, additionalReferences: additionalReferences ?? Array.Empty<MetadataReference> ())).Compilation.Compilation; + + class SimpleAnalyzerOptions : AnalyzerConfigOptionsProvider + { + public SimpleAnalyzerOptions ((string, string)[]? globalOptions) + { + globalOptions ??= Array.Empty<(string, string)> (); + GlobalOptions = new SimpleAnalyzerConfigOptions (ImmutableDictionary.CreateRange ( + StringComparer.OrdinalIgnoreCase, + globalOptions.Select (x => new KeyValuePair<string, string> (x.Item1, x.Item2)))); + } + + public override AnalyzerConfigOptions GlobalOptions { get; } + + public override AnalyzerConfigOptions GetOptions (SyntaxTree tree) + => SimpleAnalyzerConfigOptions.Empty; + + public override AnalyzerConfigOptions GetOptions (AdditionalText textFile) + => SimpleAnalyzerConfigOptions.Empty; + + class SimpleAnalyzerConfigOptions : AnalyzerConfigOptions + { + public static readonly SimpleAnalyzerConfigOptions Empty = new SimpleAnalyzerConfigOptions (ImmutableDictionary<string, string>.Empty); + + private readonly ImmutableDictionary<string, string> _dict; + public SimpleAnalyzerConfigOptions (ImmutableDictionary<string, string> dict) + { + _dict = dict; + } + + // Suppress warning about missing nullable attributes +#pragma warning disable 8765 + public override bool TryGetValue (string key, out string? value) + => _dict.TryGetValue (key, out value); +#pragma warning restore 8765 + } + } + } +} diff --git a/test/ILLink.RoslynAnalyzer.Tests/TestCaseUtils.cs b/test/ILLink.RoslynAnalyzer.Tests/TestCaseUtils.cs index a1eaa9ecc..fb0c2aef0 100644 --- a/test/ILLink.RoslynAnalyzer.Tests/TestCaseUtils.cs +++ b/test/ILLink.RoslynAnalyzer.Tests/TestCaseUtils.cs @@ -75,9 +75,8 @@ namespace ILLink.RoslynAnalyzer.Tests var testDependenciesSource = GetTestDependencies (testSyntaxTree) .Select (testDependency => CSharpSyntaxTree.ParseText (File.ReadAllText (testDependency))); - var test = new TestChecker (m, CSharpAnalyzerVerifier<TAnalyzer> - .CreateCompilation (testSyntaxTree, MSBuildProperties, additionalSources: testDependenciesSource).Result); - + var compilation = TestCaseCompilation.CreateCompilation (testSyntaxTree, MSBuildProperties, additionalSources: testDependenciesSource); + var test = new TestChecker (m, compilation.Result); test.ValidateAttributes (attrs); } diff --git a/test/ILLink.RoslynAnalyzer.Tests/TestChecker.cs b/test/ILLink.RoslynAnalyzer.Tests/TestChecker.cs index 68563e46e..ce88b0045 100644 --- a/test/ILLink.RoslynAnalyzer.Tests/TestChecker.cs +++ b/test/ILLink.RoslynAnalyzer.Tests/TestChecker.cs @@ -26,15 +26,10 @@ namespace ILLink.RoslynAnalyzer.Tests private readonly SyntaxNode MemberSyntax; - private readonly string TestingAnalyzerName; - public TestChecker (MemberDeclarationSyntax memberSyntax, (CompilationWithAnalyzers Compilation, SemanticModel SemanticModel) compilationResult) { Compilation = compilationResult.Compilation; SemanticModel = compilationResult.SemanticModel; - - // Currently, tests are only run using a single analyzer. - TestingAnalyzerName = Compilation.Analyzers.Single ().GetType ().Name; DiagnosticMessages = Compilation.GetAnalyzerDiagnosticsAsync ().Result .Where (d => { // Filter down to diagnostics which originate from this member. @@ -75,7 +70,7 @@ namespace ILLink.RoslynAnalyzer.Tests var args = TestCaseUtils.GetAttributeArguments (attribute); if (args.TryGetValue ("ProducedBy", out var producedBy)) { // Skip if this warning is not expected to be produced by any of the analyzers that we are currently testing. - return GetProducedBy (producedBy).HasFlag (Enum.Parse<ProducedBy> (TestingAnalyzerName)); + return GetProducedBy (producedBy).HasFlag (ProducedBy.Analyzer); } return true; diff --git a/test/ILLink.RoslynAnalyzer.Tests/Verifiers/CSharpAnalyzerVerifier`1.cs b/test/ILLink.RoslynAnalyzer.Tests/Verifiers/CSharpAnalyzerVerifier`1.cs index a503bbe70..5afc179b8 100644 --- a/test/ILLink.RoslynAnalyzer.Tests/Verifiers/CSharpAnalyzerVerifier`1.cs +++ b/test/ILLink.RoslynAnalyzer.Tests/Verifiers/CSharpAnalyzerVerifier`1.cs @@ -34,51 +34,10 @@ namespace ILLink.RoslynAnalyzer.Tests public static DiagnosticResult Diagnostic (DiagnosticDescriptor descriptor) => CSharpAnalyzerVerifier<TAnalyzer, XUnitVerifier>.Diagnostic (descriptor); - public static Task<(CompilationWithAnalyzers Compilation, SemanticModel SemanticModel)> CreateCompilation ( - string src, - (string, string)[]? globalAnalyzerOptions = null, - IEnumerable<MetadataReference>? additionalReferences = null, - IEnumerable<SyntaxTree>? additionalSources = null) - => CreateCompilation (CSharpSyntaxTree.ParseText (src), globalAnalyzerOptions, additionalReferences, additionalSources); - - public static async Task<Compilation> GetCompilation (string source, IEnumerable<MetadataReference>? additionalReferences = null, IEnumerable<SyntaxTree>? additionalSources = null) - => (await CSharpAnalyzerVerifier<RequiresAssemblyFilesAnalyzer>.CreateCompilation (source, additionalReferences: additionalReferences ?? Array.Empty<MetadataReference> ())).Compilation.Compilation; - - public static async Task<(CompilationWithAnalyzers Compilation, SemanticModel SemanticModel)> CreateCompilation ( - SyntaxTree src, - (string, string)[]? globalAnalyzerOptions = null, - IEnumerable<MetadataReference>? additionalReferences = null, - IEnumerable<SyntaxTree>? additionalSources = null) - { - var mdRef = MetadataReference.CreateFromFile (typeof (Mono.Linker.Tests.Cases.Expectations.Metadata.BaseMetadataAttribute).Assembly.Location); - additionalReferences ??= Array.Empty<MetadataReference> (); - var sources = new List<SyntaxTree> () { src }; - sources.AddRange (additionalSources ?? Array.Empty<SyntaxTree> ()); - var comp = CSharpCompilation.Create ( - assemblyName: Guid.NewGuid ().ToString ("N"), - syntaxTrees: sources, - references: (await TestCaseUtils.GetNet6References ()).Add (mdRef).AddRange (additionalReferences), - new CSharpCompilationOptions (OutputKind.DynamicallyLinkedLibrary)); - - var analyzerOptions = new AnalyzerOptions ( - ImmutableArray<AdditionalText>.Empty, - new SimpleAnalyzerOptions (globalAnalyzerOptions)); - - var compWithAnalyzerOptions = new CompilationWithAnalyzersOptions ( - analyzerOptions, - (_1, _2, _3) => { }, - concurrentAnalysis: true, - logAnalyzerExecutionTime: false); - - var analyzers = ImmutableArray.Create<DiagnosticAnalyzer> (new TAnalyzer ()); - return (new CompilationWithAnalyzers (comp, analyzers, compWithAnalyzerOptions), comp.GetSemanticModel (src)); - } - /// <inheritdoc cref="AnalyzerVerifier{TAnalyzer, TTest, TVerifier}.VerifyAnalyzerAsync(string, DiagnosticResult[])"/> public static async Task VerifyAnalyzerAsync (string src, (string, string)[]? analyzerOptions = null, IEnumerable<MetadataReference>? additionalReferences = null, params DiagnosticResult[] expected) { - var diags = await (await CreateCompilation (src, analyzerOptions, additionalReferences)).Compilation.GetAllDiagnosticsAsync (); - + var diags = await (await TestCaseCompilation.CreateCompilation (src, analyzerOptions, additionalReferences)).Compilation.GetAllDiagnosticsAsync (); var analyzers = ImmutableArray.Create<DiagnosticAnalyzer> (new TAnalyzer ()); VerifyDiagnosticResults (diags, analyzers, expected, DefaultVerifier); } @@ -673,44 +632,6 @@ namespace ILLink.RoslynAnalyzer.Tests return (IReadOnlyList<object?>?) diagnostic.GetType ().GetProperty ("Arguments", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)?.GetValue (diagnostic) ?? Array.Empty<object> (); } - - class SimpleAnalyzerOptions : AnalyzerConfigOptionsProvider - { - public SimpleAnalyzerOptions ((string, string)[]? globalOptions) - { - globalOptions ??= Array.Empty<(string, string)> (); - GlobalOptions = new SimpleAnalyzerConfigOptions (ImmutableDictionary.CreateRange ( - StringComparer.OrdinalIgnoreCase, - globalOptions.Select (x => new KeyValuePair<string, string> (x.Item1, x.Item2)))); - } - - public override AnalyzerConfigOptions GlobalOptions { get; } - - public override AnalyzerConfigOptions GetOptions (SyntaxTree tree) - => SimpleAnalyzerConfigOptions.Empty; - - public override AnalyzerConfigOptions GetOptions (AdditionalText textFile) - => SimpleAnalyzerConfigOptions.Empty; - - class SimpleAnalyzerConfigOptions : AnalyzerConfigOptions - { - public static readonly SimpleAnalyzerConfigOptions Empty = new SimpleAnalyzerConfigOptions (ImmutableDictionary<string, string>.Empty); - - private readonly ImmutableDictionary<string, string> _dict; - public SimpleAnalyzerConfigOptions (ImmutableDictionary<string, string> dict) - { - _dict = dict; - } - - // Suppress warning about missing nullable attributes -#pragma warning disable 8765 - public override bool TryGetValue (string key, out string? value) - => _dict.TryGetValue (key, out value); -#pragma warning restore 8765 - } - } - - } internal static class IEnumerableExtensions diff --git a/test/Mono.Linker.Tests.Cases.Expectations/Assertions/ProducedBy.cs b/test/Mono.Linker.Tests.Cases.Expectations/Assertions/ProducedBy.cs index 6b441975e..fe7d6f967 100644 --- a/test/Mono.Linker.Tests.Cases.Expectations/Assertions/ProducedBy.cs +++ b/test/Mono.Linker.Tests.Cases.Expectations/Assertions/ProducedBy.cs @@ -14,10 +14,7 @@ namespace Mono.Linker.Tests.Cases.Expectations.Assertions public enum ProducedBy { Trimmer = 1, - RequiresAssemblyFileAnalyzer = 2, - RequiresUnreferencedCodeAnalyzer = 4, - COMAnalyzer = 8, - Analyzer = RequiresAssemblyFileAnalyzer | RequiresUnreferencedCodeAnalyzer | COMAnalyzer, + Analyzer = 2, TrimmerAndAnalyzer = Trimmer | Analyzer } }
\ No newline at end of file diff --git a/test/Mono.Linker.Tests.Cases/Interop/PInvoke/Warnings/ComPInvokeWarning.cs b/test/Mono.Linker.Tests.Cases/Interop/PInvoke/Warnings/ComPInvokeWarning.cs index e82d8bc59..d64847908 100644 --- a/test/Mono.Linker.Tests.Cases/Interop/PInvoke/Warnings/ComPInvokeWarning.cs +++ b/test/Mono.Linker.Tests.Cases/Interop/PInvoke/Warnings/ComPInvokeWarning.cs @@ -175,7 +175,7 @@ namespace Mono.Linker.Tests.Cases.Interop.PInvoke.Warnings static extern void CanSuppressPInvokeWithRequiresUnreferencedCode (IFoo foo); [ExpectedWarning ("IL2050")] - [ExpectedWarning ("IL2026", ProducedBy = ProducedBy.Trimmer | ProducedBy.RequiresUnreferencedCodeAnalyzer)] + [ExpectedWarning ("IL2026")] static void Call_PInvokeWithRequiresUnreferencedCode () { PInvokeWithRequiresUnreferencedCode (null); |