From 66d2cf598c7589b2382c98c21436cd77c01f8d1e Mon Sep 17 00:00:00 2001 From: Pranav K Date: Fri, 19 Mar 2021 08:47:02 -0700 Subject: [PATCH] Update DotNetMuxerTest (#31053) Fixes https://github.com/dotnet/aspnetcore/issues/24082 --- .../CommandLineUtils/Utilities/DotNetMuxer.cs | 11 +++++----- .../test/Shared.Tests/DotNetMuxerTests.cs | 22 +++++++++++++++++-- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/Shared/CommandLineUtils/Utilities/DotNetMuxer.cs b/src/Shared/CommandLineUtils/Utilities/DotNetMuxer.cs index ac6a0985ef3d..ff4559aed1cb 100644 --- a/src/Shared/CommandLineUtils/Utilities/DotNetMuxer.cs +++ b/src/Shared/CommandLineUtils/Utilities/DotNetMuxer.cs @@ -22,7 +22,7 @@ internal static class DotNetMuxer static DotNetMuxer() { - MuxerPath = TryFindMuxerPath(); + MuxerPath = TryFindMuxerPath(Process.GetCurrentProcess().MainModule?.FileName); } /// @@ -38,7 +38,7 @@ static DotNetMuxer() public static string MuxerPathOrDefault() => MuxerPath ?? MuxerName; - private static string? TryFindMuxerPath() + internal static string? TryFindMuxerPath(string? mainModule) { var fileName = MuxerName; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) @@ -46,11 +46,10 @@ public static string MuxerPathOrDefault() fileName += ".exe"; } - var mainModule = Process.GetCurrentProcess().MainModule; - if (!string.IsNullOrEmpty(mainModule?.FileName) - && string.Equals(Path.GetFileName(mainModule!.FileName), fileName, StringComparison.OrdinalIgnoreCase)) + if (!string.IsNullOrEmpty(mainModule) + && string.Equals(Path.GetFileName(mainModule!), fileName, StringComparison.OrdinalIgnoreCase)) { - return mainModule.FileName; + return mainModule; } return null; diff --git a/src/Shared/test/Shared.Tests/DotNetMuxerTests.cs b/src/Shared/test/Shared.Tests/DotNetMuxerTests.cs index 2835380142ed..5e3b30f4f29f 100644 --- a/src/Shared/test/Shared.Tests/DotNetMuxerTests.cs +++ b/src/Shared/test/Shared.Tests/DotNetMuxerTests.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. #if NETCOREAPP +using System; using System.IO; using System.Runtime.InteropServices; using Xunit; @@ -10,14 +11,31 @@ namespace Microsoft.Extensions.CommandLineUtils { public class DotNetMuxerTests { - [Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/24082")] + [Fact] public void FindsTheMuxer() { - var muxerPath = DotNetMuxer.MuxerPath; + + var muxerPath = DotNetMuxer.TryFindMuxerPath(GetDotnetPath()); Assert.NotNull(muxerPath); Assert.True(File.Exists(muxerPath), "The file did not exist"); Assert.True(Path.IsPathRooted(muxerPath), "The path should be rooted"); Assert.Equal("dotnet", Path.GetFileNameWithoutExtension(muxerPath), ignoreCase: true); + + static string GetDotnetPath() + { + // Process.MainModule is app[.exe] and not `dotnet`. We can instead calculate the dotnet SDK path + // by looking at the shared fx directory instead. + // depsFile = /dotnet/shared/Microsoft.NETCore.App/6.0-preview2/Microsoft.NETCore.App.deps.json + var depsFile = (string)AppContext.GetData("FX_DEPS_FILE"); + return Path.GetFullPath(Path.Combine(Path.GetDirectoryName(depsFile), "..", "..", "..", "dotnet" + (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".exe" : ""))); + } + } + + [Fact] + public void ReturnsNullIfMainModuleIsNotDotNet() + { + var muxerPath = DotNetMuxer.TryFindMuxerPath(@"d:\some-path\testhost.exe"); + Assert.Null(muxerPath); } } }