-
Notifications
You must be signed in to change notification settings - Fork 326
/
Copy pathConsoleLogger.cs
345 lines (302 loc) · 14.3 KB
/
ConsoleLogger.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
// 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.Internal
{
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using Microsoft.VisualStudio.TestPlatform.Utilities;
using CommandLineResources = Microsoft.VisualStudio.TestPlatform.CommandLine.Resources.Resources;
/// <summary>
/// Logger for sending output to the console.
/// </summary>
[FriendlyName(ConsoleLogger.FriendlyName)]
[ExtensionUri(ConsoleLogger.ExtensionUri)]
internal class ConsoleLogger : ITestLogger
{
#region Constants
private const string TestMessageFormattingPrefix = " ";
/// <summary>
/// Uri used to uniquely identify the console logger.
/// </summary>
public const string ExtensionUri = "logger://Microsoft/TestPlatform/ConsoleLogger/v2";
/// <summary>
/// Alternate user friendly string to uniquely identify the console logger.
/// </summary>
public const string FriendlyName = "Console";
#endregion
#region Fields
private TestOutcome testOutcome = TestOutcome.None;
private int testsTotal = 0;
private int testsPassed = 0;
private int testsFailed = 0;
private int testsSkipped = 0;
#endregion
#region Constructor
/// <summary>
/// Default constructor.
/// </summary>
public ConsoleLogger()
{
}
/// <summary>
/// Constructor added for testing purpose
/// </summary>
/// <param name="output"></param>
internal ConsoleLogger(IOutput output)
{
ConsoleLogger.Output = output;
}
#endregion
#region Properties
/// <summary>
/// Gets instance of IOutput used for sending output.
/// </summary>
/// <remarks>Protected so this can be detoured for testing purposes.</remarks>
protected static IOutput Output
{
get;
private set;
}
#endregion
#region ITestLogger
/// <summary>
/// Initializes the Test Logger.
/// </summary>
/// <param name="events">Events that can be registered for.</param>
/// <param name="testRunDirectory">Test Run Directory</param>
public void Initialize(TestLoggerEvents events, string testRunDirectory)
{
if (events == null)
{
throw new ArgumentNullException("events");
}
if (ConsoleLogger.Output == null)
{
ConsoleLogger.Output = ConsoleOutput.Instance;
}
// Register for the events.
events.TestRunMessage += this.TestMessageHandler;
events.TestResult += this.TestResultHandler;
events.TestRunComplete += this.TestRunCompleteHandler;
}
#endregion
#region Private Methods
/// <summary>
/// Prints the timespan onto console.
/// </summary>
private static void PrintTimeSpan(TimeSpan timeSpan)
{
if (timeSpan.TotalDays >= 1)
{
Output.WriteLine(string.Format(CultureInfo.CurrentCulture, CommandLineResources.ExecutionTimeFormatString, timeSpan.TotalDays, CommandLineResources.Days), OutputLevel.Information);
}
else if (timeSpan.TotalHours >= 1)
{
Output.WriteLine(string.Format(CultureInfo.CurrentCulture, CommandLineResources.ExecutionTimeFormatString, timeSpan.TotalHours, CommandLineResources.Hours), OutputLevel.Information);
}
else if (timeSpan.TotalMinutes >= 1)
{
Output.WriteLine(string.Format(CultureInfo.CurrentCulture, CommandLineResources.ExecutionTimeFormatString, timeSpan.TotalMinutes, CommandLineResources.Minutes), OutputLevel.Information);
}
else
{
Output.WriteLine(string.Format(CultureInfo.CurrentCulture, CommandLineResources.ExecutionTimeFormatString, timeSpan.TotalSeconds, CommandLineResources.Seconds), OutputLevel.Information);
}
}
/// <summary>
/// Constructs a well formatted string using the given prefix before every message content on each line.
/// </summary>
private static string GetFormattedOutput(Collection<TestResultMessage> testMessageCollection)
{
if (testMessageCollection != null)
{
StringBuilder sb = new StringBuilder();
foreach (var message in testMessageCollection)
{
string prefix = String.Format(CultureInfo.CurrentCulture, "{0}{1}", Environment.NewLine, TestMessageFormattingPrefix);
string messageText = message.Text.Replace(Environment.NewLine, prefix).TrimEnd(TestMessageFormattingPrefix.ToCharArray());
sb.AppendFormat(CultureInfo.CurrentCulture, "{0}{1}", TestMessageFormattingPrefix, messageText);
}
return sb.ToString();
}
return String.Empty;
}
/// <summary>
/// Collects all the messages of a particular category(Standard Output/Standard Error/Debug Traces) and returns a collection.
/// </summary>
private static Collection<TestResultMessage> GetTestMessages(Collection<TestResultMessage> Messages, string requiredCategory)
{
var selectedMessages = Messages.Where(msg => msg.Category.Equals(requiredCategory, StringComparison.OrdinalIgnoreCase));
Collection<TestResultMessage> requiredMessageCollection = new Collection<TestResultMessage>(selectedMessages.ToList());
return requiredMessageCollection;
}
/// <summary>
/// outputs the Error messages, Stack Trace, and other messages for the parameter test.
/// </summary>
private static void DisplayFullInformation(TestResult result)
{
bool hasData = false;
Debug.Assert(result != null, "a null result can not be displayed");
if (!String.IsNullOrEmpty(result.ErrorMessage))
{
hasData = true;
Output.WriteLine(CommandLineResources.ErrorMessageBanner, OutputLevel.Error);
string errorMessage = String.Format(CultureInfo.CurrentCulture, "{0}{1}", TestMessageFormattingPrefix, result.ErrorMessage);
Output.WriteLine(errorMessage, OutputLevel.Error);
}
if (!String.IsNullOrEmpty(result.ErrorStackTrace))
{
hasData = true;
Output.WriteLine(CommandLineResources.StacktraceBanner, OutputLevel.Error);
string stackTrace = String.Format(CultureInfo.CurrentCulture, "{0}", result.ErrorStackTrace);
Output.Write(stackTrace, OutputLevel.Error);
}
Collection<TestResultMessage> stdOutMessagesCollection = GetTestMessages(result.Messages, TestResultMessage.StandardOutCategory);
if (stdOutMessagesCollection.Count > 0)
{
hasData = true;
string stdOutMessages = GetFormattedOutput(stdOutMessagesCollection);
Output.WriteLine(CommandLineResources.StdOutMessagesBanner, OutputLevel.Information);
Output.Write(stdOutMessages, OutputLevel.Information);
}
Collection<TestResultMessage> stdErrMessagesCollection = GetTestMessages(result.Messages, TestResultMessage.StandardErrorCategory);
if (stdErrMessagesCollection.Count > 0)
{
hasData = true;
string stdErrMessages = GetFormattedOutput(stdErrMessagesCollection);
Output.WriteLine(CommandLineResources.StdErrMessagesBanner, OutputLevel.Error);
Output.Write(stdErrMessages, OutputLevel.Error);
}
Collection<TestResultMessage> addnlInfoMessagesCollection = GetTestMessages(result.Messages, TestResultMessage.AdditionalInfoCategory);
if (addnlInfoMessagesCollection.Count > 0)
{
hasData = true;
Output.WriteLine(CommandLineResources.AddnlInfoMessagesBanner, OutputLevel.Information);
string addnlInfoMessages = GetFormattedOutput(addnlInfoMessagesCollection);
Output.Write(addnlInfoMessages, OutputLevel.Information);
}
if (hasData)
{
Output.WriteLine(String.Empty, OutputLevel.Information);
}
}
#endregion
#region Event Handlers
/// <summary>
/// Called when a test message is received.
/// </summary>
private void TestMessageHandler(object sender, TestRunMessageEventArgs e)
{
ValidateArg.NotNull<object>(sender, "sender");
ValidateArg.NotNull<TestRunMessageEventArgs>(e, "e");
switch (e.Level)
{
case TestMessageLevel.Informational:
Output.Information(e.Message);
break;
case TestMessageLevel.Warning:
Output.Warning(e.Message);
break;
case TestMessageLevel.Error:
this.testOutcome = TestOutcome.Failed;
Output.Error(e.Message);
break;
default:
Debug.Fail("ConsoleLogger.TestMessageHandler: The test message level is unrecognized: {0}", e.Level.ToString());
break;
}
Output.WriteLine(string.Empty, (OutputLevel)e.Level);
}
/// <summary>
/// Called when a test result is received.
/// </summary>
private void TestResultHandler(object sender, TestResultEventArgs e)
{
ValidateArg.NotNull<object>(sender, "sender");
ValidateArg.NotNull<TestResultEventArgs>(e, "e");
// Update the test count statistics based on the result of the test.
this.testsTotal++;
string name = null;
name = !string.IsNullOrEmpty(e.Result.DisplayName) ? e.Result.DisplayName : e.Result.TestCase.FullyQualifiedName;
if (e.Result.Outcome == TestOutcome.Skipped)
{
this.testsSkipped++;
string output = string.Format(CultureInfo.CurrentCulture, CommandLineResources.SkippedTestIndicator, name);
Output.WriteLine(output, OutputLevel.Information);
DisplayFullInformation(e.Result);
}
else if (e.Result.Outcome == TestOutcome.Failed)
{
this.testOutcome = TestOutcome.Failed;
this.testsFailed++;
string output = string.Format(CultureInfo.CurrentCulture, CommandLineResources.FailedTestIndicator, name);
Output.WriteLine(output, OutputLevel.Information);
DisplayFullInformation(e.Result);
}
else if (e.Result.Outcome == TestOutcome.Passed)
{
string output = string.Format(CultureInfo.CurrentCulture, CommandLineResources.PassedTestIndicator, name);
Output.WriteLine(output, OutputLevel.Information);
this.testsPassed++;
}
}
/// <summary>
/// Called when a test run is completed.
/// </summary>
private void TestRunCompleteHandler(object sender, TestRunCompleteEventArgs e)
{
Output.WriteLine(string.Empty, OutputLevel.Information);
// Printing Run-level Attachments
int runLevelAttachementCount = (e.AttachmentSets == null) ? 0 : e.AttachmentSets.Sum(attachmentSet => attachmentSet.Attachments.Count);
if (runLevelAttachementCount > 0)
{
Output.WriteLine(CommandLineResources.AttachmentsBanner, OutputLevel.Information);
foreach (AttachmentSet attachmentSet in e.AttachmentSets)
{
foreach (UriDataAttachment uriDataAttachment in attachmentSet.Attachments)
{
string attachmentOutput = string.Format(CultureInfo.CurrentCulture, CommandLineResources.AttachmentOutputFormat, uriDataAttachment.Uri.LocalPath);
Output.WriteLine(attachmentOutput, OutputLevel.Information);
}
}
Output.WriteLine(String.Empty, OutputLevel.Information);
}
// Output a summary.
if (this.testsTotal > 0)
{
if (this.testOutcome == TestOutcome.Failed)
{
Output.WriteLine(string.Format(CultureInfo.CurrentCulture, CommandLineResources.TestRunSummary, testsTotal, testsPassed, testsFailed, testsSkipped), OutputLevel.Information);
using (new ConsoleColorHelper(ConsoleColor.Red))
{
Output.WriteLine(CommandLineResources.TestRunFailed, OutputLevel.Error);
}
}
else
{
Output.WriteLine(string.Format(CultureInfo.CurrentCulture, CommandLineResources.TestRunSummary, testsTotal, testsPassed, testsFailed, testsSkipped), OutputLevel.Information);
using (new ConsoleColorHelper(ConsoleColor.Green))
{
Output.WriteLine(CommandLineResources.TestRunSuccessful, OutputLevel.Information);
}
}
if (!e.ElapsedTimeInRunningTests.Equals(TimeSpan.Zero))
{
PrintTimeSpan(e.ElapsedTimeInRunningTests);
}
else
{
EqtTrace.Info("Skipped printing test execution time on console because it looks like the test run had faced some errors");
}
}
}
#endregion
}
}