Skip to content

Commit

Permalink
Merge test run parameters that have spaces (#2409)
Browse files Browse the repository at this point in the history
* Merge test run parameters that have spaces
  • Loading branch information
nohwnd authored Apr 27, 2020
1 parent cd73b65 commit c132fea
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/vstest.console/CommandLine/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ private int GetArgumentProcessors(string[] args, out List<IArgumentProcessor> pr
processors.Add(cliRunSettingsProcessor);
break;
}

var processor = processorFactory.CreateArgumentProcessor(arg);

if (processor != null)
Expand Down
57 changes: 53 additions & 4 deletions src/vstest.console/Processors/CLIRunSettingsArgumentProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors
using System;
using System.Diagnostics.Contracts;
using System.Xml.XPath;
using System.Collections.Generic;

using Microsoft.VisualStudio.TestPlatform.Common;
using Microsoft.VisualStudio.TestPlatform.Common.Interfaces;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.Common.Utilities;

using CommandLineResources = Microsoft.VisualStudio.TestPlatform.CommandLine.Resources.Resources;
using System.Text.RegularExpressions;
using CommandLineResources = Microsoft.VisualStudio.TestPlatform.CommandLine.Resources.Resources;

/// <summary>
/// The argument processor for runsettings passed as argument through cli
Expand Down Expand Up @@ -136,11 +136,60 @@ public ArgumentProcessorResult Execute()

private void CreateOrOverwriteRunSettings(IRunSettingsProvider runSettingsProvider, string[] args)
{
var length = args.Length;
var mergedArgs = new List<string>();
var mergedArg = string.Empty;
var merge = false;

foreach (var arg in args)
{
// when we see that the parameter begins with TestRunParameters
// but does not end with ") we start merging the params
if (arg.StartsWith("TestRunParameters", StringComparison.OrdinalIgnoreCase))
{
if (arg.EndsWith("\")")) {
// this parameter is complete
mergedArgs.Add(arg);
}
else
{
// this parameter needs merging
merge = true;
}
}

// we merge as long as the flag is set
// hoping that we find the end of the parameter
if (merge)
{
mergedArg += string.IsNullOrWhiteSpace(mergedArg) ? arg : $" {arg}";
}
else
{
// if we are not merging just pass the param as is
mergedArgs.Add(arg);
}

// once we detect the end we add the whole parameter to the args
if (merge && arg.EndsWith("\")")) {
mergedArgs.Add(mergedArg);
mergedArg = string.Empty;
merge = false;
}
}

if (merge)
{
// we tried to merge but never found the end of that
// test paramter, add what we merged up until now
mergedArgs.Add(mergedArg);
}


var length = mergedArgs.Count;

for (int index = 0; index < length; index++)
{
var arg = args[index];
var arg = mergedArgs[index];

if (UpdateTestRunParameterNode(runSettingsProvider, arg))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,5 +347,57 @@ public static IEnumerable<object[]> TestRunParameterArgValidTestCases()
},
};
#endregion

[TestMethod]
public void InitializeShouldMergeTestRunParametersWithSpaces()
{
// in powershell call: ConsoleApp1.exe --% --TestRunParameters.Parameter(name =\"myParam\", value=\"myValue\")
// args:
//--
//TestRunParameters.Parameter(name = "myParam",
//value = "myValue")

// in cmd: ConsoleApp1.exe -- TestRunParameters.Parameter(name=\"myParam\", value=\"myValue\")
// args:
//--
//TestRunParameters.Parameter(name = "myParam",
//value = "myValue")

// in ubuntu wsl without escaping the space: ConsoleApp1.exe-- TestRunParameters.Parameter\(name =\"myParam\", value=\"myValue\"\)
// args:
//--
//TestRunParameters.Parameter(name = "myParam",
//value = "myValue")

// in ubuntu wsl with escaping the space: ConsoleApp1.exe-- TestRunParameters.Parameter\(name =\"myParam\",\ value=\"myValue\"\)
// args:
//--
//TestRunParameters.Parameter(name = "myParam", value = "myValue")

var args = new string[] {
"--",
"TestRunParameters.Parameter(name=\"myParam\",",
"value=\"myValue\")",
"TestRunParameters.Parameter(name=\"myParam2\",",
"value=\"myValue 2\")",
};

var runsettings = string.Join(Environment.NewLine, new[]{
"<?xml version=\"1.0\" encoding=\"utf-16\"?>",
"<RunSettings>",
" <DataCollectionRunSettings>",
" <DataCollectors />",
" </DataCollectionRunSettings>",
" <TestRunParameters>",
" <Parameter name=\"myParam\" value=\"myValue\" />",
" <Parameter name=\"myParam2\" value=\"myValue 2\" />",
" </TestRunParameters>",
"</RunSettings>"});

this.executor.Initialize(args);

Assert.IsNotNull(this.settingsProvider.ActiveRunSettings);
Assert.AreEqual(runsettings, settingsProvider.ActiveRunSettings.SettingsXml);
}
}
}

0 comments on commit c132fea

Please sign in to comment.