diff --git a/src/Microsoft.DotNet.MSBuildSdkResolver/InternalsVisibleToTests.cs b/src/Microsoft.DotNet.MSBuildSdkResolver/InternalsVisibleToTests.cs new file mode 100644 index 000000000..f767ffc2b --- /dev/null +++ b/src/Microsoft.DotNet.MSBuildSdkResolver/InternalsVisibleToTests.cs @@ -0,0 +1,6 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("Microsoft.DotNet.MSBuildSdkResolver.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")] diff --git a/src/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs b/src/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs index 6048d3d59..fe8cfd905 100644 --- a/src/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs +++ b/src/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs @@ -15,13 +15,31 @@ namespace Microsoft.DotNet.MSBuildSdkResolver // Default resolver has priority 10000 and we want to go before it and leave room on either side of us. public override int Priority => 5000; + private readonly Func _getEnvironmentVariable; + + public DotNetMSBuildSdkResolver() + : this(Environment.GetEnvironmentVariable) + { + } + + // Test hook to provide environment variables without polluting the test process. + internal DotNetMSBuildSdkResolver(IReadOnlyDictionary mockEnvironmentVariables) + : this(key => mockEnvironmentVariables.TryGetValue(key, out var value) ? value : null) + { + } + + private DotNetMSBuildSdkResolver(Func getEnvironmentVariable) + { + _getEnvironmentVariable = getEnvironmentVariable; + } + public override SdkResult Resolve(SdkReference sdkReference, SdkResolverContext context, SdkResultFactory factory) { // These are overrides that are used to force the resolved SDK tasks and targets to come from a given // base directory and report a given version to msbuild (which may be null if unknown. One key use case // for this is to test SDK tasks and targets without deploying them inside the .NET Core SDK. - string msbuildSdksDir = Environment.GetEnvironmentVariable("DOTNET_MSBUILD_SDK_RESOLVER_SDKS_DIR"); - string netcoreSdkVersion = Environment.GetEnvironmentVariable("DOTNET_MSBUILD_SDK_RESOLVER_SDKS_VER"); + string msbuildSdksDir = _getEnvironmentVariable("DOTNET_MSBUILD_SDK_RESOLVER_SDKS_DIR"); + string netcoreSdkVersion = _getEnvironmentVariable("DOTNET_MSBUILD_SDK_RESOLVER_SDKS_VER"); if (msbuildSdksDir == null) { @@ -91,7 +109,7 @@ namespace Microsoft.DotNet.MSBuildSdkResolver private List GetDotnetExeDirectoryCandidates() { - string environmentOverride = Environment.GetEnvironmentVariable("DOTNET_MSBUILD_SDK_RESOLVER_CLI_DIR"); + string environmentOverride = _getEnvironmentVariable("DOTNET_MSBUILD_SDK_RESOLVER_CLI_DIR"); if (environmentOverride != null) { return new List(capacity: 1) { environmentOverride }; @@ -102,7 +120,7 @@ namespace Microsoft.DotNet.MSBuildSdkResolver var candidates = new List(capacity: 2); foreach (string variable in s_programFiles) { - string directory = Environment.GetEnvironmentVariable(variable); + string directory = _getEnvironmentVariable(variable); if (directory == null) { continue;