|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using Microsoft.Testing.Extensions.AzureDevOps; |
| 5 | +using Microsoft.Testing.Extensions.AzureDevOps.Resources; |
| 6 | +using Microsoft.Testing.Extensions.Reporting; |
| 7 | +using Microsoft.Testing.Platform; |
| 8 | +using Microsoft.Testing.Platform.CommandLine; |
| 9 | +using Microsoft.Testing.Platform.Extensions.Messages; |
| 10 | +using Microsoft.Testing.Platform.Extensions.OutputDevice; |
| 11 | +using Microsoft.Testing.Platform.Extensions.TestHost; |
| 12 | +using Microsoft.Testing.Platform.Helpers; |
| 13 | +using Microsoft.Testing.Platform.OutputDevice; |
| 14 | + |
| 15 | +namespace Microsoft.Testing.Extensions.TrxReport.Abstractions; |
| 16 | + |
| 17 | +internal sealed class AzureDevOpsReporter : |
| 18 | + IDataConsumer, |
| 19 | + IDataProducer, |
| 20 | + IOutputDeviceDataProducer |
| 21 | +{ |
| 22 | + private readonly IOutputDevice _outputDisplay; |
| 23 | + |
| 24 | + private static readonly char[] NewlineCharacters = new char[] { '\r', '\n' }; |
| 25 | + private readonly ICommandLineOptions _commandLine; |
| 26 | + private readonly IEnvironment _environment; |
| 27 | + private string _severity = "error"; |
| 28 | + |
| 29 | + public AzureDevOpsReporter( |
| 30 | + ICommandLineOptions commandLine, |
| 31 | + IEnvironment environment, |
| 32 | + IOutputDevice outputDisplay) |
| 33 | + { |
| 34 | + _commandLine = commandLine; |
| 35 | + _environment = environment; |
| 36 | + _outputDisplay = outputDisplay; |
| 37 | + } |
| 38 | + |
| 39 | + public Type[] DataTypesConsumed { get; } = |
| 40 | + [ |
| 41 | + typeof(TestNodeUpdateMessage) |
| 42 | + ]; |
| 43 | + |
| 44 | + public Type[] DataTypesProduced { get; } = [typeof(SessionFileArtifact)]; |
| 45 | + |
| 46 | + /// <inheritdoc /> |
| 47 | + public string Uid { get; } = nameof(AzureDevOpsReporter); |
| 48 | + |
| 49 | + /// <inheritdoc /> |
| 50 | + public string Version { get; } = AppVersion.DefaultSemVer; |
| 51 | + |
| 52 | + /// <inheritdoc /> |
| 53 | + public string DisplayName { get; } = AzureDevOpsResources.DisplayName; |
| 54 | + |
| 55 | + /// <inheritdoc /> |
| 56 | + public string Description { get; } = AzureDevOpsResources.Description; |
| 57 | + |
| 58 | + /// <inheritdoc /> |
| 59 | + public Task<bool> IsEnabledAsync() |
| 60 | + { |
| 61 | + bool isEnabled = _commandLine.IsOptionSet(AzureDevOpsCommandLineOptions.AzureDevOpsOptionName) |
| 62 | + && string.Equals(_environment.GetEnvironmentVariable("TF_BUILD"), "true", StringComparison.OrdinalIgnoreCase); |
| 63 | + |
| 64 | + if (isEnabled) |
| 65 | + { |
| 66 | + bool found = _commandLine.TryGetOptionArgumentList(AzureDevOpsCommandLineOptions.AzureDevOpsReportSeverity, out string[]? arguments); |
| 67 | + if (found && arguments?.Length > 0) |
| 68 | + { |
| 69 | + _severity = arguments[0].ToLowerInvariant(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return Task.FromResult(isEnabled); |
| 74 | + } |
| 75 | + |
| 76 | + public async Task ConsumeAsync(IDataProducer dataProducer, IData value, CancellationToken cancellationToken) |
| 77 | + { |
| 78 | + if (cancellationToken.IsCancellationRequested) |
| 79 | + { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + if (value is not TestNodeUpdateMessage nodeUpdateMessage) |
| 84 | + { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + TestNodeStateProperty nodeState = nodeUpdateMessage.TestNode.Properties.Single<TestNodeStateProperty>(); |
| 89 | + |
| 90 | + switch (nodeState) |
| 91 | + { |
| 92 | + case FailedTestNodeStateProperty failed: |
| 93 | + await WriteExceptionAsync(failed.Explanation, failed.Exception); |
| 94 | + break; |
| 95 | + case ErrorTestNodeStateProperty error: |
| 96 | + await WriteExceptionAsync(error.Explanation, error.Exception); |
| 97 | + break; |
| 98 | + case CancelledTestNodeStateProperty cancelled: |
| 99 | + await WriteExceptionAsync(cancelled.Explanation, cancelled.Exception); |
| 100 | + break; |
| 101 | + case TimeoutTestNodeStateProperty timeout: |
| 102 | + await WriteExceptionAsync(timeout.Explanation, timeout.Exception); |
| 103 | + break; |
| 104 | + } |
| 105 | + |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + private async Task WriteExceptionAsync(string? explanation, Exception? exception) |
| 110 | + { |
| 111 | + if (exception == null || exception.StackTrace == null) |
| 112 | + { |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + string message = explanation ?? exception.Message; |
| 117 | + |
| 118 | + if (message == null) |
| 119 | + { |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + string stackTrace = exception.StackTrace; |
| 124 | + int index = stackTrace.IndexOfAny(NewlineCharacters); |
| 125 | + string firstLine = index == -1 ? stackTrace : stackTrace.Substring(0, index); |
| 126 | + if (firstLine != null) |
| 127 | + { |
| 128 | + (string Code, string File, int LineNumber)? location = GetStackFrameLocation(firstLine); |
| 129 | + if (location != null) |
| 130 | + { |
| 131 | + string root = RootFinder.Find(); |
| 132 | + string file = location.Value.File; |
| 133 | + string relativePath = file.StartsWith(root, StringComparison.CurrentCultureIgnoreCase) ? file.Substring(root.Length) : file; |
| 134 | + string relativeNormalizedPath = relativePath.Replace('\\', '/'); |
| 135 | + |
| 136 | + string err = AzDoEscaper.Escape(message); |
| 137 | + |
| 138 | + string line = $"##vso[task.logissue type={_severity};sourcepath={relativeNormalizedPath};linenumber={location.Value.LineNumber};columnnumber=1]{err}"; |
| 139 | + await _outputDisplay.DisplayAsync(this, new FormattedTextOutputDeviceData(line)); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + internal /* for testing */ static (string Code, string File, int LineNumber)? GetStackFrameLocation(string stackTraceLine) |
| 145 | + { |
| 146 | + Match match = StackTraceHelper.GetFrameRegex().Match(stackTraceLine); |
| 147 | + if (!match.Success) |
| 148 | + { |
| 149 | + return null; |
| 150 | + } |
| 151 | + |
| 152 | + bool weHaveFilePathAndCodeLine = !RoslynString.IsNullOrWhiteSpace(match.Groups["code"].Value); |
| 153 | + if (!weHaveFilePathAndCodeLine) |
| 154 | + { |
| 155 | + return null; |
| 156 | + } |
| 157 | + |
| 158 | + if (RoslynString.IsNullOrWhiteSpace(match.Groups["file"].Value)) |
| 159 | + { |
| 160 | + return null; |
| 161 | + } |
| 162 | + |
| 163 | + int line = int.TryParse(match.Groups["line"].Value, out int value) ? value : 0; |
| 164 | + |
| 165 | + return (match.Groups["code"].Value, match.Groups["file"].Value, line); |
| 166 | + } |
| 167 | +} |
0 commit comments