-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathDeploymentItemUtility.cs
More file actions
268 lines (223 loc) · 9.43 KB
/
DeploymentItemUtility.cs
File metadata and controls
268 lines (223 loc) · 9.43 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// 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.Helpers;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Deployment;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Utilities;
/// <summary>
/// The deployment utility.
/// </summary>
internal sealed class DeploymentItemUtility
{
private readonly ReflectHelper _reflectHelper;
/// <summary>
/// A cache for class level deployment items.
/// </summary>
private readonly Dictionary<Type, IList<DeploymentItem>> _classLevelDeploymentItems;
/// <summary>
/// Initializes a new instance of the <see cref="DeploymentItemUtility"/> class.
/// </summary>
/// <param name="reflectHelper"> The reflect helper. </param>
internal DeploymentItemUtility(ReflectHelper reflectHelper)
{
_reflectHelper = reflectHelper;
_classLevelDeploymentItems = [];
}
/// <summary>
/// Get the class level deployment items.
/// </summary>
/// <param name="type"> The type. </param>
/// <param name="warnings"> The warnings. </param>
/// <returns> The <see cref="IList{T}"/> of deployment items on a class. </returns>
internal IList<DeploymentItem> GetClassLevelDeploymentItems(Type type, ICollection<string> warnings)
{
if (!_classLevelDeploymentItems.TryGetValue(type, out IList<DeploymentItem>? value))
{
IEnumerable<DeploymentItemAttribute> deploymentItemAttributes = _reflectHelper.GetAttributes<DeploymentItemAttribute>(type);
value = GetDeploymentItems(deploymentItemAttributes, warnings);
_classLevelDeploymentItems[type] = value;
}
return value;
}
/// <summary>
/// The get deployment items.
/// </summary> <param name="method"> The method. </param>
/// <param name="classLevelDeploymentItems"> The class level deployment items. </param>
/// <param name="warnings"> The warnings. </param>
/// <returns> The <see cref="KeyValuePair{TKey,TValue}"/>.of deployment item information. </returns>
internal KeyValuePair<string, string>[]? GetDeploymentItems(MethodInfo method, IEnumerable<DeploymentItem> classLevelDeploymentItems,
ICollection<string> warnings)
{
List<DeploymentItem> testLevelDeploymentItems = GetDeploymentItems(_reflectHelper.GetAttributes<DeploymentItemAttribute>(method), warnings);
return ToKeyValuePairs(Concat(testLevelDeploymentItems, classLevelDeploymentItems));
}
/// <summary>
/// Checks if parameters are valid to create deployment item.
/// </summary>
/// <param name="sourcePath"> The source Path. </param>
/// <param name="relativeOutputDirectory"> The relative Output Directory. </param>
/// <param name="warning"> The warning message if it is an invalid deployment item. </param>
/// <returns> Returns true if it is a valid deployment item. </returns>
[SuppressMessage("Microsoft.Design", "CA1021:AvoidOutParameters", MessageId = "2#", Justification = "Internal method.")]
internal static bool IsValidDeploymentItem([NotNullWhen(true)] string? sourcePath, [NotNullWhen(true)] string? relativeOutputDirectory, [NotNullWhen(false)] out string? warning)
{
if (StringEx.IsNullOrEmpty(sourcePath))
{
warning = Resource.DeploymentItemPathCannotBeNullOrEmpty;
return false;
}
if (relativeOutputDirectory == null)
{
warning = Resource.DeploymentItemOutputDirectoryCannotBeNull;
return false;
}
if (IsInvalidPath(sourcePath) || IsInvalidPath(relativeOutputDirectory))
{
warning = string.Format(CultureInfo.CurrentCulture, Resource.DeploymentItemContainsInvalidCharacters, sourcePath, relativeOutputDirectory);
return false;
}
if (Path.IsPathRooted(relativeOutputDirectory))
{
warning = string.Format(CultureInfo.CurrentCulture, Resource.DeploymentItemOutputDirectoryMustBeRelative, relativeOutputDirectory);
return false;
}
warning = null;
return true;
}
/// <summary>
/// Returns whether there are any deployment items defined on the test.
/// </summary>
/// <param name="testCase"> The test Case. </param>
/// <returns> True if has deployment items.</returns>
internal static bool HasDeploymentItems(TestCase testCase)
{
KeyValuePair<string, string>[]? deploymentItems = GetDeploymentItems(testCase);
return deploymentItems is { Length: > 0 };
}
internal static IList<DeploymentItem> GetDeploymentItems(IEnumerable<TestCase> tests)
{
List<DeploymentItem> allDeploymentItems = [];
foreach (TestCase test in tests)
{
KeyValuePair<string, string>[]? items = GetDeploymentItems(test);
if (items == null || items.Length == 0)
{
continue;
}
IList<DeploymentItem>? deploymentItemsToBeAdded = FromKeyValuePairs(items);
// TODO: Check if we can avoid potential NRE here.
foreach (DeploymentItem deploymentItemToBeAdded in deploymentItemsToBeAdded!)
{
AddDeploymentItem(allDeploymentItems, deploymentItemToBeAdded);
}
}
return allDeploymentItems;
}
internal static void AddDeploymentItem(IList<DeploymentItem> deploymentItemList, DeploymentItem deploymentItem)
{
DebugEx.Assert(deploymentItemList != null, "DeploymentItem list cannot be null");
DebugEx.Assert(deploymentItem != null, "DeploymentItem cannot be null");
if (!deploymentItemList.Contains(deploymentItem))
{
deploymentItemList.Add(deploymentItem);
}
}
private static bool IsInvalidPath(string path)
{
if (path.IndexOfAny(Path.GetInvalidPathChars()) != -1)
{
return true;
}
try
{
string fileName = Path.GetFileName(path);
if (fileName.IndexOfAny(Path.GetInvalidFileNameChars()) != -1)
{
return true;
}
}
catch
{
return true;
}
return false;
}
private static List<DeploymentItem> GetDeploymentItems(IEnumerable<DeploymentItemAttribute> deploymentItemAttributes, ICollection<string> warnings)
{
var deploymentItems = new List<DeploymentItem>();
foreach (DeploymentItemAttribute deploymentItemAttribute in deploymentItemAttributes)
{
if (IsValidDeploymentItem(deploymentItemAttribute.Path, deploymentItemAttribute.OutputDirectory, out string? warning))
{
AddDeploymentItem(deploymentItems, new DeploymentItem(deploymentItemAttribute.Path, deploymentItemAttribute.OutputDirectory));
}
else
{
warnings.Add(warning);
}
}
return deploymentItems;
}
[return: NotNullIfNotNull(nameof(deploymentItemList1))]
[return: NotNullIfNotNull(nameof(deploymentItemList2))]
private static IEnumerable<DeploymentItem>? Concat(IEnumerable<DeploymentItem>? deploymentItemList1, IEnumerable<DeploymentItem>? deploymentItemList2)
{
if (deploymentItemList1 == null && deploymentItemList2 == null)
{
return null;
}
if (deploymentItemList1 == null)
{
return deploymentItemList2;
}
if (deploymentItemList2 == null)
{
return deploymentItemList1;
}
IList<DeploymentItem> result = [.. deploymentItemList1];
foreach (DeploymentItem item in deploymentItemList2)
{
AddDeploymentItem(result, item);
}
return result;
}
/// <summary>
/// Returns the deployment items defined on the test.
/// </summary>
/// <param name="testCase"> The test Case. </param>
/// <returns> The <see cref="KeyValuePair{TKey,TValue}"/>. </returns>
private static KeyValuePair<string, string>[]? GetDeploymentItems(TestCase testCase) => testCase.GetPropertyValue(EngineConstants.DeploymentItemsProperty) as
KeyValuePair<string, string>[];
private static KeyValuePair<string, string>[]? ToKeyValuePairs(IEnumerable<DeploymentItem> deploymentItemList)
{
if (!deploymentItemList.Any())
{
return null;
}
List<KeyValuePair<string, string>> result = [];
foreach (DeploymentItem deploymentItem in deploymentItemList)
{
if (deploymentItem != null)
{
result.Add(new KeyValuePair<string, string>(deploymentItem.SourcePath, deploymentItem.RelativeOutputDirectory));
}
}
return [.. result];
}
private static IList<DeploymentItem>? FromKeyValuePairs(KeyValuePair<string, string>[] deploymentItemsData)
{
if (deploymentItemsData.Length == 0)
{
return null;
}
IList<DeploymentItem> result = [];
foreach ((string? key, string? value) in deploymentItemsData)
{
AddDeploymentItem(result, new DeploymentItem(key, value));
}
return result;
}
}
#endif