Added a command factory where you can fix the output parameters that will flow to the commands, like configuration, outputpath and in the future, framework, runtime and base path.

This commit is contained in:
Livar Cunha 2016-03-01 21:15:07 -08:00
parent b5de686ba4
commit ebab175d17
6 changed files with 92 additions and 21 deletions

View file

@ -67,12 +67,14 @@ namespace Microsoft.DotNet.Cli.Utils
string commandName,
IEnumerable<string> args,
NuGetFramework framework = null,
string configuration = Constants.DefaultConfiguration)
string configuration = Constants.DefaultConfiguration,
string outputPath = null)
{
var commandSpec = CommandResolver.TryResolveCommandSpec(commandName,
args,
framework,
configuration: configuration);
configuration: configuration,
outputPath: outputPath);
if (commandSpec == null)
{

View file

@ -13,10 +13,15 @@ namespace Microsoft.DotNet.Cli.Utils
{
internal static class CommandResolver
{
public static CommandSpec TryResolveCommandSpec(string commandName, IEnumerable<string> args, NuGetFramework framework = null, string configuration=Constants.DefaultConfiguration)
public static CommandSpec TryResolveCommandSpec(
string commandName,
IEnumerable<string> args,
NuGetFramework framework = null,
string configuration = Constants.DefaultConfiguration,
string outputPath = null)
{
return ResolveFromRootedCommand(commandName, args) ??
ResolveFromProjectDependencies(commandName, args, framework, configuration) ??
ResolveFromProjectDependencies(commandName, args, framework, configuration, outputPath) ??
ResolveFromProjectTools(commandName, args) ??
ResolveFromAppBase(commandName, args) ??
ResolveFromPath(commandName, args);
@ -67,10 +72,11 @@ namespace Microsoft.DotNet.Cli.Utils
}
public static CommandSpec ResolveFromProjectDependencies(
string commandName,
IEnumerable<string> args,
NuGetFramework framework,
string configuration)
string commandName,
IEnumerable<string> args,
NuGetFramework framework,
string configuration,
string outputPath)
{
if (framework == null) return null;
@ -82,7 +88,7 @@ namespace Microsoft.DotNet.Cli.Utils
if (commandPackage == null) return null;
var depsPath = projectContext.GetOutputPaths(configuration).RuntimeFiles.Deps;
var depsPath = projectContext.GetOutputPaths(configuration, outputPath: outputPath).RuntimeFiles.Deps;
return ConfigureCommandFromPackage(commandName, args, commandPackage, projectContext, depsPath);
}

View file

@ -0,0 +1,34 @@
// 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.Collections.Generic;
using NuGet.Frameworks;
namespace Microsoft.DotNet.Cli.Utils
{
public class FixedPathCommandFactory : ICommandFactory
{
private readonly string _configuration;
private readonly string _outputPath;
public FixedPathCommandFactory(string configuration, string outputPath)
{
_configuration = configuration;
_outputPath = outputPath;
}
public ICommand Create(
string commandName,
IEnumerable<string> args,
NuGetFramework framework = null,
string configuration = Constants.DefaultConfiguration)
{
if (string.IsNullOrEmpty(configuration))
{
configuration = _configuration;
}
return Command.Create(commandName, args, framework, configuration, _outputPath);
}
}
}

View file

@ -30,6 +30,7 @@ namespace Microsoft.DotNet.Tools.Test
var parentProcessIdOption = app.Option("--parentProcessId", "Used by IDEs to specify their process ID. Test will exit if the parent process does.", CommandOptionType.SingleValue);
var portOption = app.Option("--port", "Used by IDEs to specify a port number to listen for a connection.", CommandOptionType.SingleValue);
var configurationOption = app.Option("-c|--configuration <CONFIGURATION>", "Configuration under which to build", CommandOptionType.SingleValue);
var output = app.Option("-o|--output <OUTPUT_DIR>", "Directory in which to find the binaries to be run", CommandOptionType.SingleValue);
var projectPath = app.Argument("<PROJECT>", "The project to test, defaults to the current directory. Can be a path to a project.json or a project directory.");
app.OnExecute(() =>
@ -57,6 +58,8 @@ namespace Microsoft.DotNet.Tools.Test
var configuration = configurationOption.Value() ?? Constants.DefaultConfiguration;
var outputPath = output.Value();
if (portOption.HasValue())
{
int port;
@ -66,11 +69,11 @@ namespace Microsoft.DotNet.Tools.Test
throw new InvalidOperationException($"{portOption.Value()} is not a valid port number.");
}
return RunDesignTime(port, projectContext, testRunner, configuration);
return RunDesignTime(port, projectContext, testRunner, configuration, outputPath);
}
else
{
return RunConsole(projectContext, app, testRunner, configuration);
return RunConsole(projectContext, app, testRunner, configuration, outputPath);
}
}
catch (InvalidOperationException ex)
@ -89,28 +92,53 @@ namespace Microsoft.DotNet.Tools.Test
return app.Execute(args);
}
private static int RunConsole(ProjectContext projectContext, CommandLineApplication app, string testRunner, string configuration)
private static int RunConsole(
ProjectContext projectContext,
CommandLineApplication app,
string testRunner,
string configuration,
string outputPath)
{
var commandArgs = new List<string> { projectContext.GetOutputPaths(configuration).CompilationFiles.Assembly };
var commandArgs = new List<string> { GetAssemblyUnderTest(projectContext, configuration, outputPath) };
commandArgs.AddRange(app.RemainingArguments);
return Command.Create($"dotnet-{GetCommandName(testRunner)}", commandArgs, projectContext.TargetFramework, configuration: configuration)
return Command.Create(
$"dotnet-{GetCommandName(testRunner)}",
commandArgs,
projectContext.TargetFramework,
configuration: configuration,
outputPath: outputPath)
.ForwardStdErr()
.ForwardStdOut()
.Execute()
.ExitCode;
}
private static string GetAssemblyUnderTest(ProjectContext projectContext, string configuration, string outputPath)
{
var assemblyUnderTest =
projectContext.GetOutputPaths(configuration, outputPath: outputPath).CompilationFiles.Assembly;
if (!string.IsNullOrEmpty(outputPath))
{
assemblyUnderTest =
projectContext.GetOutputPaths(configuration, outputPath: outputPath).RuntimeFiles.Assembly;
}
return assemblyUnderTest;
}
private static int RunDesignTime(
int port,
ProjectContext
projectContext,
string testRunner,
string configuration)
string configuration,
string outputPath)
{
Console.WriteLine("Listening on port {0}", port);
HandleDesignTimeMessages(projectContext, testRunner, port, configuration);
HandleDesignTimeMessages(projectContext, testRunner, port, configuration, outputPath);
return 0;
}
@ -119,18 +147,19 @@ namespace Microsoft.DotNet.Tools.Test
ProjectContext projectContext,
string testRunner,
int port,
string configuration)
string configuration,
string outputPath)
{
var reportingChannelFactory = new ReportingChannelFactory();
var adapterChannel = reportingChannelFactory.CreateChannelWithPort(port);
try
{
var assemblyUnderTest = projectContext.GetOutputPaths(configuration).CompilationFiles.Assembly;
var assemblyUnderTest = GetAssemblyUnderTest(projectContext, configuration, outputPath);
var messages = new TestMessagesCollection();
using (var dotnetTest = new DotnetTest(messages, assemblyUnderTest))
{
var commandFactory = new CommandFactory();
var commandFactory = new FixedPathCommandFactory(configuration, outputPath);
var testRunnerFactory = new TestRunnerFactory(GetCommandName(testRunner), commandFactory);
dotnetTest

View file

@ -54,7 +54,7 @@ namespace Microsoft.DotNet.Tools.Test
$"dotnet-{_testRunner}",
commandArgs,
new NuGetFramework("DNXCore", Version.Parse("5.0")),
Constants.DefaultConfiguration);
null);
}
}
}

View file

@ -41,7 +41,7 @@ namespace Microsoft.Dotnet.Tools.Test.Tests
$"dotnet-{_runner}",
_testRunnerArguments,
new NuGetFramework("DNXCore", Version.Parse("5.0")),
Constants.DefaultConfiguration)).Returns(_commandMock.Object).Verifiable();
null)).Returns(_commandMock.Object).Verifiable();
}
[Fact]