/
TestPluginManager.cs
251 lines (224 loc) · 9.67 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilities;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
/// <summary>
/// Manages test plugins information.
/// </summary>
internal class TestPluginManager
{
private static TestPluginManager instance;
#region Public Static Methods
/// <summary>
/// Gets the singleton instance of TestPluginManager.
/// </summary>
public static TestPluginManager Instance
{
get
{
if (instance == null)
{
instance = new TestPluginManager();
}
return instance;
}
}
/// <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 = null;
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)
{
if (extensionType == null)
{
throw new ArgumentNullException("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)
{
if (EqtTrace.IsErrorEnabled)
{
EqtTrace.Error("TestPluginManager.CreateTestExtension: Could not create instance of type: " + extensionType.ToString() + " Exception: " + ex);
}
throw;
}
#if NETFRAMEWORK
else if (ex is SystemException)
{
if (EqtTrace.IsErrorEnabled)
{
EqtTrace.Error("TestPluginManager.CreateTestExtension: Could not create instance of type: " + extensionType.ToString() + " Exception: " + ex);
}
throw;
}
#endif
if (EqtTrace.IsErrorEnabled)
{
EqtTrace.Error("TestPluginManager.CreateTestExtension: Could not create instance of type: " + extensionType.ToString() + " Exception: " + ex);
}
throw;
}
}
#endregion
#region Public Methods
/// <summary>
/// Retrieves the test extension collections of given extension type.
/// </summary>
/// <typeparam name="IExtension">
/// Type of the required extensions
/// </typeparam>
/// <typeparam name="IMetadata">
/// 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 void GetSpecificTestExtensions<TPluginInfo, IExtension, IMetadata, TMetadata>(
string endsWithPattern,
out IEnumerable<LazyExtension<IExtension, Dictionary<string, object>>> unfiltered,
out IEnumerable<LazyExtension<IExtension, IMetadata>> filtered) where TMetadata : IMetadata where TPluginInfo : TestPluginInformation
{
var extensions = TestPluginCache.Instance.DiscoverTestExtensions<TPluginInfo, IExtension>(endsWithPattern);
this.GetExtensions<TPluginInfo, IExtension, 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="IExtension">
/// Type of the required extensions
/// </typeparam>
/// <typeparam name="IMetadata">
/// 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>
public void GetTestExtensions<TPluginInfo, IExtension, IMetadata, TMetadata>(
string extensionAssembly,
out IEnumerable<LazyExtension<IExtension, Dictionary<string, object>>> unfiltered,
out IEnumerable<LazyExtension<IExtension, IMetadata>> filtered) where TMetadata : IMetadata where TPluginInfo : TestPluginInformation
{
var extensions = TestPluginCache.Instance.GetTestExtensions<TPluginInfo, IExtension>(extensionAssembly);
this.GetExtensions<TPluginInfo, IExtension, IMetadata, TMetadata>(extensions, out unfiltered, out filtered);
}
#endregion
#region Private Methods
/// <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 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="IExtension">
/// Type of the required extensions
/// </typeparam>
/// <typeparam name="IMetadata">
/// 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 void GetExtensions<TPluginInfo, IExtension, IMetadata, TMetadata>(
Dictionary<string, TPluginInfo> testPluginInfo,
out IEnumerable<LazyExtension<IExtension, Dictionary<string, object>>> unfiltered,
out IEnumerable<LazyExtension<IExtension, IMetadata>> filtered) where TMetadata : IMetadata where TPluginInfo : TestPluginInformation
{
var unfilteredExtensions = new List<LazyExtension<IExtension, Dictionary<string, object>>>();
var filteredExtensions = new List<LazyExtension<IExtension, IMetadata>>();
var testPlugins = this.GetValuesFromDictionary(testPluginInfo);
foreach (var plugin in testPlugins)
{
if (!string.IsNullOrEmpty(plugin.IdentifierData))
{
var testExtension = new LazyExtension<IExtension, IMetadata>(plugin, typeof(TMetadata));
filteredExtensions.Add(testExtension);
}
unfilteredExtensions.Add(new LazyExtension<IExtension, Dictionary<string, object>>(plugin, new Dictionary<string, object>()));
}
unfiltered = unfilteredExtensions;
filtered = filteredExtensions;
}
#endregion
}
}