-
Notifications
You must be signed in to change notification settings - Fork 359
Expand file tree
/
Copy pathTestPluginDiscoverer.cs
More file actions
322 lines (289 loc) · 13.9 KB
/
Copy pathTestPluginDiscoverer.cs
File metadata and controls
322 lines (289 loc) · 13.9 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilities;
using Microsoft.VisualStudio.TestPlatform.Common.Logging;
using Microsoft.VisualStudio.TestPlatform.Common.Utilities;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using CommonResources = Microsoft.VisualStudio.TestPlatform.Common.Resources.Resources;
namespace Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework;
/// <summary>
/// Discovers test extensions in a directory.
/// </summary>
internal static class TestPluginDiscoverer
{
private static readonly HashSet<string> UnloadableFiles = new();
/// <summary>
/// Files we already told the user about, so that a file that fails for every extension type is
/// reported once per run instead of once per scan. <see cref="TestPluginCache.ClearExtensions"/>
/// empties it, which is what makes this once per run rather than once per process: the runner clears
/// the extension cache before every discovery or run request, so an editor that keeps the runner alive
/// for hours still hears about a broken extension on each request, and the set cannot grow past the
/// files of a single request.
///
/// Paths are compared ignoring case. On Windows two spellings of the same path are the same file and
/// warning about both would be a duplicate the user cannot act on. Elsewhere they can be two files,
/// but the only cost of merging them is one warning less about a file that is broken anyway.
/// </summary>
private static readonly ConcurrentDictionary<string, byte> ReportedFiles = new(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Extensions that are probed speculatively when no other extension was found, see <see cref="AddKnownExtensions"/>.
/// They are absent in every environment except UWP, so failing to load them is expected and is not reported.
/// </summary>
private static readonly string[] KnownExtensions =
{
"Microsoft.VisualStudio.TestTools.CppUnitTestFramework.CppUnitTestExtension.dll",
"Microsoft.VisualStudio.TestPlatform.Extensions.MSAppContainerAdapter.dll",
};
/// <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 test plugin information.
/// </returns>
public static Dictionary<string, TPluginInfo> GetTestExtensionsInformation<TPluginInfo, TExtension>(IEnumerable<string> extensionPaths) where TPluginInfo : TestPluginInformation
{
TPDebug.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())
{
AddKnownExtensions(ref extensionPaths);
}
GetTestExtensionsFromFiles<TPluginInfo, TExtension>(extensionPaths.ToArray(), pluginInfos);
return pluginInfos;
}
private static void AddKnownExtensions(ref IEnumerable<string> extensionPaths)
{
// For C++ UWP adapter, & OLD C# UWP(MSTest V1) adapter
// 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(KnownExtensions);
}
/// <summary>
/// Tells the user that an extension file did not load. Extension scanning is best effort, so this is a
/// warning and never stops the run, but staying silent leaves the user with fewer extensions than they
/// expect and no way to find out why short of re-running with /diag.
/// </summary>
/// <param name="file">The file that failed to load.</param>
private static void ReportExtensionLoadFailure(string file)
{
// Many files are scanned per run, and the same file is scanned once per extension type, so report it once.
if (!ReportedFiles.TryAdd(file, 0))
{
return;
}
// This runs inside a catch block. Reporting a load failure must never turn into a second failure that
// escapes and takes down a run that would otherwise have finished, so swallow anything that goes wrong
// here, for instance a satellite assembly that cannot be resolved while formatting the message.
try
{
string message = string.Format(CultureInfo.CurrentCulture, CommonResources.FailedToLoadAdapaterFile, file);
TestSessionMessageLogger.Instance.SendMessage(TestMessageLevel.Warning, message);
}
catch (Exception e)
{
EqtTrace.Warning("TestPluginDiscoverer: Failed to report the load failure of file '{0}'. Error: {1}", file, e);
}
}
/// <summary>
/// Forgets which files were already reported, so the next request reports them again. Called when the
/// extension cache is cleared, because extensions are then discovered from scratch and a failure the
/// user was told about before the clear is news again.
/// </summary>
internal static void ClearReportedFiles() => ReportedFiles.Clear();
/// <summary>
/// Gets test extension information from the given collection 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>
private static void GetTestExtensionsFromFiles<TPluginInfo, TExtension>(
string[] files,
Dictionary<string, TPluginInfo> pluginInfos)
where TPluginInfo : TestPluginInformation
{
TPDebug.Assert(files != null, "null files");
TPDebug.Assert(pluginInfos != null, "null pluginInfos");
// Scan each of the files for data extensions.
foreach (var file in files)
{
if (UnloadableFiles.Contains(file))
{
continue;
}
try
{
var assemblyName = Path.GetFileNameWithoutExtension(file);
var assembly = Assembly.Load(new AssemblyName(assemblyName));
if (assembly != null)
{
GetTestExtensionsFromAssembly<TPluginInfo, TExtension>(assembly, pluginInfos, file);
}
}
catch (FileLoadException e)
{
EqtTrace.Warning("TestPluginDiscoverer-FileLoadException: Failed to load extensions from file '{0}'. Skipping test extension scan for this file. Error: {1}", file, e);
ReportExtensionLoadFailure(file);
UnloadableFiles.Add(file);
}
catch (Exception e)
{
EqtTrace.Warning("TestPluginDiscoverer: Failed to load extensions from file '{0}'. Skipping test extension scan for this file. Error: {1}", file, e);
// This is the handler that catches FileNotFoundException, which is what Assembly.Load throws when
// the extension, or one of its dependencies, cannot be found. That is a real problem for the user,
// so report it instead of only tracing it. The file is deliberately not added to UnloadableFiles:
// unlike FileLoadException this also catches failures from scanning an assembly that did load, and
// resolution can succeed on a later pass once more extension directories are registered.
//
// The speculatively probed extensions are the exception, they are expected to be missing everywhere
// except UWP and reporting them would warn on every run.
if (!KnownExtensions.Contains(file, StringComparer.OrdinalIgnoreCase))
{
ReportExtensionLoadFailure(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>
/// <param name="filePath">File path of the assembly.</param>
/// <typeparam name="TPluginInfo">
/// Type of Test Plugin Information.
/// </typeparam>
/// <typeparam name="TExtension">
/// Type of Extensions.
/// </typeparam>
internal static void GetTestExtensionsFromAssembly<TPluginInfo, TExtension>(Assembly assembly, Dictionary<string, TPluginInfo> pluginInfos, string filePath)
where TPluginInfo : TestPluginInformation
{
TPDebug.Assert(assembly != null, "null assembly");
TPDebug.Assert(pluginInfos != null, "null pluginInfos");
List<Type> types = new();
Type extension = typeof(TExtension);
try
{
var discoveredExtensions = MetadataReaderExtensionsHelper.DiscoverTestExtensionTypesV2Attribute(assembly, filePath);
if (discoveredExtensions?.Length > 0)
{
types.AddRange(discoveredExtensions);
}
}
catch (Exception e)
{
EqtTrace.Warning("TestPluginDiscoverer: Failed to get types searching for 'TestPlatformExtensionVersionAttribute' from assembly '{0}'. Error: {1}", assembly.FullName, e.ToString());
}
try
{
var typesToLoad = TypesToLoadUtilities.GetTypesToLoad(assembly);
if (typesToLoad?.Any() == true)
{
types.AddRange(typesToLoad);
}
if (types.Count == 0)
{
types.AddRange(assembly.GetTypes().Where(type => type.IsClass && !type.IsAbstract));
}
}
catch (ReflectionTypeLoadException e)
{
EqtTrace.Warning("TestPluginDiscoverer: Failed to get types from assembly '{0}'. Error: {1}", assembly.FullName, e.ToString());
// The assembly itself loaded, but some of its types did not, usually because a dependency is
// missing. Scanning continues with the types that did load, so without this the user silently
// gets fewer extensions than the assembly declares. The file is deliberately not added to
// UnloadableFiles, the types that did load are still worth scanning on the next pass.
ReportExtensionLoadFailure(filePath);
if (e.Types?.Length > 0)
{
// Unloaded types on e.Types are null, make sure we skip them.
types.AddRange(e.Types.Where(type => type != null && type.IsClass && !type.IsAbstract)!);
}
if (e.LoaderExceptions != null)
{
foreach (var ex in e.LoaderExceptions)
{
EqtTrace.Warning("LoaderExceptions: {0}", ex);
}
}
}
if (types != null && types.Count != 0)
{
foreach (var type in types)
{
GetTestExtensionFromType(type, extension, pluginInfos, filePath);
}
}
}
/// <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>
/// <param name="filePath">File path of the assembly.</param>
private static void GetTestExtensionFromType<TPluginInfo>(
Type type,
Type extensionType,
Dictionary<string, TPluginInfo> extensionCollection,
string filePath)
where TPluginInfo : TestPluginInformation
{
if (!extensionType.IsAssignableFrom(type))
{
return;
}
var rawPluginInfo = Activator.CreateInstance(typeof(TPluginInfo), type);
TPDebug.Assert(rawPluginInfo is TPluginInfo, "rawPluginInfo is not of type TPluginInfo");
var pluginInfo = (TPluginInfo)rawPluginInfo;
pluginInfo.FilePath = filePath;
if (pluginInfo == null || pluginInfo.IdentifierData == null)
{
EqtTrace.Error(
"GetTestExtensionFromType: Either PluginInformation is null or PluginInformation doesn't contain IdentifierData for type {0}.", type.FullName);
return;
}
if (extensionCollection.ContainsKey(pluginInfo.IdentifierData))
{
EqtTrace.Warning(
"GetTestExtensionFromType: Discovered multiple test extensions with identifier data '{0}' and type '{1}' inside file '{2}'; keeping the first one '{3}'.",
pluginInfo.IdentifierData, pluginInfo.AssemblyQualifiedName, filePath, extensionCollection[pluginInfo.IdentifierData].AssemblyQualifiedName);
}
else
{
extensionCollection.Add(pluginInfo.IdentifierData, pluginInfo);
EqtTrace.Info("GetTestExtensionFromType: Register extension with identifier data '{0}' and type '{1}' inside file '{2}'",
pluginInfo.IdentifierData, pluginInfo.AssemblyQualifiedName, filePath);
}
}
}