Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test adapter loading strategy #3380

Merged
merged 10 commits into from
Feb 18, 2022
129 changes: 60 additions & 69 deletions src/Microsoft.TestPlatform.Client/TestPlatform.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected DataCollectorExtensionManager(
/// </returns>
public static DataCollectorExtensionManager Create(IMessageLogger messageLogger)
{
TestPluginManager.Instance.GetSpecificTestExtensions<DataCollectorConfig, DataCollector, IDataCollectorCapabilities, DataCollectorMetadata>(
TestPluginManager.GetSpecificTestExtensions<DataCollectorConfig, DataCollector, IDataCollectorCapabilities, DataCollectorMetadata>(
TestPlatformConstants.DataCollectorEndsWithPattern,
out var unfilteredTestExtensions,
out var filteredTestExtensions);
Expand All @@ -78,7 +78,7 @@ public static DataCollectorExtensionManager Create(IMessageLogger messageLogger)
/// </returns>
public static DataCollectorExtensionManager Create(string extensionAssemblyFilePath, bool skipCache, IMessageLogger messageLogger)
{
TestPluginManager.Instance.GetTestExtensions<DataCollectorConfig, DataCollector, IDataCollectorCapabilities, DataCollectorMetadata>(
TestPluginManager.GetTestExtensions<DataCollectorConfig, DataCollector, IDataCollectorCapabilities, DataCollectorMetadata>(
extensionAssemblyFilePath,
out var unfilteredTestExtensions,
out var filteredTestExtensions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ public static TestDiscoveryExtensionManager Create()
if (s_testDiscoveryExtensionManager == null)
{

TestPluginManager.Instance
.GetSpecificTestExtensions<TestDiscovererPluginInformation, ITestDiscoverer, ITestDiscovererCapabilities, TestDiscovererMetadata>(
TestPluginManager.GetSpecificTestExtensions<TestDiscovererPluginInformation, ITestDiscoverer, ITestDiscovererCapabilities, TestDiscovererMetadata>(
TestPlatformConstants.TestAdapterEndsWithPattern,
out var unfilteredTestExtensions,
out var testExtensions);
Expand All @@ -90,8 +89,7 @@ public static TestDiscoveryExtensionManager Create()
public static TestDiscoveryExtensionManager GetDiscoveryExtensionManager(string extensionAssembly)
{

TestPluginManager.Instance
.GetTestExtensions<TestDiscovererPluginInformation, ITestDiscoverer, ITestDiscovererCapabilities, TestDiscovererMetadata>(
TestPluginManager.GetTestExtensions<TestDiscovererPluginInformation, ITestDiscoverer, ITestDiscovererCapabilities, TestDiscovererMetadata>(
extensionAssembly,
out var unfilteredTestExtensions,
out var testExtensions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,13 @@ internal static TestExecutorExtensionManager Create()
{

// Get all extensions for ITestExecutor.
TestPluginManager.Instance
.GetSpecificTestExtensions<TestExecutorPluginInformation, ITestExecutor, ITestExecutorCapabilities, TestExecutorMetadata>(
TestPluginManager.GetSpecificTestExtensions<TestExecutorPluginInformation, ITestExecutor, ITestExecutorCapabilities, TestExecutorMetadata>(
TestPlatformConstants.TestAdapterEndsWithPattern,
out var unfilteredTestExtensions1,
out var testExtensions1);

// Get all extensions for ITestExecutor2.
TestPluginManager.Instance
.GetSpecificTestExtensions<TestExecutorPluginInformation2, ITestExecutor2, ITestExecutorCapabilities, TestExecutorMetadata>(
TestPluginManager.GetSpecificTestExtensions<TestExecutorPluginInformation2, ITestExecutor2, ITestExecutorCapabilities, TestExecutorMetadata>(
TestPlatformConstants.TestAdapterEndsWithPattern,
out var unfilteredTestExtensions2,
out var testExtensions2);
Expand Down Expand Up @@ -152,15 +150,13 @@ internal static TestExecutorExtensionManager GetExecutionExtensionManager(string
{

// Get all extensions for ITestExecutor.
TestPluginManager.Instance
.GetTestExtensions<TestExecutorPluginInformation, ITestExecutor, ITestExecutorCapabilities, TestExecutorMetadata>(
TestPluginManager.GetTestExtensions<TestExecutorPluginInformation, ITestExecutor, ITestExecutorCapabilities, TestExecutorMetadata>(
extensionAssembly,
out var unfilteredTestExtensions1,
out var testExtensions1);

// Get all extensions for ITestExecutor2.
TestPluginManager.Instance
.GetTestExtensions<TestExecutorPluginInformation2, ITestExecutor2, ITestExecutorCapabilities, TestExecutorMetadata>(
TestPluginManager.GetTestExtensions<TestExecutorPluginInformation2, ITestExecutor2, ITestExecutorCapabilities, TestExecutorMetadata>(
extensionAssembly,
out var unfilteredTestExtensions2,
out var testExtensions2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected TestLoggerExtensionManager(
public static TestLoggerExtensionManager Create(IMessageLogger messageLogger)
{

TestPluginManager.Instance.GetSpecificTestExtensions<TestLoggerPluginInformation, ITestLogger, ITestLoggerCapabilities, TestLoggerMetadata>(
TestPluginManager.GetSpecificTestExtensions<TestLoggerPluginInformation, ITestLogger, ITestLoggerCapabilities, TestLoggerMetadata>(
TestPlatformConstants.TestLoggerEndsWithPattern,
out IEnumerable<LazyExtension<ITestLogger, Dictionary<string, object>>> unfilteredTestExtensions,
out IEnumerable<LazyExtension<ITestLogger, ITestLoggerCapabilities>> filteredTestExtensions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ public static T CreateTestExtension<T>(Type extensionType!!)
/// <param name="filtered">
/// Receives test extensions filtered by Identifier data
/// </param>
public void GetSpecificTestExtensions<TPluginInfo, TExtension, IMetadata, TMetadata>(
public static void GetSpecificTestExtensions<TPluginInfo, TExtension, IMetadata, TMetadata>(
string endsWithPattern,
out IEnumerable<LazyExtension<TExtension, Dictionary<string, object>>> unfiltered,
out IEnumerable<LazyExtension<TExtension, IMetadata>> filtered) where TMetadata : IMetadata where TPluginInfo : TestPluginInformation
{
var extensions = TestPluginCache.Instance.DiscoverTestExtensions<TPluginInfo, TExtension>(endsWithPattern);
GetExtensions<TPluginInfo, TExtension, IMetadata, TMetadata>(extensions, out unfiltered, out filtered);
TestPluginManager.GetExtensions<TPluginInfo, TExtension, IMetadata, TMetadata>(extensions, out unfiltered, out filtered);
}

/// <summary>
Expand Down Expand Up @@ -142,14 +142,14 @@ public void GetSpecificTestExtensions<TPluginInfo, TExtension, IMetadata, TMetad
/// <param name="skipCache">
/// Skip the extensions cache.
/// </param>
public void GetTestExtensions<TPluginInfo, TExtension, IMetadata, TMetadata>(
public static void GetTestExtensions<TPluginInfo, TExtension, IMetadata, TMetadata>(
string extensionAssembly,
out IEnumerable<LazyExtension<TExtension, Dictionary<string, object>>> unfiltered,
out IEnumerable<LazyExtension<TExtension, IMetadata>> filtered,
bool skipCache = false) where TMetadata : IMetadata where TPluginInfo : TestPluginInformation
{
var extensions = TestPluginCache.Instance.GetTestExtensions<TPluginInfo, TExtension>(extensionAssembly, skipCache);
GetExtensions<TPluginInfo, TExtension, IMetadata, TMetadata>(extensions, out unfiltered, out filtered);
TestPluginManager.GetExtensions<TPluginInfo, TExtension, IMetadata, TMetadata>(extensions, out unfiltered, out filtered);
}

/// <summary>
Expand All @@ -158,7 +158,7 @@ public void GetTestExtensions<TPluginInfo, TExtension, IMetadata, TMetadata>(
/// <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
private static IEnumerable<TestPluginInformation> GetValuesFromDictionary<T>(Dictionary<string, T> dictionary) where T : TestPluginInformation
{
var values = new List<TestPluginInformation>();

Expand Down Expand Up @@ -193,15 +193,15 @@ private IEnumerable<TestPluginInformation> GetValuesFromDictionary<T>(Dictionary
/// <param name="filtered">
/// Receives test extensions filtered by Identifier data
/// </param>
private void GetExtensions<TPluginInfo, TExtension, IMetadata, TMetadata>(
private static void GetExtensions<TPluginInfo, TExtension, IMetadata, TMetadata>(
Dictionary<string, TPluginInfo> testPluginInfo,
out IEnumerable<LazyExtension<TExtension, Dictionary<string, object>>> unfiltered,
out IEnumerable<LazyExtension<TExtension, IMetadata>> filtered) where TMetadata : IMetadata where TPluginInfo : TestPluginInformation
{
var unfilteredExtensions = new List<LazyExtension<TExtension, Dictionary<string, object>>>();
var filteredExtensions = new List<LazyExtension<TExtension, IMetadata>>();

var testPlugins = GetValuesFromDictionary(testPluginInfo);
var testPlugins = TestPluginManager.GetValuesFromDictionary(testPluginInfo);
foreach (var plugin in testPlugins)
{
if (!string.IsNullOrEmpty(plugin.IdentifierData))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected TestRuntimeExtensionManager(
/// </returns>
public static TestRuntimeExtensionManager Create(IMessageLogger messageLogger)
{
TestPluginManager.Instance.GetSpecificTestExtensions<TestRuntimePluginInformation, ITestRuntimeProvider, ITestRuntimeCapabilities, TestRuntimeMetadata>(
TestPluginManager.GetSpecificTestExtensions<TestRuntimePluginInformation, ITestRuntimeProvider, ITestRuntimeCapabilities, TestRuntimeMetadata>(
TestPlatformConstants.RunTimeEndsWithPattern,
out IEnumerable<LazyExtension<ITestRuntimeProvider, Dictionary<string, object>>> unfilteredTestExtensions,
out IEnumerable<LazyExtension<ITestRuntimeProvider, ITestRuntimeCapabilities>> filteredTestExtensions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ public static SettingsProviderExtensionManager Create()
if (s_settingsProviderExtensionManager == null)
{

TestPluginManager.Instance
.GetSpecificTestExtensions<TestSettingsProviderPluginInformation, ISettingsProvider, ISettingsProviderCapabilities, TestSettingsProviderMetadata>(
TestPluginManager.GetSpecificTestExtensions<TestSettingsProviderPluginInformation, ISettingsProvider, ISettingsProviderCapabilities, TestSettingsProviderMetadata>(
TestPlatformConstants.TestAdapterEndsWithPattern,
out var unfilteredTestExtensions,
out var testExtensions);
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.TestPlatform.ObjectModel/Friends.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
[assembly: InternalsVisibleTo("datacollector.UnitTests, PublicKey=002400000480000094000000060200000024000052534131000400000100010007d1fa57c4aed9f0a32e84aa0faefd0de9e8fd6aec8f87fb03766c834c99921eb23be79ad9d5dcc1dd9ad236132102900b723cf980957fc4e177108fc607774f29e8320e92ea05ece4e821c0a5efe8f1645c4c0c93c1ab99285d622caa652c1dfad63d745d6f2de5f17e5eaf0fc4963d261c8a12436518206dc093344d5ad293")]
[assembly: InternalsVisibleTo("Microsoft.TestPlatform.Extensions.EventLogCollector.UnitTests, PublicKey=002400000480000094000000060200000024000052534131000400000100010007d1fa57c4aed9f0a32e84aa0faefd0de9e8fd6aec8f87fb03766c834c99921eb23be79ad9d5dcc1dd9ad236132102900b723cf980957fc4e177108fc607774f29e8320e92ea05ece4e821c0a5efe8f1645c4c0c93c1ab99285d622caa652c1dfad63d745d6f2de5f17e5eaf0fc4963d261c8a12436518206dc093344d5ad293")]
[assembly: InternalsVisibleTo("Microsoft.TestPlatform.ObjectModel.ManagedNameUtilities.UnitTests, PublicKey=002400000480000094000000060200000024000052534131000400000100010007d1fa57c4aed9f0a32e84aa0faefd0de9e8fd6aec8f87fb03766c834c99921eb23be79ad9d5dcc1dd9ad236132102900b723cf980957fc4e177108fc607774f29e8320e92ea05ece4e821c0a5efe8f1645c4c0c93c1ab99285d622caa652c1dfad63d745d6f2de5f17e5eaf0fc4963d261c8a12436518206dc093344d5ad293")]
[assembly: InternalsVisibleTo("vstest.console.UnitTests, PublicKey=002400000480000094000000060200000024000052534131000400000100010007d1fa57c4aed9f0a32e84aa0faefd0de9e8fd6aec8f87fb03766c834c99921eb23be79ad9d5dcc1dd9ad236132102900b723cf980957fc4e177108fc607774f29e8320e92ea05ece4e821c0a5efe8f1645c4c0c93c1ab99285d622caa652c1dfad63d745d6f2de5f17e5eaf0fc4963d261c8a12436518206dc093344d5ad293")]
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors;

using System;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq;

Expand Down Expand Up @@ -117,18 +116,7 @@ internal class TestAdapterLoadingStrategyArgumentExecutor : IArgumentExecutor
/// </summary>
private readonly IFileHelper _fileHelper;

Haplois marked this conversation as resolved.
Show resolved Hide resolved
private static readonly string[] EmptyStringArray =
#if NET451
new string[0];
#else
Array.Empty<string>();
#endif

#endregion

public const string RunSettingsPath = "RunConfiguration.TestAdapterLoadingStrategy";
public const string DefaultStrategy = "Default";
public const string ExplicitStrategy = "Explicit";

/// <summary>
/// Default constructor.
Expand All @@ -137,16 +125,13 @@ internal class TestAdapterLoadingStrategyArgumentExecutor : IArgumentExecutor
/// <param name="testPlatform">The test platform</param>
public TestAdapterLoadingStrategyArgumentExecutor(CommandLineOptions options!!, IRunSettingsProvider runSettingsManager!!, IOutput output!!, IFileHelper fileHelper!!)
{
Contract.Requires(options != null);

_commandLineOptions = options;
_runSettingsManager = runSettingsManager;
_output = output;
_fileHelper = fileHelper;
}

#region IArgumentExecutor
Haplois marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Initializes with the argument that was provided with the command.
/// </summary>
Expand All @@ -169,6 +154,17 @@ public void Initialize(string argument)
InitializeStrategy(strategy);
}

/// <summary>
/// Executes the argument processor.
/// </summary>
/// <returns> The <see cref="ArgumentProcessorResult"/>. </returns>
public ArgumentProcessorResult Execute()
{
// Nothing to do since we updated the parameter during initialize parameter
return ArgumentProcessorResult.Success;
}
#endregion

private void ExtractStrategy(string value, out TestAdapterLoadingStrategy strategy)
{
value ??= _runSettingsManager.QueryRunSettingsNode(RunSettingsPath);
Expand Down Expand Up @@ -196,9 +192,9 @@ private void InitializeStrategy(TestAdapterLoadingStrategy strategy)
{
ValidateTestAdapterPaths(strategy);

if (!_commandLineOptions.TestAdapterPathsSet && (strategy & TestAdapterLoadingStrategy.Explicit) == TestAdapterLoadingStrategy.Explicit)
if (!_commandLineOptions.TestAdapterPathsSet && strategy.HasFlag(TestAdapterLoadingStrategy.Explicit))
{
throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, CommandLineResources.TestAdapterPathValueRequiredWhenStrategyXIsUsed, ExplicitStrategy));
throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, CommandLineResources.TestAdapterPathValueRequiredWhenStrategyXIsUsed, nameof(TestAdapterLoadingStrategy.Explicit)));
}

SetStrategy(strategy);
Expand All @@ -211,13 +207,19 @@ private void ForceIsolation()
return;
}

EqtTrace.Warning(
$"{nameof(TestAdapterLoadingStrategyArgumentExecutor)}.{nameof(ForceIsolation)}: InIsolation setting is forced when {nameof(TestAdapterLoadingStrategy.Explicit)} strategy is used." +
"Test's will run in isolation."
Haplois marked this conversation as resolved.
Show resolved Hide resolved
);
_commandLineOptions.InIsolation = true;
_runSettingsManager.UpdateRunSettingsNode(InIsolationArgumentExecutor.RunSettingsPath, "true");
}

private void ValidateTestAdapterPaths(TestAdapterLoadingStrategy strategy)
{
var testAdapterPaths = _commandLineOptions.TestAdapterPath ?? EmptyStringArray;
#pragma warning disable CA1825 // Avoid zero-length array allocations
Haplois marked this conversation as resolved.
Show resolved Hide resolved
var testAdapterPaths = _commandLineOptions.TestAdapterPath ?? new string[0];
#pragma warning restore CA1825 // Avoid zero-length array allocations
if (!_commandLineOptions.TestAdapterPathsSet)
{
testAdapterPaths = TestAdapterPathArgumentExecutor.SplitPaths(_runSettingsManager.QueryRunSettingsNode(TestAdapterPathArgumentExecutor.RunSettingsPath)).Union(testAdapterPaths).Distinct().ToArray();
Expand Down Expand Up @@ -245,21 +247,9 @@ private void SetStrategy(TestAdapterLoadingStrategy strategy)
{
_commandLineOptions.TestAdapterLoadingStrategy = strategy;
_runSettingsManager.UpdateRunSettingsNode(RunSettingsPath, strategy.ToString());
if ((strategy & TestAdapterLoadingStrategy.Explicit) == TestAdapterLoadingStrategy.Explicit)
if (strategy.HasFlag(TestAdapterLoadingStrategy.Explicit))
{
ForceIsolation();
}
}

/// <summary>
/// Executes the argument processor.
/// </summary>
/// <returns> The <see cref="ArgumentProcessorResult"/>. </returns>
public ArgumentProcessorResult Execute()
{
// Nothing to do since we updated the parameter during initialize parameter
return ArgumentProcessorResult.Success;
}

#endregion
}
Loading