-
Notifications
You must be signed in to change notification settings - Fork 758
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
xunit/visualstudio.xunit#317: Slow performance on discovery / running…
… due to discovering TestReporters
- Loading branch information
1 parent
b229a8d
commit f835a7d
Showing
3 changed files
with
106 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/xunit.runner.utility/Reporters/RunnerReporterUtility.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#if NETFRAMEWORK || NETCOREAPP || NETSTANDARD1_5_OR_GREATER | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace Xunit | ||
{ | ||
/// <summary> | ||
/// A utility class for finding runner reporters. | ||
/// </summary> | ||
public static class RunnerReporterUtility | ||
{ | ||
/// <summary> | ||
/// Gets a list of runner reporters from DLLs in the given folder. The only DLLs that are searched are those | ||
/// named "*reporters*.dll" | ||
/// </summary> | ||
/// <param name="folder">The folder to search for reporters in</param> | ||
/// <param name="messages">Messages that were generated during discovery</param> | ||
/// <returns>List of available reporters</returns> | ||
public static List<IRunnerReporter> GetAvailableRunnerReporters(string folder, out List<string> messages) | ||
{ | ||
var result = new List<IRunnerReporter>(); | ||
messages = new List<string>(); | ||
string[] dllFiles; | ||
|
||
try | ||
{ | ||
dllFiles = Directory.GetFiles(folder, "*reporters*.dll").Select(f => Path.Combine(folder, f)).ToArray(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
messages.Add($"Exception thrown looking for reporters in folder '{folder}':{Environment.NewLine}{ex}"); | ||
return result; | ||
} | ||
|
||
foreach (var dllFile in dllFiles) | ||
{ | ||
Type[] types = new Type[0]; | ||
|
||
try | ||
{ | ||
#if NETFRAMEWORK | ||
var assembly = Assembly.LoadFile(dllFile); | ||
#else | ||
var assembly = Assembly.Load(new AssemblyName(Path.GetFileNameWithoutExtension(dllFile))); | ||
#endif | ||
types = assembly.GetTypes(); | ||
} | ||
catch (ReflectionTypeLoadException ex) | ||
{ | ||
types = ex.Types; | ||
} | ||
catch | ||
{ | ||
continue; | ||
} | ||
|
||
foreach (var type in types) | ||
{ | ||
#if NETFRAMEWORK | ||
if (type == null || type.IsAbstract || !type.GetInterfaces().Any(t => t == typeof(IRunnerReporter))) | ||
#else | ||
if (type == null || type.GetTypeInfo().IsAbstract || !type.GetInterfaces().Any(t => t == typeof(IRunnerReporter))) | ||
#endif | ||
continue; | ||
|
||
try | ||
{ | ||
var ctor = type.GetConstructor(new Type[0]); | ||
if (ctor == null) | ||
{ | ||
messages.Add($"Type '{type.FullName ?? type.Name}' in assembly '{dllFile}' appears to be a runner reporter, but does not have an empty constructor."); | ||
continue; | ||
} | ||
|
||
result.Add((IRunnerReporter)ctor.Invoke(new object[0])); | ||
} | ||
catch (Exception ex) | ||
{ | ||
messages.Add($"Exception thrown while inspecting type '{type.FullName ?? type.Name}' in assembly '{dllFile}':{Environment.NewLine}{ex}"); | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} | ||
|
||
#endif |