/
DynamicCoverageDataCollectorImpl.cs
348 lines (299 loc) · 12.8 KB
/
DynamicCoverageDataCollectorImpl.cs
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.VisualStudio.Coverage
{
using System;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Xml;
using Interfaces;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;
using Microsoft.VisualStudio.TestPlatform.Utilities;
using TestPlatform.ObjectModel;
using TraceCollector;
using TraceCollector.Interfaces;
using TraceDataCollector.Resources;
using IDataCollectionSink = TraceCollector.IDataCollectionSink;
/// <summary>
/// Create config file and output directory required for vanguard process and manages life cycle of vanguard process.
/// </summary>
internal class DynamicCoverageDataCollectorImpl : IDynamicCoverageDataCollectorImpl
{
/// <summary>
/// Name of elements under which all the config details required for vanguard process.
/// </summary>
private const string ConfigCodeCoverageElementName = "CodeCoverage";
/// <summary>
/// File name which contains config for vanguard process.
/// </summary>
private const string VanguardConfigFileName = "CodeCoverage.config";
/// <summary>
/// Name of element for custom coverage filename.
/// </summary>
private const string CoverageFileSettingName = "CoverageFileName";
/// <summary>
/// Coverage file name
/// </summary>
private string coverageFileName;
/// <summary>
/// Logger
/// </summary>
private IDataCollectionLogger logger;
/// <summary>
/// Data sink
/// </summary>
private TraceCollector.IDataCollectionSink dataSink;
/// <summary>
/// Directory helper
/// </summary>
private IDirectoryHelper directoryHelper;
/// <summary>
/// File helper
/// </summary>
private IFileHelper fileHelper;
/// <summary>
/// Folder to store temporary files
/// </summary>
private string sessionDirectory;
private string coverageFilePath;
public DynamicCoverageDataCollectorImpl()
: this(new Vanguard(), new DirectoryHelper(), new FileHelper())
{
}
internal DynamicCoverageDataCollectorImpl(IVanguard vanguard, IDirectoryHelper directoryHelper, IFileHelper fileHelper)
{
this.Vanguard = vanguard;
this.directoryHelper = directoryHelper;
this.fileHelper = fileHelper;
}
/// <summary>
/// Gets or sets session name
/// </summary>
private string SessionName { get; set; }
/// <summary>
/// Gets or sets vanguard instance
/// </summary>
private IVanguard Vanguard { get; set; }
public string GetSessionName()
{
return this.SessionName;
}
/// <summary>
/// Initialize
/// </summary>
/// <param name="configurationElement">Configuration element</param>
/// <param name="dataSink">Data sink</param>
/// <param name="logger">Logger</param>
public virtual void Initialize(
XmlElement configurationElement,
IDataCollectionSink dataSink,
IDataCollectionLogger logger)
{
var defaultConfigurationElement = DynamicCoverageDataCollectorImpl.GetDefaultConfiguration();
try
{
// WARNING: Do NOT remove this function call !!!
//
// Due to a dependency we took on Microsoft.TestPlatform.Utilities.dll, an
// exception may be thrown if we cannot resolve CodeCoverageRunSettingsProcessor.
// If such an exception is thrown we cannot catch it in this try-catch block
// because all method dependencies must be resolved before the method call, thus
// we introduced an additional layer of indirection.
configurationElement = this.AddDefaultExclusions(configurationElement, defaultConfigurationElement);
}
catch (Exception ex)
{
EqtTrace.Warning(
string.Format(
CultureInfo.CurrentCulture,
string.Join(
" ",
"DynamicCoverageDataCollectorImpl.Initialize: Exception encountered while processing the configuration element.",
"Keeping the configuration element unaltered. More info about the exception: {0}"),
ex.Message));
}
EqtTrace.Info("DynamicCoverageDataCollectorImpl.Initialize: Initialize configuration. ");
if (string.IsNullOrEmpty(configurationElement?.InnerXml))
{
configurationElement = defaultConfigurationElement;
}
this.logger = logger;
this.dataSink = dataSink;
this.dataSink.SendFileCompleted += this.OnSendFileCompletedEvent;
this.SessionName = Guid.NewGuid().ToString();
this.sessionDirectory = Path.Combine(Path.GetTempPath(), this.SessionName);
this.directoryHelper.CreateDirectory(this.sessionDirectory);
this.SetCoverageFileName(configurationElement);
this.PrepareVanguardProcess(configurationElement);
}
/// <summary>
/// Cleanup temp folder
/// </summary>
public virtual void Dispose()
{
if (this.Vanguard != null)
{
this.Vanguard.Stop();
this.Vanguard.Dispose();
}
if (this.dataSink != null)
{
this.dataSink.SendFileCompleted -= this.OnSendFileCompletedEvent;
}
this.CleanupDirectory();
}
/// <summary>
/// Session start
/// </summary>
/// <param name="sender">Sender</param>
/// <param name="e">Event arguments</param>
public void SessionStart(object sender, SessionStartEventArgs e)
{
this.StartVanguard(e.Context);
}
/// <summary>
/// Session end
/// </summary>
/// <param name="sender">Sender</param>
/// <param name="e">Event arguments</param>
public void SessionEnd(object sender, SessionEndEventArgs e)
{
this.StopVanguard(e.Context);
}
/// <summary>
/// Start vanguard
/// </summary>
/// <param name="context">Context</param>
protected void StartVanguard(DataCollectionContext context)
{
if (this.Vanguard != null)
{
string outputCoverageFolder = Path.Combine(this.sessionDirectory, Guid.NewGuid().ToString());
this.CreateDirectory(context, outputCoverageFolder);
this.coverageFilePath = Path.Combine(outputCoverageFolder, this.coverageFileName);
try
{
this.Vanguard.Start(this.coverageFilePath, context);
}
catch (Exception ex)
{
EqtTrace.Error(
"DynamicCoverageDataCollectorImpl.StartVanguard: Failed to start Vanguard for datacollection context sessionID: {0}, with exception: {1}",
context.SessionId,
ex);
this.logger.LogError(context, ex);
throw;
}
}
}
/// <summary>
/// Stop vanguard
/// </summary>
/// <param name="context">Context</param>
protected void StopVanguard(DataCollectionContext context)
{
EqtTrace.Info("DynamicCoverageDataCollectorImpl.StopVanguard: Calling Stop Vanguard. datacollection context sessionID: {0}", context.SessionId);
if (this.Vanguard != null)
{
this.Vanguard.Stop();
if (this.fileHelper.Exists(this.coverageFilePath))
{
this.dataSink.SendFileAsync(context, this.coverageFilePath, false);
}
this.Vanguard = null;
}
}
protected void OnSendFileCompletedEvent(object sender, AsyncCompletedEventArgs e)
{
this.CleanupDirectory();
}
/// <summary>
/// Generate the file name for coverage file.
/// </summary>
/// <returns>Returns code coverage file name.</returns>
private static string GenerateCoverageFileName()
{
string GetUserName()
{
return Environment.GetEnvironmentVariable("USERNAME") ?? Environment.GetEnvironmentVariable("USER");
}
return string.Format(
CultureInfo.InvariantCulture,
"{0}_{1}_{2}.coverage",
GetUserName(),
Environment.MachineName,
DateTime.Now.ToString("yyyy-MM-dd.HH_mm_ss", CultureInfo.InvariantCulture));
}
private static XmlElement GetDefaultConfiguration()
{
// Add default configuration specific to CodeCoverage. https://msdn.microsoft.com/en-us/library/jj635153.aspx
var doc = new XmlDocument();
Assembly a = typeof(DynamicCoverageDataCollectorImpl).GetTypeInfo().Assembly;
using (Stream s = a.GetManifestResourceStream(
"Microsoft.VisualStudio.TraceDataCollector.VanguardCollector.DefaultCodeCoverageConfig.xml"))
{
doc.Load(s);
}
return doc.DocumentElement;
}
private void SetCoverageFileName(XmlElement configurationElement)
{
XmlElement coverageFileNameElement = configurationElement[CoverageFileSettingName];
this.coverageFileName = coverageFileNameElement != null
? coverageFileNameElement.InnerText
: GenerateCoverageFileName();
}
private void PrepareVanguardProcess(XmlElement configurationElement)
{
EqtTrace.Info("DynamicCoverageDataCollectorImpl.PrepareVanguardProcess: Preparing Vanguard process.");
XmlElement config = configurationElement[ConfigCodeCoverageElementName]
?? DynamicCoverageDataCollectorImpl.GetDefaultConfiguration()[ConfigCodeCoverageElementName];
string configurationFileName = Path.Combine(this.sessionDirectory, VanguardConfigFileName);
this.fileHelper.WriteAllText(configurationFileName, config.OuterXml);
EqtTrace.Info("DynamicCoverageDataCollectorImpl.PrepareVanguardProcess: Initializing with config: {0}.", config.OuterXml);
this.Vanguard.Initialize(this.SessionName, configurationFileName, this.logger);
}
private void CleanupDirectory()
{
try
{
if (this.directoryHelper.Exists(this.sessionDirectory))
{
this.directoryHelper.Delete(this.sessionDirectory, true);
}
}
catch (Exception ex)
{
EqtTrace.Warning("DynamicCoverageDataCollectorImpl.CleanupDirectory:Failed to delete directory: {0}, with exception: {1}", this.sessionDirectory, ex);
}
}
private void CreateDirectory(DataCollectionContext context, string path)
{
try
{
this.directoryHelper.CreateDirectory(path);
}
catch (Exception ex)
{
EqtTrace.Error("DynamicCoverageDataCollectorImpl.CreateDirectory:Failed to create directory: {0}, with exception: {1}", path, ex);
this.logger.LogError(
context,
string.Format(CultureInfo.CurrentUICulture, Resources.FailedToCreateDirectory, path, ex));
throw;
}
}
/// <summary>
/// Adding default exclusions to the configuration element.
/// </summary>
/// <param name="configurationElement">The configuration element.</param>
/// <param name="defaultConfigurationElement">The default configuration element.</param>
/// <returns>The original configuration element with additional default exclusions.</returns>
private XmlElement AddDefaultExclusions(XmlElement configurationElement, XmlElement defaultConfigurationElement)
{
var processor = new CodeCoverageRunSettingsProcessor(defaultConfigurationElement);
return (XmlElement)processor.Process(configurationElement);
}
}
}