-
Notifications
You must be signed in to change notification settings - Fork 335
/
Copy pathTestPluginManager.cs
220 lines (198 loc) · 8.28 KB
/
TestPluginManager.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#nullable disable
namespace Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework;
using System;
using System.Collections.Generic;
using System.Reflection;
using Utilities;
using ObjectModel;
/// <summary>
/// Manages test plugins information.
/// </summary>
internal class TestPluginManager
{
private static TestPluginManager s_instance;
/// <summary>
/// Gets the singleton instance of TestPluginManager.
/// </summary>
public static TestPluginManager Instance
=> s_instance ??= new TestPluginManager();
/// <summary>
/// Gets data type of test extension with given assembly qualified name.
/// </summary>
/// <param name="extensionTypeName">Assembly qualified name of the test extension</param>
/// <returns>Data type of the test extension</returns>
public static Type GetTestExtensionType(string extensionTypeName)
{
Type extensionType;
try
{
extensionType = Type.GetType(extensionTypeName, true);
}
catch (Exception ex)
{
EqtTrace.Error(
"GetTestExtensionType: Failed to get type for test extension '{0}': {1}",
extensionTypeName,
ex);
throw;
}
return extensionType;
}
/// <summary>
/// Instantiates a given data type.
/// </summary>
/// <typeparam name="T">Return type of the test extension</typeparam>
/// <param name="extensionType">Data type of the extension to be instantiated</param>
/// <returns>Test extension instance</returns>
public static T CreateTestExtension<T>(Type extensionType!!)
{
EqtTrace.Info("TestPluginManager.CreateTestExtension: Attempting to load test extension: " + extensionType);
try
{
object rawPlugin = Activator.CreateInstance(extensionType);
T testExtension = (T)rawPlugin;
return testExtension;
}
catch (Exception ex)
{
if (ex is TargetInvocationException)
{
EqtTrace.Error("TestPluginManager.CreateTestExtension: Could not create instance of type: " + extensionType.ToString() + " Exception: " + ex);
throw;
}
#if NETFRAMEWORK
else if (ex is SystemException)
{
EqtTrace.Error("TestPluginManager.CreateTestExtension: Could not create instance of type: " + extensionType.ToString() + " Exception: " + ex);
throw;
}
#endif
EqtTrace.Error("TestPluginManager.CreateTestExtension: Could not create instance of type: " + extensionType.ToString() + " Exception: " + ex);
throw;
}
}
/// <summary>
/// Retrieves the test extension collections of given extension type.
/// </summary>
/// <typeparam name="TExtension">
/// Type of the required extensions
/// </typeparam>
/// <typeparam name="TMetadata">
/// Type of metadata of required extensions
/// </typeparam>
/// <typeparam name="TMetadata">
/// Concrete type of metadata
/// </typeparam>
/// <param name="endsWithPattern">
/// Pattern used to select files using String.EndsWith
/// </param>
/// <param name="unfiltered">
/// Receives unfiltered list of test extensions
/// </param>
/// <param name="filtered">
/// Receives test extensions filtered by Identifier data
/// </param>
public static void GetSpecificTestExtensions<TPluginInfo, TExtension, IMetadata, TMetadata>(
string endsWithPattern,
out IEnumerable<LazyExtension<TExtension, Dictionary<string, object>>> unfiltered,
out IEnumerable<LazyExtension<TExtension, IMetadata>> filtered) where TMetadata : IMetadata where TPluginInfo : TestPluginInformation
{
var extensions = TestPluginCache.Instance.DiscoverTestExtensions<TPluginInfo, TExtension>(endsWithPattern);
TestPluginManager.GetExtensions<TPluginInfo, TExtension, IMetadata, TMetadata>(extensions, out unfiltered, out filtered);
}
/// <summary>
/// Retrieves the test extension collections of given extension type for the provided extension assembly.
/// </summary>
/// <param name="extensionAssembly">
/// The extension assembly.
/// </param>
/// <typeparam name="TPluginInfo">
/// </typeparam>
/// <typeparam name="TExtension">
/// Type of the required extensions
/// </typeparam>
/// <typeparam name="TMetadata">
/// Type of metadata of required extensions
/// </typeparam>
/// <typeparam name="TMetadata">
/// Concrete type of metadata
/// </typeparam>
/// <param name="unfiltered">
/// Receives unfiltered list of test extensions
/// </param>
/// <param name="filtered">
/// Receives test extensions filtered by Identifier data
/// </param>
/// <param name="skipCache">
/// Skip the extensions cache.
/// </param>
public static void GetTestExtensions<TPluginInfo, TExtension, IMetadata, TMetadata>(
string extensionAssembly,
out IEnumerable<LazyExtension<TExtension, Dictionary<string, object>>> unfiltered,
out IEnumerable<LazyExtension<TExtension, IMetadata>> filtered,
bool skipCache = false) where TMetadata : IMetadata where TPluginInfo : TestPluginInformation
{
var extensions = TestPluginCache.Instance.GetTestExtensions<TPluginInfo, TExtension>(extensionAssembly, skipCache);
TestPluginManager.GetExtensions<TPluginInfo, TExtension, IMetadata, TMetadata>(extensions, out unfiltered, out filtered);
}
/// <summary>
/// Prepares a List of TestPluginInformation>
/// </summary>
/// <typeparam name="T"> Type of TestPluginIInformation. </typeparam>
/// <param name="dictionary"> The dictionary containing plugin identifier data and its info. </param>
/// <returns> Collection of test plugins information </returns>
private static IEnumerable<TestPluginInformation> GetValuesFromDictionary<T>(Dictionary<string, T> dictionary) where T : TestPluginInformation
{
var values = new List<TestPluginInformation>();
foreach (var key in dictionary.Keys)
{
values.Add(dictionary[key]);
}
return values;
}
/// <summary>
/// Gets unfiltered and filtered extensions from the provided test extension collection.
/// </summary>
/// <typeparam name="TPluginInfo">
/// </typeparam>
/// <typeparam name="TExtension">
/// Type of the required extensions
/// </typeparam>
/// <typeparam name="TMetadata">
/// Type of metadata of required extensions
/// </typeparam>
/// <typeparam name="TMetadata">
/// Concrete type of metadata
/// </typeparam>
/// <param name="testPluginInfo">
/// The test extension dictionary.
/// </param>
/// <param name="unfiltered">
/// Receives unfiltered list of test extensions
/// </param>
/// <param name="filtered">
/// Receives test extensions filtered by Identifier data
/// </param>
private static void GetExtensions<TPluginInfo, TExtension, IMetadata, TMetadata>(
Dictionary<string, TPluginInfo> testPluginInfo,
out IEnumerable<LazyExtension<TExtension, Dictionary<string, object>>> unfiltered,
out IEnumerable<LazyExtension<TExtension, IMetadata>> filtered) where TMetadata : IMetadata where TPluginInfo : TestPluginInformation
{
var unfilteredExtensions = new List<LazyExtension<TExtension, Dictionary<string, object>>>();
var filteredExtensions = new List<LazyExtension<TExtension, IMetadata>>();
var testPlugins = TestPluginManager.GetValuesFromDictionary(testPluginInfo);
foreach (var plugin in testPlugins)
{
if (!string.IsNullOrEmpty(plugin.IdentifierData))
{
var testExtension = new LazyExtension<TExtension, IMetadata>(plugin, typeof(TMetadata));
filteredExtensions.Add(testExtension);
}
unfilteredExtensions.Add(new LazyExtension<TExtension, Dictionary<string, object>>(plugin, new Dictionary<string, object>()));
}
unfiltered = unfilteredExtensions;
filtered = filteredExtensions;
}
}