Add test hook

This commit is contained in:
Nick Guerrera 2017-04-28 10:45:37 -07:00
parent cf5f1d92c7
commit e7ebe6d594
2 changed files with 28 additions and 4 deletions

View file

@ -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")]

View file

@ -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<string, string> _getEnvironmentVariable;
public DotNetMSBuildSdkResolver()
: this(Environment.GetEnvironmentVariable)
{
}
// Test hook to provide environment variables without polluting the test process.
internal DotNetMSBuildSdkResolver(IReadOnlyDictionary<string, string> mockEnvironmentVariables)
: this(key => mockEnvironmentVariables.TryGetValue(key, out var value) ? value : null)
{
}
private DotNetMSBuildSdkResolver(Func<string, string> 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<string> 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<string>(capacity: 1) { environmentOverride };
@ -102,7 +120,7 @@ namespace Microsoft.DotNet.MSBuildSdkResolver
var candidates = new List<string>(capacity: 2);
foreach (string variable in s_programFiles)
{
string directory = Environment.GetEnvironmentVariable(variable);
string directory = _getEnvironmentVariable(variable);
if (directory == null)
{
continue;