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;
|
|
|
|
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
|
|
|
|
{
|
|
|
|
public class GivenAProjectDependenciesCommandResolver
|
|
|
|
{
|
|
|
|
|
2016-04-28 23:30:32 +00:00
|
|
|
private static readonly string s_liveProjectDirectory =
|
2016-03-07 19:50:52 +00:00
|
|
|
Path.Combine(AppContext.BaseDirectory, "TestAssets/TestProjects/AppWithDirectDependency");
|
|
|
|
|
|
|
|
[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();
|
|
|
|
|
|
|
|
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 = s_liveProjectDirectory,
|
|
|
|
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();
|
|
|
|
|
|
|
|
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 = s_liveProjectDirectory,
|
|
|
|
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();
|
|
|
|
|
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
|
|
|
{
|
|
|
|
CommandName = "nonexistent-command",
|
|
|
|
CommandArguments = null,
|
|
|
|
ProjectDirectory = s_liveProjectDirectory,
|
|
|
|
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();
|
|
|
|
|
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
|
|
|
{
|
|
|
|
CommandName = "dotnet-hello",
|
|
|
|
CommandArguments = null,
|
|
|
|
ProjectDirectory = s_liveProjectDirectory,
|
|
|
|
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();
|
|
|
|
|
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
|
|
|
{
|
|
|
|
CommandName = "dotnet-hello",
|
2016-04-28 23:30:32 +00:00
|
|
|
CommandArguments = new[] { "arg with space" },
|
2016-03-07 19:50:52 +00:00
|
|
|
ProjectDirectory = s_liveProjectDirectory,
|
|
|
|
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-06-02 17:50:38 +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();
|
|
|
|
|
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
|
|
|
{
|
|
|
|
CommandName = "dotnet-hello",
|
|
|
|
CommandArguments = null,
|
|
|
|
ProjectDirectory = s_liveProjectDirectory,
|
|
|
|
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-05-08 21:20:34 +00:00
|
|
|
var outputDir = Path.Combine(AppContext.BaseDirectory, "outdir");
|
2016-03-15 00:38:36 +00:00
|
|
|
|
|
|
|
var commandResolverArguments = new CommandResolverArguments
|
|
|
|
{
|
|
|
|
CommandName = "dotnet-hello",
|
|
|
|
CommandArguments = null,
|
|
|
|
ProjectDirectory = s_liveProjectDirectory,
|
|
|
|
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(
|
|
|
|
Path.Combine(s_liveProjectDirectory, "project.json"),
|
|
|
|
output: outputDir,
|
|
|
|
framework: FrameworkConstants.CommonFrameworks.NetCoreApp10.ToString())
|
|
|
|
.Execute().Should().Pass();
|
|
|
|
|
2016-03-15 00:38:36 +00:00
|
|
|
var projectContext = ProjectContext.Create(
|
|
|
|
s_liveProjectDirectory,
|
2016-04-13 00:29:07 +00:00
|
|
|
FrameworkConstants.CommonFrameworks.NetCoreApp10,
|
2016-04-28 23:30:32 +00:00
|
|
|
RuntimeEnvironmentRidExtensions.GetAllCandidateRuntimeIdentifiers());
|
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-05-08 21:20:34 +00:00
|
|
|
var buildBasePath = Path.Combine(AppContext.BaseDirectory, "basedir");
|
2016-03-15 00:38:36 +00:00
|
|
|
|
|
|
|
var commandResolverArguments = new CommandResolverArguments
|
|
|
|
{
|
|
|
|
CommandName = "dotnet-hello",
|
|
|
|
CommandArguments = null,
|
|
|
|
ProjectDirectory = s_liveProjectDirectory,
|
|
|
|
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(
|
|
|
|
Path.Combine(s_liveProjectDirectory, "project.json"),
|
|
|
|
buildBasePath: buildBasePath,
|
|
|
|
framework: FrameworkConstants.CommonFrameworks.NetCoreApp10.ToString())
|
|
|
|
.Execute().Should().Pass();
|
|
|
|
|
2016-03-15 00:38:36 +00:00
|
|
|
var projectContext = ProjectContext.Create(
|
|
|
|
s_liveProjectDirectory,
|
2016-04-13 00:29:07 +00:00
|
|
|
FrameworkConstants.CommonFrameworks.NetCoreApp10,
|
2016-04-28 23:30:32 +00:00
|
|
|
RuntimeEnvironmentRidExtensions.GetAllCandidateRuntimeIdentifiers());
|
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();
|
|
|
|
|
|
|
|
var commandResolverArguments = new CommandResolverArguments()
|
|
|
|
{
|
|
|
|
CommandName = "dotnet-hello",
|
|
|
|
CommandArguments = null,
|
|
|
|
ProjectDirectory = s_liveProjectDirectory,
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|