From ecb0b2b47ea9627264257ab5b6e15a815560c5d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Wed, 29 Jan 2025 17:33:44 +0100 Subject: [PATCH] Add MSTest trace logs when using MTP --- .../IPlatformServiceProvider.cs | 4 +-- .../PlatformServiceProvider.cs | 8 ++---- .../BridgedTraceLogger.cs | 27 +++++++++++++++++++ .../MSTestBridgedTestFramework.cs | 9 ++++++- .../TestablePlatformServiceProvider.cs | 2 +- 5 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 src/Adapter/MSTest.TestAdapter/TestingPlatformAdapter/BridgedTraceLogger.cs diff --git a/src/Adapter/MSTest.TestAdapter/IPlatformServiceProvider.cs b/src/Adapter/MSTest.TestAdapter/IPlatformServiceProvider.cs index 8923a5c1c8..487e5fe10a 100644 --- a/src/Adapter/MSTest.TestAdapter/IPlatformServiceProvider.cs +++ b/src/Adapter/MSTest.TestAdapter/IPlatformServiceProvider.cs @@ -30,9 +30,9 @@ internal interface IPlatformServiceProvider IFileOperations FileOperations { get; } /// - /// Gets an instance to the platform service for trace logging. + /// Gets or sets an instance to the platform service for trace logging. /// - IAdapterTraceLogger AdapterTraceLogger { get; } + IAdapterTraceLogger AdapterTraceLogger { get; set; } /// /// Gets an instance of the test deployment service. diff --git a/src/Adapter/MSTest.TestAdapter/PlatformServiceProvider.cs b/src/Adapter/MSTest.TestAdapter/PlatformServiceProvider.cs index d68840c889..1a5880c966 100644 --- a/src/Adapter/MSTest.TestAdapter/PlatformServiceProvider.cs +++ b/src/Adapter/MSTest.TestAdapter/PlatformServiceProvider.cs @@ -58,15 +58,11 @@ public IFileOperations FileOperations } /// - /// Gets an instance to the platform service for trace logging. + /// Gets or sets an instance to the platform service for trace logging. /// [field: AllowNull] [field: MaybeNull] - public IAdapterTraceLogger AdapterTraceLogger - { - get => field ??= new AdapterTraceLogger(); - private set; - } + public IAdapterTraceLogger AdapterTraceLogger { get => field ??= new AdapterTraceLogger(); set; } /// /// Gets an instance of the test deployment service. diff --git a/src/Adapter/MSTest.TestAdapter/TestingPlatformAdapter/BridgedTraceLogger.cs b/src/Adapter/MSTest.TestAdapter/TestingPlatformAdapter/BridgedTraceLogger.cs new file mode 100644 index 0000000000..e5332169f0 --- /dev/null +++ b/src/Adapter/MSTest.TestAdapter/TestingPlatformAdapter/BridgedTraceLogger.cs @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +#if !WINDOWS_UWP +using Microsoft.Testing.Platform.Logging; +using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface; + +namespace Microsoft.VisualStudio.TestTools.UnitTesting; + +[SuppressMessage("ApiDesign", "RS0030:Do not use banned APIs", Justification = "MTP logger bridge")] +internal sealed class BridgedTraceLogger : IAdapterTraceLogger +{ + private readonly ILogger _logger; + + public BridgedTraceLogger(ILogger logger) + => _logger = logger; + + public void LogError(string format, params object?[] args) + => _logger.LogError(string.Format(CultureInfo.CurrentCulture, format, args)); + + public void LogInfo(string format, params object?[] args) + => _logger.LogInformation(string.Format(CultureInfo.CurrentCulture, format, args)); + + public void LogWarning(string format, params object?[] args) + => _logger.LogWarning(string.Format(CultureInfo.CurrentCulture, format, args)); +} +#endif diff --git a/src/Adapter/MSTest.TestAdapter/TestingPlatformAdapter/MSTestBridgedTestFramework.cs b/src/Adapter/MSTest.TestAdapter/TestingPlatformAdapter/MSTestBridgedTestFramework.cs index 4fd518a98f..44723557f9 100644 --- a/src/Adapter/MSTest.TestAdapter/TestingPlatformAdapter/MSTestBridgedTestFramework.cs +++ b/src/Adapter/MSTest.TestAdapter/TestingPlatformAdapter/MSTestBridgedTestFramework.cs @@ -5,6 +5,7 @@ using Microsoft.Testing.Extensions.VSTestBridge; using Microsoft.Testing.Extensions.VSTestBridge.Requests; using Microsoft.Testing.Platform.Capabilities.TestFramework; +using Microsoft.Testing.Platform.Logging; using Microsoft.Testing.Platform.Messages; using Microsoft.Testing.Platform.Services; using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter; @@ -15,11 +16,15 @@ namespace Microsoft.VisualStudio.TestTools.UnitTesting; internal sealed class MSTestBridgedTestFramework : SynchronizedSingleSessionVSTestBridgedTestFramework { private readonly BridgedConfiguration? _configuration; + private readonly ILoggerFactory _loggerFactory; public MSTestBridgedTestFramework(MSTestExtension mstestExtension, Func> getTestAssemblies, IServiceProvider serviceProvider, ITestFrameworkCapabilities capabilities) : base(mstestExtension, getTestAssemblies, serviceProvider, capabilities) - => _configuration = new(serviceProvider.GetConfiguration()); + { + _configuration = new(serviceProvider.GetConfiguration()); + _loggerFactory = serviceProvider.GetRequiredService(); + } /// protected override Task SynchronizedDiscoverTestsAsync(VSTestDiscoverTestExecutionRequest request, IMessageBus messageBus, @@ -31,6 +36,7 @@ protected override Task SynchronizedDiscoverTestsAsync(VSTestDiscoverTestExecuti Debugger.Launch(); } + PlatformServiceProvider.Instance.AdapterTraceLogger = new BridgedTraceLogger(_loggerFactory.CreateLogger("mstest-trace")); MSTestDiscoverer.DiscoverTests(request.AssemblyPaths, request.DiscoveryContext, request.MessageLogger, request.DiscoverySink, _configuration); return Task.CompletedTask; } @@ -45,6 +51,7 @@ protected override async Task SynchronizedRunTestsAsync(VSTestRunTestExecutionRe Debugger.Launch(); } + PlatformServiceProvider.Instance.AdapterTraceLogger = new BridgedTraceLogger(_loggerFactory.CreateLogger("mstest-trace")); MSTestExecutor testExecutor = new(cancellationToken); if (request.VSTestFilter.TestCases is { } testCases) diff --git a/test/UnitTests/MSTestAdapter.UnitTests/TestableImplementations/TestablePlatformServiceProvider.cs b/test/UnitTests/MSTestAdapter.UnitTests/TestableImplementations/TestablePlatformServiceProvider.cs index d747410ba1..b608959490 100644 --- a/test/UnitTests/MSTestAdapter.UnitTests/TestableImplementations/TestablePlatformServiceProvider.cs +++ b/test/UnitTests/MSTestAdapter.UnitTests/TestableImplementations/TestablePlatformServiceProvider.cs @@ -103,7 +103,7 @@ public Mock MockReflectionOperations public IFileOperations FileOperations => MockFileOperations.Object; - public IAdapterTraceLogger AdapterTraceLogger => MockTraceLogger.Object; + public IAdapterTraceLogger AdapterTraceLogger { get => MockTraceLogger.Object; set => throw new NotSupportedException(); } public ITestDeployment TestDeployment => MockTestDeployment.Object;