-
Couldn't load subscription status.
- Fork 1.4k
Infer dotnet host to run integration scenarios #8321
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| #if !NET6_0_OR_GREATER | ||
| using System.Diagnostics; | ||
| #endif | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| namespace Microsoft.Build.UnitTests.Shared | ||
| { | ||
| public class EnvironmentProvider | ||
| { | ||
| private static class Constants | ||
| { | ||
| public const string DotNet = "dotnet"; | ||
| public const string Path = "PATH"; | ||
| public const string DotnetMsbuildSdkResolverCliDir = "DOTNET_MSBUILD_SDK_RESOLVER_CLI_DIR"; | ||
| public static readonly bool RunningOnWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); | ||
| public static readonly string ExeSuffix = RunningOnWindows ? ".exe" : string.Empty; | ||
| } | ||
|
|
||
| private IEnumerable<string>? _searchPaths; | ||
|
|
||
| private readonly Func<string, string?> _getEnvironmentVariable; | ||
| private readonly Func<string?> _getCurrentProcessPath; | ||
|
|
||
| public EnvironmentProvider(Func<string, string?> getEnvironmentVariable) | ||
| : this(getEnvironmentVariable, GetCurrentProcessPath) | ||
| { } | ||
|
|
||
| public EnvironmentProvider(Func<string, string?> getEnvironmentVariable, Func<string?> getCurrentProcessPath) | ||
| { | ||
| _getEnvironmentVariable = getEnvironmentVariable; | ||
| _getCurrentProcessPath = getCurrentProcessPath; | ||
| } | ||
|
|
||
| private IEnumerable<string> SearchPaths | ||
| { | ||
| get | ||
| { | ||
| if (_searchPaths == null) | ||
| { | ||
| var searchPaths = new List<string>(); | ||
|
|
||
| searchPaths.AddRange( | ||
| (_getEnvironmentVariable(Constants.Path) ?? string.Empty) | ||
| .Split(new char[] { Path.PathSeparator }, options: StringSplitOptions.RemoveEmptyEntries) | ||
| .Select(p => p.Trim('"'))); | ||
|
|
||
| _searchPaths = searchPaths; | ||
| } | ||
|
|
||
| return _searchPaths; | ||
| } | ||
| } | ||
|
|
||
| public string? GetCommandPath(string commandName) | ||
| { | ||
| var commandNameWithExtension = commandName + Constants.ExeSuffix; | ||
| var commandPath = SearchPaths | ||
| .Where(p => !Path.GetInvalidPathChars().Any(p.Contains)) | ||
| .Select(p => Path.Combine(p, commandNameWithExtension)) | ||
| .FirstOrDefault(File.Exists); | ||
|
|
||
| return commandPath; | ||
| } | ||
|
|
||
| public string? GetDotnetExePath() | ||
| { | ||
| string? environmentOverride = _getEnvironmentVariable(Constants.DotnetMsbuildSdkResolverCliDir); | ||
| if (!string.IsNullOrEmpty(environmentOverride)) | ||
| { | ||
| return Path.Combine(environmentOverride, Constants.DotNet + Constants.ExeSuffix); | ||
| } | ||
|
|
||
| string? dotnetExe = _getCurrentProcessPath(); | ||
|
|
||
| if (string.IsNullOrEmpty(dotnetExe) || !Path.GetFileNameWithoutExtension(dotnetExe) | ||
| .Equals(Constants.DotNet, StringComparison.InvariantCultureIgnoreCase)) | ||
| { | ||
| string? dotnetExeFromPath = GetCommandPath(Constants.DotNet); | ||
| #if NET | ||
| if (dotnetExeFromPath != null && !Constants.RunningOnWindows) | ||
| { | ||
| // on Linux the 'dotnet' command from PATH is a symlink so we need to | ||
| // resolve it to get the actual path to the binary | ||
| FileInfo fi = new FileInfo(dotnetExeFromPath); | ||
| while (fi.LinkTarget != null) | ||
| { | ||
| dotnetExeFromPath = fi.LinkTarget; | ||
| fi = new FileInfo(dotnetExeFromPath); | ||
| } | ||
| } | ||
| #endif | ||
| if (!string.IsNullOrWhiteSpace(dotnetExeFromPath)) | ||
| { | ||
| dotnetExe = dotnetExeFromPath; | ||
| } | ||
| } | ||
|
|
||
| return dotnetExe; | ||
| } | ||
|
|
||
| public static string? GetDotnetExePath(Func<string, string?>? getEnvironmentVariable = null) | ||
| { | ||
| if (getEnvironmentVariable == null) | ||
| { | ||
| getEnvironmentVariable = Environment.GetEnvironmentVariable; | ||
| } | ||
| var environmentProvider = new EnvironmentProvider(getEnvironmentVariable); | ||
| return environmentProvider.GetDotnetExePath(); | ||
| } | ||
|
|
||
| public static string? GetDotnetExePath(Func<string, string?> getEnvironmentVariable, Func<string?> getCurrentProcessPath) | ||
| { | ||
| getEnvironmentVariable ??= Environment.GetEnvironmentVariable; | ||
| getCurrentProcessPath ??= GetCurrentProcessPath; | ||
| var environmentProvider = new EnvironmentProvider(getEnvironmentVariable, getCurrentProcessPath); | ||
| return environmentProvider.GetDotnetExePath(); | ||
| } | ||
|
|
||
| private static string? GetCurrentProcessPath() | ||
| { | ||
| string? currentProcessPath; | ||
| #if NET6_0_OR_GREATER | ||
| currentProcessPath = Environment.ProcessPath; | ||
| #else | ||
| currentProcessPath = Process.GetCurrentProcess().MainModule.FileName; | ||
| #endif | ||
| return currentProcessPath; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: just see there is
Shared\UnitTestsandUnitTests.Shared: what is the difference here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch!
For historical reasons, there is a bunch of code in MSBuild that is compiled into multiple assemblies (sometimes with slightly different ifdefs). That includes test assemblies. However, the reasons for doing this in non-test assemblies (they aren't great reasons but they are reasons) don't really apply for test, so we introduced a shared assembly for test stuff to build that sort of thing once--but didn't move everything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually it looks like shortly after introducing that assembly, we dropped all references to it (in ~2017).
It's still a good idea.