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, string commandName,
IEnumerable<string> args, IEnumerable<string> args,
NuGetFramework framework = null, NuGetFramework framework = null,
string configuration = Constants.DefaultConfiguration) string configuration = Constants.DefaultConfiguration,
string outputPath = null)
{ {
var commandSpec = CommandResolver.TryResolveCommandSpec(commandName, var commandSpec = CommandResolver.TryResolveCommandSpec(commandName,
args, args,
framework, framework,
configuration: configuration); configuration: configuration,
outputPath: outputPath);
if (commandSpec == null) if (commandSpec == null)
{ {

View file

@ -13,10 +13,15 @@ namespace Microsoft.DotNet.Cli.Utils
{ {
internal static class CommandResolver 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) ?? return ResolveFromRootedCommand(commandName, args) ??
ResolveFromProjectDependencies(commandName, args, framework, configuration) ?? ResolveFromProjectDependencies(commandName, args, framework, configuration, outputPath) ??
ResolveFromProjectTools(commandName, args) ?? ResolveFromProjectTools(commandName, args) ??
ResolveFromAppBase(commandName, args) ?? ResolveFromAppBase(commandName, args) ??
ResolveFromPath(commandName, args); ResolveFromPath(commandName, args);
@ -67,10 +72,11 @@ namespace Microsoft.DotNet.Cli.Utils
} }
public static CommandSpec ResolveFromProjectDependencies( public static CommandSpec ResolveFromProjectDependencies(
string commandName, string commandName,
IEnumerable<string> args, IEnumerable<string> args,
NuGetFramework framework, NuGetFramework framework,
string configuration) string configuration,
string outputPath)
{ {
if (framework == null) return null; if (framework == null) return null;
@ -82,7 +88,7 @@ namespace Microsoft.DotNet.Cli.Utils
if (commandPackage == null) return null; 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); 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 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 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 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."); 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(() => app.OnExecute(() =>
@ -57,6 +58,8 @@ namespace Microsoft.DotNet.Tools.Test
var configuration = configurationOption.Value() ?? Constants.DefaultConfiguration; var configuration = configurationOption.Value() ?? Constants.DefaultConfiguration;
var outputPath = output.Value();
if (portOption.HasValue()) if (portOption.HasValue())
{ {
int port; int port;
@ -66,11 +69,11 @@ namespace Microsoft.DotNet.Tools.Test
throw new InvalidOperationException($"{portOption.Value()} is not a valid port number."); 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 else
{ {
return RunConsole(projectContext, app, testRunner, configuration); return RunConsole(projectContext, app, testRunner, configuration, outputPath);
} }
} }
catch (InvalidOperationException ex) catch (InvalidOperationException ex)
@ -89,28 +92,53 @@ namespace Microsoft.DotNet.Tools.Test
return app.Execute(args); 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); 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() .ForwardStdErr()
.ForwardStdOut() .ForwardStdOut()
.Execute() .Execute()
.ExitCode; .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( private static int RunDesignTime(
int port, int port,
ProjectContext ProjectContext
projectContext, projectContext,
string testRunner, string testRunner,
string configuration) string configuration,
string outputPath)
{ {
Console.WriteLine("Listening on port {0}", port); Console.WriteLine("Listening on port {0}", port);
HandleDesignTimeMessages(projectContext, testRunner, port, configuration); HandleDesignTimeMessages(projectContext, testRunner, port, configuration, outputPath);
return 0; return 0;
} }
@ -119,18 +147,19 @@ namespace Microsoft.DotNet.Tools.Test
ProjectContext projectContext, ProjectContext projectContext,
string testRunner, string testRunner,
int port, int port,
string configuration) string configuration,
string outputPath)
{ {
var reportingChannelFactory = new ReportingChannelFactory(); var reportingChannelFactory = new ReportingChannelFactory();
var adapterChannel = reportingChannelFactory.CreateChannelWithPort(port); var adapterChannel = reportingChannelFactory.CreateChannelWithPort(port);
try try
{ {
var assemblyUnderTest = projectContext.GetOutputPaths(configuration).CompilationFiles.Assembly; var assemblyUnderTest = GetAssemblyUnderTest(projectContext, configuration, outputPath);
var messages = new TestMessagesCollection(); var messages = new TestMessagesCollection();
using (var dotnetTest = new DotnetTest(messages, assemblyUnderTest)) using (var dotnetTest = new DotnetTest(messages, assemblyUnderTest))
{ {
var commandFactory = new CommandFactory(); var commandFactory = new FixedPathCommandFactory(configuration, outputPath);
var testRunnerFactory = new TestRunnerFactory(GetCommandName(testRunner), commandFactory); var testRunnerFactory = new TestRunnerFactory(GetCommandName(testRunner), commandFactory);
dotnetTest dotnetTest

View file

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

View file

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