Skip to content
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

Add MSTest trace logs when using MTP #4833

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading