Initial port of dotnet test
This commit is contained in:
parent
0df2462344
commit
18a2b95dec
3 changed files with 136 additions and 304 deletions
|
@ -32,6 +32,8 @@ namespace Microsoft.DotNet.Tools.Test
|
|||
public const string CmdTestAdapterPathDescription = @"Use custom adapters from the given path in the test run.
|
||||
Example: --test-adapter-path <PATH_TO_ADAPTER>";
|
||||
|
||||
public const string CmdTestAdapterPath = "PATH_TO_ADAPTER";
|
||||
|
||||
public const string CmdLoggerOption = "LoggerUri/FriendlyName";
|
||||
|
||||
public const string CmdLoggerDescription = @"Specify a logger for test results.
|
||||
|
|
|
@ -2,283 +2,74 @@
|
|||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.DotNet.Cli;
|
||||
using Microsoft.DotNet.Cli.CommandLine;
|
||||
using Microsoft.DotNet.Cli.Utils;
|
||||
using Microsoft.DotNet.Tools.MSBuild;
|
||||
using System.IO;
|
||||
using System.Text.RegularExpressions;
|
||||
using Parser = Microsoft.DotNet.Cli.Parser;
|
||||
|
||||
namespace Microsoft.DotNet.Tools.Test
|
||||
{
|
||||
public class TestCommand
|
||||
public class TestCommand : MSBuildForwardingApp
|
||||
{
|
||||
public TestCommand(IEnumerable<string> msbuildArgs, string msbuildPath = null)
|
||||
: base(msbuildArgs, msbuildPath)
|
||||
{
|
||||
}
|
||||
|
||||
public static TestCommand FromArgs(string[] args, string msbuildPath=null)
|
||||
{
|
||||
var msbuildArgs = new List<string>()
|
||||
{
|
||||
"/t:VSTest",
|
||||
"/v:quiet",
|
||||
"/nologo"
|
||||
};
|
||||
|
||||
var parser = Parser.Instance;
|
||||
|
||||
var result = parser.ParseFrom("dotnet test", args);
|
||||
|
||||
Reporter.Output.WriteLine(result.Diagram());
|
||||
|
||||
result.ShowHelpIfRequested();
|
||||
|
||||
var parsedTest = result["dotnet"]["test"];
|
||||
|
||||
var runSettingsOptions =
|
||||
result.UnparsedTokens
|
||||
.Select(t => GetSemiColonEsacpedstring(t));
|
||||
|
||||
if (runSettingsOptions.Any())
|
||||
{
|
||||
var runSettingsArg = string.Join(";", runSettingsOptions);
|
||||
|
||||
msbuildArgs.Add($"/p:VSTestCLIRunSettings=\"{runSettingsArg}\"");
|
||||
}
|
||||
|
||||
return new TestCommand(msbuildArgs, msbuildPath);
|
||||
}
|
||||
|
||||
public static int Run(string[] args)
|
||||
{
|
||||
DebugHelper.HandleDebugSwitch(ref args);
|
||||
|
||||
var cmd = new CommandLineApplication(throwOnUnexpectedArg: false)
|
||||
TestCommand cmd;
|
||||
|
||||
try
|
||||
{
|
||||
Name = "dotnet test",
|
||||
FullName = LocalizableStrings.AppFullName,
|
||||
Description = LocalizableStrings.AppDescription,
|
||||
HandleRemainingArguments = true,
|
||||
ArgumentSeparatorHelpText = LocalizableStrings.RunSettingsArgsHelpText
|
||||
};
|
||||
|
||||
cmd.HelpOption("-h|--help");
|
||||
|
||||
var argRoot = cmd.Argument(
|
||||
$"<{LocalizableStrings.CmdArgProject}>",
|
||||
LocalizableStrings.CmdArgDescription,
|
||||
multipleValues: false);
|
||||
|
||||
var settingOption = cmd.Option(
|
||||
$"-s|--settings <{LocalizableStrings.CmdSettingsFile}>",
|
||||
LocalizableStrings.CmdSettingsDescription,
|
||||
CommandOptionType.SingleValue);
|
||||
|
||||
var listTestsOption = cmd.Option(
|
||||
"-t|--list-tests",
|
||||
LocalizableStrings.CmdListTestsDescription,
|
||||
CommandOptionType.NoValue);
|
||||
|
||||
var testCaseFilterOption = cmd.Option(
|
||||
$"--filter <{LocalizableStrings.CmdTestCaseFilterExpression}>",
|
||||
LocalizableStrings.CmdTestCaseFilterDescription,
|
||||
CommandOptionType.SingleValue);
|
||||
|
||||
var testAdapterPathOption = cmd.Option(
|
||||
"-a|--test-adapter-path",
|
||||
LocalizableStrings.CmdTestAdapterPathDescription,
|
||||
CommandOptionType.SingleValue);
|
||||
|
||||
var loggerOption = cmd.Option(
|
||||
$"-l|--logger <{LocalizableStrings.CmdLoggerOption}>",
|
||||
LocalizableStrings.CmdLoggerDescription,
|
||||
CommandOptionType.SingleValue);
|
||||
|
||||
var configurationOption = cmd.Option(
|
||||
$"-c|--configuration <{LocalizableStrings.CmdConfiguration}>",
|
||||
LocalizableStrings.CmdConfigDescription,
|
||||
CommandOptionType.SingleValue);
|
||||
|
||||
var frameworkOption = cmd.Option(
|
||||
$"-f|--framework <{LocalizableStrings.CmdFramework}>",
|
||||
LocalizableStrings.CmdFrameworkDescription,
|
||||
CommandOptionType.SingleValue);
|
||||
|
||||
var outputOption = cmd.Option(
|
||||
$"-o|--output <{LocalizableStrings.CmdOutputDir}>",
|
||||
LocalizableStrings.CmdOutputDescription,
|
||||
CommandOptionType.SingleValue);
|
||||
|
||||
var diagOption = cmd.Option(
|
||||
$"-d|--diag <{LocalizableStrings.CmdPathToLogFile}>",
|
||||
LocalizableStrings.CmdPathTologFileDescription,
|
||||
CommandOptionType.SingleValue);
|
||||
|
||||
var noBuildtOption = cmd.Option(
|
||||
"--no-build",
|
||||
LocalizableStrings.CmdNoBuildDescription,
|
||||
CommandOptionType.NoValue);
|
||||
|
||||
CommandOption verbosityOption = MSBuildForwardingApp.AddVerbosityOption(cmd);
|
||||
|
||||
cmd.OnExecute(() =>
|
||||
{
|
||||
var msbuildArgs = new List<string>()
|
||||
{
|
||||
"/t:VSTest"
|
||||
};
|
||||
|
||||
msbuildArgs.Add("/nologo");
|
||||
|
||||
if (settingOption.HasValue())
|
||||
{
|
||||
msbuildArgs.Add($"/p:VSTestSetting={settingOption.Value()}");
|
||||
}
|
||||
|
||||
if (listTestsOption.HasValue())
|
||||
{
|
||||
msbuildArgs.Add($"/p:VSTestListTests=true");
|
||||
}
|
||||
|
||||
if (testCaseFilterOption.HasValue())
|
||||
{
|
||||
msbuildArgs.Add($"/p:VSTestTestCaseFilter={testCaseFilterOption.Value()}");
|
||||
}
|
||||
|
||||
if (testAdapterPathOption.HasValue())
|
||||
{
|
||||
msbuildArgs.Add($"/p:VSTestTestAdapterPath={testAdapterPathOption.Value()}");
|
||||
}
|
||||
|
||||
if (loggerOption.HasValue())
|
||||
{
|
||||
msbuildArgs.Add($"/p:VSTestLogger={string.Join(";", GetSemiColonEscapedArgs(loggerOption.Values))}");
|
||||
}
|
||||
|
||||
if (configurationOption.HasValue())
|
||||
{
|
||||
msbuildArgs.Add($"/p:Configuration={configurationOption.Value()}");
|
||||
}
|
||||
|
||||
if (frameworkOption.HasValue())
|
||||
{
|
||||
msbuildArgs.Add($"/p:TargetFramework={frameworkOption.Value()}");
|
||||
}
|
||||
|
||||
if (outputOption.HasValue())
|
||||
{
|
||||
msbuildArgs.Add($"/p:OutputPath={outputOption.Value()}");
|
||||
}
|
||||
|
||||
if (diagOption.HasValue())
|
||||
{
|
||||
msbuildArgs.Add($"/p:VSTestDiag={diagOption.Value()}");
|
||||
}
|
||||
|
||||
if (noBuildtOption.HasValue())
|
||||
{
|
||||
msbuildArgs.Add($"/p:VSTestNoBuild=true");
|
||||
}
|
||||
|
||||
if (verbosityOption.HasValue())
|
||||
{
|
||||
msbuildArgs.Add($"/verbosity:{verbosityOption.Value()}");
|
||||
}
|
||||
else
|
||||
{
|
||||
msbuildArgs.Add("/verbosity:quiet");
|
||||
}
|
||||
|
||||
string defaultproject = GetSingleTestProjectToRunTestIfNotProvided(argRoot.Value, cmd.RemainingArguments);
|
||||
|
||||
if (!string.IsNullOrEmpty(defaultproject))
|
||||
{
|
||||
msbuildArgs.Add(defaultproject);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(argRoot.Value))
|
||||
{
|
||||
msbuildArgs.Add(argRoot.Value);
|
||||
}
|
||||
|
||||
// Get runsetings options specified after --
|
||||
if (cmd.RemainingArguments != null && cmd.RemainingArguments.Count > 0)
|
||||
{
|
||||
var runSettingsOptions = GetRunSettingsOptions(cmd.RemainingArguments);
|
||||
msbuildArgs.Add(string.Format("/p:VSTestCLIRunSettings=\"{0}\"", string.Join(";", runSettingsOptions)));
|
||||
}
|
||||
|
||||
// Add remaining arguments that the parser did not understand,
|
||||
msbuildArgs.AddRange(cmd.RemainingArguments);
|
||||
|
||||
return new MSBuildForwardingApp(msbuildArgs).Execute();
|
||||
});
|
||||
|
||||
return cmd.Execute(args);
|
||||
}
|
||||
|
||||
private static string GetSingleTestProjectToRunTestIfNotProvided(string args, List<string> remainingArguments)
|
||||
{
|
||||
string result = string.Empty;
|
||||
int projectFound = NumberOfTestProjectInRemainingArgs(remainingArguments) + NumberOfTestProjectInArgsRoot(args);
|
||||
|
||||
if (projectFound > 1)
|
||||
{
|
||||
throw new GracefulException(
|
||||
$"Specify a single project file to run tests from.");
|
||||
cmd = FromArgs(args);
|
||||
}
|
||||
else if (projectFound == 0)
|
||||
catch (CommandCreationException e)
|
||||
{
|
||||
result = GetDefaultTestProject();
|
||||
return e.ExitCode;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static int NumberOfTestProjectInArgsRoot(string args)
|
||||
{
|
||||
Regex pattern = new Regex(@"^.*\..*proj$");
|
||||
|
||||
if (!string.IsNullOrEmpty(args))
|
||||
{
|
||||
return pattern.IsMatch(args) ? 1 : 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static int NumberOfTestProjectInRemainingArgs(List<string> remainingArguments)
|
||||
{
|
||||
int count = 0;
|
||||
if (remainingArguments.Count != 0)
|
||||
{
|
||||
Regex pattern = new Regex(@"^.*\..*proj$");
|
||||
|
||||
foreach (var x in remainingArguments)
|
||||
{
|
||||
if (pattern.IsMatch(x))
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
private static string GetDefaultTestProject()
|
||||
{
|
||||
string directory = Directory.GetCurrentDirectory();
|
||||
string[] projectFiles = Directory.GetFiles(directory, "*.*proj");
|
||||
|
||||
if (projectFiles.Length == 0)
|
||||
{
|
||||
throw new GracefulException(
|
||||
$"Couldn't find a project to run test from. Ensure a project exists in {directory}." + Environment.NewLine +
|
||||
"Or pass the path to the project");
|
||||
}
|
||||
else if (projectFiles.Length > 1)
|
||||
{
|
||||
throw new GracefulException(
|
||||
$"Specify which project file to use because this '{directory}' contains more than one project file.");
|
||||
}
|
||||
|
||||
return projectFiles[0];
|
||||
}
|
||||
|
||||
private static string[] GetRunSettingsOptions(List<string> remainingArgs)
|
||||
{
|
||||
List<string> runsettingsArgs = new List<string>();
|
||||
List<string> argsToRemove = new List<string>();
|
||||
|
||||
bool readRunSettings = false;
|
||||
foreach (string arg in remainingArgs)
|
||||
{
|
||||
if (!readRunSettings)
|
||||
{
|
||||
if (arg.Equals("--"))
|
||||
{
|
||||
readRunSettings = true;
|
||||
argsToRemove.Add(arg);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
runsettingsArgs.Add(GetSemiColonEsacpedstring(arg));
|
||||
argsToRemove.Add(arg);
|
||||
}
|
||||
|
||||
foreach (string arg in argsToRemove)
|
||||
{
|
||||
remainingArgs.Remove(arg);
|
||||
}
|
||||
|
||||
return runsettingsArgs.ToArray();
|
||||
return cmd.Execute();
|
||||
}
|
||||
|
||||
private static string GetSemiColonEsacpedstring(string arg)
|
||||
|
|
|
@ -1,54 +1,93 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.DotNet.Cli.CommandLine;
|
||||
using LocalizableStrings = Microsoft.DotNet.Tools.Test.LocalizableStrings;
|
||||
|
||||
namespace Microsoft.DotNet.Cli
|
||||
{
|
||||
internal static class TestCommandParser
|
||||
{
|
||||
public static Command Test() =>
|
||||
Create.Command("test",
|
||||
".NET Test Driver",
|
||||
Create.Option("-h|--help",
|
||||
"Show help information"),
|
||||
Create.Option("-s|--settings",
|
||||
"Settings to use when running tests.",
|
||||
Accept.ExactlyOneArgument
|
||||
.With(name: "SETTINGS_FILE")),
|
||||
Create.Option("-t|--list-tests",
|
||||
"Lists discovered tests"),
|
||||
Create.Option("--filter",
|
||||
@"Run tests that match the given expression.
|
||||
Examples:
|
||||
Run tests with priority set to 1: --filter ""Priority = 1""
|
||||
Run a test with the specified full name: --filter ""FullyQualifiedName=Namespace.ClassName.MethodName""
|
||||
Run tests that contain the specified name: --filter ""FullyQualifiedName~Namespace.Class""
|
||||
More info on filtering support: https://aka.ms/vstest-filtering",
|
||||
Accept.ExactlyOneArgument
|
||||
.With(name: "EXPRESSION")),
|
||||
Create.Option("-a|--test-adapter-path",
|
||||
"Use custom adapters from the given path in the test run.\r\n Example: --test-adapter-path <PATH_TO_ADAPTER>"),
|
||||
Create.Option("-l|--logger",
|
||||
"Specify a logger for test results.\r\n Example: --logger \"trx[;LogFileName=<Defaults to unique file name>]\"",
|
||||
Accept.ExactlyOneArgument
|
||||
.With(name: "LoggerUri/FriendlyName")),
|
||||
Create.Option("-c|--configuration", "Configuration to use for building the project. Default for most projects is \"Debug\".",
|
||||
Accept.ExactlyOneArgument
|
||||
.With(name: "CONFIGURATION")
|
||||
.WithSuggestionsFrom("DEBUG", "RELEASE")),
|
||||
Create.Option("-f|--framework",
|
||||
"Looks for test binaries for a specific framework",
|
||||
Accept.AnyOneOf(Suggest.TargetFrameworksFromProjectFile)
|
||||
.With(name: "FRAMEWORK")),
|
||||
Create.Option("-o|--output",
|
||||
"Directory in which to find the binaries to be run",
|
||||
Accept.ExactlyOneArgument
|
||||
.With(name: "OUTPUT_DIR")),
|
||||
Create.Option("-d|--diag",
|
||||
"Enable verbose logs for test platform.\r\n Logs are written to the provided file.",
|
||||
Accept.ExactlyOneArgument
|
||||
.With(name: "PATH_TO_FILE")),
|
||||
Create.Option("--no-build",
|
||||
"Do not build project before testing."),
|
||||
CommonOptions.VerbosityOption());
|
||||
Create.Command(
|
||||
"test",
|
||||
".NET Test Driver",
|
||||
CommonOptions.HelpOption(),
|
||||
Create.Option(
|
||||
"-s|--settings",
|
||||
LocalizableStrings.CmdSettingsDescription,
|
||||
Accept.ExactlyOneArgument
|
||||
.With(name: LocalizableStrings.CmdSettingsFile)
|
||||
.ForwardAs(o => $"/p:VSTestSetting={o.Arguments.Single()}")),
|
||||
Create.Option(
|
||||
"-t|--list-tests",
|
||||
LocalizableStrings.CmdListTestsDescription,
|
||||
Accept.NoArguments
|
||||
.ForwardAs(o => "/p:VSTestListTests=true")),
|
||||
Create.Option(
|
||||
"--filter",
|
||||
LocalizableStrings.CmdTestCaseFilterDescription,
|
||||
Accept.ExactlyOneArgument
|
||||
.With(name: LocalizableStrings.CmdTestCaseFilterExpression)
|
||||
.ForwardAs(o => $"/p:VSTestTestCaseFilter={o.Arguments.Single()}")),
|
||||
Create.Option(
|
||||
"-a|--test-adapter-path",
|
||||
LocalizableStrings.CmdTestAdapterPathDescription,
|
||||
Accept.ExactlyOneArgument
|
||||
.With(name: LocalizableStrings.CmdTestAdapterPath)
|
||||
.ForwardAs(o => $"/p:VSTestTestAdapterPath={o.Arguments.Single()}")),
|
||||
Create.Option(
|
||||
"-l|--logger",
|
||||
LocalizableStrings.CmdLoggerDescription,
|
||||
Accept.ExactlyOneArgument
|
||||
.With(name: LocalizableStrings.CmdLoggerOption)
|
||||
.ForwardAs(o =>
|
||||
{
|
||||
var loggersString = string.Join(";", GetSemiColonEscapedArgs(o.Arguments));
|
||||
|
||||
return $"/p:VSTestLogger={loggersString}";
|
||||
})),
|
||||
CommonOptions.ConfigurationOption(),
|
||||
CommonOptions.FrameworkOption(),
|
||||
Create.Option(
|
||||
"-o|--output",
|
||||
LocalizableStrings.CmdOutputDescription,
|
||||
Accept.ExactlyOneArgument
|
||||
.With(name: LocalizableStrings.CmdOutputDir)
|
||||
.ForwardAs(o => $"/p:OutputPath={o.Arguments.Single()}")),
|
||||
Create.Option(
|
||||
"-d|--diag",
|
||||
LocalizableStrings.CmdPathTologFileDescription,
|
||||
Accept.ExactlyOneArgument
|
||||
.With(name: LocalizableStrings.CmdPathToLogFile)
|
||||
.ForwardAs(o => $"/p:VSTestDiag={o.Arguments.Single()}")),
|
||||
Create.Option(
|
||||
"--no-build",
|
||||
LocalizableStrings.CmdNoBuildDescription,
|
||||
Accept.NoArguments
|
||||
.ForwardAs(o => "/p:VSTestNoBuild=true")),
|
||||
CommonOptions.VerbosityOption());
|
||||
|
||||
private static string GetSemiColonEsacpedstring(string arg)
|
||||
{
|
||||
if (arg.IndexOf(";") != -1)
|
||||
{
|
||||
return arg.Replace(";", "%3b");
|
||||
}
|
||||
|
||||
return arg;
|
||||
}
|
||||
|
||||
private static string[] GetSemiColonEscapedArgs(IReadOnlyCollection<string> args)
|
||||
{
|
||||
int counter = 0;
|
||||
string[] array = new string[args.Count];
|
||||
|
||||
foreach (string arg in args)
|
||||
{
|
||||
array[counter++] = GetSemiColonEsacpedstring(arg);
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue