Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Discovery;
Expand All @@ -23,7 +22,7 @@ internal sealed class AssemblyEnumeratorWrapper
/// Gets test elements from an assembly.
/// </summary>
/// <returns> A collection of test elements. </returns>
internal static ICollection<UnitTestElement>? GetTests(string? assemblyFileName, IRunSettings? runSettings, ITestSourceHandler testSourceHandler, bool isMTP, out List<string> warnings)
internal static ICollection<UnitTestElement>? GetTests(string? assemblyFileName, string? settingsXml, ITestSourceHandler testSourceHandler, bool isMTP, out List<string> warnings)
{
warnings = [];

Expand All @@ -48,7 +47,7 @@ internal sealed class AssemblyEnumeratorWrapper
try
{
// Load the assembly in isolation if required.
AssemblyEnumerationResult result = GetTestsInIsolation(fullFilePath, runSettings, isMTP);
AssemblyEnumerationResult result = GetTestsInIsolation(fullFilePath, settingsXml, isMTP);
warnings.AddRange(result.Warnings);
return result.TestElements;
}
Expand Down Expand Up @@ -86,9 +85,9 @@ internal sealed class AssemblyEnumeratorWrapper
}
}

private static AssemblyEnumerationResult GetTestsInIsolation(string fullFilePath, IRunSettings? runSettings, bool isMTP)
private static AssemblyEnumerationResult GetTestsInIsolation(string fullFilePath, string? settingsXml, bool isMTP)
{
using ITestSourceHost isolationHost = PlatformServiceProvider.Instance.CreateTestSourceHost(fullFilePath, runSettings);
using ITestSourceHost isolationHost = PlatformServiceProvider.Instance.CreateTestSourceHost(fullFilePath, settingsXml);

// Create an instance of a type defined in adapter so that adapter gets loaded in the child app domain
var assemblyEnumerator = (AssemblyEnumerator)isolationHost.CreateInstanceForType(typeof(AssemblyEnumerator), [MSTestSettings.CurrentSettings])!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal virtual void DiscoverTestsInSource(
IDiscoveryContext? discoveryContext,
bool isMTP)
{
ICollection<UnitTestElement>? testElements = AssemblyEnumeratorWrapper.GetTests(source, discoveryContext?.RunSettings, _testSource, isMTP, out List<string> warnings);
ICollection<UnitTestElement>? testElements = AssemblyEnumeratorWrapper.GetTests(source, discoveryContext?.RunSettings?.SettingsXml, _testSource, isMTP, out List<string> warnings);

if (MSTestSettings.CurrentSettings.TreatDiscoveryWarningsAsErrors)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private async Task ExecuteTestsInSourceAsync(IEnumerable<UnitTestElement> tests,

IAdapterMessageLogger adapterMessageLogger = frameworkHandle.ToAdapterMessageLogger();

using ITestSourceHost isolationHost = PlatformServiceProvider.Instance.CreateTestSourceHost(source, runContext?.RunSettings);
using ITestSourceHost isolationHost = PlatformServiceProvider.Instance.CreateTestSourceHost(source, runContext?.RunSettings?.SettingsXml);
bool usesAppDomains = isolationHost is TestSourceHost { UsesAppDomain: true };

if (PlatformServiceProvider.Instance.AdapterTraceLogger.IsInfoEnabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution;
Expand Down Expand Up @@ -99,16 +98,16 @@ private static void ValidateAndAssignTestProperty(
}

[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Requirement is to handle errors in user specified run parameters")]
private void CacheSessionParameters(IRunContext? runContext, IAdapterMessageLogger messageLogger)
private void CacheSessionParameters(string? settingsXml, IAdapterMessageLogger messageLogger)
{
if (StringEx.IsNullOrEmpty(runContext?.RunSettings?.SettingsXml))
if (StringEx.IsNullOrEmpty(settingsXml))
{
return;
}

try
{
Dictionary<string, object>? testRunParameters = RunSettingsUtilities.GetTestRunParameters(runContext.RunSettings.SettingsXml);
Dictionary<string, object>? testRunParameters = RunSettingsUtilities.GetTestRunParameters(settingsXml);
if (testRunParameters != null)
{
// Clear sessionParameters to prevent key collisions of test run parameters in case
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ internal async Task RunTestsAsync(IEnumerable<UnitTestElement> tests, IRunContex
#endif

// Placing this after deployment since we need information post deployment that we pass in as properties.
CacheSessionParameters(runContext, frameworkHandle.ToAdapterMessageLogger());
CacheSessionParameters(runContext?.RunSettings?.SettingsXml, frameworkHandle.ToAdapterMessageLogger());

// Execute the tests
await ExecuteTestsAsync(tests, runContext, frameworkHandle, testResultRecorder, isDeploymentDone).ConfigureAwait(false);
Expand Down Expand Up @@ -154,7 +154,7 @@ internal async Task RunTestsAsync(IEnumerable<string> sources, IRunContext? runC
#endif

// Placing this after deployment since we need information post deployment that we pass in as properties.
CacheSessionParameters(runContext, frameworkHandle.ToAdapterMessageLogger());
CacheSessionParameters(runContext?.RunSettings?.SettingsXml, frameworkHandle.ToAdapterMessageLogger());

// Run tests.
await ExecuteTestsAsync(tests, runContext, frameworkHandle, testResultRecorder, isDeploymentDone).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ internal interface IPlatformServiceProvider
/// <param name="source">
/// The source.
/// </param>
/// <param name="runSettings">
/// <param name="settingsXml">
/// The run Settings for the session.
/// </param>
/// <returns>
/// Returns the host for the source provided.
/// </returns>
ITestSourceHost CreateTestSourceHost(
string source,
TestPlatform.ObjectModel.Adapter.IRunSettings? runSettings);
string? settingsXml);

/// <summary>
/// Gets the TestContext object for a platform.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,17 @@ internal static IPlatformServiceProvider Instance
/// <param name="source">
/// The source.
/// </param>
/// <param name="runSettings">
/// <param name="settingsXml">
/// The run Settings for the session.
/// </param>
/// <returns>
/// Returns the host for the source provided.
/// </returns>
public ITestSourceHost CreateTestSourceHost(
string source,
TestPlatform.ObjectModel.Adapter.IRunSettings? runSettings)
string? settingsXml)
{
var testSourceHost = new TestSourceHost(source, runSettings);
var testSourceHost = new TestSourceHost(source, settingsXml);
testSourceHost.SetupHost();

return testSourceHost;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#if NETFRAMEWORK
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
#endif
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
#if NETFRAMEWORK || (NET && !WINDOWS_UWP)
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities;
#endif
Expand Down Expand Up @@ -66,10 +65,10 @@ internal class TestSourceHost : ITestSourceHost
/// Initializes a new instance of the <see cref="TestSourceHost"/> class.
/// </summary>
/// <param name="sourceFileName"> The source file name. </param>
/// <param name="runSettings"> The run-settings provided for this session. </param>
public TestSourceHost(string sourceFileName, IRunSettings? runSettings)
/// <param name="settingsXml"> The run-settings XML provided for this session. </param>
public TestSourceHost(string sourceFileName, string? settingsXml)
#if NETFRAMEWORK
: this(sourceFileName, runSettings, new AppDomainWrapper())
: this(sourceFileName, settingsXml, new AppDomainWrapper())
#endif
{
#if !WINDOWS_UWP && !NETFRAMEWORK
Expand All @@ -81,7 +80,7 @@ public TestSourceHost(string sourceFileName, IRunSettings? runSettings)
}

#if NETFRAMEWORK
internal TestSourceHost(string sourceFileName, IRunSettings? runSettings, IAppDomain appDomain)
internal TestSourceHost(string sourceFileName, string? settingsXml, IAppDomain appDomain)
{
_sourceFileName = sourceFileName;
_appDomain = appDomain;
Expand All @@ -90,7 +89,7 @@ internal TestSourceHost(string sourceFileName, IRunSettings? runSettings, IAppDo
SetContext(sourceFileName);

// Set isAppDomainCreationDisabled flag
_isAppDomainCreationDisabled = runSettings != null && MSTestAdapterSettings.IsAppDomainCreationDisabled(runSettings.SettingsXml);
_isAppDomainCreationDisabled = settingsXml is not null && MSTestAdapterSettings.IsAppDomainCreationDisabled(settingsXml);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@

using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Utilities;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities;

using Moq;

using TestFramework.ForTestingMSTest;

namespace PlatformServices.Desktop.ComponentTests;
Expand Down Expand Up @@ -37,9 +34,10 @@ public void ParentDomainShouldHonorSearchDirectoriesSpecifiedInRunsettings()
</RunSettings>
""";

LoadMSTestSettings(runSettingsXml);
_testSourceHost = new TestSourceHost(
GetTestAssemblyPath("DesktopTestProjectx86Debug"),
GetMockedIRunSettings(runSettingsXml).Object);
runSettingsXml);
_testSourceHost.SetupHost();

// Loading SampleProjectForAssemblyResolution.dll should not throw.
Expand Down Expand Up @@ -67,9 +65,10 @@ public void ChildDomainResolutionPathsShouldHaveSearchDirectoriesSpecifiedInRuns
</RunSettings>
""";

LoadMSTestSettings(runSettingsXml);
_testSourceHost = new TestSourceHost(
GetTestAssemblyPath("DesktopTestProjectx86Debug"),
GetMockedIRunSettings(runSettingsXml).Object);
runSettingsXml);
_testSourceHost.SetupHost();

var asm = Assembly.LoadFrom(sampleProjectPath);
Expand Down Expand Up @@ -125,17 +124,12 @@ private static string GetTestAssemblyPath(string assetName)
return testAssetPath;
}

private static Mock<IRunSettings> GetMockedIRunSettings(string runSettingsXml)
private static void LoadMSTestSettings(string runSettingsXml)
{
var mockRunSettings = new Mock<IRunSettings>();
mockRunSettings.Setup(rs => rs.SettingsXml).Returns(runSettingsXml);

StringReader stringReader = new(runSettingsXml);
var reader = XmlReader.Create(stringReader, XmlRunSettingsUtilities.ReaderSettings);
MSTestSettingsProvider mstestSettingsProvider = new();
reader.ReadToFollowing("MSTestV2");
mstestSettingsProvider.Load(reader);

return mockRunSettings;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Utilities;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities;

using Moq;
Expand Down Expand Up @@ -135,10 +134,7 @@ public void SetupHostShouldHaveParentDomainsAppBaseSetToTestSourceLocation()
""";

string location = typeof(TestSourceHost).Assembly.Location;
var mockRunSettings = new Mock<IRunSettings>();
mockRunSettings.Setup(rs => rs.SettingsXml).Returns(runSettingsXml);

TestSourceHost sourceHost = new(location, mockRunSettings.Object);
TestSourceHost sourceHost = new(location, runSettingsXml);

try
{
Expand Down Expand Up @@ -189,10 +185,7 @@ public void NoAppDomainShouldGetCreatedWhenDisableAppDomainIsSetToTrue()
</RunSettings>";

string location = typeof(TestSourceHost).Assembly.Location;
var mockRunSettings = new Mock<IRunSettings>();
mockRunSettings.Setup(rs => rs.SettingsXml).Returns(runSettingsXml);

Mock<TestSourceHost> testSourceHost = new(location, mockRunSettings.Object) { CallBase = true };
Mock<TestSourceHost> testSourceHost = new(location, runSettingsXml) { CallBase = true };

try
{
Expand Down Expand Up @@ -220,10 +213,7 @@ public void AppDomainShouldGetCreatedWhenDisableAppDomainIsSetToFalse()
""";

string location = typeof(TestSourceHost).Assembly.Location;
var mockRunSettings = new Mock<IRunSettings>();
mockRunSettings.Setup(rs => rs.SettingsXml).Returns(runSettingsXml);

Mock<TestSourceHost> testSourceHost = new(location, mockRunSettings.Object) { CallBase = true };
Mock<TestSourceHost> testSourceHost = new(location, runSettingsXml) { CallBase = true };

try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public ITestContext GetTestContext(ITestMethod? testMethod, string? testClassFul
return testContextImpl;
}

public ITestSourceHost CreateTestSourceHost(string source, TestPlatform.ObjectModel.Adapter.IRunSettings? runSettings) => MockTestSourceHost.Object;
public ITestSourceHost CreateTestSourceHost(string source, string? settingsXml) => MockTestSourceHost.Object;

public void SetupMockReflectionOperations()
{
Expand Down