Skip to content

Commit 22a6cfc

Browse files
committed
Fix DeploymentItem when appdomain is disabled or not available
1 parent 8db4aae commit 22a6cfc

File tree

8 files changed

+57
-24
lines changed

8 files changed

+57
-24
lines changed

src/Adapter/MSTestAdapter.PlatformServices/Deployment/TestRunDirectories.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,23 @@ internal sealed class TestRunDirectories
3232
/// Initializes a new instance of the <see cref="TestRunDirectories"/> class.
3333
/// </summary>
3434
/// <param name="rootDirectory">The root directory path.</param>
35-
public TestRunDirectories(string rootDirectory)
35+
/// <param name="firstTestSource">
36+
/// The path to the test assembly of the first test case. In most cases, all
37+
/// test cases belong to the same assembly, but not guaranteed. We are using the path from
38+
/// the first test case as a "best effort" implementation. DeploymentItem isn't correctly designed and should be deprecated in future.
39+
/// </param>
40+
/// <param name="isAppDomainCreationDisabled">Whether or not app domain is disabled.</param>
41+
public TestRunDirectories(string rootDirectory, string? firstTestSource, bool isAppDomainCreationDisabled)
3642
{
3743
DebugEx.Assert(!StringEx.IsNullOrEmpty(rootDirectory), "rootDirectory");
3844

3945
RootDeploymentDirectory = rootDirectory;
4046
InDirectory = Path.Combine(RootDeploymentDirectory, DeploymentInDirectorySuffix);
41-
OutDirectory = Path.Combine(RootDeploymentDirectory, DeploymentOutDirectorySuffix);
47+
48+
OutDirectory = isAppDomainCreationDisabled && firstTestSource is not null
49+
? Path.GetDirectoryName(firstTestSource)!
50+
: Path.Combine(RootDeploymentDirectory, DeploymentOutDirectorySuffix);
51+
4252
InMachineNameDirectory = Path.Combine(InDirectory, Environment.MachineName);
4353
}
4454

