From 343c843a3808aa378836bd88c6edfc31203fde40 Mon Sep 17 00:00:00 2001 From: Jonathon Marolf Date: Tue, 23 Jun 2020 10:25:59 -0700 Subject: [PATCH] use testing library to resolve assemblies --- tests/Analyzers/AnalyzerAssemblyGenerator.cs | 39 ++++++------------- tests/Analyzers/FilterDiagnosticsTests.cs | 9 +++-- .../Analyzers/LoadAnalyzersAndFixersTests.cs | 32 ++++++++------- tests/binaries/System.Runtime.dll | 3 -- tests/binaries/netstandard.dll | 3 -- 5 files changed, 33 insertions(+), 53 deletions(-) delete mode 100644 tests/binaries/System.Runtime.dll delete mode 100644 tests/binaries/netstandard.dll diff --git a/tests/Analyzers/AnalyzerAssemblyGenerator.cs b/tests/Analyzers/AnalyzerAssemblyGenerator.cs index eb026dc91e..d0ae19429c 100644 --- a/tests/Analyzers/AnalyzerAssemblyGenerator.cs +++ b/tests/Analyzers/AnalyzerAssemblyGenerator.cs @@ -7,11 +7,13 @@ using System.IO; using System.Linq; using System.Reflection; +using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Testing; namespace Microsoft.CodeAnalysis.Tools.Tests.Analyzers { @@ -70,39 +72,20 @@ public override void Initialize(AnalysisContext context) return CSharpSyntaxTree.ParseText(analyzer); } - public static Assembly GenerateAssembly(params SyntaxTree[] trees) + public static async Task GenerateAssemblyAsync(params SyntaxTree[] trees) { var assemblyName = Guid.NewGuid().ToString(); - var paths = new HashSet + var references = new List() { - typeof(object).Assembly.Location, - typeof(ValueType).Assembly.Location, - typeof(Enumerable).Assembly.Location, - typeof(SharedAttribute).Assembly.Location, - typeof(Task).Assembly.Location, - typeof(ImmutableArray).Assembly.Location, - typeof(LanguageNames).Assembly.Location, - typeof(CSharpCompilation).Assembly.Location, - typeof(DiagnosticAnalyzer).Assembly.Location, - typeof(CodeFixProvider).Assembly.Location + MetadataReference.CreateFromFile(typeof(ImmutableArray).Assembly.Location), + MetadataReference.CreateFromFile(typeof(SharedAttribute).Assembly.Location), + MetadataReference.CreateFromFile(typeof(CSharpCompilation).Assembly.Location), + MetadataReference.CreateFromFile(typeof(DiagnosticAnalyzer).Assembly.Location), + MetadataReference.CreateFromFile(typeof(CodeFixProvider).Assembly.Location), }; - var references = new MetadataReference[paths.Count + 2]; - for (var i = 0; i < paths.Count; i++) - { - references[i] = MetadataReference.CreateFromFile(paths.ElementAt(i)); - } - - var netstandardPath = Path.Combine( - Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), - @"..\..\..\..\..\tests\binaries\netstandard.dll"); - references[paths.Count] = MetadataReference.CreateFromFile(netstandardPath); - - var systemRuntimePath = Path.Combine( - Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), - @"..\..\..\..\..\tests\binaries\System.Runtime.dll"); - references[paths.Count + 1] = MetadataReference.CreateFromFile(systemRuntimePath); - + var netstandardMetaDataReferences = await ReferenceAssemblies.NetStandard.NetStandard20.ResolveAsync(LanguageNames.CSharp, CancellationToken.None); + references.AddRange(netstandardMetaDataReferences); var compilation = CSharpCompilation.Create(assemblyName, trees, references, options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); using (var ms = new MemoryStream()) diff --git a/tests/Analyzers/FilterDiagnosticsTests.cs b/tests/Analyzers/FilterDiagnosticsTests.cs index b48de439e7..65c4ae573e 100644 --- a/tests/Analyzers/FilterDiagnosticsTests.cs +++ b/tests/Analyzers/FilterDiagnosticsTests.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Testing; using Microsoft.CodeAnalysis.Text; using Microsoft.CodeAnalysis.Tools.Analyzers; using Microsoft.CodeAnalysis.Tools.Formatters; @@ -24,7 +25,7 @@ public class FilterDiagnosticsTests : CSharpFormatterTests public async Task TestFilterWarning() { var projects = GetProjects(); - var allAnalyzers = GetAnalyzers(); + var allAnalyzers = await GetAnalyzersAsync(); var formattablePaths = ImmutableHashSet.Create(projects.First().Documents.First().FilePath); var minimumSeverity = DiagnosticSeverity.Warning; var result = await AnalyzerFinderHelpers.FilterBySeverityAsync(projects, @@ -40,7 +41,7 @@ public async Task TestFilterWarning() public async Task TestFilterError() { var projects = GetProjects(); - var allAnalyzers = GetAnalyzers(); + var allAnalyzers = await GetAnalyzersAsync(); var formattablePaths = ImmutableHashSet.Create(projects.First().Documents.First().FilePath); var minimumSeverity = DiagnosticSeverity.Error; var result = await AnalyzerFinderHelpers.FilterBySeverityAsync(projects, @@ -52,11 +53,11 @@ public async Task TestFilterError() Assert.Empty(analyzers); } - private ImmutableArray GetAnalyzers() + private async Task> GetAnalyzersAsync() { var assemblies = new[] { - GenerateAssembly( + await GenerateAssemblyAsync( GenerateAnalyzerCode("DiagnosticAnalyzer1", "DiagnosticAnalyzerId"), GenerateCodeFix("CodeFixProvider1", "DiagnosticAnalyzerId")) }; diff --git a/tests/Analyzers/LoadAnalyzersAndFixersTests.cs b/tests/Analyzers/LoadAnalyzersAndFixersTests.cs index 75f2f0e6fc..e844945853 100644 --- a/tests/Analyzers/LoadAnalyzersAndFixersTests.cs +++ b/tests/Analyzers/LoadAnalyzersAndFixersTests.cs @@ -1,5 +1,7 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System.Threading.Tasks; + using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Tools.Analyzers; @@ -13,11 +15,11 @@ namespace Microsoft.CodeAnalysis.Tools.Tests.Analyzers public class LoadAnalyzersAndFixersTests { [Fact] - public static void TestSingleAnalyzerAndFixer() + public static async Task TestSingleAnalyzerAndFixerAsync() { var assemblies = new[] { - GenerateAssembly( + await GenerateAssemblyAsync( GenerateAnalyzerCode("DiagnosticAnalyzer1", "DiagnosticAnalyzerId"), GenerateCodeFix("CodeFixProvider1", "DiagnosticAnalyzerId")) }; @@ -30,11 +32,11 @@ public static void TestSingleAnalyzerAndFixer() } [Fact] - public static void TestMultipleAnalyzersAndFixers() + public static async Task TestMultipleAnalyzersAndFixersAsync() { var assemblies = new[] { - GenerateAssembly( + await GenerateAssemblyAsync( GenerateAnalyzerCode("DiagnosticAnalyzer1", "DiagnosticAnalyzerId1"), GenerateAnalyzerCode("DiagnosticAnalyzer2", "DiagnosticAnalyzerId2"), GenerateCodeFix("CodeFixProvider1", "DiagnosticAnalyzerId1"), @@ -47,14 +49,14 @@ public static void TestMultipleAnalyzersAndFixers() } [Fact] - public static void TestMultipleAnalyzersAndFixersFromTwoAssemblies() + public static async Task TestMultipleAnalyzersAndFixersFromTwoAssembliesAsync() { var assemblies = new[] { - GenerateAssembly( + await GenerateAssemblyAsync( GenerateAnalyzerCode("DiagnosticAnalyzer1", "DiagnosticAnalyzerId1"), GenerateCodeFix("CodeFixProvider1", "DiagnosticAnalyzerId1")), - GenerateAssembly( + await GenerateAssemblyAsync( GenerateAnalyzerCode("DiagnosticAnalyzer2", "DiagnosticAnalyzerId2"), GenerateCodeFix("CodeFixProvider2", "DiagnosticAnalyzerId2")), }; @@ -64,11 +66,11 @@ public static void TestMultipleAnalyzersAndFixersFromTwoAssemblies() } [Fact] - public static void NonMatchingIds() + public static async Task NonMatchingIdsAsync() { var assemblies = new[] { - GenerateAssembly( + await GenerateAssemblyAsync( GenerateAnalyzerCode("DiagnosticAnalyzer1", "DiagnosticAnalyzerId"), GenerateCodeFix("CodeFixProvider1", "CodeFixProviderId")) }; @@ -78,11 +80,11 @@ public static void NonMatchingIds() } [Fact] - public static void SomeMatchingIds() + public static async Task SomeMatchingIdsAsync() { var assemblies = new[] { - GenerateAssembly( + await GenerateAssemblyAsync( GenerateAnalyzerCode("DiagnosticAnalyzer1", "DiagnosticAnalyzerId1"), GenerateAnalyzerCode("DiagnosticAnalyzer2", "DiagnosticAnalyzerId2"), GenerateCodeFix("CodeFixProvider1", "DiagnosticAnalyzerId1"), @@ -97,11 +99,11 @@ public static void SomeMatchingIds() } [Fact] - public static void SingleIdMapstoMultipleFixers() + public static async Task SingleIdMapstoMultipleFixersAsync() { var assemblies = new[] { - GenerateAssembly( + await GenerateAssemblyAsync( GenerateAnalyzerCode("DiagnosticAnalyzer1", "DiagnosticAnalyzerId1"), GenerateAnalyzerCode("DiagnosticAnalyzer2", "DiagnosticAnalyzerId1"), GenerateCodeFix("CodeFixProvider1", "DiagnosticAnalyzerId1"), @@ -114,11 +116,11 @@ public static void SingleIdMapstoMultipleFixers() } [Fact] - public static void MultipleIdsMaptoSingleFixer() + public static async Task MultipleIdsMaptoSingleFixerAsync() { var assemblies = new[] { - GenerateAssembly( + await GenerateAssemblyAsync( GenerateAnalyzerCode("DiagnosticAnalyzer1", "DiagnosticAnalyzerId1"), GenerateAnalyzerCode("DiagnosticAnalyzer2", "DiagnosticAnalyzerId1"), GenerateCodeFix("CodeFixProvider1", "DiagnosticAnalyzerId1")) diff --git a/tests/binaries/System.Runtime.dll b/tests/binaries/System.Runtime.dll deleted file mode 100644 index 2a946b5153..0000000000 --- a/tests/binaries/System.Runtime.dll +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:9d6f0e2207fb7cc5be1f3f47f23e2f1308c43fe0ad81b66925045badf6ceac56 -size 49016 diff --git a/tests/binaries/netstandard.dll b/tests/binaries/netstandard.dll deleted file mode 100644 index 7df56dd60e..0000000000 --- a/tests/binaries/netstandard.dll +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:e6d5e98d83a4d061b91aa53dcbc0ace72393885dd0e4537d9de82dc299b04d6b -size 106360