forked from microsoft/vstest
/
TestPluginDiscoverer.cs
264 lines (234 loc) · 10.7 KB
/
TestPluginDiscoverer.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
252
253
254
255
256
257
258
259
260
261
262
263
264
// 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.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml;
using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilities;
using Microsoft.VisualStudio.TestPlatform.Common.Logging;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;
using CommonResources = Microsoft.VisualStudio.TestPlatform.Common.Resources.Resources;
/// <summary>
/// Discovers test extensions in a directory.
/// </summary>
internal class TestPluginDiscoverer
{
private IFileHelper fileHelper;
private static List<string> UnloadableFiles = new List<string>();
/// <summary>
/// Initializes a new instance of the <see cref="TestPluginDiscoverer"/> class.
/// </summary>
public TestPluginDiscoverer() : this(new FileHelper())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="TestPluginDiscoverer"/> class.
/// </summary>
/// <param name="fileHelper">
/// The file Helper.
/// </param>
internal TestPluginDiscoverer(IFileHelper fileHelper)
{
this.fileHelper = fileHelper;
}
#region Fields
#if WINDOWS_UAP
private static HashSet<string> platformAssemblies = new HashSet<string>(new string[] {
"MICROSOFT.VISUALSTUDIO.TESTPLATFORM.UNITTESTFRAMEWORK.DLL",
"MICROSOFT.VISUALSTUDIO.TESTPLATFORM.TESTEXECUTOR.CORE.DLL",
"MICROSOFT.VISUALSTUDIO.TESTPLATFORM.OBJECTMODEL.DLL",
"VSTEST_EXECUTIONENGINE_PLATFORMBRIDGE.DLL",
"VSTEST_EXECUTIONENGINE_PLATFORMBRIDGE.WINMD",
"VSTEST.EXECUTIONENGINE.WINDOWSPHONE.DLL",
"MICROSOFT.CSHARP.DLL",
"MICROSOFT.VISUALBASIC.DLL",
"CLRCOMPRESSION.DLL",
});
private const string SYSTEM_ASSEMBLY_PREFIX = "system.";
#endif
#endregion
#region Public Methods
/// <summary>
/// Gets information about each of the test extensions available.
/// </summary>
/// <param name="extensionPaths">
/// The path to the extensions.
/// </param>
/// <returns>
/// A dictionary of assembly qualified name and testplugin information.
/// </returns>
public Dictionary<string, TPluginInfo> GetTestExtensionsInformation<TPluginInfo, TExtension>(IEnumerable<string> extensionPaths) where TPluginInfo : TestPluginInformation
{
Debug.Assert(extensionPaths != null);
var pluginInfos = new Dictionary<string, TPluginInfo>();
// C++ UWP adapters do not follow TestAdapater naming convention, so making this exception
if (!extensionPaths.Any())
{
this.AddKnownExtensions(ref extensionPaths);
}
this.GetTestExtensionsFromFiles<TPluginInfo, TExtension>(extensionPaths.ToArray(), pluginInfos);
return pluginInfos;
}
#endregion
#region Private Methods
private void AddKnownExtensions(ref IEnumerable<string> extensionPaths)
{
// For C++ UWP adatper, & OLD C# UWP(MSTest V1) adatper
// In UWP .Net Native Compilation mode managed dll's are packaged differently, & File.Exists() fails.
// Include these two dll's if so far no adapters(extensions) were found, & let Assembly.Load() fail if they are not present.
extensionPaths = extensionPaths.Concat(new[] { "Microsoft.VisualStudio.TestTools.CppUnitTestFramework.CppUnitTestExtension.dll", "Microsoft.VisualStudio.TestPlatform.Extensions.MSAppContainerAdapter.dll" });
}
/// <summary>
/// Gets test extension information from the given colletion of files.
/// </summary>
/// <typeparam name="TPluginInfo">
/// Type of Test Plugin Information.
/// </typeparam>
/// <typeparam name="TExtension">
/// Type of extension.
/// </typeparam>
/// <param name="files">
/// List of dll's to check for test extension availability
/// </param>
/// <param name="pluginInfos">
/// Test plugins collection to add to.
/// </param>
[SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods", MessageId = "System.Reflection.Assembly.LoadFrom")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "We would like to continue discovering all plugins even if some dll in Extensions folder is not able to be load properly")]
private void GetTestExtensionsFromFiles<TPluginInfo, TExtension>(
string[] files,
Dictionary<string, TPluginInfo> pluginInfos) where TPluginInfo : TestPluginInformation
{
Debug.Assert(files != null, "null files");
Debug.Assert(pluginInfos != null, "null pluginInfos");
// Scan each of the files for data extensions.
foreach (var file in files)
{
if(UnloadableFiles.Contains(file))
{
continue;
}
try
{
Assembly assembly = null;
var assemblyName = Path.GetFileNameWithoutExtension(file);
assembly = Assembly.Load(new AssemblyName(assemblyName));
if (assembly != null)
{
this.GetTestExtensionsFromAssembly<TPluginInfo, TExtension>(assembly, pluginInfos);
}
}
catch (Exception e)
{
EqtTrace.Warning("TestPluginDiscoverer: Failed to load extensions from file '{0}'. Skipping test extension scan for this file. Error: {1}", file, e);
if (e is FileLoadException)
{
string fileLoadErrorMessage = string.Format(CultureInfo.CurrentUICulture, CommonResources.FailedToLoadAdapaterFile, file);
TestSessionMessageLogger.Instance.SendMessage(TestMessageLevel.Warning, fileLoadErrorMessage);
UnloadableFiles.Add(file);
}
}
}
}
/// <summary>
/// Gets test extensions from a given assembly.
/// </summary>
/// <param name="assembly">Assembly to check for test extension availability</param>
/// <param name="pluginInfos">Test extensions collection to add to.</param>
/// <typeparam name="TPluginInfo">
/// Type of Test Plugin Information.
/// </typeparam>
/// <typeparam name="TExtension">
/// Type of Extensions.
/// </typeparam>
private void GetTestExtensionsFromAssembly<TPluginInfo, TExtension>(Assembly assembly, Dictionary<string, TPluginInfo> pluginInfos) where TPluginInfo : TestPluginInformation
{
Debug.Assert(assembly != null, "null assembly");
Debug.Assert(pluginInfos != null, "null pluginInfos");
Type[] types;
try
{
types = assembly.GetTypes();
}
catch (ReflectionTypeLoadException e)
{
EqtTrace.Warning("TestPluginDiscoverer: Failed to get types from assembly '{0}'. Skipping test extension scan for this assembly. Error: {1}", assembly.FullName, e.ToString());
if (e.LoaderExceptions != null)
{
foreach (var ex in e.LoaderExceptions)
{
EqtTrace.Warning("LoaderExceptions: {0}", ex);
}
}
return;
}
if ((types != null) && (types.Length > 0))
{
foreach (var type in types)
{
if (type.GetTypeInfo().IsClass && !type.GetTypeInfo().IsAbstract)
{
this.GetTestExtensionFromType(type, typeof(TExtension), pluginInfos);
}
}
}
}
/// <summary>
/// Attempts to find a test extension from given type.
/// </summary>
/// <typeparam name="TPluginInfo">
/// Type of the test plugin information
/// </typeparam>
/// <param name="type">
/// Type to inspect for being test extension
/// </param>
/// <param name="extensionType">
/// Test extension type to look for.
/// </param>
/// <param name="extensionCollection">
/// Test extensions collection to add to.
/// </param>
private void GetTestExtensionFromType<TPluginInfo>(
Type type,
Type extensionType,
Dictionary<string, TPluginInfo> extensionCollection)
where TPluginInfo : TestPluginInformation
{
if (extensionType.GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()))
{
var rawPluginInfo = Activator.CreateInstance(typeof(TPluginInfo), type);
var pluginInfo = (TPluginInfo)rawPluginInfo;
if (pluginInfo == null || pluginInfo.IdentifierData == null)
{
if (EqtTrace.IsErrorEnabled)
{
EqtTrace.Error(
"TryGetTestExtensionFromType: Either PluginInformation is null or PluginInformation doesn't contain IdentifierData for type {0}.", type.FullName);
}
return;
}
if (extensionCollection.ContainsKey(pluginInfo.IdentifierData))
{
EqtTrace.Error(
"TryGetTestExtensionFromType: Discovered multiple test extensions with identifier data '{0}'; keeping the first one.",
pluginInfo.IdentifierData);
}
else
{
extensionCollection.Add(pluginInfo.IdentifierData, pluginInfo);
}
}
}
#endregion
}
}