-
Notifications
You must be signed in to change notification settings - Fork 508
/
GenericAnalyzerTest.cs
75 lines (60 loc) · 3.31 KB
/
GenericAnalyzerTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#nullable disable
namespace StyleCop.Analyzers.Test.Verifiers
{
using System;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.VisualStudio.Composition;
internal static class GenericAnalyzerTest
{
internal static readonly ReferenceAssemblies ReferenceAssemblies;
internal static readonly ReferenceAssemblies ReferenceAssembliesNet50;
internal static readonly ReferenceAssemblies ReferenceAssembliesNet60;
internal static readonly ReferenceAssemblies ReferenceAssembliesNet70;
private static readonly Lazy<IExportProviderFactory> ExportProviderFactory;
static GenericAnalyzerTest()
{
string codeAnalysisTestVersion =
typeof(Compilation).Assembly.GetName().Version.Major switch
{
1 => "1.2.1",
2 => "2.8.2",
3 => "3.6.0",
4 => "4.0.1",
_ => throw new InvalidOperationException("Unknown version."),
};
ReferenceAssemblies = ReferenceAssemblies.Default.AddPackages(ImmutableArray.Create(
new PackageIdentity("Microsoft.CodeAnalysis.CSharp", codeAnalysisTestVersion),
new PackageIdentity("System.ValueTuple", "4.5.0")));
ReferenceAssembliesNet50 = ReferenceAssemblies.Net.Net50.AddPackages(ImmutableArray.Create(
new PackageIdentity("Microsoft.CodeAnalysis.CSharp", codeAnalysisTestVersion)));
ReferenceAssembliesNet60 = ReferenceAssemblies.Net.Net60.AddPackages(ImmutableArray.Create(
new PackageIdentity("Microsoft.CodeAnalysis.CSharp", codeAnalysisTestVersion)));
ReferenceAssembliesNet70 = ReferenceAssemblies.Net.Net70.AddPackages(ImmutableArray.Create(
new PackageIdentity("Microsoft.CodeAnalysis.CSharp", codeAnalysisTestVersion)));
ExportProviderFactory = new Lazy<IExportProviderFactory>(
() =>
{
var discovery = new AttributedPartDiscovery(Resolver.DefaultInstance, isNonPublicSupported: true);
var parts = Task.Run(() => discovery.CreatePartsAsync(MefHostServices.DefaultAssemblies)).GetAwaiter().GetResult();
var catalog = ComposableCatalog.Create(Resolver.DefaultInstance).AddParts(parts);
var configuration = CompositionConfiguration.Create(catalog);
var runtimeComposition = RuntimeComposition.CreateRuntimeComposition(configuration);
return runtimeComposition.CreateExportProviderFactory();
},
LazyThreadSafetyMode.ExecutionAndPublication);
}
internal static AdhocWorkspace CreateWorkspace()
{
var exportProvider = ExportProviderFactory.Value.CreateExportProvider();
var host = MefV1HostServices.Create(exportProvider.AsExportProvider());
return new AdhocWorkspace(host);
}
}
}