2016-03-07 19:50:52 +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.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
2016-04-28 23:30:32 +00:00
|
|
|
using FluentAssertions;
|
2016-10-11 22:31:18 +00:00
|
|
|
using Microsoft.DotNet.Cli;
|
2016-10-08 18:45:19 +00:00
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
2016-04-28 23:30:32 +00:00
|
|
|
using Microsoft.DotNet.InternalAbstractions;
|
2016-10-11 00:13:46 +00:00
|
|
|
using Microsoft.DotNet.TestFramework;
|
2016-03-07 19:50:52 +00:00
|
|
|
using Microsoft.DotNet.Tools.Test.Utilities;
|
|
|
|
using NuGet.Frameworks;
|
2016-04-28 23:30:32 +00:00
|
|
|
using Xunit;
|
2017-08-19 01:07:14 +00:00
|
|
|
using Microsoft.DotNet.Tools.Tests.Utilities;
|
2016-03-07 19:50:52 +00:00
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Cli.Utils.Tests
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
public class GivenAProjectDependencyCommandResolver : TestBase
|
2016-03-07 19:50:52 +00:00
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
private TestAssetInstance MSBuildTestProjectInstance;
|
2016-03-07 19:50:52 +00:00
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
public GivenAProjectDependencyCommandResolver()
|
2016-03-07 19:50:52 +00:00
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
Environment.SetEnvironmentVariable(
|
|
|
|
Constants.MSBUILD_EXE_PATH,
|
|
|
|
Path.Combine(new RepoDirectoriesProvider().Stage2Sdk, "MSBuild.dll"));
|
2016-03-07 19:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
2016-10-28 01:46:43 +00:00
|
|
|
public void ItReturnsACommandSpecWhenToolIsInAProjectRef()
|
2016-03-07 19:50:52 +00:00
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
MSBuildTestProjectInstance =
|
|
|
|
TestAssets.Get("TestAppWithProjDepTool")
|
|
|
|
.CreateInstance()
|
|
|
|
.WithSourceFiles()
|
|
|
|
.WithRestoreFiles();
|
|
|
|
|
|
|
|
new BuildCommand()
|
|
|
|
.WithProjectDirectory(MSBuildTestProjectInstance.Root)
|
|
|
|
.WithConfiguration("Debug")
|
|
|
|
.Execute()
|
|
|
|
.Should().Pass();
|
2016-03-07 19:50:52 +00:00
|
|
|
|
|
|
|
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
|
|
|
|
|
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
CommandName = "dotnet-portable",
|
2016-03-07 19:50:52 +00:00
|
|
|
Configuration = "Debug",
|
2016-10-28 01:46:43 +00:00
|
|
|
ProjectDirectory = MSBuildTestProjectInstance.Root.FullName,
|
2017-08-19 01:07:14 +00:00
|
|
|
Framework = NuGetFrameworks.NetCoreApp21
|
2016-03-07 19:50:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
|
|
|
|
|
|
|
|
result.Should().NotBeNull();
|
|
|
|
|
|
|
|
var commandFile = Path.GetFileNameWithoutExtension(result.Path);
|
|
|
|
|
2016-06-02 17:50:38 +00:00
|
|
|
commandFile.Should().Be("dotnet");
|
2016-03-07 19:50:52 +00:00
|
|
|
|
|
|
|
result.Args.Should().Contain(commandResolverArguments.CommandName);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
2016-10-28 01:46:43 +00:00
|
|
|
public void ItPassesDepsfileArgToHostWhenReturningACommandSpecForMSBuildProject()
|
2016-03-07 19:50:52 +00:00
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
MSBuildTestProjectInstance =
|
|
|
|
TestAssets.Get("TestAppWithProjDepTool")
|
|
|
|
.CreateInstance()
|
|
|
|
.WithSourceFiles()
|
|
|
|
.WithRestoreFiles();
|
|
|
|
|
|
|
|
new BuildCommand()
|
|
|
|
.WithProjectDirectory(MSBuildTestProjectInstance.Root)
|
|
|
|
.WithConfiguration("Debug")
|
|
|
|
.Execute()
|
|
|
|
.Should().Pass();
|
2016-03-07 19:50:52 +00:00
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
|
2016-08-10 06:54:37 +00:00
|
|
|
|
2016-03-07 19:50:52 +00:00
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
CommandName = "dotnet-portable",
|
2016-03-07 19:50:52 +00:00
|
|
|
Configuration = "Debug",
|
2016-10-28 01:46:43 +00:00
|
|
|
ProjectDirectory = MSBuildTestProjectInstance.Root.FullName,
|
2017-08-19 01:07:14 +00:00
|
|
|
Framework = NuGetFrameworks.NetCoreApp21
|
2016-03-07 19:50:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
|
|
|
|
|
|
|
|
result.Should().NotBeNull();
|
2016-10-28 01:46:43 +00:00
|
|
|
|
|
|
|
result.Args.Should().Contain("--depsfile");
|
2016-03-07 19:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
2016-10-28 01:46:43 +00:00
|
|
|
public void ItReturnsNullWhenCommandNameDoesNotExistInProjectDependenciesForMSBuildProject()
|
2016-03-07 19:50:52 +00:00
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
MSBuildTestProjectInstance =
|
|
|
|
TestAssets.Get("TestAppWithProjDepTool")
|
|
|
|
.CreateInstance()
|
|
|
|
.WithSourceFiles()
|
|
|
|
.WithRestoreFiles();
|
2016-03-07 19:50:52 +00:00
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
|
2016-08-10 06:54:37 +00:00
|
|
|
|
2016-03-07 19:50:52 +00:00
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
CommandName = "nonexistent-command",
|
2016-03-07 19:50:52 +00:00
|
|
|
CommandArguments = null,
|
2016-10-28 01:46:43 +00:00
|
|
|
ProjectDirectory = MSBuildTestProjectInstance.Root.FullName,
|
2017-08-19 01:07:14 +00:00
|
|
|
Framework = NuGetFrameworks.NetCoreApp21
|
2016-03-07 19:50:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
result.Should().BeNull();
|
2016-03-07 19:50:52 +00:00
|
|
|
}
|
|
|
|
|
2016-03-15 00:38:36 +00:00
|
|
|
[Fact]
|
2016-10-28 01:46:43 +00:00
|
|
|
public void ItSetsDepsfileToOutputInCommandspecForMSBuild()
|
2016-03-15 00:38:36 +00:00
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
var testInstance = TestAssets
|
|
|
|
.Get("TestAppWithProjDepTool")
|
|
|
|
.CreateInstance()
|
|
|
|
.WithSourceFiles()
|
|
|
|
.WithRestoreFiles();
|
2016-03-15 00:38:36 +00:00
|
|
|
|
|
|
|
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
|
2016-08-10 06:54:37 +00:00
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
var outputDir = testInstance.Root.GetDirectory("out");
|
2016-03-15 00:38:36 +00:00
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
2016-03-15 00:38:36 +00:00
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
CommandName = "dotnet-portable",
|
2016-03-15 00:38:36 +00:00
|
|
|
Configuration = "Debug",
|
2016-10-28 01:46:43 +00:00
|
|
|
ProjectDirectory = testInstance.Root.FullName,
|
2017-08-19 01:07:14 +00:00
|
|
|
Framework = NuGetFrameworks.NetCoreApp21,
|
2016-10-28 01:46:43 +00:00
|
|
|
OutputPath = outputDir.FullName
|
2016-03-15 00:38:36 +00:00
|
|
|
};
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
new BuildCommand()
|
|
|
|
.WithWorkingDirectory(testInstance.Root)
|
|
|
|
.Execute($"-o {outputDir.FullName}")
|
|
|
|
.Should()
|
|
|
|
.Pass();
|
2016-03-15 00:38:36 +00:00
|
|
|
|
|
|
|
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
var depsFilePath = outputDir.GetFile("TestAppWithProjDepTool.deps.json");
|
2016-03-07 19:50:52 +00:00
|
|
|
|
|
|
|
result.Should().NotBeNull();
|
2016-10-28 01:46:43 +00:00
|
|
|
result.Args.Should().Contain($"--depsfile {depsFilePath.FullName}");
|
2016-03-07 19:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private ProjectDependenciesCommandResolver SetupProjectDependenciesCommandResolver(
|
|
|
|
IEnvironmentProvider environment = null,
|
|
|
|
IPackagedCommandSpecFactory packagedCommandSpecFactory = null)
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
Environment.SetEnvironmentVariable(
|
|
|
|
Constants.MSBUILD_EXE_PATH,
|
|
|
|
Path.Combine(new RepoDirectoriesProvider().Stage2Sdk, "MSBuild.dll"));
|
|
|
|
|
|
|
|
CommandContext.SetVerbose(true);
|
|
|
|
|
2016-03-07 19:50:52 +00:00
|
|
|
environment = environment ?? new EnvironmentProvider();
|
2016-10-28 01:46:43 +00:00
|
|
|
|
2016-03-07 19:50:52 +00:00
|
|
|
packagedCommandSpecFactory = packagedCommandSpecFactory ?? new PackagedCommandSpecFactory();
|
|
|
|
|
|
|
|
var projectDependenciesCommandResolver = new ProjectDependenciesCommandResolver(environment, packagedCommandSpecFactory);
|
|
|
|
|
|
|
|
return projectDependenciesCommandResolver;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|