Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge test run parameters that have spaces #2409

Merged
merged 2 commits into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}