diff --git a/src/Microsoft.TestPlatform.Common/Utilities/RunSettingsProviderExtensions.cs b/src/Microsoft.TestPlatform.Common/Utilities/RunSettingsProviderExtensions.cs index d864305a9c..406db01feb 100644 --- a/src/Microsoft.TestPlatform.Common/Utilities/RunSettingsProviderExtensions.cs +++ b/src/Microsoft.TestPlatform.Common/Utilities/RunSettingsProviderExtensions.cs @@ -12,6 +12,8 @@ namespace Microsoft.VisualStudio.TestPlatform.Common.Utilities using Microsoft.VisualStudio.TestPlatform.ObjectModel; using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities; using Microsoft.VisualStudio.TestPlatform.Common; + using System.Collections.Generic; + using System.Text.RegularExpressions; /// /// Utilities to get the run settings from the provider and the commandline options specified. @@ -20,6 +22,31 @@ internal static class RunSettingsProviderExtensions { public const string EmptyRunSettings = @""; + /// + /// Pattern used to find parameter node. + /// + private const string ParameterString = "Parameter"; + + /// + /// Pattern that indicates Attribute name. + /// + private const string AttributeNameString = "AttrName"; + + /// + /// Pattern that indicates Attribute value. + /// + private const string AttributeValueString = "AttrValue"; + + /// + /// Attribute name key for test run parameter node + /// + private const string NameString = "name"; + + /// + /// Attribute value key for test run parameter node + /// + private const string ValueString = "value"; + public static void UpdateRunSettings(this IRunSettingsProvider runSettingsProvider, string runsettingsXml) { ValidateArg.NotNull(runSettingsProvider, nameof(runSettingsProvider)); @@ -62,6 +89,61 @@ public static void UpdateRunSettingsNode(this IRunSettingsProvider runSettingsPr runSettingsProvider.UpdateRunSettings(xmlDocument.OuterXml); } + /// + /// Matches with test run parameter node pattern and returns that match. + /// + /// + /// + /// + public static Match GetTestRunParameterNodeMatch(this IRunSettingsProvider runSettingsProvider, string node) + { + var attrName = $"(?<{AttributeNameString}>\\w+)"; + var attrValue = $"(?<{AttributeValueString}>.+)"; + Regex regex = new Regex($"{Constants.TestRunParametersName}.{ParameterString}\\(name\\s*=\\s*\"{attrName}\"\\s*,\\s*value\\s*=\\s*\"{attrValue}\"\\)"); + Match match = regex.Match(node); + return match; + } + + /// + /// If test run parameter exists already it will override with new value otherwise this will add new test run parameter. + /// + /// + /// + public static void UpdateTestRunParameterSettingsNode(this IRunSettingsProvider runSettingsProvider, Match match) + { + ValidateArg.NotNull(runSettingsProvider, nameof(runSettingsProvider)); + + var xmlDocument = runSettingsProvider.GetRunSettingXmlDocument(); + XmlNode testRunParameterNode = GetXmlNode(xmlDocument, Constants.TestRunParametersName) ?? xmlDocument.CreateElement(Constants.TestRunParametersName); + var attrName = match.Groups[AttributeNameString].Value; + var attrValue = match.Groups[AttributeValueString].Value; + + if (!TryOverrideAttributeValue(testRunParameterNode, attrName, attrValue)) + { + XmlElement element = xmlDocument.CreateElement(ParameterString); + element.SetAttribute(NameString, attrName); + element.SetAttribute(ValueString, attrValue); + testRunParameterNode.AppendChild(element); + xmlDocument.DocumentElement.AppendChild(testRunParameterNode); + } + + runSettingsProvider.UpdateRunSettings(xmlDocument.OuterXml); + } + + private static bool TryOverrideAttributeValue(XmlNode xmlNode, string attrName, string attrValue) + { + foreach (XmlNode node in xmlNode.ChildNodes) + { + if (string.Compare(node.Attributes[NameString].Value, attrName) == 0) + { + node.Attributes[ValueString].Value = attrValue; + return true; + } + } + + return false; + } + public static void UpdateRunSettingsNodeInnerXml(this IRunSettingsProvider runSettingsProvider, string key, string xml) { ValidateArg.NotNull(runSettingsProvider, nameof(runSettingsProvider)); @@ -133,7 +215,7 @@ private static XmlNode CreateNode(XmlDocument doc, string xPath) } return node; - } + } private static XmlDocument GetRunSettingXmlDocument(this IRunSettingsProvider runSettingsProvider) { diff --git a/src/Microsoft.TestPlatform.ObjectModel/Constants.cs b/src/Microsoft.TestPlatform.ObjectModel/Constants.cs index 1a06464645..0c326c8486 100644 --- a/src/Microsoft.TestPlatform.ObjectModel/Constants.cs +++ b/src/Microsoft.TestPlatform.ObjectModel/Constants.cs @@ -121,6 +121,9 @@ public static class Constants public const string DataCollectorSettingName = "DataCollector"; + /// + /// Pattern used to find test run parameter node. + /// public const string TestRunParametersName = "TestRunParameters"; /// diff --git a/src/vstest.console/Processors/CLIRunSettingsArgumentProcessor.cs b/src/vstest.console/Processors/CLIRunSettingsArgumentProcessor.cs index 2dcc983da4..c27853c619 100644 --- a/src/vstest.console/Processors/CLIRunSettingsArgumentProcessor.cs +++ b/src/vstest.console/Processors/CLIRunSettingsArgumentProcessor.cs @@ -13,6 +13,7 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors using Microsoft.VisualStudio.TestPlatform.Common.Utilities; using CommandLineResources = Microsoft.VisualStudio.TestPlatform.CommandLine.Resources.Resources; + using System.Text.RegularExpressions; /// /// The argument processor for runsettings passed as argument through cli @@ -139,14 +140,22 @@ private void CreateOrOverwriteRunSettings(IRunSettingsProvider runSettingsProvid for (int index = 0; index < length; index++) { - var keyValuePair = args[index]; - var indexOfSeparator = keyValuePair.IndexOf("="); - if (indexOfSeparator <= 0 || indexOfSeparator >= keyValuePair.Length - 1) + var arg = args[index]; + + if (UpdateTestRunParameterNode(runSettingsProvider, arg)) { continue; } - var key = keyValuePair.Substring(0, indexOfSeparator).Trim(); - var value = keyValuePair.Substring(indexOfSeparator + 1); + + var indexOfSeparator = arg.IndexOf("="); + + if (indexOfSeparator <= 0 || indexOfSeparator >= arg.Length - 1) + { + continue; + } + + var key = arg.Substring(0, indexOfSeparator).Trim(); + var value = arg.Substring(indexOfSeparator + 1); if (string.IsNullOrWhiteSpace(key)) { @@ -160,6 +169,25 @@ private void CreateOrOverwriteRunSettings(IRunSettingsProvider runSettingsProvid } } + private bool UpdateTestRunParameterNode(IRunSettingsProvider runSettingsProvider, string node) + { + if (!node.Contains(Constants.TestRunParametersName)) + { + return false; + } + + var match = runSettingsProvider.GetTestRunParameterNodeMatch(node); + + if (string.Compare(match.Value, node) == 0) + { + runSettingsProvider.UpdateTestRunParameterSettingsNode(match); + return true; + } + + var exceptionMessage = string.Format(CommandLineResources.InvalidTestRunParameterArgument, node); + throw new CommandLineException(exceptionMessage); + } + private void UpdateFrameworkAndPlatform(string key, string value) { if (key.Equals(FrameworkArgumentExecutor.RunSettingsPath)) diff --git a/src/vstest.console/Resources/Resources.Designer.cs b/src/vstest.console/Resources/Resources.Designer.cs index 04393581bc..17706664d3 100644 --- a/src/vstest.console/Resources/Resources.Designer.cs +++ b/src/vstest.console/Resources/Resources.Designer.cs @@ -13,7 +13,6 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Resources using System; using System.Reflection; - /// /// A strongly-typed resource class, for looking up localized strings, etc. /// @@ -826,6 +825,16 @@ internal static string InvalidTestCaseFilterValueForSpecificTests { } } + /// + /// Looks up a localized string similar to The test run parameter argument '{0}' is invalid. Please use the format below. + /// Format: TestRunParameters.Parameter(name=\"<name>\", value=\"<value>\"). + /// + internal static string InvalidTestRunParameterArgument { + get { + return ResourceManager.GetString("InvalidTestRunParameterArgument", resourceCulture); + } + } + /// /// Looks up a localized string similar to Argument {0} is not expected in the 'UseVsixExtensions' command. Specify the command indicating whether the vsix extensions should be used or skipped (Example: vstest.console.exe myTests.dll /UseVsixExtensions:true) and try again.. /// diff --git a/src/vstest.console/Resources/Resources.resx b/src/vstest.console/Resources/Resources.resx index 33cf76e005..911ce94288 100644 --- a/src/vstest.console/Resources/Resources.resx +++ b/src/vstest.console/Resources/Resources.resx @@ -734,4 +734,8 @@ A total of {0} test files matched the specified pattern. + + The test run parameter argument '{0}' is invalid. Please use the format below. + Format: TestRunParameters.Parameter(name=\"<name>\", value=\"<value>\") + \ No newline at end of file diff --git a/src/vstest.console/Resources/xlf/Resources.cs.xlf b/src/vstest.console/Resources/xlf/Resources.cs.xlf index b1593f23ca..b3df2ad5e6 100644 --- a/src/vstest.console/Resources/xlf/Resources.cs.xlf +++ b/src/vstest.console/Resources/xlf/Resources.cs.xlf @@ -1656,6 +1656,13 @@ Celkový počet testovacích souborů, které odpovídají zadanému vzoru: {0} + + The test run parameter argument '{0}' is invalid. Please use the format below. + Format: TestRunParameters.Parameter(name=\"<name>\", value=\"<value>\") + The test run parameter argument '{0}' is invalid. Please use the format below. +Format : TestRunParameters.Parameter(name="<name>", value="<value>") + + \ No newline at end of file diff --git a/src/vstest.console/Resources/xlf/Resources.de.xlf b/src/vstest.console/Resources/xlf/Resources.de.xlf index 20ab7d8d2d..767d993a65 100644 --- a/src/vstest.console/Resources/xlf/Resources.de.xlf +++ b/src/vstest.console/Resources/xlf/Resources.de.xlf @@ -1656,6 +1656,13 @@ Insgesamt {0} Testdateien stimmten mit dem angegebenen Muster überein. + + The test run parameter argument '{0}' is invalid. Please use the format below. + Format: TestRunParameters.Parameter(name=\"<name>\", value=\"<value>\") + The test run parameter argument '{0}' is invalid. Please use the format below. +Format : TestRunParameters.Parameter(name="<name>", value="<value>") + + \ No newline at end of file diff --git a/src/vstest.console/Resources/xlf/Resources.es.xlf b/src/vstest.console/Resources/xlf/Resources.es.xlf index 1aa4e67d8c..c3ab4e9b84 100644 --- a/src/vstest.console/Resources/xlf/Resources.es.xlf +++ b/src/vstest.console/Resources/xlf/Resources.es.xlf @@ -1659,6 +1659,13 @@ {0} archivos de prueba en total coincidieron con el patrón especificado. + + The test run parameter argument '{0}' is invalid. Please use the format below. + Format: TestRunParameters.Parameter(name=\"<name>\", value=\"<value>\") + The test run parameter argument '{0}' is invalid. Please use the format below. +Format : TestRunParameters.Parameter(name="<name>", value="<value>") + + \ No newline at end of file diff --git a/src/vstest.console/Resources/xlf/Resources.fr.xlf b/src/vstest.console/Resources/xlf/Resources.fr.xlf index a71d6a23b9..5efd828080 100644 --- a/src/vstest.console/Resources/xlf/Resources.fr.xlf +++ b/src/vstest.console/Resources/xlf/Resources.fr.xlf @@ -1656,6 +1656,13 @@ Au total, {0} fichiers de test ont correspondu au modèle spécifié. + + The test run parameter argument '{0}' is invalid. Please use the format below. + Format: TestRunParameters.Parameter(name=\"<name>\", value=\"<value>\") + The test run parameter argument '{0}' is invalid. Please use the format below. +Format : TestRunParameters.Parameter(name="<name>", value="<value>") + + \ No newline at end of file diff --git a/src/vstest.console/Resources/xlf/Resources.it.xlf b/src/vstest.console/Resources/xlf/Resources.it.xlf index 08954c552d..66f8d6b9a9 100644 --- a/src/vstest.console/Resources/xlf/Resources.it.xlf +++ b/src/vstest.console/Resources/xlf/Resources.it.xlf @@ -1656,6 +1656,13 @@ Un totale di {0} file di test corrisponde al criterio specificato. + + The test run parameter argument '{0}' is invalid. Please use the format below. + Format: TestRunParameters.Parameter(name=\"<name>\", value=\"<value>\") + The test run parameter argument '{0}' is invalid. Please use the format below. +Format : TestRunParameters.Parameter(name="<name>", value="<value>") + + \ No newline at end of file diff --git a/src/vstest.console/Resources/xlf/Resources.ja.xlf b/src/vstest.console/Resources/xlf/Resources.ja.xlf index 972403200a..26f4c1156f 100644 --- a/src/vstest.console/Resources/xlf/Resources.ja.xlf +++ b/src/vstest.console/Resources/xlf/Resources.ja.xlf @@ -1656,6 +1656,13 @@ 合計 {0} 個のテスト ファイルが指定されたパターンと一致しました。 + + The test run parameter argument '{0}' is invalid. Please use the format below. + Format: TestRunParameters.Parameter(name=\"<name>\", value=\"<value>\") + The test run parameter argument '{0}' is invalid. Please use the format below. +Format : TestRunParameters.Parameter(name="<name>", value="<value>") + + \ No newline at end of file diff --git a/src/vstest.console/Resources/xlf/Resources.ko.xlf b/src/vstest.console/Resources/xlf/Resources.ko.xlf index 9779563bfe..d80a84e3e4 100644 --- a/src/vstest.console/Resources/xlf/Resources.ko.xlf +++ b/src/vstest.console/Resources/xlf/Resources.ko.xlf @@ -1656,6 +1656,13 @@ 지정된 패턴과 일치한 총 테스트 파일 수는 {0}개입니다. + + The test run parameter argument '{0}' is invalid. Please use the format below. + Format: TestRunParameters.Parameter(name=\"<name>\", value=\"<value>\") + The test run parameter argument '{0}' is invalid. Please use the format below. +Format : TestRunParameters.Parameter(name="<name>", value="<value>") + + \ No newline at end of file diff --git a/src/vstest.console/Resources/xlf/Resources.pl.xlf b/src/vstest.console/Resources/xlf/Resources.pl.xlf index a59a2094f1..d05d81125f 100644 --- a/src/vstest.console/Resources/xlf/Resources.pl.xlf +++ b/src/vstest.console/Resources/xlf/Resources.pl.xlf @@ -1656,6 +1656,13 @@ Łączna liczba plików testowych dopasowanych do określonego wzorca: {0}. + + The test run parameter argument '{0}' is invalid. Please use the format below. + Format: TestRunParameters.Parameter(name=\"<name>\", value=\"<value>\") + The test run parameter argument '{0}' is invalid. Please use the format below. +Format : TestRunParameters.Parameter(name="<name>", value="<value>") + + \ No newline at end of file diff --git a/src/vstest.console/Resources/xlf/Resources.pt-BR.xlf b/src/vstest.console/Resources/xlf/Resources.pt-BR.xlf index 286327d711..81debb3c5e 100644 --- a/src/vstest.console/Resources/xlf/Resources.pt-BR.xlf +++ b/src/vstest.console/Resources/xlf/Resources.pt-BR.xlf @@ -1656,6 +1656,13 @@ Altere o prefixo de nível de diagnóstico do agente de console, como mostrado a {0} arquivos de teste no total corresponderam ao padrão especificado. + + The test run parameter argument '{0}' is invalid. Please use the format below. + Format: TestRunParameters.Parameter(name=\"<name>\", value=\"<value>\") + The test run parameter argument '{0}' is invalid. Please use the format below. +Format : TestRunParameters.Parameter(name="<name>", value="<value>") + + \ No newline at end of file diff --git a/src/vstest.console/Resources/xlf/Resources.ru.xlf b/src/vstest.console/Resources/xlf/Resources.ru.xlf index 9d803eba73..40daac4ca2 100644 --- a/src/vstest.console/Resources/xlf/Resources.ru.xlf +++ b/src/vstest.console/Resources/xlf/Resources.ru.xlf @@ -1656,6 +1656,13 @@ Общее количество тестовых файлов ({0}), соответствующих указанному шаблону. + + The test run parameter argument '{0}' is invalid. Please use the format below. + Format: TestRunParameters.Parameter(name=\"<name>\", value=\"<value>\") + The test run parameter argument '{0}' is invalid. Please use the format below. +Format : TestRunParameters.Parameter(name="<name>", value="<value>") + + \ No newline at end of file diff --git a/src/vstest.console/Resources/xlf/Resources.tr.xlf b/src/vstest.console/Resources/xlf/Resources.tr.xlf index 4bdadc4131..f73b42ab42 100644 --- a/src/vstest.console/Resources/xlf/Resources.tr.xlf +++ b/src/vstest.console/Resources/xlf/Resources.tr.xlf @@ -1656,6 +1656,13 @@ Günlükler için izleme düzeyini aşağıda gösterildiği gibi değiştirin Toplam {0} test dosyası belirtilen desenle eşleşti. + + The test run parameter argument '{0}' is invalid. Please use the format below. + Format: TestRunParameters.Parameter(name=\"<name>\", value=\"<value>\") + The test run parameter argument '{0}' is invalid. Please use the format below. +Format : TestRunParameters.Parameter(name="<name>", value="<value>") + + \ No newline at end of file diff --git a/src/vstest.console/Resources/xlf/Resources.xlf b/src/vstest.console/Resources/xlf/Resources.xlf index ba252597b1..d9147b5b1e 100644 --- a/src/vstest.console/Resources/xlf/Resources.xlf +++ b/src/vstest.console/Resources/xlf/Resources.xlf @@ -847,6 +847,13 @@ A total of {0} test source files are discovered. + + The test run parameter argument '{0}' is invalid. Please use the format below. + Format: TestRunParameters.Parameter(name=\"<name>\", value=\"<value>\") + The test run parameter argument '{0}' is invalid. Please use the format below. +Format : TestRunParameters.Parameter(name="<name>", value="<value>") + + \ No newline at end of file diff --git a/src/vstest.console/Resources/xlf/Resources.zh-Hans.xlf b/src/vstest.console/Resources/xlf/Resources.zh-Hans.xlf index 597051fc39..7b6c454949 100644 --- a/src/vstest.console/Resources/xlf/Resources.zh-Hans.xlf +++ b/src/vstest.console/Resources/xlf/Resources.zh-Hans.xlf @@ -1656,6 +1656,13 @@ 总共 {0} 个测试文件与指定模式相匹配。 + + The test run parameter argument '{0}' is invalid. Please use the format below. + Format: TestRunParameters.Parameter(name=\"<name>\", value=\"<value>\") + The test run parameter argument '{0}' is invalid. Please use the format below. +Format : TestRunParameters.Parameter(name="<name>", value="<value>") + + \ No newline at end of file diff --git a/src/vstest.console/Resources/xlf/Resources.zh-Hant.xlf b/src/vstest.console/Resources/xlf/Resources.zh-Hant.xlf index 37d2f4047d..21f2801571 100644 --- a/src/vstest.console/Resources/xlf/Resources.zh-Hant.xlf +++ b/src/vstest.console/Resources/xlf/Resources.zh-Hant.xlf @@ -1657,6 +1657,13 @@ 總共有 {0} 個測試檔案與指定的模式相符。 + + The test run parameter argument '{0}' is invalid. Please use the format below. + Format: TestRunParameters.Parameter(name=\"<name>\", value=\"<value>\") + The test run parameter argument '{0}' is invalid. Please use the format below. +Format : TestRunParameters.Parameter(name="<name>", value="<value>") + + \ No newline at end of file diff --git a/test/Microsoft.TestPlatform.Common.UnitTests/ExtensionFramework/Utilities/RunSettingsProviderExtensionsTests.cs b/test/Microsoft.TestPlatform.Common.UnitTests/ExtensionFramework/Utilities/RunSettingsProviderExtensionsTests.cs index 8083c71a57..82e834be78 100644 --- a/test/Microsoft.TestPlatform.Common.UnitTests/ExtensionFramework/Utilities/RunSettingsProviderExtensionsTests.cs +++ b/test/Microsoft.TestPlatform.Common.UnitTests/ExtensionFramework/Utilities/RunSettingsProviderExtensionsTests.cs @@ -4,13 +4,14 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors.Utilities { using System; - + using Microsoft.VisualStudio.TestPlatform.Common; using Microsoft.VisualStudio.TestPlatform.Common.Interfaces; using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities; using Microsoft.VisualStudio.TestPlatform.ObjectModel; using Microsoft.VisualStudio.TestPlatform.Common.Utilities; + using System.Text.RegularExpressions; [TestClass] public class RunSettingsProviderExtensionsTests @@ -24,7 +25,7 @@ public void Init() { runSettingsProvider = new TestableRunSettingsProvider(); } - + [TestMethod] public void UpdateRunSettingsShouldUpdateGivenSettingsXml() { @@ -130,6 +131,31 @@ public void UpdateRunSettingsNodeShouldAddNewKeyIfNotPresent() Assert.AreEqual("data", this.runSettingsProvider.QueryRunSettingsNode("Key.Path")); } + [TestMethod] + public void UpdateTestRunParameterSettingsNodeShouldAddNewKeyIfNotPresent() + { + var match = this.runSettingsProvider.GetTestRunParameterNodeMatch("TestRunParameters.Parameter(name=\"weburl\",value=\"http://localhost//abc\")"); + var runSettingsWithTestRunParameters = "\r\n\r\n \r\n \r\n \r\n"; + + this.runSettingsProvider.UpdateRunSettings("\r\n "); + this.runSettingsProvider.UpdateTestRunParameterSettingsNode(match); + + Assert.AreEqual(runSettingsWithTestRunParameters, this.runSettingsProvider.ActiveRunSettings.SettingsXml); + } + + [TestMethod] + public void UpdateTetsRunParameterSettingsNodeShouldOverrideValueIfKeyIsAlreadyPresent() + { + var runSettingsWithTestRunParameters = "\r\n\r\n \r\n \r\n \r\n"; + var runSettingsWithTestRunParametersOverrode = "\r\n\r\n \r\n \r\n \r\n"; + + this.runSettingsProvider.UpdateRunSettings(runSettingsWithTestRunParameters); + var match = this.runSettingsProvider.GetTestRunParameterNodeMatch("TestRunParameters.Parameter(name=\"weburl\",value=\"http://localhost//def\")"); + this.runSettingsProvider.UpdateTestRunParameterSettingsNode(match); + + Assert.AreEqual(runSettingsWithTestRunParametersOverrode, this.runSettingsProvider.ActiveRunSettings.SettingsXml); + } + [TestMethod] public void UpdateRunSettingsNodeShouldUpdateKeyIfAlreadyPresent() { @@ -203,7 +229,7 @@ public void QueryRunSettingsNodeShouldReturnCorrectValue() this.runSettingsProvider.UpdateRunSettings(" x86 "); Assert.AreEqual("x86", this.runSettingsProvider.QueryRunSettingsNode("RunConfiguration.TargetPlatform")); } - + private class TestableRunSettingsProvider : IRunSettingsProvider { public RunSettings ActiveRunSettings @@ -211,7 +237,6 @@ public RunSettings ActiveRunSettings get; set; } - public void SetActiveRunSettings(RunSettings runSettings) { this.ActiveRunSettings = runSettings; diff --git a/test/vstest.console.UnitTests/Processors/CLIRunSettingsArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/CLIRunSettingsArgumentProcessorTests.cs index 51aa3107db..2a8d4ea7bb 100644 --- a/test/vstest.console.UnitTests/Processors/CLIRunSettingsArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/CLIRunSettingsArgumentProcessorTests.cs @@ -14,6 +14,7 @@ namespace vstest.console.UnitTests.Processors using Microsoft.VisualStudio.TestPlatform.Common; using Microsoft.VisualStudio.TestPlatform.ObjectModel; using Microsoft.VisualStudio.TestTools.UnitTesting; + using CommandLineResources = Microsoft.VisualStudio.TestPlatform.CommandLine.Resources.Resources; [TestClass] public class CLIRunSettingsArgumentProcessorTests @@ -122,6 +123,19 @@ public void InitializeShouldIgnoreKeyIfValueIsNotPassed() Assert.AreEqual(RunSettingsWithDeploymentDisabled, settingsProvider.ActiveRunSettings.SettingsXml); } + [DataRow("Testameter.Parameter(name=\"asf\",value=\"rgq\")")] + [DataRow("TestRunParameter.Parameter(name=\"asf\",value=\"rgq\")")] + [TestMethod] + public void InitializeShouldThrowErrorIfArgumentIsInValid(string arg) + { + var args = new string[] { arg }; + var str = string.Format(CommandLineResources.MalformedRunSettingsKey); + + CommandLineException ex = Assert.ThrowsException(() => this.executor.Initialize(args)); + + Assert.AreEqual(str, ex.Message); + } + [TestMethod] public void InitializeShouldIgnoreWhiteSpaceInBeginningOrEndOfKey() { @@ -258,6 +272,82 @@ public void InitializeShouldNotUpdateCommandLineOptionsArchitectureAndFxIfNotPro Assert.IsFalse(this.commandLineOptions.FrameworkVersionSpecified); } + [DynamicData(nameof(TestRunParameterArgValidTestCases), DynamicDataSourceType.Method)] + [DataTestMethod] + public void InitializeShouldValidateTestRunParameter(string arg, string runSettingsWithTestRunParameters) + { + var args = new string[] { arg }; + + this.executor.Initialize(args); + + Assert.IsNotNull(this.settingsProvider.ActiveRunSettings); + Assert.AreEqual(runSettingsWithTestRunParameters, settingsProvider.ActiveRunSettings.SettingsXml); + } + + [DynamicData(nameof(TestRunParameterArgInvalidTestCases), DynamicDataSourceType.Method)] + [DataTestMethod] + public void InitializeShouldThrowErrorIfTestRunParameterNodeIsInValid(string arg) + { + var args = new string[] { arg }; + var str = string.Format(CommandLineResources.InvalidTestRunParameterArgument, arg); + + CommandLineException ex = Assert.ThrowsException(() => this.executor.Initialize(args)); + + Assert.AreEqual(str, ex.Message); + } + + public static IEnumerable TestRunParameterArgInvalidTestCases() + { + return invalidTestCases; + } + + private static readonly List invalidTestCases = new List + { + new object[] { "TestRunParameters.Parameter(name=asf,value=rgq)" }, + new object[] { "TestRunParameters.Parameter(name=\"asf\",value=\"rgq\" )"}, + new object[] { "TestRunParameters.Parameter( name=\"asf\",value=\"rgq\")" }, + new object[] { "TestRunParametersParameter(name=\"asf\",value=\"rgq\")" }, + new object[] { "TestRunParameters.Paramete(name=\"asf\",value=\"rgq\")" }, + new object[] { "TestRunParameters.Parametername=\"asf\",value=\"rgq\")" }, + new object[] { "TestRunParameters.Parameter(ame=\"asf\",value=\"rgq\")" }, + new object[] { "TestRunParameters.Parameter(name\"asf\",value=\"rgq\")" }, + new object[] { "TestRunParameters.Parameter(name=\"asf\" value=\"rgq\")" }, + new object[] { "TestRunParameters.Parameter(name=\"asf\",alue=\"rgq\")" }, + new object[] { "TestRunParameters.Parameter(name=\"asf\",value\"rgq\")" }, + new object[] { "TestRunParameters.Parameter(name=\"asf\",value=\"rgq\"" }, + new object[] { "TestRunParameters.Parameter(name=\"asf\",value=\"rgq\")wfds" }, + new object[] { "TestRunParameters.Parameter(name=\"\",value=\"rgq\")" }, + new object[] { "TestRunParameters.Parameter(name=\"asf\",value=\"\")" }, + new object[] { "TestRunParameters.Parameter(name=asf\",value=\"rgq\")" }, + new object[] { "TestRunParameters.Parameter(name=\"asf,value=\"rgq\")" }, + new object[] { "TestRunParameters.Parameter(name=\"asf\",value=rgq\")" }, + new object[] { "TestRunParameters.Parameter(name=\"asf\",value=\"rgq)" }, + new object[] { "TestRunParameters.Parameter(name=\"asf@#!\",value=\"rgq\")" }, + new object[] { "TestRunParameters.Parameter(name=\"\",value=\"fgf\")" }, + new object[] { "TestRunParameters.Parameter(name=\"gag\",value=\"\")" }, + new object[] { "TestRunParameters.Parameter(name=\"gag\")" } + }; + + public static IEnumerable TestRunParameterArgValidTestCases() + { + return validTestCases; + } + + private static readonly List validTestCases = new List + { + new object[] { "TestRunParameters.Parameter(name=\"weburl\",value=\"&><\")" , + "\r\n\r\n \r\n \r\n \r\n \r\n \r\n \r\n" + }, + new object[] { "TestRunParameters.Parameter(name=\"weburl\",value=\"http://localhost//abc\")" , + "\r\n\r\n \r\n \r\n \r\n \r\n \r\n \r\n" + }, + new object[] { "TestRunParameters.Parameter(name= \"a_sf123_12\",value= \"2324346a!@#$%^*()_+-=':;.,/?{}[]|\")" , + "\r\n\r\n \r\n \r\n \r\n \r\n \r\n \r\n" + }, + new object[] { "TestRunParameters.Parameter(name = \"weburl\" , value = \"http://localhost//abc\")" , + "\r\n\r\n \r\n \r\n \r\n \r\n \r\n \r\n" + }, + }; #endregion } }