Skip to content

Commit

Permalink
Adjust GetDotnetPath utility to work for xcopy installs and test env
Browse files Browse the repository at this point in the history
  • Loading branch information
JanKrivanek committed Aug 1, 2022
1 parent ef90c67 commit de30783
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
33 changes: 20 additions & 13 deletions src/Resolvers/Microsoft.DotNet.NativeWrapper/EnvironmentProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,29 @@ public string GetDotnetExeDirectory()
return environmentOverride;
}

var dotnetExe = GetCommandPath(Constants.DotNet);

if (dotnetExe != null && !Interop.RunningOnWindows)
{
// e.g. on Linux the 'dotnet' command from PATH is a symlink so we need to
// resolve it to get the actual path to the binary
dotnetExe = Interop.Unix.realpath(dotnetExe) ?? dotnetExe;
}

if (string.IsNullOrWhiteSpace(dotnetExe))
{
string dotnetExe;
#if NET6_0_OR_GREATER
dotnetExe = Environment.ProcessPath;
dotnetExe = Environment.ProcessPath;
#else
dotnetExe = Process.GetCurrentProcess().MainModule.FileName;
dotnetExe = Process.GetCurrentProcess().MainModule.FileName;
#endif

if (string.IsNullOrEmpty(dotnetExe) || !Path.GetFileNameWithoutExtension(dotnetExe)
.Equals(Constants.DotNet, StringComparison.InvariantCultureIgnoreCase))
{
string dotnetExeFromPath = GetCommandPath(Constants.DotNet);

if (dotnetExeFromPath != null && !Interop.RunningOnWindows)
{
// e.g. on Linux the 'dotnet' command from PATH is a symlink so we need to
// resolve it to get the actual path to the binary
dotnetExeFromPath = Interop.Unix.realpath(dotnetExeFromPath) ?? dotnetExeFromPath;
}

if (!string.IsNullOrWhiteSpace(dotnetExeFromPath))
{
dotnetExe = dotnetExeFromPath;
}
}

return Path.GetDirectoryName(dotnetExe);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,22 @@

namespace Microsoft.DotNet.PackageInstall.Tests
{
public class ToolPackageInstallerTests : SdkTest
internal class DotnetEnvironmentTestFixture : IDisposable
{
private readonly string _originalPath;
private const string _PATH_VAR_NAME = "PATH";

public DotnetEnvironmentTestFixture()
{
string dotnetRootUnderTest = TestContext.Current.ToolsetUnderTest.DotNetRoot;
_originalPath = Environment.GetEnvironmentVariable(_PATH_VAR_NAME);
Environment.SetEnvironmentVariable(_PATH_VAR_NAME, dotnetRootUnderTest + Path.PathSeparator + _originalPath);
}

public void Dispose() => Environment.SetEnvironmentVariable(_PATH_VAR_NAME, _originalPath);
}

public class ToolPackageInstallerTests : SdkTest, IClassFixture<DotnetEnvironmentTestFixture>
{
[Theory]
[InlineData(false)]
Expand Down

0 comments on commit de30783

Please sign in to comment.