Skip to content

Commit

Permalink
use testing library to resolve assemblies
Browse files Browse the repository at this point in the history
  • Loading branch information
jmarolf committed Jun 23, 2020
1 parent 16ff140 commit 343c843
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 53 deletions.
39 changes: 11 additions & 28 deletions tests/Analyzers/AnalyzerAssemblyGenerator.cs
Expand Up @@ -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
{
Expand Down Expand Up @@ -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<Assembly> GenerateAssemblyAsync(params SyntaxTree[] trees)
{
var assemblyName = Guid.NewGuid().ToString();
var paths = new HashSet<string>
var references = new List<MetadataReference>()
{
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())
Expand Down
9 changes: 5 additions & 4 deletions tests/Analyzers/FilterDiagnosticsTests.cs
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -52,11 +53,11 @@ public async Task TestFilterError()
Assert.Empty(analyzers);
}

private ImmutableArray<DiagnosticAnalyzer> GetAnalyzers()
private async Task<ImmutableArray<DiagnosticAnalyzer>> GetAnalyzersAsync()
{
var assemblies = new[]
{
GenerateAssembly(
await GenerateAssemblyAsync(
GenerateAnalyzerCode("DiagnosticAnalyzer1", "DiagnosticAnalyzerId"),
GenerateCodeFix("CodeFixProvider1", "DiagnosticAnalyzerId"))
};
Expand Down
32 changes: 17 additions & 15 deletions 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;
Expand All @@ -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"))
};
Expand All @@ -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"),
Expand All @@ -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")),
};
Expand All @@ -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"))
};
Expand All @@ -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"),
Expand All @@ -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"),
Expand All @@ -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"))
Expand Down
3 changes: 0 additions & 3 deletions tests/binaries/System.Runtime.dll

This file was deleted.

3 changes: 0 additions & 3 deletions tests/binaries/netstandard.dll

This file was deleted.

0 comments on commit 343c843

Please sign in to comment.