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.
|
public const string CmdTestAdapterPathDescription = @"Use custom adapters from the given path in the test run.
|
||||||
Example: --test-adapter-path <PATH_TO_ADAPTER>";
|
Example: --test-adapter-path <PATH_TO_ADAPTER>";
|
||||||
|
|
||||||
|
public const string CmdTestAdapterPath = "PATH_TO_ADAPTER";
|
||||||
|
|
||||||
public const string CmdLoggerOption = "LoggerUri/FriendlyName";
|
public const string CmdLoggerOption = "LoggerUri/FriendlyName";
|
||||||
|
|
||||||
public const string CmdLoggerDescription = @"Specify a logger for test results.
|
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.
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using Microsoft.DotNet.Cli;
|
||||||
using Microsoft.DotNet.Cli.CommandLine;
|
using Microsoft.DotNet.Cli.CommandLine;
|
||||||
using Microsoft.DotNet.Cli.Utils;
|
using Microsoft.DotNet.Cli.Utils;
|
||||||
using Microsoft.DotNet.Tools.MSBuild;
|
using Microsoft.DotNet.Tools.MSBuild;
|
||||||
using System.IO;
|
using Parser = Microsoft.DotNet.Cli.Parser;
|
||||||
using System.Text.RegularExpressions;
|
|
||||||
|
|
||||||
namespace Microsoft.DotNet.Tools.Test
|
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)
|
public static int Run(string[] args)
|
||||||
{
|
{
|
||||||
DebugHelper.HandleDebugSwitch(ref args);
|
DebugHelper.HandleDebugSwitch(ref args);
|
||||||
|
|
||||||
var cmd = new CommandLineApplication(throwOnUnexpectedArg: false)
|
TestCommand cmd;
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
Name = "dotnet test",
|
cmd = FromArgs(args);
|
||||||
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.");
|
|
||||||
}
|
}
|
||||||
else if (projectFound == 0)
|
catch (CommandCreationException e)
|
||||||
{
|
{
|
||||||
result = GetDefaultTestProject();
|
return e.ExitCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return cmd.Execute();
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string GetSemiColonEsacpedstring(string arg)
|
private static string GetSemiColonEsacpedstring(string arg)
|
||||||
|
|
|
@ -1,54 +1,93 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.DotNet.Cli.CommandLine;
|
using Microsoft.DotNet.Cli.CommandLine;
|
||||||
|
using LocalizableStrings = Microsoft.DotNet.Tools.Test.LocalizableStrings;
|
||||||
|
|
||||||
namespace Microsoft.DotNet.Cli
|
namespace Microsoft.DotNet.Cli
|
||||||
{
|
{
|
||||||
internal static class TestCommandParser
|
internal static class TestCommandParser
|
||||||
{
|
{
|
||||||
public static Command Test() =>
|
public static Command Test() =>
|
||||||
Create.Command("test",
|
Create.Command(
|
||||||
".NET Test Driver",
|
"test",
|
||||||
Create.Option("-h|--help",
|
".NET Test Driver",
|
||||||
"Show help information"),
|
CommonOptions.HelpOption(),
|
||||||
Create.Option("-s|--settings",
|
Create.Option(
|
||||||
"Settings to use when running tests.",
|
"-s|--settings",
|
||||||
Accept.ExactlyOneArgument
|
LocalizableStrings.CmdSettingsDescription,
|
||||||
.With(name: "SETTINGS_FILE")),
|
Accept.ExactlyOneArgument
|
||||||
Create.Option("-t|--list-tests",
|
.With(name: LocalizableStrings.CmdSettingsFile)
|
||||||
"Lists discovered tests"),
|
.ForwardAs(o => $"/p:VSTestSetting={o.Arguments.Single()}")),
|
||||||
Create.Option("--filter",
|
Create.Option(
|
||||||
@"Run tests that match the given expression.
|
"-t|--list-tests",
|
||||||
Examples:
|
LocalizableStrings.CmdListTestsDescription,
|
||||||
Run tests with priority set to 1: --filter ""Priority = 1""
|
Accept.NoArguments
|
||||||
Run a test with the specified full name: --filter ""FullyQualifiedName=Namespace.ClassName.MethodName""
|
.ForwardAs(o => "/p:VSTestListTests=true")),
|
||||||
Run tests that contain the specified name: --filter ""FullyQualifiedName~Namespace.Class""
|
Create.Option(
|
||||||
More info on filtering support: https://aka.ms/vstest-filtering",
|
"--filter",
|
||||||
Accept.ExactlyOneArgument
|
LocalizableStrings.CmdTestCaseFilterDescription,
|
||||||
.With(name: "EXPRESSION")),
|
Accept.ExactlyOneArgument
|
||||||
Create.Option("-a|--test-adapter-path",
|
.With(name: LocalizableStrings.CmdTestCaseFilterExpression)
|
||||||
"Use custom adapters from the given path in the test run.\r\n Example: --test-adapter-path <PATH_TO_ADAPTER>"),
|
.ForwardAs(o => $"/p:VSTestTestCaseFilter={o.Arguments.Single()}")),
|
||||||
Create.Option("-l|--logger",
|
Create.Option(
|
||||||
"Specify a logger for test results.\r\n Example: --logger \"trx[;LogFileName=<Defaults to unique file name>]\"",
|
"-a|--test-adapter-path",
|
||||||
Accept.ExactlyOneArgument
|
LocalizableStrings.CmdTestAdapterPathDescription,
|
||||||
.With(name: "LoggerUri/FriendlyName")),
|
Accept.ExactlyOneArgument
|
||||||
Create.Option("-c|--configuration", "Configuration to use for building the project. Default for most projects is \"Debug\".",
|
.With(name: LocalizableStrings.CmdTestAdapterPath)
|
||||||
Accept.ExactlyOneArgument
|
.ForwardAs(o => $"/p:VSTestTestAdapterPath={o.Arguments.Single()}")),
|
||||||
.With(name: "CONFIGURATION")
|
Create.Option(
|
||||||
.WithSuggestionsFrom("DEBUG", "RELEASE")),
|
"-l|--logger",
|
||||||
Create.Option("-f|--framework",
|
LocalizableStrings.CmdLoggerDescription,
|
||||||
"Looks for test binaries for a specific framework",
|
Accept.ExactlyOneArgument
|
||||||
Accept.AnyOneOf(Suggest.TargetFrameworksFromProjectFile)
|
.With(name: LocalizableStrings.CmdLoggerOption)
|
||||||
.With(name: "FRAMEWORK")),
|
.ForwardAs(o =>
|
||||||
Create.Option("-o|--output",
|
{
|
||||||
"Directory in which to find the binaries to be run",
|
var loggersString = string.Join(";", GetSemiColonEscapedArgs(o.Arguments));
|
||||||
Accept.ExactlyOneArgument
|
|
||||||
.With(name: "OUTPUT_DIR")),
|
return $"/p:VSTestLogger={loggersString}";
|
||||||
Create.Option("-d|--diag",
|
})),
|
||||||
"Enable verbose logs for test platform.\r\n Logs are written to the provided file.",
|
CommonOptions.ConfigurationOption(),
|
||||||
Accept.ExactlyOneArgument
|
CommonOptions.FrameworkOption(),
|
||||||
.With(name: "PATH_TO_FILE")),
|
Create.Option(
|
||||||
Create.Option("--no-build",
|
"-o|--output",
|
||||||
"Do not build project before testing."),
|
LocalizableStrings.CmdOutputDescription,
|
||||||
CommonOptions.VerbosityOption());
|
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…
Add table
Add a link
Reference in a new issue