src/Adapter/MSTestAdapter.PlatformServices/Services/TestDeployment.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,33 +113,33 @@ public void Cleanup()
113113
/// <summary>
114114
/// Deploy files related to the list of tests specified.
115115
/// </summary>
116-
/// <param name="tests"> The tests. </param>
116+
/// <param name="testCases"> The tests. </param>
117117
/// <param name="runContext"> The run context. </param>
118118
/// <param name="frameworkHandle"> The framework handle. </param>
119119
/// <returns> Return true if deployment is done. </returns>
120-
[SuppressMessage("Naming", "CA1725:Parameter names should match base declaration", Justification = "Part of the public API")]
121-
public bool Deploy(IEnumerable<TestCase> tests, IRunContext? runContext, IFrameworkHandle frameworkHandle)
120+
public bool Deploy(IEnumerable<TestCase> testCases, IRunContext? runContext, IFrameworkHandle frameworkHandle)
122121
{
123122
#if WINDOWS_UWP
124123
return false;
125124
#else
126-
DebugEx.Assert(tests != null, "tests");
125+
DebugEx.Assert(testCases != null, "tests");
127126

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

132131
_adapterSettings = MSTestSettingsProvider.Settings;
133132
bool canDeploy = CanDeploy();
134-
bool hasDeploymentItems = tests.Any(DeploymentItemUtility.HasDeploymentItems);
133+
bool hasDeploymentItems = testCases.Any(DeploymentItemUtility.HasDeploymentItems);
135134

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

142-
RunDirectories = _deploymentUtility.CreateDeploymentDirectories(runContext);
141+
string? firstTestSource = testCases.FirstOrDefault()?.Source;
142+
RunDirectories = _deploymentUtility.CreateDeploymentDirectories(runContext, firstTestSource);
143143

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

src/Adapter/MSTestAdapter.PlatformServices/Utilities/DeploymentUtilityBase.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,25 @@ public bool Deploy(IEnumerable<TestCase> tests, string source, IRunContext? runC
5353
/// Create deployment directories.
5454
/// </summary>
5555
/// <param name="runContext">The run context.</param>
56+
/// <param name="firstTestSource">
57+
/// The path to the test assembly of the first test case. In most cases, all
58+
/// test cases belong to the same assembly, but not guaranteed. We are using the path from
59+
/// the first test case as a "best effort" implementation. DeploymentItem isn't correctly designed and should be deprecated in future.
60+
/// </param>
5661
/// <returns>TestRunDirectories instance.</returns>
57-
public TestRunDirectories CreateDeploymentDirectories(IRunContext? runContext)
62+
public TestRunDirectories CreateDeploymentDirectories(IRunContext? runContext, string? firstTestSource)
5863
{
5964
string resultsDirectory = GetTestResultsDirectory(runContext);
6065
string rootDeploymentDirectory = GetRootDeploymentDirectory(resultsDirectory);
6166

62-
var result = new TestRunDirectories(rootDeploymentDirectory);
67+
#if NETFRAMEWORK
68+
bool isAppDomainCreationDisabled = runContext?.RunSettings != null && MSTestAdapterSettings.IsAppDomainCreationDisabled(runContext.RunSettings.SettingsXml);
69+
#else
70+
// AppDomains are only supported in .NET Framework.
71+
const bool isAppDomainCreationDisabled = true;
72+
#endif
73+
74+
var result = new TestRunDirectories(rootDeploymentDirectory, firstTestSource, isAppDomainCreationDisabled);
6375

6476
FileUtility.CreateDirectoryIfNotExists(rootDeploymentDirectory);
6577
FileUtility.CreateDirectoryIfNotExists(result.InDirectory);
@@ -221,6 +233,10 @@ protected IEnumerable<string> Deploy(IList<DeploymentItem> deploymentItems, stri
221233
if (!destToSource.TryGetValue(relativeDestination, out string? value))
222234
{
223235
destToSource.Add(relativeDestination, fileToDeploy);
236+
if (fileToDeploy == destination)
237+
{
238+
continue;
239+
}
224240

225241
// Now, finally we can copy the file...
226242
destination = FileUtility.CopyFileOverwrite(fileToDeploy, destination, out string? warning);

test/IntegrationTests/MSTest.Acceptance.IntegrationTests/DeploymentItemTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class DeploymentItemTests : AcceptanceTestBase<DeploymentItemTests.TestAs
1414

1515
[TestMethod]
1616
[OSCondition(OperatingSystems.Windows)]
17-
[DataRow("AppDomainDisabled.runsettings", IgnoreMessage = "https://github.com/microsoft/testfx/issues/6738")]
17+
[DataRow("AppDomainDisabled.runsettings")]
1818
[DataRow("AppDomainEnabled.runsettings")]
1919
public async Task AssemblyIsLoadedOnceFromDeploymentDirectory(string runsettings)
2020
{

test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Deployment/TestRunDirectoriesTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace MSTestAdapter.PlatformServices.Tests.Deployment;
99

1010
public class TestRunDirectoriesTests : TestContainer
1111
{
12-
private readonly TestRunDirectories _testRunDirectories = new(@"C:\temp");
12+
private readonly TestRunDirectories _testRunDirectories = new(@"C:\temp", @"C:\temp\asm.dll", isAppDomainCreationDisabled: true);
1313

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

test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/DesktopTestDeploymentTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ private TestDeployment CreateAndSetupDeploymentRelatedUtilities(out TestRunDirec
151151
{
152152
string currentExecutingFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
153153

154-
testRunDirectories = new TestRunDirectories(currentExecutingFolder);
154+
testRunDirectories = new TestRunDirectories(currentExecutingFolder, Path.Combine(currentExecutingFolder, "asm.dll"), isAppDomainCreationDisabled: true);
155155

156156
_mockFileUtility.Setup(fu => fu.DoesDirectoryExist(It.Is<string>(s => !s.EndsWith(".dll") && !s.EndsWith(".exe")))).Returns(true);
157157
_mockFileUtility.Setup(fu => fu.DoesFileExist(It.IsAny<string>())).Returns(true);

test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/TestDeploymentTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public void GetDeploymentDirectoryShouldReturnDeploymentOutputDirectory()
169169

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

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

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

396-
testRunDirectories = new TestRunDirectories(currentExecutingFolder);
396+
testRunDirectories = new TestRunDirectories(currentExecutingFolder, Path.Combine(currentExecutingFolder, "asm.dll"), isAppDomainCreationDisabled: true);
397397

398398
_mockFileUtility.Setup(fu => fu.DoesDirectoryExist(It.Is<string>(s => !s.EndsWith(".dll")))).Returns(true);
399399
_mockFileUtility.Setup(fu => fu.DoesFileExist(It.IsAny<string>())).Returns(true);

test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Utilities/DeploymentUtilityTests.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ public DeploymentUtilityTests()
5757

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

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

423423
// Act.
424-
_deploymentUtility.CreateDeploymentDirectories(_mockRunContext.Object);
424+
_deploymentUtility.CreateDeploymentDirectories(_mockRunContext.Object, null);
425425

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

440440
// Act.
441-
_deploymentUtility.CreateDeploymentDirectories(_mockRunContext.Object);
441+
_deploymentUtility.CreateDeploymentDirectories(_mockRunContext.Object, null);
442442

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

456456
// Act.
457-
_deploymentUtility.CreateDeploymentDirectories(null);
457+
_deploymentUtility.CreateDeploymentDirectories(null, null);
458458

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

482-
testRunDirectories = new TestRunDirectories(currentExecutingFolder);
482+
const bool isAppDomainCreationDisabled =
483+
#if NETFRAMEWORK
484+
false;
485+
#else
486+
true;
487+
#endif
488+
489+
testRunDirectories = new TestRunDirectories(currentExecutingFolder, Path.Combine(currentExecutingFolder, "asm.dll"), isAppDomainCreationDisabled);
483490

484491
return testCase;
485492
}

0 commit comments

Comments
 (0)