Skip to content
Merged
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 @@ -32,13 +32,23 @@ internal sealed class TestRunDirectories
/// Initializes a new instance of the <see cref="TestRunDirectories"/> class.
/// </summary>
/// <param name="rootDirectory">The root directory path.</param>
public TestRunDirectories(string rootDirectory)
/// <param name="firstTestSource">
/// The path to the test assembly of the first test case. In most cases, all
/// test cases belong to the same assembly, but not guaranteed. We are using the path from
/// the first test case as a "best effort" implementation. DeploymentItem isn't correctly designed and should be deprecated in future.
/// </param>
/// <param name="isAppDomainCreationDisabled">Whether or not app domain is disabled.</param>
public TestRunDirectories(string rootDirectory, string? firstTestSource, bool isAppDomainCreationDisabled)
{
DebugEx.Assert(!StringEx.IsNullOrEmpty(rootDirectory), "rootDirectory");

RootDeploymentDirectory = rootDirectory;
InDirectory = Path.Combine(RootDeploymentDirectory, DeploymentInDirectorySuffix);
OutDirectory = Path.Combine(RootDeploymentDirectory, DeploymentOutDirectorySuffix);

OutDirectory = isAppDomainCreationDisabled && firstTestSource is not null
? Path.GetDirectoryName(firstTestSource)!
: Path.Combine(RootDeploymentDirectory, DeploymentOutDirectorySuffix);

InMachineNameDirectory = Path.Combine(InDirectory, Environment.MachineName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,33 +113,33 @@ public void Cleanup()
/// <summary>
/// Deploy files related to the list of tests specified.
/// </summary>
/// <param name="tests"> The tests. </param>
/// <param name="testCases"> The tests. </param>
/// <param name="runContext"> The run context. </param>
/// <param name="frameworkHandle"> The framework handle. </param>
/// <returns> Return true if deployment is done. </returns>
[SuppressMessage("Naming", "CA1725:Parameter names should match base declaration", Justification = "Part of the public API")]
public bool Deploy(IEnumerable<TestCase> tests, IRunContext? runContext, IFrameworkHandle frameworkHandle)
public bool Deploy(IEnumerable<TestCase> testCases, IRunContext? runContext, IFrameworkHandle frameworkHandle)
{
#if WINDOWS_UWP
return false;
#else
DebugEx.Assert(tests != null, "tests");
DebugEx.Assert(testCases != null, "tests");

// Reset runDirectories before doing deployment, so that older values of runDirectories is not picked
// even if test host is kept alive.
RunDirectories = null;

_adapterSettings = MSTestSettingsProvider.Settings;
bool canDeploy = CanDeploy();
bool hasDeploymentItems = tests.Any(DeploymentItemUtility.HasDeploymentItems);
bool hasDeploymentItems = testCases.Any(DeploymentItemUtility.HasDeploymentItems);

// deployment directories should not be created in this case,simply return
if (!canDeploy && hasDeploymentItems)
{
return false;
}

RunDirectories = _deploymentUtility.CreateDeploymentDirectories(runContext);
string? firstTestSource = testCases.FirstOrDefault()?.Source;
RunDirectories = _deploymentUtility.CreateDeploymentDirectories(runContext, firstTestSource);

// Deployment directories are created but deployment will not happen.
// This is added just to keep consistency with MSTest v1 behavior.
Expand All @@ -154,7 +154,7 @@ public bool Deploy(IEnumerable<TestCase> tests, IRunContext? runContext, IFramew
#endif
{
// Group the tests by source
var testsBySource = from test in tests
var testsBySource = from test in testCases
group test by test.Source into testGroup
select new { Source = testGroup.Key, Tests = testGroup };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,25 @@ public bool Deploy(IEnumerable<TestCase> tests, string source, IRunContext? runC
/// Create deployment directories.
/// </summary>
/// <param name="runContext">The run context.</param>
/// <param name="firstTestSource">
/// The path to the test assembly of the first test case. In most cases, all
/// test cases belong to the same assembly, but not guaranteed. We are using the path from
/// the first test case as a "best effort" implementation. DeploymentItem isn't correctly designed and should be deprecated in future.
/// </param>
/// <returns>TestRunDirectories instance.</returns>
public TestRunDirectories CreateDeploymentDirectories(IRunContext? runContext)
public TestRunDirectories CreateDeploymentDirectories(IRunContext? runContext, string? firstTestSource)
{
string resultsDirectory = GetTestResultsDirectory(runContext);
string rootDeploymentDirectory = GetRootDeploymentDirectory(resultsDirectory);

var result = new TestRunDirectories(rootDeploymentDirectory);
#if NETFRAMEWORK
bool isAppDomainCreationDisabled = runContext?.RunSettings != null && MSTestAdapterSettings.IsAppDomainCreationDisabled(runContext.RunSettings.SettingsXml);
#else
// AppDomains are only supported in .NET Framework.
const bool isAppDomainCreationDisabled = true;
#endif

var result = new TestRunDirectories(rootDeploymentDirectory, firstTestSource, isAppDomainCreationDisabled);

FileUtility.CreateDirectoryIfNotExists(rootDeploymentDirectory);
FileUtility.CreateDirectoryIfNotExists(result.InDirectory);
Expand Down Expand Up @@ -221,6 +233,10 @@ protected IEnumerable<string> Deploy(IList<DeploymentItem> deploymentItems, stri
if (!destToSource.TryGetValue(relativeDestination, out string? value))
{
destToSource.Add(relativeDestination, fileToDeploy);
if (fileToDeploy == destination)
{
continue;
}

// Now, finally we can copy the file...
destination = FileUtility.CopyFileOverwrite(fileToDeploy, destination, out string? warning);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class DeploymentItemTests : AcceptanceTestBase<DeploymentItemTests.TestAs

[TestMethod]
[OSCondition(OperatingSystems.Windows)]
[DataRow("AppDomainDisabled.runsettings", IgnoreMessage = "https://github.com/microsoft/testfx/issues/6738")]
[DataRow("AppDomainDisabled.runsettings")]
[DataRow("AppDomainEnabled.runsettings")]
public async Task AssemblyIsLoadedOnceFromDeploymentDirectory(string runsettings)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace MSTestAdapter.PlatformServices.Tests.Deployment;

public class TestRunDirectoriesTests : TestContainer
{
private readonly TestRunDirectories _testRunDirectories = new(@"C:\temp");
private readonly TestRunDirectories _testRunDirectories = new(@"C:\temp", @"C:\temp\asm.dll", isAppDomainCreationDisabled: false);

public void InDirectoryShouldReturnCorrectDirectory() => Verify(_testRunDirectories.InDirectory == @"C:\temp\In");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private TestDeployment CreateAndSetupDeploymentRelatedUtilities(out TestRunDirec
{
string currentExecutingFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

testRunDirectories = new TestRunDirectories(currentExecutingFolder);
testRunDirectories = new TestRunDirectories(currentExecutingFolder, Path.Combine(currentExecutingFolder, "asm.dll"), isAppDomainCreationDisabled: false);

_mockFileUtility.Setup(fu => fu.DoesDirectoryExist(It.Is<string>(s => !s.EndsWith(".dll") && !s.EndsWith(".exe")))).Returns(true);
_mockFileUtility.Setup(fu => fu.DoesFileExist(It.IsAny<string>())).Returns(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public void GetDeploymentDirectoryShouldReturnDeploymentOutputDirectory()

public void DeployShouldReturnFalseWhenDeploymentEnabledSetToFalseButHasDeploymentItems()
{
var testCase = new TestCase("A.C.M", new Uri("executor://testExecutor"), "A");
var testCase = new TestCase("A.C.M", new Uri("executor://testExecutor"), "path/to/asm.dll");
KeyValuePair<string, string>[] kvpArray =
[
new KeyValuePair<string, string>(
Expand Down Expand Up @@ -199,7 +199,7 @@ public void DeployShouldReturnFalseWhenDeploymentEnabledSetToFalseButHasDeployme

public void DeployShouldReturnFalseWhenDeploymentEnabledSetToFalseAndHasNoDeploymentItems()
{
var testCase = new TestCase("A.C.M", new Uri("executor://testExecutor"), "A");
var testCase = new TestCase("A.C.M", new Uri("executor://testExecutor"), "path/to/asm.dll");
testCase.SetPropertyValue(DeploymentItemUtilityTests.DeploymentItemsProperty, null);
var testDeployment = new TestDeployment(
new DeploymentItemUtility(_mockReflectionUtility.Object),
Expand All @@ -222,7 +222,7 @@ public void DeployShouldReturnFalseWhenDeploymentEnabledSetToFalseAndHasNoDeploy

public void DeployShouldReturnFalseWhenDeploymentEnabledSetToTrueButHasNoDeploymentItems()
{
var testCase = new TestCase("A.C.M", new Uri("executor://testExecutor"), "A");
var testCase = new TestCase("A.C.M", new Uri("executor://testExecutor"), "path/to/asm.dll");
testCase.SetPropertyValue(DeploymentItemUtilityTests.DeploymentItemsProperty, null);
var testDeployment = new TestDeployment(
new DeploymentItemUtility(_mockReflectionUtility.Object),
Expand Down Expand Up @@ -393,7 +393,14 @@ private TestDeployment CreateAndSetupDeploymentRelatedUtilities(out TestRunDirec
{
string currentExecutingFolder = Path.GetDirectoryName(typeof(TestDeploymentTests).Assembly.Location)!;

testRunDirectories = new TestRunDirectories(currentExecutingFolder);
const bool isAppDomainCreationDisabled =
#if NETFRAMEWORK
false;
#else
true;
#endif

testRunDirectories = new TestRunDirectories(currentExecutingFolder, Path.Combine(currentExecutingFolder, "asm.dll"), isAppDomainCreationDisabled);

_mockFileUtility.Setup(fu => fu.DoesDirectoryExist(It.Is<string>(s => !s.EndsWith(".dll")))).Returns(true);
_mockFileUtility.Setup(fu => fu.DoesFileExist(It.IsAny<string>())).Returns(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public DeploymentUtilityTests()

public void DeployShouldReturnFalseWhenNoDeploymentItemsOnTestCase()
{
var testCase = new TestCase("A.C.M", new Uri("executor://testExecutor"), "A");
var testCase = new TestCase("A.C.M", new Uri("executor://testExecutor"), Path.Combine(RootDeploymentDirectory, "asm.dll"));
testCase.SetPropertyValue(DeploymentItemUtilityTests.DeploymentItemsProperty, null);
var testRunDirectories = new TestRunDirectories(RootDeploymentDirectory);
var testRunDirectories = new TestRunDirectories(RootDeploymentDirectory, Path.Combine(RootDeploymentDirectory, "asm.dll"), isAppDomainCreationDisabled: true);

_mockFileUtility.Setup(fu => fu.DoesDirectoryExist(It.Is<string>(s => !s.EndsWith(".dll") && !s.EndsWith(".exe") && !s.EndsWith(".config"))))
.Returns(true);
Expand Down Expand Up @@ -421,7 +421,7 @@ public void CreateDeploymentDirectoriesShouldCreateDeploymentDirectoryFromRunCon
.Returns(RootDeploymentDirectory);

// Act.
_deploymentUtility.CreateDeploymentDirectories(_mockRunContext.Object);
_deploymentUtility.CreateDeploymentDirectories(_mockRunContext.Object, null);

// Assert.
_mockFileUtility.Verify(fu => fu.CreateDirectoryIfNotExists(RootDeploymentDirectory), Times.Once);
Expand All @@ -438,7 +438,7 @@ public void CreateDeploymentDirectoriesShouldCreateDefaultDeploymentDirectoryIfT
.Returns(RootDeploymentDirectory);

// Act.
_deploymentUtility.CreateDeploymentDirectories(_mockRunContext.Object);
_deploymentUtility.CreateDeploymentDirectories(_mockRunContext.Object, null);

// Assert.
_mockFileUtility.Verify(fu => fu.CreateDirectoryIfNotExists(RootDeploymentDirectory), Times.Once);
Expand All @@ -454,7 +454,7 @@ public void CreateDeploymentDirectoriesShouldCreateDefaultDeploymentDirectoryIfR
.Returns(RootDeploymentDirectory);

// Act.
_deploymentUtility.CreateDeploymentDirectories(null);
_deploymentUtility.CreateDeploymentDirectories(null, null);

// Assert.
_mockFileUtility.Verify(fu => fu.CreateDirectoryIfNotExists(RootDeploymentDirectory), Times.Once);
Expand All @@ -479,7 +479,14 @@ private static TestCase GetTestCaseAndTestRunDirectories(string deploymentItemPa
testCase.SetPropertyValue(DeploymentItemUtilityTests.DeploymentItemsProperty, kvpArray);
string currentExecutingFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;

testRunDirectories = new TestRunDirectories(currentExecutingFolder);
const bool isAppDomainCreationDisabled =
#if NETFRAMEWORK
false;
#else
true;
#endif

testRunDirectories = new TestRunDirectories(currentExecutingFolder, Path.Combine(currentExecutingFolder, "asm.dll"), isAppDomainCreationDisabled);

return testCase;
}
Expand Down