-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathTestDeployment.cs
More file actions
210 lines (178 loc) · 8.54 KB
/
TestDeployment.cs
File metadata and controls
210 lines (178 loc) · 8.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#if !WINDOWS_UWP && !WIN_UI
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter;
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Deployment;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Utilities;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
#if NETFRAMEWORK
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities;
#endif
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices;
/// <summary>
/// The test deployment.
/// </summary>
internal sealed class TestDeployment : ITestDeployment
{
#region Service Utility Variables
private readonly DeploymentItemUtility _deploymentItemUtility;
private readonly DeploymentUtility _deploymentUtility;
private readonly FileUtility _fileUtility;
private MSTestAdapterSettings? _adapterSettings;
#endregion
/// <summary>
/// Initializes a new instance of the <see cref="TestDeployment"/> class.
/// </summary>
public TestDeployment()
: this(new DeploymentItemUtility(ReflectHelper.Instance), new DeploymentUtility(), new FileUtility())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="TestDeployment"/> class. Used for unit tests.
/// </summary>
/// <param name="deploymentItemUtility"> The deployment item utility. </param>
/// <param name="deploymentUtility"> The deployment utility. </param>
/// <param name="fileUtility"> The file utility. </param>
internal TestDeployment(DeploymentItemUtility deploymentItemUtility, DeploymentUtility deploymentUtility, FileUtility fileUtility)
{
_deploymentItemUtility = deploymentItemUtility;
_deploymentUtility = deploymentUtility;
_fileUtility = fileUtility;
_adapterSettings = null;
RunDirectories = null;
}
/// <summary>
/// Gets the current run directories for this session.
/// </summary>
/// <remarks>
/// This is initialized at the beginning of a run session when Deploy is called.
/// Leaving this as a static variable since the testContext needs to be filled in with this information.
/// </remarks>
internal static TestRunDirectories? RunDirectories { get; private set; }
/// <summary>
/// The get deployment items.
/// </summary>
/// <param name="method"> The method. </param>
/// <param name="type"> The type. </param>
/// <param name="warnings"> The warnings. </param>
/// <returns> A string of deployment items. </returns>
public KeyValuePair<string, string>[]? GetDeploymentItems(MethodInfo method, Type type, ICollection<string> warnings) =>
_deploymentItemUtility.GetDeploymentItems(method, _deploymentItemUtility.GetClassLevelDeploymentItems(type, warnings), warnings);
/// <summary>
/// Cleanup deployment item directories.
/// </summary>
public void Cleanup()
{
// Delete the deployment directory
if (RunDirectories != null && _adapterSettings?.DeleteDeploymentDirectoryAfterTestRunIsComplete == true)
{
if (PlatformServiceProvider.Instance.AdapterTraceLogger.IsInfoEnabled)
{
PlatformServiceProvider.Instance.AdapterTraceLogger.Info("Deleting deployment directory {0}", RunDirectories.RootDeploymentDirectory);
}
_fileUtility.DeleteDirectories(RunDirectories.RootDeploymentDirectory);
if (PlatformServiceProvider.Instance.AdapterTraceLogger.IsInfoEnabled)
{
PlatformServiceProvider.Instance.AdapterTraceLogger.Info("Deleted deployment directory {0}", RunDirectories.RootDeploymentDirectory);
}
}
}
/// <summary>
/// Gets the deployment output directory where the source file along with all its dependencies is dropped.
/// </summary>
/// <returns> The deployment output directory. </returns>
public string? GetDeploymentDirectory() =>
RunDirectories?.OutDirectory;
/// <summary>
/// Deploy files related to the list of tests specified.
/// </summary>
/// <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>
public bool Deploy(IEnumerable<TestCase> testCases, IRunContext? runContext, IFrameworkHandle frameworkHandle)
{
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 = testCases.Any(DeploymentItemUtility.HasDeploymentItems);
// deployment directories should not be created in this case,simply return
if (!canDeploy && hasDeploymentItems)
{
return false;
}
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.
if (!hasDeploymentItems)
{
return false;
}
// Object model currently does not have support for SuspendCodeCoverage. We can remove this once support is added
#if NETFRAMEWORK
using (new SuspendCodeCoverage())
#endif
{
// Group the tests by source
var testsBySource = from test in testCases
group test by test.Source into testGroup
select new { Source = testGroup.Key, Tests = testGroup };
TestRunDirectories runDirectories = RunDirectories;
foreach (var group in testsBySource)
{
// do the deployment
_deploymentUtility.Deploy(@group.Tests, @group.Source, runContext, frameworkHandle, RunDirectories);
}
// Update the runDirectories
RunDirectories = runDirectories;
}
return true;
}
internal static IDictionary<string, object> GetDeploymentInformation(string? source)
{
var properties = new Dictionary<string, object>(capacity: 8);
string applicationBaseDirectory = string.Empty;
// Run directories can be null in win8.
if (RunDirectories == null && !StringEx.IsNullOrEmpty(source))
{
// applicationBaseDirectory is set at source level
applicationBaseDirectory = Path.GetDirectoryName(source)!;
}
properties[TestContext.TestRunDirectoryLabel] = RunDirectories?.RootDeploymentDirectory ?? applicationBaseDirectory;
properties[TestContext.DeploymentDirectoryLabel] = RunDirectories?.OutDirectory ?? applicationBaseDirectory;
properties[TestContext.ResultsDirectoryLabel] = RunDirectories?.InDirectory ?? applicationBaseDirectory;
properties[TestContext.TestRunResultsDirectoryLabel] = RunDirectories?.InMachineNameDirectory ?? applicationBaseDirectory;
properties[TestContext.TestResultsDirectoryLabel] = RunDirectories?.InDirectory ?? applicationBaseDirectory;
return properties;
}
/// <summary>
/// Reset the static variable to default values. Used only for testing purposes.
/// </summary>
internal static void Reset() => RunDirectories = null;
/// <summary>
/// Returns whether deployment can happen or not.
/// </summary>
/// <returns>True if deployment can be done.</returns>
private bool CanDeploy()
{
DebugEx.Assert(_adapterSettings is not null, "Adapter settings should not be null.");
if (!_adapterSettings.DeploymentEnabled)
{
if (PlatformServiceProvider.Instance.AdapterTraceLogger.IsInfoEnabled)
{
PlatformServiceProvider.Instance.AdapterTraceLogger.Info("MSTestExecutor: CanDeploy is false.");
}
return false;
}
return true;
}
}
#endif