Skip to content

Commit

Permalink
Update DotNetMuxerTest (#31053)
Browse files Browse the repository at this point in the history
Fixes #24082
  • Loading branch information
pranavkm committed Mar 19, 2021
1 parent 36e5113 commit 66d2cf5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
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

0 comments on commit 66d2cf5

Please sign in to comment.