-
Notifications
You must be signed in to change notification settings - Fork 315
/
EnableDiagArgumentProcessor.cs
259 lines (214 loc) · 9.76 KB
/
EnableDiagArgumentProcessor.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
// 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.TestPlatform.CommandLine.Processors
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using Microsoft.VisualStudio.TestPlatform.CommandLine.Internal;
using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.Utilities;
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;
using CommandLineResources = Microsoft.VisualStudio.TestPlatform.CommandLine.Resources.Resources;
internal class EnableDiagArgumentProcessor : IArgumentProcessor
{
/// <summary>
/// The name of the command line argument that the ListTestsArgumentExecutor handles.
/// </summary>
public const string CommandName = "/Diag";
private readonly IFileHelper fileHelper;
private Lazy<IArgumentProcessorCapabilities> metadata;
private Lazy<IArgumentExecutor> executor;
/// <summary>
/// Initializes a new instance of the <see cref="EnableDiagArgumentProcessor"/> class.
/// </summary>
public EnableDiagArgumentProcessor() : this(new FileHelper())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="EnableDiagArgumentProcessor"/> class.
/// </summary>
/// <param name="fileHelper">A file helper instance.</param>
protected EnableDiagArgumentProcessor(IFileHelper fileHelper)
{
this.fileHelper = fileHelper;
}
public Lazy<IArgumentProcessorCapabilities> Metadata
{
get
{
if (this.metadata == null)
{
this.metadata = new Lazy<IArgumentProcessorCapabilities>(() => new EnableDiagArgumentProcessorCapabilities());
}
return this.metadata;
}
}
/// <summary>
/// Gets or sets the executor.
/// </summary>
public Lazy<IArgumentExecutor> Executor
{
get
{
if (this.executor == null)
{
this.executor = new Lazy<IArgumentExecutor>(() => new EnableDiagArgumentExecutor(this.fileHelper));
}
return this.executor;
}
set
{
this.executor = value;
}
}
}
/// <summary>
/// The argument capabilities.
/// </summary>
internal class EnableDiagArgumentProcessorCapabilities : BaseArgumentProcessorCapabilities
{
public override string CommandName => EnableDiagArgumentProcessor.CommandName;
public override bool AllowMultiple => false;
public override bool IsAction => false;
public override ArgumentProcessorPriority Priority => ArgumentProcessorPriority.Diag;
public override string HelpContentResourceName => CommandLineResources.EnableDiagUsage;
public override HelpContentPriority HelpPriority => HelpContentPriority.EnableDiagArgumentProcessorHelpPriority;
}
/// <summary>
/// The argument executor.
/// </summary>
internal class EnableDiagArgumentExecutor : IArgumentExecutor
{
private readonly IFileHelper fileHelper;
/// <summary>
/// Parameter for trace level
/// </summary>
public const string TraceLevelParam = "tracelevel";
#region Constructor
/// <summary>
/// Default constructor.
/// </summary>
/// <param name="fileHelper">The file helper.</param>
public EnableDiagArgumentExecutor(IFileHelper fileHelper)
{
this.fileHelper = fileHelper;
}
#endregion
#region IArgumentExecutor
/// <summary>
/// Initializes with the argument that was provided with the command.
/// </summary>
/// <param name="argument">Argument that was provided with the command.</param>
public void Initialize(string argument)
{
string exceptionMessage = string.Format(CultureInfo.CurrentUICulture, CommandLineResources.InvalidDiagArgument, argument);
// Throw error if argument is null or empty.
if (string.IsNullOrWhiteSpace(argument))
{
throw new CommandLineException(exceptionMessage);
}
// Get diag argument list.
var diagArgumentList = ArgumentProcessorUtilities.GetArgumentList(argument, ArgumentProcessorUtilities.SemiColonArgumentSeparator, exceptionMessage);
// Get diag file path.
// Note: Even though semi colon is valid file path, we are not respecting the file name having semi-colon [As we are separating arguments based on semi colon].
var diagFilePathArg = diagArgumentList[0];
var diagFilePath = GetDiagFilePath(diagFilePathArg);
// Get diag parameters.
var diagParameterArgs = diagArgumentList.Skip(1);
var diagParameters = ArgumentProcessorUtilities.GetArgumentParameters(diagParameterArgs, ArgumentProcessorUtilities.EqualNameValueSeparator, exceptionMessage);
// Initialize diag logging.
InitializeDiagLogging(diagFilePath, diagParameters);
// Write version to the log here, because that is the
// first place where we know if we log or not.
EqtTrace.Verbose($"Version: {Product.Version}");
}
/// <summary>
/// Executes the argument processor.
/// </summary>
/// <returns>The <see cref="ArgumentProcessorResult"/>.</returns>
public ArgumentProcessorResult Execute()
{
// Nothing to do since we updated the parameter during initialize parameter
return ArgumentProcessorResult.Success;
}
/// <summary>
/// Initialize diag logging.
/// </summary>
/// <param name="diagFilePath">Diag file path.</param>
/// <param name="diagParameters">Diag parameters</param>
private void InitializeDiagLogging(string diagFilePath, Dictionary<string, string> diagParameters)
{
// Get trace level from diag parameters.
var traceLevel = GetDiagTraceLevel(diagParameters);
// Initialize trace.
// Trace initialized is false in case of any exception at time of initialization like Catch exception(UnauthorizedAccessException, PathTooLongException...)
var traceInitialized = EqtTrace.InitializeTrace(diagFilePath, traceLevel);
// Show console warning in case trace is not initialized.
if (!traceInitialized && !string.IsNullOrEmpty(EqtTrace.ErrorOnInitialization))
{
ConsoleOutput.Instance.Warning(false, EqtTrace.ErrorOnInitialization);
}
}
/// <summary>
/// Gets diag trace level.
/// </summary>
/// <param name="diagParameters">Diag parameters.</param>
/// <returns>Diag trace level.</returns>
private PlatformTraceLevel GetDiagTraceLevel(Dictionary<string, string> diagParameters)
{
// If diag parameters is null, set value of trace level as verbose.
if (diagParameters == null)
{
return PlatformTraceLevel.Verbose;
}
// Get trace level from diag parameters.
var traceLevelExists = diagParameters.TryGetValue(TraceLevelParam, out string traceLevelStr);
if (traceLevelExists && Enum.TryParse(traceLevelStr, true, out PlatformTraceLevel traceLevel))
{
return traceLevel;
}
// Default value of diag trace level is verbose.
return PlatformTraceLevel.Verbose;
}
/// <summary>
/// Gets diag file path.
/// </summary>
/// <param name="diagFilePathArgument">Diag file path argument.</param>
/// <returns>Diag file path.</returns>
private string GetDiagFilePath(string diagFilePathArgument)
{
// Remove double quotes if present.
diagFilePathArgument = diagFilePathArgument.Replace("\"", "");
// Throw error in case diag file path is not a valid file path
var fileExtension = Path.GetExtension(diagFilePathArgument);
if (string.IsNullOrWhiteSpace(fileExtension))
{
throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, CommandLineResources.InvalidDiagFilePath, diagFilePathArgument));
}
// Create base directory for diag file path (if doesn't exist)
CreateDirectoryIfNotExists(diagFilePathArgument);
// return full diag file path. (This is done so that vstest and testhost create logs at same location.)
return Path.GetFullPath(diagFilePathArgument);
}
/// <summary>
/// Create directory if not exists.
/// </summary>
/// <param name="filePath">File path.</param>
private void CreateDirectoryIfNotExists(string filePath)
{
// Create the base directory of file path if doesn't exist.
// Directory could be empty if just a filename is provided. E.g. log.txt
var directory = Path.GetDirectoryName(filePath);
if (!string.IsNullOrEmpty(directory) && !this.fileHelper.DirectoryExists(directory))
{
this.fileHelper.CreateDirectory(directory);
}
}
#endregion
}
}