-
Notifications
You must be signed in to change notification settings - Fork 307
Add Azure DevOps per-assembly log groups (#9141) #9177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
88a782b
Add Azure DevOps per-assembly log groups (#9141)
6cc9018
Merge remote-tracking branch 'origin/main' into dev/amauryleve/azdo-l…
865945b
Address review: reuse AzureDevOpsConstants.IsRunningInAzureDevOps
f2c558c
Address review: fix IDE0046 build break; make log group last-wrapping…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
src/Platform/Microsoft.Testing.Extensions.AzureDevOpsReport/AzureDevOpsLogGroupReporter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| // 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.Extensions.TestHost; | ||
| using Microsoft.Testing.Platform.Helpers; | ||
| using Microsoft.Testing.Platform.Logging; | ||
| using Microsoft.Testing.Platform.OutputDevice; | ||
| using Microsoft.Testing.Platform.Services; | ||
|
|
||
| namespace Microsoft.Testing.Extensions.AzureDevOpsReport; | ||
|
|
||
| /// <summary> | ||
| /// Wraps each test assembly's output in an Azure DevOps log group (<c>##[group]</c> / | ||
| /// <c>##[endgroup]</c>) so the raw pipeline log view collapses the assembly's test output by | ||
| /// default. Unlike <c>##vso[task.logdetail]</c> records, the <c>##[group]</c> format commands are | ||
| /// rendered by the modern Azure DevOps Pipelines log viewer. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This handler also implements <see cref="IDataConsumer"/> (with a no-op | ||
| /// <see cref="ConsumeAsync(IDataProducer, IData, CancellationToken)"/>) purely so that, at session | ||
| /// end, <c>CommonTestHost.NotifyTestSessionEndAsync</c> runs its | ||
| /// <see cref="OnTestSessionFinishingAsync(ITestSessionContext)"/> in the consumer phase — i.e. | ||
| /// after the producer-only AzDO handlers. Combined with registering it last, this ensures the | ||
| /// closing <c>##[endgroup]</c> is emitted after the other reporters' final <c>##vso[...]</c> lines, | ||
| /// so the group truly wraps the whole assembly's output. | ||
| /// </remarks> | ||
| internal sealed class AzureDevOpsLogGroupReporter : IDataConsumer, ITestSessionLifetimeHandler, IOutputDeviceDataProducer | ||
| { | ||
| private readonly ICommandLineOptions _commandLineOptions; | ||
| private readonly IEnvironment _environment; | ||
| private readonly IOutputDevice _outputDevice; | ||
| private readonly ITestApplicationModuleInfo _testApplicationModuleInfo; | ||
| private readonly ILogger _logger; | ||
| private readonly Lazy<string> _targetFrameworkMoniker; | ||
|
|
||
| private bool _groupOpened; | ||
|
|
||
| public AzureDevOpsLogGroupReporter( | ||
| ICommandLineOptions commandLineOptions, | ||
| IEnvironment environment, | ||
| IOutputDevice outputDevice, | ||
| ITestApplicationModuleInfo testApplicationModuleInfo, | ||
| ILoggerFactory loggerFactory) | ||
| { | ||
| _commandLineOptions = commandLineOptions; | ||
| _environment = environment; | ||
| _outputDevice = outputDevice; | ||
| _testApplicationModuleInfo = testApplicationModuleInfo; | ||
| _logger = loggerFactory.CreateLogger<AzureDevOpsLogGroupReporter>(); | ||
| _targetFrameworkMoniker = new(TargetFrameworkMonikerHelper.GetTargetFrameworkMoniker); | ||
| } | ||
|
|
||
| public string Uid => nameof(AzureDevOpsLogGroupReporter); | ||
|
|
||
| public string Version => ExtensionVersion.DefaultSemVer; | ||
|
|
||
| public string DisplayName => AzureDevOpsResources.DisplayName; | ||
|
|
||
| public string Description => AzureDevOpsResources.Description; | ||
|
|
||
| public Type[] DataTypesConsumed { get; } = [typeof(TestNodeUpdateMessage)]; | ||
|
|
||
| public Task<bool> IsEnabledAsync() | ||
| => Task.FromResult( | ||
| _commandLineOptions.IsOptionSet(AzureDevOpsCommandLineOptions.AzureDevOpsOptionName) | ||
| && AzureDevOpsConstants.IsRunningInAzureDevOps(_environment)); | ||
|
|
||
| // No-op: this consumer subscribes to data only to be ordered in the consumer phase at session | ||
| // end (see the type-level remarks). It does not act on individual messages. | ||
| public Task ConsumeAsync(IDataProducer dataProducer, IData value, CancellationToken cancellationToken) | ||
| => Task.CompletedTask; | ||
|
|
||
| public async Task OnTestSessionStartingAsync(ITestSessionContext testSessionContext) | ||
| { | ||
| try | ||
| { | ||
| testSessionContext.CancellationToken.ThrowIfCancellationRequested(); | ||
|
|
||
| string name = $"{_testApplicationModuleInfo.TryGetAssemblyName() ?? "unknown"} ({_targetFrameworkMoniker.Value})"; | ||
| string line = $"##[group]{AzDoEscaper.Escape(string.Format(CultureInfo.InvariantCulture, AzureDevOpsResources.LogGroupHeader, name))}"; | ||
| await _outputDevice.DisplayAsync(this, new FormattedTextOutputDeviceData(line), testSessionContext.CancellationToken).ConfigureAwait(false); | ||
| _groupOpened = true; | ||
| } | ||
| catch (OperationCanceledException) | ||
| { | ||
| throw; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| LogUnexpectedException(nameof(OnTestSessionStartingAsync), ex); | ||
| } | ||
| } | ||
|
|
||
| public async Task OnTestSessionFinishingAsync(ITestSessionContext testSessionContext) | ||
| { | ||
| try | ||
| { | ||
| testSessionContext.CancellationToken.ThrowIfCancellationRequested(); | ||
|
|
||
| if (!_groupOpened) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| await _outputDevice.DisplayAsync(this, new FormattedTextOutputDeviceData("##[endgroup]"), testSessionContext.CancellationToken).ConfigureAwait(false); | ||
| _groupOpened = false; | ||
| } | ||
| catch (OperationCanceledException) | ||
| { | ||
| throw; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| LogUnexpectedException(nameof(OnTestSessionFinishingAsync), ex); | ||
| } | ||
| } | ||
|
|
||
| private void LogUnexpectedException(string callbackName, Exception ex) | ||
| { | ||
| if (_logger.IsEnabled(LogLevel.Warning)) | ||
| { | ||
| _logger.LogWarning($"Unexpected exception in {callbackName}: {ex}"); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.