Skip to content

Commit 66d2cf5

Browse files
authored
Update DotNetMuxerTest (#31053)
Fixes #24082
1 parent 36e5113 commit 66d2cf5

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/Shared/CommandLineUtils/Utilities/DotNetMuxer.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal static class DotNetMuxer
2222

2323
static DotNetMuxer()
2424
{
25-
MuxerPath = TryFindMuxerPath();
25+
MuxerPath = TryFindMuxerPath(Process.GetCurrentProcess().MainModule?.FileName);
2626
}
2727

2828
/// <summary>
@@ -38,19 +38,18 @@ static DotNetMuxer()
3838
public static string MuxerPathOrDefault()
3939
=> MuxerPath ?? MuxerName;
4040

41-
private static string? TryFindMuxerPath()
41+
internal static string? TryFindMuxerPath(string? mainModule)
4242
{
4343
var fileName = MuxerName;
4444
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
4545
{
4646
fileName += ".exe";
4747
}
4848

49-
var mainModule = Process.GetCurrentProcess().MainModule;
50-
if (!string.IsNullOrEmpty(mainModule?.FileName)
51-
&& string.Equals(Path.GetFileName(mainModule!.FileName), fileName, StringComparison.OrdinalIgnoreCase))
49+
if (!string.IsNullOrEmpty(mainModule)
50+
&& string.Equals(Path.GetFileName(mainModule!), fileName, StringComparison.OrdinalIgnoreCase))
5251
{
53-
return mainModule.FileName;
52+
return mainModule;
5453
}
5554

5655
return null;

src/Shared/test/Shared.Tests/DotNetMuxerTests.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
#if NETCOREAPP
5+
using System;
56
using System.IO;
67
using System.Runtime.InteropServices;
78
using Xunit;
@@ -10,14 +11,31 @@ namespace Microsoft.Extensions.CommandLineUtils
1011
{
1112
public class DotNetMuxerTests
1213
{
13-
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/24082")]
14+
[Fact]
1415
public void FindsTheMuxer()
1516
{
16-
var muxerPath = DotNetMuxer.MuxerPath;
17+
18+
var muxerPath = DotNetMuxer.TryFindMuxerPath(GetDotnetPath());
1719
Assert.NotNull(muxerPath);
1820
Assert.True(File.Exists(muxerPath), "The file did not exist");
1921
Assert.True(Path.IsPathRooted(muxerPath), "The path should be rooted");
2022
Assert.Equal("dotnet", Path.GetFileNameWithoutExtension(muxerPath), ignoreCase: true);
23+
24+
static string GetDotnetPath()
25+
{
26+
// Process.MainModule is app[.exe] and not `dotnet`. We can instead calculate the dotnet SDK path
27+
// by looking at the shared fx directory instead.
28+
// depsFile = /dotnet/shared/Microsoft.NETCore.App/6.0-preview2/Microsoft.NETCore.App.deps.json
29+
var depsFile = (string)AppContext.GetData("FX_DEPS_FILE");
30+
return Path.GetFullPath(Path.Combine(Path.GetDirectoryName(depsFile), "..", "..", "..", "dotnet" + (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".exe" : "")));
31+
}
32+
}
33+
34+
[Fact]
35+
public void ReturnsNullIfMainModuleIsNotDotNet()
36+
{
37+
var muxerPath = DotNetMuxer.TryFindMuxerPath(@"d:\some-path\testhost.exe");
38+
Assert.Null(muxerPath);
2139
}
2240
}
2341
}

0 commit comments

Comments
 (0)