2017-04-26 22:01:59 +00:00
|
|
|
// 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.
|
|
|
|
|
2017-04-28 22:02:02 +00:00
|
|
|
using FluentAssertions;
|
2017-04-26 22:01:59 +00:00
|
|
|
using Microsoft.Build.Framework;
|
2017-04-28 22:02:02 +00:00
|
|
|
using Microsoft.DotNet.MSBuildSdkResolver;
|
|
|
|
using Microsoft.DotNet.Tools.Test.Utilities;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Runtime.CompilerServices;
|
2017-05-23 06:09:32 +00:00
|
|
|
using System.Runtime.InteropServices;
|
2017-04-26 22:01:59 +00:00
|
|
|
using Xunit;
|
|
|
|
using Xunit.Abstractions;
|
2017-05-01 20:13:48 +00:00
|
|
|
using System;
|
2017-04-26 22:01:59 +00:00
|
|
|
|
2017-07-05 16:54:46 +00:00
|
|
|
[assembly: CollectionBehavior(DisableTestParallelization = true)]
|
|
|
|
|
2017-04-26 22:01:59 +00:00
|
|
|
namespace Microsoft.DotNet.Cli.Utils.Tests
|
|
|
|
{
|
2017-04-28 22:02:02 +00:00
|
|
|
public class GivenAnMSBuildSdkResolver : TestBase
|
2017-04-26 22:01:59 +00:00
|
|
|
{
|
|
|
|
private ITestOutputHelper _logger;
|
|
|
|
|
|
|
|
public GivenAnMSBuildSdkResolver(ITestOutputHelper logger)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void ItHasCorrectNameAndPriority()
|
|
|
|
{
|
|
|
|
var resolver = new DotNetMSBuildSdkResolver();
|
|
|
|
|
|
|
|
Assert.Equal(5000, resolver.Priority);
|
|
|
|
Assert.Equal("Microsoft.DotNet.MSBuildSdkResolver", resolver.Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
2017-04-28 22:02:02 +00:00
|
|
|
public void ItFindsTheVersionSpecifiedInGlobalJson()
|
2017-04-26 22:01:59 +00:00
|
|
|
{
|
2017-04-28 22:02:02 +00:00
|
|
|
var environment = new TestEnvironment();
|
|
|
|
environment.CreateSdkDirectory(ProgramFiles.X64, "Some.Test.Sdk", "99.99.97");
|
|
|
|
var expected = environment.CreateSdkDirectory(ProgramFiles.X64, "Some.Test.Sdk", "99.99.98");
|
|
|
|
environment.CreateSdkDirectory(ProgramFiles.X64, "Some.Test.Sdk", "99.99.99");
|
|
|
|
environment.CreateGlobalJson(environment.TestDirectory, "99.99.98");
|
2017-05-23 06:09:32 +00:00
|
|
|
environment.CreateMuxerAndAddToPath(ProgramFiles.X64);
|
2017-04-28 22:02:02 +00:00
|
|
|
|
|
|
|
var resolver = environment.CreateResolver();
|
2017-04-26 22:01:59 +00:00
|
|
|
var result = (MockResult)resolver.Resolve(
|
2017-04-28 22:02:02 +00:00
|
|
|
new SdkReference("Some.Test.Sdk", null, null),
|
|
|
|
new MockContext { ProjectFilePath = environment.TestDirectory.FullName },
|
2017-04-26 22:01:59 +00:00
|
|
|
new MockFactory());
|
|
|
|
|
2017-05-20 05:40:11 +00:00
|
|
|
result.Success.Should().BeTrue();
|
2017-04-28 22:02:02 +00:00
|
|
|
result.Path.Should().Be(expected.FullName);
|
|
|
|
result.Version.Should().Be("99.99.98");
|
|
|
|
result.Warnings.Should().BeNullOrEmpty();
|
|
|
|
result.Errors.Should().BeNullOrEmpty();
|
|
|
|
}
|
|
|
|
|
2017-05-20 05:40:11 +00:00
|
|
|
[Fact]
|
|
|
|
public void ItReturnsNullIfTheVersionFoundDoesNotSatisfyTheMinVersion()
|
|
|
|
{
|
|
|
|
var environment = new TestEnvironment();
|
|
|
|
environment.CreateSdkDirectory(ProgramFiles.X64, "Some.Test.Sdk", "99.99.99");
|
2017-05-24 06:19:56 +00:00
|
|
|
environment.CreateMuxerAndAddToPath(ProgramFiles.X64);
|
2017-05-20 05:40:11 +00:00
|
|
|
|
|
|
|
var resolver = environment.CreateResolver();
|
|
|
|
var result = (MockResult)resolver.Resolve(
|
|
|
|
new SdkReference("Some.Test.Sdk", null, "999.99.99"),
|
|
|
|
new MockContext { ProjectFilePath = environment.TestDirectory.FullName },
|
|
|
|
new MockFactory());
|
|
|
|
|
|
|
|
result.Success.Should().BeFalse();
|
|
|
|
result.Path.Should().BeNull();
|
|
|
|
result.Version.Should().BeNull();
|
|
|
|
result.Warnings.Should().BeNullOrEmpty();
|
2017-05-25 00:55:18 +00:00
|
|
|
result.Errors.Should().Contain("Version 99.99.99 of the .NET Core SDK is smaller than the minimum version 999.99.99"
|
2017-05-22 17:28:12 +00:00
|
|
|
+ " requested. Check that a recent enough .NET Core SDK is installed, increase the minimum version"
|
|
|
|
+ " specified in the project, or increase the version specified in global.json.");
|
2017-05-20 05:40:11 +00:00
|
|
|
}
|
|
|
|
|
2017-05-24 23:12:11 +00:00
|
|
|
[Fact]
|
|
|
|
public void ItReturnsNullWhenTheSDKRequiresAHigherVersionOfMSBuildThanTheOneAvailable()
|
|
|
|
{
|
|
|
|
var environment = new TestEnvironment();
|
|
|
|
var expected =
|
|
|
|
environment.CreateSdkDirectory(ProgramFiles.X64, "Some.Test.Sdk", "99.99.99", new Version(2, 0));
|
|
|
|
environment.CreateMuxerAndAddToPath(ProgramFiles.X64);
|
|
|
|
|
|
|
|
var resolver = environment.CreateResolver();
|
|
|
|
var result = (MockResult)resolver.Resolve(
|
|
|
|
new SdkReference("Some.Test.Sdk", null, "99.99.99"),
|
|
|
|
new MockContext
|
|
|
|
{
|
2017-05-26 21:40:50 +00:00
|
|
|
MSBuildVersion = new Version(1, 0),
|
2017-05-24 23:12:11 +00:00
|
|
|
ProjectFilePath = environment.TestDirectory.FullName
|
|
|
|
},
|
|
|
|
new MockFactory());
|
|
|
|
|
|
|
|
result.Success.Should().BeFalse();
|
|
|
|
result.Path.Should().BeNull();
|
|
|
|
result.Version.Should().BeNull();
|
|
|
|
result.Warnings.Should().BeNullOrEmpty();
|
2017-05-25 01:01:55 +00:00
|
|
|
result.Errors.Should().Contain("Version 99.99.99 of the .NET Core SDK requires at least version 2.0 of MSBuild."
|
|
|
|
+ " The current available version of MSBuild is 1.0. Change the .NET Core SDK specified in global.json to an older"
|
|
|
|
+ " version that requires the MSBuild version currently available.");
|
2017-05-24 23:12:11 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 05:40:11 +00:00
|
|
|
[Fact]
|
2017-07-05 16:54:46 +00:00
|
|
|
public void ItReturnsNullWhenTheDefaultVSRequiredSDKVersionIsHigherThanTheSDKVersionAvailable()
|
|
|
|
{
|
|
|
|
var environment = new TestEnvironment();
|
|
|
|
var expected =
|
|
|
|
environment.CreateSdkDirectory(ProgramFiles.X64, "Some.Test.Sdk", "1.0.1");
|
|
|
|
environment.CreateMuxerAndAddToPath(ProgramFiles.X64);
|
|
|
|
|
|
|
|
var resolver = environment.CreateResolver();
|
|
|
|
var result = (MockResult)resolver.Resolve(
|
|
|
|
new SdkReference("Some.Test.Sdk", null, "1.0.0"),
|
|
|
|
new MockContext { ProjectFilePath = environment.TestDirectory.FullName },
|
|
|
|
new MockFactory());
|
|
|
|
|
|
|
|
result.Success.Should().BeFalse();
|
|
|
|
result.Path.Should().BeNull();
|
|
|
|
result.Version.Should().BeNull();
|
|
|
|
result.Warnings.Should().BeNullOrEmpty();
|
|
|
|
result.Errors.Should().Contain($"Version 1.0.1 of the .NET Core SDK is smaller than the minimum version"
|
|
|
|
+ " 1.0.4 required by Visual Studio. Check that a recent enough"
|
|
|
|
+ " .NET Core SDK is installed or increase the version specified in global.json.");
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void ItReturnsNullWhenTheTheVSRequiredSDKVersionIsHigherThanTheSDKVersionAvailable()
|
|
|
|
{
|
|
|
|
var environment = new TestEnvironment();
|
|
|
|
var expected =
|
|
|
|
environment.CreateSdkDirectory(ProgramFiles.X64, "Some.Test.Sdk", "1.0.1");
|
|
|
|
environment.CreateMuxerAndAddToPath(ProgramFiles.X64);
|
|
|
|
environment.CreateMinimumVSDefinedSDKVersionFile("2.0.0");
|
|
|
|
|
|
|
|
var resolver = environment.CreateResolver();
|
|
|
|
var result = (MockResult)resolver.Resolve(
|
|
|
|
new SdkReference("Some.Test.Sdk", null, "1.0.0"),
|
|
|
|
new MockContext { ProjectFilePath = environment.TestDirectory.FullName },
|
|
|
|
new MockFactory());
|
|
|
|
|
|
|
|
result.Success.Should().BeFalse();
|
|
|
|
result.Path.Should().BeNull();
|
|
|
|
result.Version.Should().BeNull();
|
|
|
|
result.Warnings.Should().BeNullOrEmpty();
|
|
|
|
result.Errors.Should().Contain($"Version 1.0.1 of the .NET Core SDK is smaller than the minimum version"
|
|
|
|
+ " 2.0.0 required by Visual Studio. Check that a recent enough"
|
|
|
|
+ " .NET Core SDK is installed or increase the version specified in global.json.");
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void ItReturnsTheVersionIfItIsEqualToTheMinVersionAndTheVSDefinedMinVersion()
|
2017-05-20 05:40:11 +00:00
|
|
|
{
|
|
|
|
var environment = new TestEnvironment();
|
|
|
|
var expected = environment.CreateSdkDirectory(ProgramFiles.X64, "Some.Test.Sdk", "99.99.99");
|
2017-05-24 06:19:56 +00:00
|
|
|
environment.CreateMuxerAndAddToPath(ProgramFiles.X64);
|
2017-07-05 16:54:46 +00:00
|
|
|
environment.CreateMinimumVSDefinedSDKVersionFile("99.99.99");
|
2017-05-20 05:40:11 +00:00
|
|
|
|
|
|
|
var resolver = environment.CreateResolver();
|
|
|
|
var result = (MockResult)resolver.Resolve(
|
|
|
|
new SdkReference("Some.Test.Sdk", null, "99.99.99"),
|
|
|
|
new MockContext { ProjectFilePath = environment.TestDirectory.FullName },
|
|
|
|
new MockFactory());
|
|
|
|
|
|
|
|
result.Success.Should().BeTrue();
|
|
|
|
result.Path.Should().Be(expected.FullName);
|
|
|
|
result.Version.Should().Be("99.99.99");
|
|
|
|
result.Warnings.Should().BeNullOrEmpty();
|
|
|
|
result.Errors.Should().BeNullOrEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
2017-07-05 16:54:46 +00:00
|
|
|
public void ItReturnsTheVersionIfItIsHigherThanTheMinVersionAndTheVSDefinedMinVersion()
|
2017-05-20 05:40:11 +00:00
|
|
|
{
|
|
|
|
var environment = new TestEnvironment();
|
|
|
|
var expected = environment.CreateSdkDirectory(ProgramFiles.X64, "Some.Test.Sdk", "999.99.99");
|
2017-05-24 06:19:56 +00:00
|
|
|
environment.CreateMuxerAndAddToPath(ProgramFiles.X64);
|
2017-07-05 16:54:46 +00:00
|
|
|
environment.CreateMinimumVSDefinedSDKVersionFile("999.99.98");
|
2017-05-20 05:40:11 +00:00
|
|
|
|
|
|
|
var resolver = environment.CreateResolver();
|
|
|
|
var result = (MockResult)resolver.Resolve(
|
|
|
|
new SdkReference("Some.Test.Sdk", null, "99.99.99"),
|
|
|
|
new MockContext { ProjectFilePath = environment.TestDirectory.FullName },
|
|
|
|
new MockFactory());
|
|
|
|
|
|
|
|
result.Success.Should().BeTrue();
|
|
|
|
result.Path.Should().Be(expected.FullName);
|
|
|
|
result.Version.Should().Be("999.99.99");
|
|
|
|
result.Warnings.Should().BeNullOrEmpty();
|
|
|
|
result.Errors.Should().BeNullOrEmpty();
|
|
|
|
}
|
|
|
|
|
2017-04-28 22:02:02 +00:00
|
|
|
private enum ProgramFiles
|
|
|
|
{
|
|
|
|
X64,
|
|
|
|
X86,
|
|
|
|
Default,
|
|
|
|
}
|
|
|
|
|
|
|
|
private sealed class TestEnvironment : SdkResolverContext
|
|
|
|
{
|
2017-05-23 06:09:32 +00:00
|
|
|
public string Muxer => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "dotnet.exe" : "dotnet";
|
|
|
|
|
|
|
|
public string PathEnvironmentVariable { get; set; }
|
|
|
|
|
2017-04-28 22:02:02 +00:00
|
|
|
public DirectoryInfo TestDirectory { get; }
|
|
|
|
|
|
|
|
public TestEnvironment(string identifier = "", [CallerMemberName] string callingMethod = "")
|
|
|
|
{
|
|
|
|
TestDirectory = TestAssets.CreateTestDirectory(
|
|
|
|
"temp",
|
|
|
|
identifier: identifier,
|
|
|
|
callingMethod: callingMethod);
|
2017-05-23 06:09:32 +00:00
|
|
|
|
2017-07-05 16:54:46 +00:00
|
|
|
DeleteMinimumVSDefinedSDKVersionFile();
|
|
|
|
|
2017-05-23 06:09:32 +00:00
|
|
|
PathEnvironmentVariable = string.Empty;
|
2017-04-28 22:02:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public SdkResolver CreateResolver()
|
2017-05-01 20:13:48 +00:00
|
|
|
=> new DotNetMSBuildSdkResolver(GetEnvironmentVariable);
|
2017-04-28 22:02:02 +00:00
|
|
|
|
|
|
|
public DirectoryInfo GetSdkDirectory(ProgramFiles programFiles, string sdkName, string sdkVersion)
|
2017-05-24 23:12:11 +00:00
|
|
|
=> TestDirectory.GetDirectory(
|
|
|
|
GetProgramFilesDirectory(programFiles).FullName,
|
|
|
|
"dotnet",
|
|
|
|
"sdk",
|
|
|
|
sdkVersion,
|
|
|
|
"Sdks",
|
|
|
|
sdkName,
|
|
|
|
"Sdk");
|
2017-04-28 22:02:02 +00:00
|
|
|
|
|
|
|
public DirectoryInfo GetProgramFilesDirectory(ProgramFiles programFiles)
|
|
|
|
=> TestDirectory.GetDirectory($"ProgramFiles{programFiles}");
|
|
|
|
|
2017-05-24 23:12:11 +00:00
|
|
|
public DirectoryInfo CreateSdkDirectory(
|
|
|
|
ProgramFiles programFiles,
|
|
|
|
string sdkName,
|
|
|
|
string sdkVersion,
|
|
|
|
Version minimumMSBuildVersion = null)
|
2017-04-28 22:02:02 +00:00
|
|
|
{
|
2017-05-24 23:12:11 +00:00
|
|
|
var dir = GetSdkDirectory(programFiles, sdkName, sdkVersion);
|
2017-04-28 22:02:02 +00:00
|
|
|
dir.Create();
|
2017-05-24 23:12:11 +00:00
|
|
|
|
2017-05-26 21:40:50 +00:00
|
|
|
if (minimumMSBuildVersion != null)
|
|
|
|
{
|
|
|
|
CreateMSBuildRequiredVersionFile(programFiles, sdkVersion, minimumMSBuildVersion);
|
|
|
|
}
|
2017-05-24 23:12:11 +00:00
|
|
|
|
2017-04-28 22:02:02 +00:00
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
2017-05-23 06:09:32 +00:00
|
|
|
public void CreateMuxerAndAddToPath(ProgramFiles programFiles)
|
|
|
|
{
|
2017-05-24 23:12:11 +00:00
|
|
|
var muxerDirectory =
|
|
|
|
TestDirectory.GetDirectory(GetProgramFilesDirectory(programFiles).FullName, "dotnet");
|
2017-05-23 06:09:32 +00:00
|
|
|
|
|
|
|
new FileInfo(Path.Combine(muxerDirectory.FullName, Muxer)).Create();
|
|
|
|
|
|
|
|
PathEnvironmentVariable = $"{muxerDirectory}{Path.PathSeparator}{PathEnvironmentVariable}";
|
|
|
|
}
|
|
|
|
|
2017-05-24 23:12:11 +00:00
|
|
|
private void CreateMSBuildRequiredVersionFile(
|
|
|
|
ProgramFiles programFiles,
|
|
|
|
string sdkVersion,
|
|
|
|
Version minimumMSBuildVersion)
|
|
|
|
{
|
|
|
|
if (minimumMSBuildVersion == null)
|
|
|
|
{
|
|
|
|
minimumMSBuildVersion = new Version(1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
var cliDirectory = TestDirectory.GetDirectory(
|
|
|
|
GetProgramFilesDirectory(programFiles).FullName,
|
|
|
|
"dotnet",
|
|
|
|
"sdk",
|
|
|
|
sdkVersion);
|
|
|
|
|
|
|
|
File.WriteAllText(
|
|
|
|
Path.Combine(cliDirectory.FullName, "minimumMSBuildVersion"),
|
|
|
|
minimumMSBuildVersion.ToString());
|
|
|
|
}
|
|
|
|
|
2017-04-28 22:02:02 +00:00
|
|
|
public void CreateGlobalJson(DirectoryInfo directory, string version)
|
|
|
|
=> File.WriteAllText(directory.GetFile("global.json").FullName,
|
|
|
|
$@"{{ ""sdk"": {{ ""version"": ""{version}"" }} }}");
|
2017-05-01 20:13:48 +00:00
|
|
|
|
|
|
|
public string GetEnvironmentVariable(string variable)
|
|
|
|
{
|
|
|
|
switch (variable)
|
|
|
|
{
|
2017-05-23 06:09:32 +00:00
|
|
|
case "PATH":
|
|
|
|
return PathEnvironmentVariable;
|
2017-05-01 20:13:48 +00:00
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2017-07-05 16:54:46 +00:00
|
|
|
|
|
|
|
public void CreateMinimumVSDefinedSDKVersionFile(string version)
|
|
|
|
{
|
|
|
|
File.WriteAllText(GetMinimumVSDefinedSDKVersionFilePath(), version);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void DeleteMinimumVSDefinedSDKVersionFile()
|
|
|
|
{
|
|
|
|
File.Delete(GetMinimumVSDefinedSDKVersionFilePath());
|
|
|
|
}
|
|
|
|
|
|
|
|
private string GetMinimumVSDefinedSDKVersionFilePath()
|
|
|
|
{
|
|
|
|
string baseDirectory = AppContext.BaseDirectory;
|
|
|
|
return Path.Combine(baseDirectory, "minimumVSDefinedSDKVersion");
|
|
|
|
}
|
2017-04-26 22:01:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private sealed class MockContext : SdkResolverContext
|
|
|
|
{
|
2017-04-28 22:02:02 +00:00
|
|
|
public new string ProjectFilePath { get => base.ProjectFilePath; set => base.ProjectFilePath = value; }
|
|
|
|
public new string SolutionFilePath { get => base.SolutionFilePath; set => base.SolutionFilePath = value; }
|
2017-05-24 23:12:11 +00:00
|
|
|
public new Version MSBuildVersion { get => base.MSBuildVersion; set => base.MSBuildVersion = value; }
|
|
|
|
|
|
|
|
public MockContext()
|
|
|
|
{
|
2017-05-26 21:40:50 +00:00
|
|
|
MSBuildVersion = new Version(15, 3, 0);
|
2017-05-24 23:12:11 +00:00
|
|
|
}
|
2017-04-26 22:01:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private sealed class MockFactory : SdkResultFactory
|
|
|
|
{
|
|
|
|
public override SdkResult IndicateFailure(IEnumerable<string> errors, IEnumerable<string> warnings = null)
|
2018-05-21 21:16:16 +00:00
|
|
|
=> new MockResult(success: false, path: null, version: null, warnings: warnings, errors: errors);
|
2017-04-26 22:01:59 +00:00
|
|
|
|
|
|
|
public override SdkResult IndicateSuccess(string path, string version, IEnumerable<string> warnings = null)
|
2018-05-21 21:16:16 +00:00
|
|
|
=> new MockResult(success: true, path: path, version: version, warnings: warnings);
|
2017-04-26 22:01:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private sealed class MockResult : SdkResult
|
|
|
|
{
|
2018-05-21 21:16:16 +00:00
|
|
|
public MockResult(bool success, string path, string version, IEnumerable<string> warnings = null,
|
|
|
|
IEnumerable<string> errors = null)
|
|
|
|
{
|
|
|
|
Success = success;
|
|
|
|
Path = path;
|
|
|
|
Version = version;
|
|
|
|
Warnings = warnings;
|
|
|
|
Errors = errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
public new bool Success
|
|
|
|
{
|
|
|
|
get => base.Success;
|
|
|
|
private set => base.Success = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string Version { get; protected set; }
|
|
|
|
public override string Path { get; protected set; }
|
|
|
|
public IEnumerable<string> Errors { get; }
|
|
|
|
public IEnumerable<string> Warnings { get; }
|
2017-04-26 22:01:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|