Skip to content

Commit

Permalink
Add MSTest trace logs when using MTP (#4833)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evangelink authored Jan 30, 2025
1 parent 8660ba9 commit 935e299
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/Adapter/MSTest.TestAdapter/IPlatformServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ internal interface IPlatformServiceProvider
IFileOperations FileOperations { get; }

/// <summary>
/// Gets an instance to the platform service for trace logging.
/// Gets or sets an instance to the platform service for trace logging.
/// </summary>
IAdapterTraceLogger AdapterTraceLogger { get; }
IAdapterTraceLogger AdapterTraceLogger { get; set; }

/// <summary>
/// Gets an instance of the test deployment service.
Expand Down
8 changes: 2 additions & 6 deletions src/Adapter/MSTest.TestAdapter/PlatformServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,11 @@ public IFileOperations FileOperations
}

/// <summary>
/// Gets an instance to the platform service for trace logging.
/// Gets or sets an instance to the platform service for trace logging.
/// </summary>
[field: AllowNull]
[field: MaybeNull]
public IAdapterTraceLogger AdapterTraceLogger
{
get => field ??= new AdapterTraceLogger();
private set;
}
public IAdapterTraceLogger AdapterTraceLogger { get => field ??= new AdapterTraceLogger(); set; }

/// <summary>
/// Gets an instance of the test deployment service.
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<IEnumerable<Assembly>> getTestAssemblies,
IServiceProvider serviceProvider, ITestFrameworkCapabilities capabilities)
: base(mstestExtension, getTestAssemblies, serviceProvider, capabilities)
=> _configuration = new(serviceProvider.GetConfiguration());
{
_configuration = new(serviceProvider.GetConfiguration());
_loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
}

/// <inheritdoc />
protected override Task SynchronizedDiscoverTestsAsync(VSTestDiscoverTestExecutionRequest request, IMessageBus messageBus,
Expand All @@ -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;
}
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public Mock<IReflectionOperations2> 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;

Expand Down

0 comments on commit 935e299

Please sign in to comment.