forked from microsoft/vstest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestPluginDiscoverer.cs
More file actions
244 lines (218 loc) · 9.56 KB
/
Copy pathTestPluginDiscoverer.cs
File metadata and controls
244 lines (218 loc) · 9.56 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
// 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.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>
/// 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(new[] { "Microsoft.VisualStudio.TestTools.CppUnitTestFramework.CppUnitTestExtension.dll", "Microsoft.VisualStudio.TestPlatform.Extensions.MSAppContainerAdapter.dll" });
}
/// <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);
string fileLoadErrorMessage = string.Format(CultureInfo.CurrentCulture, CommonResources.FailedToLoadAdapaterFile, file);
TestSessionMessageLogger.Instance.SendMessage(TestMessageLevel.Warning, fileLoadErrorMessage);
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);
}
}
}
/// <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>
private 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());
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);
}
}
}