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]
|
|
|
|
public void It_returns_a_CommandSpec_with_CoreHost_as_FileName_and_CommandName_in_Args_when_CommandName_exists_in_ProjectDependencies()
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
commandFile.Should().Be("corehost");
|
|
|
|
|
|
|
|
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]
|
|
|
|
public void It_passes_depsfile_arg_to_corehost_when_returning_a_commandspec()
|
|
|
|
{
|
|
|
|
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]
|
|
|
|
public void It_sets_depsfile_based_on_output_path_when_returning_a_commandspec()
|
|
|
|
{
|
|
|
|
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-15 00:38:36 +00:00
|
|
|
OutputPath = AppContext.BaseDirectory
|
|
|
|
};
|
|
|
|
|
|
|
|
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 =
|
|
|
|
projectContext.GetOutputPaths("Debug", outputPath: AppContext.BaseDirectory).RuntimeFiles.DepsJson;
|
|
|
|
|
|
|
|
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
|
|
|
|
|
|
|
|
result.Should().NotBeNull();
|
|
|
|
result.Args.Should().Contain($"--depsfile {depsFilePath}");
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void It_sets_depsfile_based_on_build_base_path_when_returning_a_commandspec()
|
|
|
|
{
|
|
|
|
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-15 00:38:36 +00:00
|
|
|
BuildBasePath = AppContext.BaseDirectory
|
|
|
|
};
|
|
|
|
|
|
|
|
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 =
|
|
|
|
projectContext.GetOutputPaths("Debug", AppContext.BaseDirectory).RuntimeFiles.DepsJson;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|