-
Notifications
You must be signed in to change notification settings - Fork 308
Expand file tree
/
Copy pathAzureDevOpsReporter.cs
More file actions
435 lines (375 loc) · 19.2 KB
/
Copy pathAzureDevOpsReporter.cs
File metadata and controls
435 lines (375 loc) · 19.2 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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.Testing.Extensions.AzureDevOpsReport.Resources;
using Microsoft.Testing.Extensions.Reporting;
using Microsoft.Testing.Platform.CommandLine;
using Microsoft.Testing.Platform.Extensions;
using Microsoft.Testing.Platform.Extensions.Messages;
using Microsoft.Testing.Platform.Extensions.OutputDevice;
using Microsoft.Testing.Platform.Helpers;
using Microsoft.Testing.Platform.Logging;
using Microsoft.Testing.Platform.OutputDevice;
using Microsoft.Testing.TestInfrastructure;
namespace Microsoft.Testing.Extensions.AzureDevOpsReport;
internal sealed class AzureDevOpsReporter :
IDataConsumer,
IOutputDeviceDataProducer
{
internal const double KnownFlakyFailureRateThreshold = 0.25;
private const int MinSamplesForRegressionAnnotation = 5;
private const string QuarantineBuildTagLine = "##vso[build.addbuildtag]has-quarantined-test-failure";
private const string WarningSeverity = "warning";
private readonly IOutputDevice _outputDisplay;
private readonly ILogger _logger;
private readonly ICommandLineOptions _commandLine;
private readonly IEnvironment _environment;
private readonly IFileSystem _fileSystem;
private readonly IAzureDevOpsHistoryService _historyService;
private readonly string _targetFrameworkMoniker;
private string? _severity;
private bool _demoteKnownFlaky;
private QuarantineFile? _quarantineFile;
private bool _hasLoadedEnabledConfiguration;
private int _quarantineBuildTagEmitted;
private Regex[]? _userStackFrameFilters;
public AzureDevOpsReporter(
ICommandLineOptions commandLine,
IEnvironment environment,
IFileSystem fileSystem,
IOutputDevice outputDisplay,
ILoggerFactory loggerFactory,
IAzureDevOpsHistoryService historyService)
{
_commandLine = commandLine;
_environment = environment;
_fileSystem = fileSystem;
_outputDisplay = outputDisplay;
_historyService = historyService;
_logger = loggerFactory.CreateLogger<AzureDevOpsReporter>();
_targetFrameworkMoniker = TargetFrameworkMonikerHelper.GetTargetFrameworkMonikerIncludingPlatform();
}
public Type[] DataTypesConsumed { get; } =
[
typeof(TestNodeUpdateMessage)
];
/// <inheritdoc />
public string Uid => nameof(AzureDevOpsReporter);
/// <inheritdoc />
public string Version => ExtensionVersion.DefaultSemVer;
/// <inheritdoc />
public string DisplayName { get; } = AzureDevOpsResources.DisplayName;
/// <inheritdoc />
public string Description { get; } = AzureDevOpsResources.Description;
/// <inheritdoc />
public Task<bool> IsEnabledAsync()
{
bool isEnabledByParameter = _commandLine.IsOptionSet(AzureDevOpsCommandLineOptions.AzureDevOpsOptionName);
if (_logger.IsEnabled(LogLevel.Trace))
{
_logger.LogTrace($"{nameof(AzureDevOpsReport)} is {(isEnabledByParameter ? "enabled" : "disabled")}.");
}
if (!isEnabledByParameter)
{
return Task.FromResult(false);
}
bool isEnabledByEnvVariable = AzureDevOpsConstants.IsRunningInAzureDevOps(_environment);
if (isEnabledByEnvVariable)
{
EnsureEnabledConfigurationLoaded();
}
bool annotationsEnabled = AzureDevOpsConstants.IsFeatureKnobEnabled(_commandLine, AzureDevOpsCommandLineOptions.AzureDevOpsAnnotations);
if (_logger.IsEnabled(LogLevel.Trace))
{
_logger.LogTrace($"{AzureDevOpsConstants.TfBuildEnvironmentVariableName} environment variable is {(isEnabledByEnvVariable ? "enabled. Will report errors to Azure DevOps, because we are running in CI." : "disabled. Will not report errors to Azure DevOps.")}");
_logger.LogTrace($"Severity is set to '{_severity ?? "error"}', you can override it by using --report-azdo-severity parameter.");
_logger.LogTrace($"Failure annotations are {(annotationsEnabled ? "enabled" : "disabled")}, you can toggle them by using --report-azdo-annotations parameter.");
}
return Task.FromResult(isEnabledByEnvVariable && annotationsEnabled);
}
public async Task ConsumeAsync(IDataProducer dataProducer, IData value, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
if (value is not TestNodeUpdateMessage nodeUpdateMessage)
{
return;
}
EnsureEnabledConfigurationLoaded();
TestNodeStateProperty? nodeState = nodeUpdateMessage.TestNode.Properties.SingleOrDefault<TestNodeStateProperty>();
string testDisplayName = nodeUpdateMessage.TestNode.DisplayName;
// Defer GetTestName() to failure branches only: for passing/skipped/in-progress tests
// nodeState falls through the switch with no match and testName is never needed.
switch (nodeState)
{
case FailedTestNodeStateProperty failed:
await WriteExceptionAsync(testDisplayName, GetTestName(nodeUpdateMessage.TestNode), failed.Explanation, failed.Exception, cancellationToken).ConfigureAwait(false);
break;
case ErrorTestNodeStateProperty error:
await WriteExceptionAsync(testDisplayName, GetTestName(nodeUpdateMessage.TestNode), error.Explanation, error.Exception, cancellationToken).ConfigureAwait(false);
break;
#pragma warning disable CS0618, MTP0001 // Type or member is obsolete
case CancelledTestNodeStateProperty cancelled:
#pragma warning restore CS0618, MTP0001 // Type or member is obsolete
await WriteExceptionAsync(testDisplayName, GetTestName(nodeUpdateMessage.TestNode), cancelled.Explanation, cancelled.Exception, cancellationToken).ConfigureAwait(false);
break;
case TimeoutTestNodeStateProperty timeout:
await WriteExceptionAsync(testDisplayName, GetTestName(nodeUpdateMessage.TestNode), timeout.Explanation, timeout.Exception, cancellationToken).ConfigureAwait(false);
break;
}
}
private async Task WriteExceptionAsync(string testDisplayName, string testName, string? explanation, Exception? exception, CancellationToken cancellationToken)
{
if (_logger.IsEnabled(LogLevel.Trace))
{
_logger.LogTrace("Failure received.");
}
bool isQuarantined = _quarantineFile?.Matches(testName) == true;
if (isQuarantined && Interlocked.Exchange(ref _quarantineBuildTagEmitted, 1) == 0)
{
await _outputDisplay.DisplayAsync(this, new AzureDevOpsCommandOutputDeviceData(QuarantineBuildTagLine), cancellationToken).ConfigureAwait(false);
}
string severity = GetSeverity(testName, isQuarantined);
string annotationSuffix = BuildAnnotationSuffix(testName, isQuarantined);
string? line = GetErrorText(testDisplayName, explanation, exception, severity, _fileSystem, _logger, _targetFrameworkMoniker, annotationSuffix, _userStackFrameFilters, StackTraceSourceLocationResolver.SkipAssertionFramesForCurrentRuntime);
if (line is null)
{
if (_logger.IsEnabled(LogLevel.Trace))
{
_logger.LogTrace("Failure message is null, returning.");
}
return;
}
if (_logger.IsEnabled(LogLevel.Trace))
{
_logger.LogTrace($"Showing failure message '{line}'.");
}
await _outputDisplay.DisplayAsync(this, new AzureDevOpsCommandOutputDeviceData(line), cancellationToken).ConfigureAwait(false);
}
internal static /* for testing */ string? GetErrorText(string testDisplayName, string? explanation, Exception? exception, string severity, IFileSystem fileSystem, ILogger logger, string targetFrameworkMoniker)
=> GetErrorText(testDisplayName, explanation, exception, severity, fileSystem, logger, targetFrameworkMoniker, additionalMessageSuffix: null, userStackFrameFilters: null, skipAssertionFrames: true);
internal static /* for testing */ string? GetErrorText(string testDisplayName, string? explanation, Exception? exception, string severity, IFileSystem fileSystem, ILogger logger, string targetFrameworkMoniker, string? additionalMessageSuffix)
=> GetErrorText(testDisplayName, explanation, exception, severity, fileSystem, logger, targetFrameworkMoniker, additionalMessageSuffix, userStackFrameFilters: null, skipAssertionFrames: true);
internal static /* for testing */ string? GetErrorText(string testDisplayName, string? explanation, Exception? exception, string severity, IFileSystem fileSystem, ILogger logger, string targetFrameworkMoniker, string? additionalMessageSuffix, Regex[]? userStackFrameFilters, bool skipAssertionFrames)
{
string message = explanation ?? exception?.Message ?? AzureDevOpsResources.NoFailureMessageFallback;
string formattedMessage = $"{FormatErrorMessage(testDisplayName, targetFrameworkMoniker, message)}{additionalMessageSuffix}";
if (exception?.StackTrace is { } stackTrace)
{
// A missing repository root only means source locations cannot be made repo-relative, so it
// must degrade to the message-only annotation below rather than fault the data consumer.
// This happens whenever the test application runs outside a git checkout — including on
// browser-wasm, whose virtual filesystem has no '.git' anywhere. The GitHub Actions reporter
// makes the same allowance in GitHubActionsRepositoryRoot.FindGitRoot.
if (TryFindRepoRoot(logger) is { } repoRoot)
{
(string RelativeNormalizedPath, int LineNumber)? location = StackTraceSourceLocationResolver.TryResolve(
stackTrace,
repoRoot,
fileSystem,
logger,
skipAssertionFrames,
code => IsUserStackFrameFilterMatch(code, userStackFrameFilters, logger));
if (location is not null)
{
string line = $"##vso[task.logissue type={severity};sourcepath={location.Value.RelativeNormalizedPath};linenumber={location.Value.LineNumber};columnnumber=1]{AzDoEscaper.Escape(formattedMessage)}";
if (logger.IsEnabled(LogLevel.Trace))
{
logger.LogTrace($"Reported full message '{line}'.");
}
return line;
}
if (logger.IsEnabled(LogLevel.Trace))
{
logger.LogTrace("No stack trace line matched criteria, falling back to a message-only annotation.");
}
}
}
else if (logger.IsEnabled(LogLevel.Trace))
{
logger.LogTrace(exception is null
? "Exception was null, emitting a message-only annotation."
: "Exception stack trace was null, emitting a message-only annotation.");
}
// Fallback: source location could not be resolved. The Azure DevOps logissue command only
// requires 'type' and the message; sourcepath/linenumber/columnnumber are optional. Without
// this fallback, failures whose stack frame cannot be resolved to a local repo file (no
// exception, no stack trace, frames outside the repo root, or paths that do not exist on
// disk) would be silently suppressed. See https://github.com/microsoft/testfx/issues/5979.
string fallbackLine = $"##vso[task.logissue type={severity}]{AzDoEscaper.Escape(formattedMessage)}";
if (logger.IsEnabled(LogLevel.Trace))
{
logger.LogTrace($"Reported message-only annotation '{fallbackLine}'.");
}
return fallbackLine;
}
/// <summary>
/// Resolves the repository root used to turn an absolute stack-frame path into a repo-relative
/// <c>sourcepath</c>, or <see langword="null"/> when the test application does not run inside a git
/// checkout (a published app, a container, or the browser-wasm virtual filesystem).
/// </summary>
private static string? TryFindRepoRoot(ILogger logger)
{
try
{
string repoRoot = RootFinder.Find();
if (logger.IsEnabled(LogLevel.Trace))
{
logger.LogTrace($"Found repo root '{repoRoot}'");
}
return repoRoot;
}
catch (InvalidOperationException)
{
if (logger.IsEnabled(LogLevel.Trace))
{
logger.LogTrace("No repository root was found, emitting a message-only annotation.");
}
return null;
}
}
private string GetConfiguredSeverity()
=> _commandLine.TryGetOptionArgumentList(AzureDevOpsCommandLineOptions.AzureDevOpsReportSeverity, out string[]? arguments)
&& arguments is [string configuredSeverity]
? configuredSeverity.ToLowerInvariant()
: "error";
private QuarantineFile? LoadQuarantineFile()
{
if (!_commandLine.TryGetOptionArgumentList(AzureDevOpsCommandLineOptions.AzureDevOpsQuarantineFile, out string[]? arguments)
|| arguments is not [string quarantineFilePath])
{
return null;
}
// NOTE: The value is treated as an explicit filesystem path supplied by the caller; this extension only validates existence and readability.
if (!_fileSystem.ExistFile(quarantineFilePath))
{
_logger.LogWarning(string.Format(CultureInfo.InvariantCulture, AzureDevOpsResources.QuarantineFileMissingWarning, quarantineFilePath));
return null;
}
try
{
return new QuarantineFile(quarantineFilePath, _fileSystem, _logger);
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
_logger.LogWarning(string.Format(CultureInfo.InvariantCulture, AzureDevOpsResources.QuarantineFileLoadFailedWarning, quarantineFilePath, ex.Message));
return null;
}
}
private string GetSeverity(string testName, bool isQuarantined)
=> isQuarantined || (_demoteKnownFlaky && _historyService.IsLikelyFlaky(testName, KnownFlakyFailureRateThreshold))
? WarningSeverity
: _severity ?? "error";
private string BuildAnnotationSuffix(string testName, bool isQuarantined)
{
string? historyAnnotation = GetHistoryAnnotation(testName);
if (historyAnnotation is null && !isQuarantined)
{
return string.Empty;
}
StringBuilder builder = new();
if (historyAnnotation is not null)
{
builder.Append(' ').Append(historyAnnotation);
}
if (isQuarantined)
{
builder.Append(' ').Append(AzureDevOpsResources.QuarantinedAnnotation);
}
return builder.ToString();
}
private string? GetHistoryAnnotation(string testName)
=> !_historyService.TryGetStats(testName, out FlakyStats stats) || stats.TotalCount == 0
? null
: stats.FailCount == 0
? stats.TotalCount >= MinSamplesForRegressionAnnotation
? AzureDevOpsResources.FlakyHistoryRegressionAnnotation
: null
: string.Format(CultureInfo.InvariantCulture, AzureDevOpsResources.FlakyHistoryFailureAnnotation, stats.FailCount, stats.TotalCount, _historyService.HistoryWindowInDays);
private void EnsureEnabledConfigurationLoaded()
{
if (_hasLoadedEnabledConfiguration)
{
return;
}
_severity = GetConfiguredSeverity();
_demoteKnownFlaky = _commandLine.IsOptionSet(AzureDevOpsCommandLineOptions.AzureDevOpsDemoteKnownFlaky);
_quarantineFile = LoadQuarantineFile();
_userStackFrameFilters = LoadUserStackFrameFilters();
_hasLoadedEnabledConfiguration = true;
}
private Regex[] LoadUserStackFrameFilters()
{
if (!_commandLine.TryGetOptionArgumentList(AzureDevOpsCommandLineOptions.AzureDevOpsStackFrameFilter, out string[]? patterns)
|| patterns is not { Length: > 0 })
{
return [];
}
var compiled = new List<Regex>(patterns.Length);
foreach (string pattern in patterns)
{
try
{
compiled.Add(new Regex(
pattern,
RegexOptions.CultureInvariant | RegexOptions.Compiled,
TimeSpan.FromMilliseconds(AzureDevOpsCommandLineProvider.StackFrameFilterMatchTimeoutMs)));
}
catch (ArgumentException ex)
{
// Should have been caught at validation time, but log and skip if not.
if (_logger.IsEnabled(LogLevel.Warning))
{
_logger.LogWarning($"Skipping invalid '--report-azdo-stackframe-filter' regex '{pattern}': {ex.Message}");
}
}
}
return [.. compiled];
}
private static string GetTestName(TestNode testNode)
=> TestNodeIdentity.GetTestName(testNode);
/// <summary>
/// Formats the reporter message so the test name lands on its own line.
/// PR check UIs (GitHub Checks via the dotnet problem matcher and Azure DevOps)
/// render the first line of the message as the bold annotation title, so we
/// keep the test display name compact and push the assertion text to the body.
/// </summary>
/// <remarks>
/// MTP includes the TFM in the display name in multi-TFM mode (e.g. "MyTest (net8.0)").
/// To avoid noise like "MyTest (net8.0) [net8.0]" we skip the bracketed TFM
/// suffix when the display name already ends with "({tfm})" or "(\"{tfm}\")".
/// </remarks>
internal static /* for testing */ string FormatErrorMessage(string testDisplayName, string targetFrameworkMoniker, string message)
{
string titleLine = DisplayNameContainsTfm(testDisplayName, targetFrameworkMoniker)
? testDisplayName
: $"{testDisplayName} [{targetFrameworkMoniker}]";
return $"{titleLine}\n{message}";
}
private static bool DisplayNameContainsTfm(string displayName, string tfm)
=> displayName.EndsWith($"({tfm})", StringComparison.Ordinal)
|| displayName.EndsWith($"(\"{tfm}\")", StringComparison.Ordinal);
private static bool IsUserStackFrameFilterMatch(string code, Regex[]? userStackFrameFilters, ILogger logger)
{
if (userStackFrameFilters is null || userStackFrameFilters.Length == 0)
{
return false;
}
foreach (Regex filter in userStackFrameFilters)
{
try
{
if (filter.IsMatch(code))
{
return true;
}
}
catch (RegexMatchTimeoutException ex)
{
if (logger.IsEnabled(LogLevel.Warning))
{
logger.LogWarning($"'--report-azdo-stackframe-filter' regex '{filter}' timed out matching frame '{code}': {ex.Message}. Treating as no-match.");
}
}
}
return false;
}
}