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-03-07 19:50:52 +00:00
|
|
|
using Microsoft.DotNet.ProjectModel;
|
|
|
|
using Microsoft.DotNet.Tools.Test.Utilities;
|
|
|
|
using NuGet.Frameworks;
|
2016-04-28 23:30:32 +00:00
|
|
|
using Xunit;
|
2016-03-07 19:50:52 +00:00
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Cli.Utils.Tests
|
|
|
|
{
|
2016-08-10 06:54:37 +00:00
|
|
|
public class GivenAProjectDependenciesCommandResolver : TestBase
|
2016-03-07 19:50:52 +00:00
|
|
|
{
|
|
|
|
|
2016-08-18 17:17:58 +00:00
|
|
|
private const string TestProjectName = "AppWithDirectDep";
|
2016-03-07 19:50:52 +00:00
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void It_returns_null_when_CommandName_is_null()
|
|
|
|
{
|
|
|
|
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
|
|
|
|
|
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
|
|
|
{
|
|
|
|
CommandName = null,
|
2016-04-28 23:30:32 +00:00
|
|
|
CommandArguments = new string[] { "" },
|
2016-03-07 19:50:52 +00:00
|
|
|
ProjectDirectory = "/some/directory",
|
|
|
|
Configuration = "Debug",
|
2016-04-13 00:29:07 +00:00
|
|
|
Framework = FrameworkConstants.CommonFrameworks.NetCoreApp10
|
2016-03-07 19:50:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
|
|
|
|
|
|
|
|
result.Should().BeNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void It_returns_null_when_ProjectDirectory_is_null()
|
|
|
|
{
|
|
|
|
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
|
|
|
|
|
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
|
|
|
{
|
|
|
|
CommandName = "command",
|
2016-04-28 23:30:32 +00:00
|
|
|
CommandArguments = new string[] { "" },
|
2016-03-07 19:50:52 +00:00
|
|
|
ProjectDirectory = null,
|
|
|
|
Configuration = "Debug",
|
2016-04-13 00:29:07 +00:00
|
|
|
Framework = FrameworkConstants.CommonFrameworks.NetCoreApp10
|
2016-03-07 19:50:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
|
|
|
|
|
|
|
|
result.Should().BeNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void It_returns_null_when_Framework_is_null()
|
|
|
|
{
|
|
|
|
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
|
|
|
|
|
2016-08-10 06:54:37 +00:00
|
|
|
var testInstance = TestAssetsManager.CreateTestInstance(TestProjectName)
|
|
|
|
.WithLockFiles();
|
|
|
|
|
2016-03-07 19:50:52 +00:00
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
|
|
|
{
|
|
|
|
CommandName = "command",
|
2016-04-28 23:30:32 +00:00
|
|
|
CommandArguments = new string[] { "" },
|
2016-08-10 06:54:37 +00:00
|
|
|
ProjectDirectory = testInstance.Path,
|
2016-03-07 19:50:52 +00:00
|
|
|
Configuration = "Debug",
|
|
|
|
Framework = null
|
|
|
|
};
|
|
|
|
|
|
|
|
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
|
|
|
|
|
|
|
|
result.Should().BeNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void It_returns_null_when_Configuration_is_null()
|
|
|
|
{
|
|
|
|
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
|
|
|
|
|
2016-08-10 06:54:37 +00:00
|
|
|
var testInstance = TestAssetsManager.CreateTestInstance(TestProjectName)
|
|
|
|
.WithLockFiles();
|
|
|
|
|
2016-03-07 19:50:52 +00:00
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
|
|
|
{
|
|
|
|
CommandName = "command",
|
2016-04-28 23:30:32 +00:00
|
|
|
CommandArguments = new string[] { "" },
|
2016-08-10 06:54:37 +00:00
|
|
|
ProjectDirectory = testInstance.Path,
|
2016-03-07 19:50:52 +00:00
|
|
|
Configuration = null,
|
2016-04-13 00:29:07 +00:00
|
|
|
Framework = FrameworkConstants.CommonFrameworks.NetCoreApp10
|
2016-03-07 19:50:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
|
|
|
|
|
|
|
|
result.Should().BeNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void It_returns_null_when_CommandName_does_not_exist_in_ProjectDependencies()
|
|
|
|
{
|
|
|
|
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
|
|
|
|
|
2016-08-10 06:54:37 +00:00
|
|
|
var testInstance = TestAssetsManager.CreateTestInstance(TestProjectName)
|
|
|
|
.WithLockFiles();
|
|
|
|
|
2016-03-07 19:50:52 +00:00
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
|
|
|
{
|
|
|
|
CommandName = "nonexistent-command",
|
|
|
|
CommandArguments = null,
|
2016-08-10 06:54:37 +00:00
|
|
|
ProjectDirectory = testInstance.Path,
|
2016-03-07 19:50:52 +00:00
|
|
|
Configuration = "Debug",
|
2016-04-13 00:29:07 +00:00
|
|
|
Framework = FrameworkConstants.CommonFrameworks.NetCoreApp10
|
2016-03-07 19:50:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
|
|
|
|
|
|
|
|
result.Should().BeNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
2016-06-02 17:50:38 +00:00
|
|
|
public void It_returns_a_CommandSpec_with_Dotnet_as_FileName_and_CommandName_in_Args_when_CommandName_exists_in_ProjectDependencies()
|
2016-03-07 19:50:52 +00:00
|
|
|
{
|
|
|
|
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
|
|
|
|
|
2016-08-10 06:54:37 +00:00
|
|
|
var testInstance = TestAssetsManager.CreateTestInstance(TestProjectName)
|
|
|
|
.WithBuildArtifacts()
|
|
|
|
.WithLockFiles();
|
|
|
|
|
2016-03-07 19:50:52 +00:00
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
|
|
|
{
|
|
|
|
CommandName = "dotnet-hello",
|
|
|
|
CommandArguments = null,
|
2016-08-10 06:54:37 +00:00
|
|
|
ProjectDirectory = testInstance.Path,
|
2016-03-07 19:50:52 +00:00
|
|
|
Configuration = "Debug",
|
2016-04-13 00:29:07 +00:00
|
|
|
Framework = FrameworkConstants.CommonFrameworks.NetCoreApp10
|
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]
|
|
|
|
public void It_escapes_CommandArguments_when_returning_a_CommandSpec()
|
|
|
|
{
|
|
|
|
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
|
|
|
|
|
2016-08-10 06:54:37 +00:00
|
|
|
var testInstance = TestAssetsManager.CreateTestInstance(TestProjectName)
|
|
|
|
.WithBuildArtifacts()
|
|
|
|
.WithLockFiles();
|
|
|
|
|
2016-03-07 19:50:52 +00:00
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
|
|
|
{
|
|
|
|
CommandName = "dotnet-hello",
|
2016-04-28 23:30:32 +00:00
|
|
|
CommandArguments = new[] { "arg with space" },
|
2016-08-10 06:54:37 +00:00
|
|
|
ProjectDirectory = testInstance.Path,
|
2016-03-07 19:50:52 +00:00
|
|
|
Configuration = "Debug",
|
2016-04-13 00:29:07 +00:00
|
|
|
Framework = FrameworkConstants.CommonFrameworks.NetCoreApp10
|
2016-03-07 19:50:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
|
|
|
|
|
|
|
|
result.Should().NotBeNull();
|
|
|
|
result.Args.Should().Contain("\"arg with space\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
2016-08-10 06:54:37 +00:00
|
|
|
public void It_passes_depsfile_arg_to_host_when_returning_a_CommandSpec()
|
2016-03-07 19:50:52 +00:00
|
|
|
{
|
|
|
|
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
|
|
|
|
|
2016-08-10 06:54:37 +00:00
|
|
|
var testInstance = TestAssetsManager.CreateTestInstance(TestProjectName)
|
|
|
|
.WithBuildArtifacts()
|
|
|
|
.WithLockFiles();
|
|
|
|
|
2016-03-07 19:50:52 +00:00
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
|
|
|
{
|
|
|
|
CommandName = "dotnet-hello",
|
|
|
|
CommandArguments = null,
|
2016-08-10 06:54:37 +00:00
|
|
|
ProjectDirectory = testInstance.Path,
|
2016-03-07 19:50:52 +00:00
|
|
|
Configuration = "Debug",
|
2016-04-13 00:29:07 +00:00
|
|
|
Framework = FrameworkConstants.CommonFrameworks.NetCoreApp10
|
2016-03-07 19:50:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
|
|
|
|
|
|
|
|
result.Should().NotBeNull();
|
|
|
|
result.Args.Should().Contain("--depsfile");
|
|
|
|
}
|
|
|
|
|
2016-03-15 00:38:36 +00:00
|
|
|
[Fact]
|
2016-05-08 21:20:34 +00:00
|
|
|
public void It_sets_depsfile_in_output_path_in_commandspec()
|
2016-03-15 00:38:36 +00:00
|
|
|
{
|
|
|
|
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
|
2016-08-10 06:54:37 +00:00
|
|
|
|
|
|
|
var testInstance = TestAssetsManager.CreateTestInstance(TestProjectName)
|
|
|
|
.WithLockFiles();
|
|
|
|
|
|
|
|
var outputDir = Path.Combine(testInstance.Path, "outdir");
|
2016-03-15 00:38:36 +00:00
|
|
|
|
|
|
|
var commandResolverArguments = new CommandResolverArguments
|
|
|
|
{
|
|
|
|
CommandName = "dotnet-hello",
|
|
|
|
CommandArguments = null,
|
2016-08-10 06:54:37 +00:00
|
|
|
ProjectDirectory = testInstance.Path,
|
2016-03-15 00:38:36 +00:00
|
|
|
Configuration = "Debug",
|
2016-04-13 00:29:07 +00:00
|
|
|
Framework = FrameworkConstants.CommonFrameworks.NetCoreApp10,
|
2016-05-08 21:20:34 +00:00
|
|
|
OutputPath = outputDir
|
2016-03-15 00:38:36 +00:00
|
|
|
};
|
|
|
|
|
2016-05-08 21:20:34 +00:00
|
|
|
var buildCommand = new BuildCommand(
|
2016-08-10 06:54:37 +00:00
|
|
|
Path.Combine(testInstance.Path, "project.json"),
|
2016-05-08 21:20:34 +00:00
|
|
|
output: outputDir,
|
|
|
|
framework: FrameworkConstants.CommonFrameworks.NetCoreApp10.ToString())
|
|
|
|
.Execute().Should().Pass();
|
|
|
|
|
2016-03-15 00:38:36 +00:00
|
|
|
var projectContext = ProjectContext.Create(
|
2016-08-10 06:54:37 +00:00
|
|
|
testInstance.Path,
|
2016-04-13 00:29:07 +00:00
|
|
|
FrameworkConstants.CommonFrameworks.NetCoreApp10,
|
2016-10-11 22:31:18 +00:00
|
|
|
DotnetRuntimeIdentifiers.InferCurrentRuntimeIdentifiers(DotnetFiles.VersionFileObject));
|
2016-10-08 18:45:19 +00:00
|
|
|
|
2016-03-15 00:38:36 +00:00
|
|
|
var depsFilePath =
|
2016-05-08 21:20:34 +00:00
|
|
|
projectContext.GetOutputPaths("Debug", outputPath: outputDir).RuntimeFiles.DepsJson;
|
2016-03-15 00:38:36 +00:00
|
|
|
|
|
|
|
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
|
|
|
|
|
|
|
|
result.Should().NotBeNull();
|
|
|
|
result.Args.Should().Contain($"--depsfile {depsFilePath}");
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
2016-05-08 21:20:34 +00:00
|
|
|
public void It_sets_depsfile_in_build_base_path_in_commandspec()
|
2016-03-15 00:38:36 +00:00
|
|
|
{
|
|
|
|
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
|
2016-08-10 06:54:37 +00:00
|
|
|
|
|
|
|
var testInstance = TestAssetsManager.CreateTestInstance(TestProjectName)
|
|
|
|
.WithLockFiles();
|
|
|
|
|
|
|
|
var buildBasePath = Path.Combine(testInstance.Path, "basedir");
|
2016-03-15 00:38:36 +00:00
|
|
|
|
|
|
|
var commandResolverArguments = new CommandResolverArguments
|
|
|
|
{
|
|
|
|
CommandName = "dotnet-hello",
|
|
|
|
CommandArguments = null,
|
2016-08-10 06:54:37 +00:00
|
|
|
ProjectDirectory = testInstance.Path,
|
2016-03-15 00:38:36 +00:00
|
|
|
Configuration = "Debug",
|
2016-04-13 00:29:07 +00:00
|
|
|
Framework = FrameworkConstants.CommonFrameworks.NetCoreApp10,
|
2016-05-08 21:20:34 +00:00
|
|
|
BuildBasePath = buildBasePath
|
2016-03-15 00:38:36 +00:00
|
|
|
};
|
|
|
|
|
2016-05-08 21:20:34 +00:00
|
|
|
var buildCommand = new BuildCommand(
|
2016-08-10 06:54:37 +00:00
|
|
|
Path.Combine(testInstance.Path, "project.json"),
|
2016-05-08 21:20:34 +00:00
|
|
|
buildBasePath: buildBasePath,
|
|
|
|
framework: FrameworkConstants.CommonFrameworks.NetCoreApp10.ToString())
|
|
|
|
.Execute().Should().Pass();
|
|
|
|
|
2016-03-15 00:38:36 +00:00
|
|
|
var projectContext = ProjectContext.Create(
|
2016-08-10 06:54:37 +00:00
|
|
|
testInstance.Path,
|
2016-04-13 00:29:07 +00:00
|
|
|
FrameworkConstants.CommonFrameworks.NetCoreApp10,
|
2016-10-11 22:31:18 +00:00
|
|
|
DotnetRuntimeIdentifiers.InferCurrentRuntimeIdentifiers(DotnetFiles.VersionFileObject));
|
2016-03-15 00:38:36 +00:00
|
|
|
|
|
|
|
var depsFilePath =
|
2016-05-08 21:20:34 +00:00
|
|
|
projectContext.GetOutputPaths("Debug", buildBasePath).RuntimeFiles.DepsJson;
|
2016-03-15 00:38:36 +00:00
|
|
|
|
|
|
|
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
|
|
|
|
|
|
|
|
result.Should().NotBeNull();
|
|
|
|
result.Args.Should().Contain($"--depsfile {depsFilePath}");
|
|
|
|
}
|
|
|
|
|
2016-03-07 19:50:52 +00:00
|
|
|
[Fact]
|
|
|
|
public void It_returns_a_CommandSpec_with_CommandName_in_Args_when_returning_a_CommandSpec_and_CommandArguments_are_null()
|
|
|
|
{
|
|
|
|
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
|
|
|
|
|
2016-08-10 06:54:37 +00:00
|
|
|
var testInstance = TestAssetsManager.CreateTestInstance(TestProjectName)
|
|
|
|
.WithBuildArtifacts()
|
|
|
|
.WithLockFiles();
|
|
|
|
|
2016-03-07 19:50:52 +00:00
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
|
|
|
{
|
|
|
|
CommandName = "dotnet-hello",
|
|
|
|
CommandArguments = null,
|
2016-08-10 06:54:37 +00:00
|
|
|
ProjectDirectory = testInstance.Path,
|
2016-03-07 19:50:52 +00:00
|
|
|
Configuration = "Debug",
|
2016-04-13 00:29:07 +00:00
|
|
|
Framework = FrameworkConstants.CommonFrameworks.NetCoreApp10
|
2016-03-07 19:50:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
|
|
|
|
|
|
|
|
result.Should().NotBeNull();
|
2016-04-28 23:30:32 +00:00
|
|
|
|
2016-03-07 19:50:52 +00:00
|
|
|
result.Args.Should().Contain("dotnet-hello");
|
|
|
|
}
|
|
|
|
|
|
|
|
private ProjectDependenciesCommandResolver SetupProjectDependenciesCommandResolver(
|
|
|
|
IEnvironmentProvider environment = null,
|
|
|
|
IPackagedCommandSpecFactory packagedCommandSpecFactory = null)
|
|
|
|
{
|
|
|
|
environment = environment ?? new EnvironmentProvider();
|
|
|
|
packagedCommandSpecFactory = packagedCommandSpecFactory ?? new PackagedCommandSpecFactory();
|
|
|
|
|
|
|
|
var projectDependenciesCommandResolver = new ProjectDependenciesCommandResolver(environment, packagedCommandSpecFactory);
|
|
|
|
|
|
|
|
return projectDependenciesCommandResolver;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|