Skip to content

Update DotNetMuxerTest #31053

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

Merged
merged 1 commit into from
Mar 19, 2021
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
11 changes: 5 additions & 6 deletions src/Shared/CommandLineUtils/Utilities/DotNetMuxer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal static class DotNetMuxer

static DotNetMuxer()
{
MuxerPath = TryFindMuxerPath();
MuxerPath = TryFindMuxerPath(Process.GetCurrentProcess().MainModule?.FileName);
}

/// <summary>
Expand All @@ -38,19 +38,18 @@ 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))
{
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;
Expand Down
22 changes: 20 additions & 2 deletions src/Shared/test/Shared.Tests/DotNetMuxerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
}
Expand Down