diff --git a/src/Microsoft.DotNet.RemoteExecutor/src/RemoteExecutor.cs b/src/Microsoft.DotNet.RemoteExecutor/src/RemoteExecutor.cs index ae6ab0db014..d7cf364973f 100644 --- a/src/Microsoft.DotNet.RemoteExecutor/src/RemoteExecutor.cs +++ b/src/Microsoft.DotNet.RemoteExecutor/src/RemoteExecutor.cs @@ -10,6 +10,7 @@ using System.Runtime.InteropServices; using System.Threading.Tasks; using Xunit; +using IOPath = System.IO.Path; namespace Microsoft.DotNet.RemoteExecutor { @@ -51,17 +52,44 @@ static RemoteExecutor() return; } - HostRunnerName = System.IO.Path.GetFileName(processFileName); Path = typeof(RemoteExecutor).Assembly.Location; if (IsNetCore()) { HostRunner = processFileName; + + string hostName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "dotnet.exe" : "dotnet"; + + // Partially addressing https://github.com/dotnet/arcade/issues/6371 + // We expect to run tests with dotnet. However in certain scenarios we may have a different apphost (e.g. Visual Studio testhost). + // Attempt to find and use dotnet. + if (!IOPath.GetFileName(HostRunner).Equals(hostName, StringComparison.OrdinalIgnoreCase)) + { + string runtimePath = IOPath.GetDirectoryName(typeof(object).Assembly.Location); + + // In case we are running the app via a runtime, dotnet.exe is located 3 folders above the runtime. Example: + // runtime -> C:\Program Files\dotnet\shared\Microsoft.NETCore.App\5.0.6\ + // dotnet.exe -> C:\Program Files\dotnet\shared\dotnet.exe + // This should also work on Unix and locally built runtime/testhost. + string directory = GetDirectoryName(GetDirectoryName(GetDirectoryName(runtimePath))); + if (directory != string.Empty) + { + string dotnetExe = IOPath.Combine(directory, hostName); + if (File.Exists(dotnetExe)) + { + HostRunner = dotnetExe; + } + } + } } else if (RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework", StringComparison.OrdinalIgnoreCase)) { HostRunner = Path; } + + HostRunnerName = IOPath.GetFileName(HostRunner); + + static string GetDirectoryName(string path) => string.IsNullOrEmpty(path) ? string.Empty : IOPath.GetDirectoryName(path); } private static bool IsNetCore() => diff --git a/src/Microsoft.DotNet.RemoteExecutor/tests/RemoteExecutorTests.cs b/src/Microsoft.DotNet.RemoteExecutor/tests/RemoteExecutorTests.cs index cba294ebbfe..251577cc61d 100644 --- a/src/Microsoft.DotNet.RemoteExecutor/tests/RemoteExecutorTests.cs +++ b/src/Microsoft.DotNet.RemoteExecutor/tests/RemoteExecutorTests.cs @@ -10,7 +10,7 @@ namespace Microsoft.DotNet.RemoteExecutor.Tests { public class RemoteExecutorTests { - [Fact(Skip = "Remote executor is broken in VS test explorer")] + [Fact] public void AsyncAction_ThrowException() { Assert.Throws(() => @@ -22,7 +22,7 @@ public void AsyncAction_ThrowException() ); } - [Fact(Skip = "Remote executor is broken in VS test explorer")] + [Fact] public void AsyncAction() { RemoteExecutor.Invoke(async () => @@ -31,7 +31,7 @@ public void AsyncAction() }, new RemoteInvokeOptions { RollForward = "Major" }).Dispose(); } - [Fact(Skip = "Remote executor is broken in VS test explorer")] + [Fact] public void AsyncFunc_ThrowException() { Assert.Throws(() => @@ -44,7 +44,7 @@ public void AsyncFunc_ThrowException() ); } - [Fact(Skip = "Remote executor is broken in VS test explorer")] + [Fact] public void AsyncFunc_InvalidReturnCode() { Assert.Throws(() => @@ -56,7 +56,7 @@ public void AsyncFunc_InvalidReturnCode() ); } - [Fact(Skip = "Remote executor is broken in VS test explorer")] + [Fact] public void AsyncFunc_NoThrow_ValidReturnCode() { RemoteExecutor.Invoke(async () =>