2016-10-04 15:37:36 +05:30
|
|
|
|
// 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;
|
2017-03-10 13:49:49 -08:00
|
|
|
|
using System.Collections.Generic;
|
2017-03-10 13:18:14 -08:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using Microsoft.DotNet.Cli;
|
2016-10-04 15:37:36 +05:30
|
|
|
|
using Microsoft.DotNet.Cli.CommandLine;
|
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
|
|
|
|
using Microsoft.DotNet.Tools.MSBuild;
|
2017-03-10 13:18:14 -08:00
|
|
|
|
using Parser = Microsoft.DotNet.Cli.Parser;
|
2016-10-04 15:37:36 +05:30
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
namespace Microsoft.DotNet.Tools.Test
|
2016-10-04 15:37:36 +05:30
|
|
|
|
{
|
2017-03-10 13:18:14 -08:00
|
|
|
|
public class TestCommand : MSBuildForwardingApp
|
2016-10-04 15:37:36 +05:30
|
|
|
|
{
|
2017-03-10 13:18:14 -08:00
|
|
|
|
public TestCommand(IEnumerable<string> msbuildArgs, string msbuildPath = null)
|
|
|
|
|
: base(msbuildArgs, msbuildPath)
|
2016-10-04 15:37:36 +05:30
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-10 13:18:14 -08:00
|
|
|
|
public static TestCommand FromArgs(string[] args, string msbuildPath=null)
|
2016-10-04 15:37:36 +05:30
|
|
|
|
{
|
2017-03-10 13:18:14 -08:00
|
|
|
|
var msbuildArgs = new List<string>()
|
2016-10-04 15:37:36 +05:30
|
|
|
|
{
|
2017-03-10 13:18:14 -08:00
|
|
|
|
"/t:VSTest",
|
|
|
|
|
"/v:quiet",
|
|
|
|
|
"/nologo"
|
|
|
|
|
};
|
2016-10-04 15:37:36 +05:30
|
|
|
|
|
2017-03-10 13:18:14 -08:00
|
|
|
|
var parser = Parser.Instance;
|
2016-10-04 15:37:36 +05:30
|
|
|
|
|
2017-03-10 13:18:14 -08:00
|
|
|
|
var result = parser.ParseFrom("dotnet test", args);
|
2016-10-04 15:37:36 +05:30
|
|
|
|
|
2017-03-10 13:18:14 -08:00
|
|
|
|
result.ShowHelpIfRequested();
|
2016-10-04 15:37:36 +05:30
|
|
|
|
|
2017-03-10 13:18:14 -08:00
|
|
|
|
var parsedTest = result["dotnet"]["test"];
|
2016-10-04 15:37:36 +05:30
|
|
|
|
|
2017-03-10 13:49:49 -08:00
|
|
|
|
msbuildArgs.AddRange(parsedTest.OptionValuesToBeForwarded());
|
|
|
|
|
|
|
|
|
|
msbuildArgs.AddRange(parsedTest.Arguments);
|
|
|
|
|
|
2017-03-10 13:18:14 -08:00
|
|
|
|
var runSettingsOptions =
|
|
|
|
|
result.UnparsedTokens
|
2017-03-11 11:00:18 -08:00
|
|
|
|
.Select(GetSemiColonEscapedString);
|
2016-10-04 15:37:36 +05:30
|
|
|
|
|
2017-03-10 13:18:14 -08:00
|
|
|
|
if (runSettingsOptions.Any())
|
2016-10-04 15:37:36 +05:30
|
|
|
|
{
|
2017-03-10 13:18:14 -08:00
|
|
|
|
var runSettingsArg = string.Join(";", runSettingsOptions);
|
|
|
|
|
|
|
|
|
|
msbuildArgs.Add($"/p:VSTestCLIRunSettings=\"{runSettingsArg}\"");
|
2016-10-04 15:37:36 +05:30
|
|
|
|
}
|
|
|
|
|
|
2017-03-10 13:18:14 -08:00
|
|
|
|
return new TestCommand(msbuildArgs, msbuildPath);
|
2016-10-04 15:37:36 +05:30
|
|
|
|
}
|
2016-12-30 11:24:03 +05:30
|
|
|
|
|
2017-03-10 13:18:14 -08:00
|
|
|
|
public static int Run(string[] args)
|
2016-12-30 11:24:03 +05:30
|
|
|
|
{
|
2017-03-10 13:18:14 -08:00
|
|
|
|
DebugHelper.HandleDebugSwitch(ref args);
|
2016-12-30 11:24:03 +05:30
|
|
|
|
|
2017-03-10 13:18:14 -08:00
|
|
|
|
TestCommand cmd;
|
|
|
|
|
|
|
|
|
|
try
|
2016-12-30 11:24:03 +05:30
|
|
|
|
{
|
2017-03-10 13:18:14 -08:00
|
|
|
|
cmd = FromArgs(args);
|
2016-12-30 15:34:00 +05:30
|
|
|
|
}
|
2017-03-10 13:18:14 -08:00
|
|
|
|
catch (CommandCreationException e)
|
2016-12-30 15:34:00 +05:30
|
|
|
|
{
|
2017-03-10 13:18:14 -08:00
|
|
|
|
return e.ExitCode;
|
2016-12-30 11:24:03 +05:30
|
|
|
|
}
|
|
|
|
|
|
2017-03-10 13:18:14 -08:00
|
|
|
|
return cmd.Execute();
|
2016-12-30 15:34:00 +05:30
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 11:00:18 -08:00
|
|
|
|
private static string GetSemiColonEscapedString(string arg)
|
2016-12-30 15:34:00 +05:30
|
|
|
|
{
|
2017-01-06 01:12:26 +05:30
|
|
|
|
if (arg.IndexOf(";") != -1)
|
|
|
|
|
{
|
|
|
|
|
return arg.Replace(";", "%3b");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return arg;
|
2016-12-30 11:24:03 +05:30
|
|
|
|
}
|
2016-12-30 15:34:00 +05:30
|
|
|
|
|
2016-12-30 15:47:15 +05:30
|
|
|
|
private static string[] GetSemiColonEscapedArgs(List<string> args)
|
|
|
|
|
{
|
|
|
|
|
int counter = 0;
|
|
|
|
|
string[] array = new string[args.Count];
|
|
|
|
|
|
|
|
|
|
foreach (string arg in args)
|
|
|
|
|
{
|
2017-03-11 11:00:18 -08:00
|
|
|
|
array[counter++] = GetSemiColonEscapedString(arg);
|
2016-12-30 15:47:15 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return array;
|
|
|
|
|
}
|
2016-10-04 15:37:36 +05:30
|
|
|
|
}
|
|
|
|
|
}
|