From 51c208b71fcf35052241b9e1e10030f67c4daa11 Mon Sep 17 00:00:00 2001 From: ema Date: Tue, 1 Dec 2026 15:06:00 -0500 Subject: [PATCH] Expose dotnet host selection in translation layer Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../PublicAPI/PublicAPI.Unshipped.txt | 1 + .../README.md | 15 +++++++++++ .../VsTestConsoleWrapper.cs | 12 ++++++--- .../VsTestConsoleWrapperTests.cs | 26 +++++++++++++++++++ 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/PublicAPI/PublicAPI.Unshipped.txt b/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/PublicAPI/PublicAPI.Unshipped.txt index 643d0f56f4..b9b6a1604e 100644 --- a/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/PublicAPI/PublicAPI.Unshipped.txt +++ b/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/PublicAPI/PublicAPI.Unshipped.txt @@ -1,4 +1,5 @@ #nullable enable +Microsoft.TestPlatform.VsTestConsole.TranslationLayer.VsTestConsoleWrapper.VsTestConsoleWrapper(string! vstestConsolePath, string! dotnetExePath, Microsoft.TestPlatform.VsTestConsole.TranslationLayer.ConsoleParameters! consoleParameters) -> void Microsoft.TestPlatform.VsTestConsole.TranslationLayer.Interfaces.IVsTestConsoleWrapper.RunTests(System.Collections.Generic.IEnumerable! testCases, string? runSettings, Microsoft.VisualStudio.TestPlatform.ObjectModel.Client.TestPlatformOptions? options, Microsoft.VisualStudio.TestPlatform.ObjectModel.Client.TestSessionInfo? testSessionInfo, Microsoft.VisualStudio.TestPlatform.ObjectModel.Client.ITestRunEventsHandler! testRunEventsHandler, Microsoft.VisualStudio.TestPlatform.ObjectModel.Client.ITelemetryEventsHandler! telemetryEventsHandler) -> void Microsoft.TestPlatform.VsTestConsole.TranslationLayer.Interfaces.IVsTestConsoleWrapper.RunTests(System.Collections.Generic.IEnumerable! sources, string? runSettings, Microsoft.VisualStudio.TestPlatform.ObjectModel.Client.TestPlatformOptions? options, Microsoft.VisualStudio.TestPlatform.ObjectModel.Client.TestSessionInfo? testSessionInfo, Microsoft.VisualStudio.TestPlatform.ObjectModel.Client.ITestRunEventsHandler! testRunEventsHandler, Microsoft.VisualStudio.TestPlatform.ObjectModel.Client.ITelemetryEventsHandler! telemetryEventsHandler) -> void Microsoft.TestPlatform.VsTestConsole.TranslationLayer.Interfaces.IVsTestConsoleWrapper.RunTestsWithCustomTestHost(System.Collections.Generic.IEnumerable! testCases, string? runSettings, Microsoft.VisualStudio.TestPlatform.ObjectModel.Client.TestPlatformOptions? options, Microsoft.VisualStudio.TestPlatform.ObjectModel.Client.TestSessionInfo? testSessionInfo, Microsoft.VisualStudio.TestPlatform.ObjectModel.Client.ITestRunEventsHandler! testRunEventsHandler, Microsoft.VisualStudio.TestPlatform.ObjectModel.Client.ITelemetryEventsHandler! telemetryEventsHandler, Microsoft.VisualStudio.TestPlatform.ObjectModel.Client.Interfaces.ITestHostLauncher! customTestHostLauncher) -> void diff --git a/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/README.md b/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/README.md index 74e2184a41..47031fafba 100644 --- a/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/README.md +++ b/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/README.md @@ -24,6 +24,21 @@ consoleWrapper.DiscoverTests(testAssemblies, settings, discoveryHandler); consoleWrapper.RunTests(testAssemblies, settings, executionHandler); ``` +To launch `vstest.console.dll` with a particular .NET installation, specify its +`dotnet` executable when creating the wrapper: + +```csharp +var consoleWrapper = new VsTestConsoleWrapper( + vstestConsolePath, + dotnetExePath: "", + consoleParameters: new ConsoleParameters()); +``` + +The host selection applies only to this wrapper; it does not modify the +process-wide `PATH`. The existing constructors continue to resolve the host +automatically, as does an empty `dotnetExePath`. When launching +`vstest.console.exe`, the runner is started directly and `dotnetExePath` is ignored. + ## Links - [Visual Studio Test Platform Documentation](https://github.com/microsoft/vstest) diff --git a/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleWrapper.cs b/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleWrapper.cs index 697a7f0dc4..f4aea172bc 100644 --- a/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleWrapper.cs +++ b/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleWrapper.cs @@ -91,12 +91,16 @@ public VsTestConsoleWrapper( /// Initializes a new instance of the class. /// /// - /// Defined for testing purposes. + /// + /// The specified dotnet host is used only when points to a DLL. + /// When launching an executable, the runner is started directly. + /// /// - /// Path to the test runner vstest.console.exe. - /// Path to dotnet exe, needed for CI builds. + /// Path to the test runner vstest.console.exe or vstest.console.dll. + /// Path to the dotnet executable used to launch vstest.console.dll. + /// An empty string uses the default dotnet host resolution. /// The parameters to be passed onto the runner process. - internal VsTestConsoleWrapper( + public VsTestConsoleWrapper( string vstestConsolePath, string dotnetExePath, ConsoleParameters consoleParameters) diff --git a/test/TranslationLayer.UnitTests/VsTestConsoleWrapperTests.cs b/test/TranslationLayer.UnitTests/VsTestConsoleWrapperTests.cs index 37201ed12e..840d182d52 100644 --- a/test/TranslationLayer.UnitTests/VsTestConsoleWrapperTests.cs +++ b/test/TranslationLayer.UnitTests/VsTestConsoleWrapperTests.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; +using System.IO; using System.Threading; using System.Threading.Tasks; @@ -57,6 +58,31 @@ public VsTestConsoleWrapperTests() _mockRequestSender.Setup(rs => rs.InitializeCommunicationAsync(It.IsAny())).Returns(Task.FromResult(100)); } + [TestMethod] + public void ConstructorWithDotnetHostShouldBePublic() + { + var constructor = typeof(VsTestConsoleWrapper).GetConstructor( + [typeof(string), typeof(string), typeof(ConsoleParameters)]); + + Assert.IsNotNull(constructor); + } + + [TestMethod] + public void ProcessManagerShouldUseSpecifiedDotnetHostWithoutChangingPath() + { + var dotnetExePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString(), "custom dotnet", "dotnet"); + var originalPath = Environment.GetEnvironmentVariable("PATH"); + var consoleParameters = new ConsoleParameters(); + using var processManager = new VsTestConsoleProcessManager( + typeof(VsTestConsoleWrapper).Assembly.Location, + dotnetExePath); + + var exception = Assert.ThrowsExactly(() => processManager.StartProcess(consoleParameters)); + + Assert.AreEqual(dotnetExePath, exception.FileName); + Assert.AreEqual(originalPath, Environment.GetEnvironmentVariable("PATH")); + } + [TestMethod] public void StartSessionShouldStartVsTestConsoleWithCorrectArguments() {