From 7fc001080a8b4cc9f2a80824e5c9ef0c7cea8ad7 Mon Sep 17 00:00:00 2001 From: samadala Date: Tue, 3 Jan 2017 19:13:00 +0530 Subject: [PATCH 01/10] Add ResultsDirectory switch and create runsettings on fly --- .../CommandLine/CommandLineOptions.cs | 4 +- .../CLIRunSettingsArgumentProcessor.cs | 64 +------ .../Processors/FrameworkArgumentProcessor.cs | 23 ++- .../Processors/ParallelArgumentProcessor.cs | 21 ++- .../Processors/PlatformArgumentProcessor.cs | 21 ++- .../ResultsDirectoryArgumentProcessor.cs | 166 ++++++++++++++++++ .../Utilities/ArgumentProcessorFactory.cs | 3 +- .../Utilities/ArgumentProcessorPriority.cs | 8 +- .../Utilities/HelpContentPriority.cs | 5 + .../Utilities/RunSettingsUtilities.cs | 98 ++++++++++- .../Resources/Resources.Designer.cs | 27 ++- src/vstest.console/Resources/Resources.resx | 9 + src/vstest.console/vstest.console.csproj | 2 +- .../AcceptanceTestBase.cs | 22 +++ .../RunsettingsTests.cs | 145 ++++++--------- ...osoft.TestPlatform.Common.UnitTests.csproj | 15 +- ...rm.CommunicationUtilities.UnitTests.csproj | 11 +- ...tPlatform.CrossPlatEngine.UnitTests.csproj | 13 +- .../FrameworkArgumentProcessorTests.cs | 15 +- .../ParallelArgumentProcessorTests.cs | 7 +- .../PlatformArgumentProcessorTests.cs | 15 +- .../vstest.console.UnitTests.csproj | 13 +- 22 files changed, 477 insertions(+), 230 deletions(-) create mode 100644 src/vstest.console/Processors/ResultsDirectoryArgumentProcessor.cs diff --git a/src/vstest.console/CommandLine/CommandLineOptions.cs b/src/vstest.console/CommandLine/CommandLineOptions.cs index 3e57adafc1..b7af0eb5d9 100644 --- a/src/vstest.console/CommandLine/CommandLineOptions.cs +++ b/src/vstest.console/CommandLine/CommandLineOptions.cs @@ -221,12 +221,14 @@ internal Framework TargetFrameworkVersion this.FrameworkVersionSpecified = true; } } - + /// /// Gets a value indicating whether /Framework has been specified on command line or not. /// internal bool FrameworkVersionSpecified { get; private set; } + internal string ResultsDirectory { get; set; } + #endregion #region Public Methods diff --git a/src/vstest.console/Processors/CLIRunSettingsArgumentProcessor.cs b/src/vstest.console/Processors/CLIRunSettingsArgumentProcessor.cs index ac41bcb65f..3638335706 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.Interfaces; using Microsoft.VisualStudio.TestPlatform.ObjectModel; using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities; + using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; using CommandLineResources = Microsoft.VisualStudio.TestPlatform.CommandLine.Resources.Resources; @@ -114,41 +115,13 @@ public void Initialize(string[] arguments) // Load up the run settings and set it as the active run settings. try { - var doc = new XmlDocument(); - - if (this.runSettingsManager.ActiveRunSettings != null && !string.IsNullOrEmpty(this.runSettingsManager.ActiveRunSettings.SettingsXml)) - { - var settingsXml = this.runSettingsManager.ActiveRunSettings.SettingsXml; - -#if net46 - using (var reader = XmlReader.Create(new StringReader(settingsXml), new XmlReaderSettings() { XmlResolver = null, CloseInput = true, DtdProcessing = DtdProcessing.Prohibit })) - { -#else - using (var reader = XmlReader.Create(new StringReader(settingsXml), new XmlReaderSettings() { CloseInput = true, DtdProcessing = DtdProcessing.Prohibit })) - { -#endif - doc.Load(reader); - } - } - else - { -#if net46 - doc = (XmlDocument)XmlRunSettingsUtilities.CreateDefaultRunSettings(); -#else - using (var reader = XmlReader.Create(new StringReader(XmlRunSettingsUtilities.CreateDefaultRunSettings().CreateNavigator().OuterXml), new XmlReaderSettings() { CloseInput = true, DtdProcessing = DtdProcessing.Prohibit })) - { - doc.Load(reader); - } -#endif - } + var doc = RunSettingsUtilities.GetRunSettingXmlDocument(this.runSettingsManager); // Append / Override run settings supplied in CLI CreateOrOverwriteRunSettings(doc, arguments); // Set Active Run Settings. - var runSettings = new RunSettings(); - runSettings.LoadSettingsXml(doc.OuterXml); - this.runSettingsManager.SetActiveRunSettings(runSettings); + RunSettingsUtilities.SetRunSettingXmlDocument(this.runSettingsManager, doc); } catch (XPathException exception) { @@ -186,37 +159,8 @@ private void CreateOrOverwriteRunSettings(XmlDocument xmlDoc, string[] args) continue; } - // Check if the key exists. - var xPath = key.Replace('.', '/'); - var node = xmlDoc.SelectSingleNode(string.Format("//RunSettings/{0}", xPath)); - - if (node == null) - { - node = CreateNode(xmlDoc, key.Split('.')); - } - - node.InnerText = value; - } - } - - private XmlNode CreateNode(XmlDocument doc, string[] xPath) - { - XmlNode node = null; - XmlNode parent = doc.DocumentElement; - - for (int i = 0; i < xPath.Length; i++) - { - node = parent.SelectSingleNode(xPath[i]); - - if (node == null) - { - node = parent.AppendChild(doc.CreateElement(xPath[i])); - } - - parent = node; + RunSettingsUtilities.UpdateRunSettingsXmlDocument(xmlDoc, key, value); } - - return node; } } } diff --git a/src/vstest.console/Processors/FrameworkArgumentProcessor.cs b/src/vstest.console/Processors/FrameworkArgumentProcessor.cs index 1311afd957..00ea40395a 100644 --- a/src/vstest.console/Processors/FrameworkArgumentProcessor.cs +++ b/src/vstest.console/Processors/FrameworkArgumentProcessor.cs @@ -6,7 +6,11 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors using System; using System.Diagnostics.Contracts; using System.Globalization; + using Microsoft.VisualStudio.TestPlatform.ObjectModel; + using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; + using Microsoft.VisualStudio.TestPlatform.Common; + using Microsoft.VisualStudio.TestPlatform.Common.Interfaces; using CommandLineResources = Microsoft.VisualStudio.TestPlatform.CommandLine.Resources.Resources; @@ -54,7 +58,7 @@ public Lazy Executor { if (this.executor == null) { - this.executor = new Lazy(() => new FrameworkArgumentExecutor(CommandLineOptions.Instance)); + this.executor = new Lazy(() => new FrameworkArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance)); } return this.executor; @@ -94,6 +98,10 @@ internal class FrameworkArgumentExecutor : IArgumentExecutor /// private CommandLineOptions commandLineOptions; + private IRunSettingsProvider runSettingsManager; + + private const string RunSettingsPath = "RunConfiguration.TargetFrameworkVersion"; + #endregion #region Constructor @@ -101,14 +109,16 @@ internal class FrameworkArgumentExecutor : IArgumentExecutor /// /// Default constructor. /// - /// - /// The options. - /// - public FrameworkArgumentExecutor(CommandLineOptions options) + /// The options. + /// The runsettings manager. + public FrameworkArgumentExecutor(CommandLineOptions options, IRunSettingsProvider runSettingsManager) { Contract.Requires(options != null); + Contract.Requires(runSettingsManager != null); this.commandLineOptions = options; + this.runSettingsManager = runSettingsManager; } + #endregion #region IArgumentExecutor @@ -130,7 +140,8 @@ public void Initialize(string argument) throw new CommandLineException( string.Format(CultureInfo.CurrentCulture, CommandLineResources.InvalidFrameworkVersion, argument)); } - commandLineOptions.TargetFrameworkVersion = validFramework; + this.commandLineOptions.TargetFrameworkVersion = validFramework; + RunSettingsUtilities.UpdateRunSettings(this.runSettingsManager, FrameworkArgumentExecutor.RunSettingsPath, validFramework.ToString()); if (EqtTrace.IsInfoEnabled) { diff --git a/src/vstest.console/Processors/ParallelArgumentProcessor.cs b/src/vstest.console/Processors/ParallelArgumentProcessor.cs index 3ff835b7e0..8668113f4b 100644 --- a/src/vstest.console/Processors/ParallelArgumentProcessor.cs +++ b/src/vstest.console/Processors/ParallelArgumentProcessor.cs @@ -7,6 +7,10 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors using System.Diagnostics.Contracts; using System.Globalization; + using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; + using Microsoft.VisualStudio.TestPlatform.Common; + using Microsoft.VisualStudio.TestPlatform.Common.Interfaces; + using CommandLineResources = Microsoft.VisualStudio.TestPlatform.CommandLine.Resources.Resources; /// @@ -49,7 +53,7 @@ public Lazy Executor { if (this.executor == null) { - this.executor = new Lazy(() => new ParallelArgumentExecutor(CommandLineOptions.Instance)); + this.executor = new Lazy(() => new ParallelArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance)); } return this.executor; @@ -89,6 +93,10 @@ internal class ParallelArgumentExecutor : IArgumentExecutor /// private CommandLineOptions commandLineOptions; + private IRunSettingsProvider runSettingsManager; + + private const string RunSettingsPath = "RunConfiguration.MaxCpuCount"; + #endregion #region Constructor @@ -96,14 +104,16 @@ internal class ParallelArgumentExecutor : IArgumentExecutor /// /// Default constructor. /// - /// - /// The options. - /// - public ParallelArgumentExecutor(CommandLineOptions options) + /// The options. + /// The runsettings manager. + public ParallelArgumentExecutor(CommandLineOptions options, IRunSettingsProvider runSettingsManager) { Contract.Requires(options != null); + Contract.Requires(runSettingsManager != null); this.commandLineOptions = options; + this.runSettingsManager = runSettingsManager; } + #endregion #region IArgumentExecutor @@ -122,6 +132,7 @@ public void Initialize(string argument) } commandLineOptions.Parallel = true; + RunSettingsUtilities.UpdateRunSettings(this.runSettingsManager, ParallelArgumentExecutor.RunSettingsPath, "0"); } /// diff --git a/src/vstest.console/Processors/PlatformArgumentProcessor.cs b/src/vstest.console/Processors/PlatformArgumentProcessor.cs index e5dd73170b..fe617c49c7 100644 --- a/src/vstest.console/Processors/PlatformArgumentProcessor.cs +++ b/src/vstest.console/Processors/PlatformArgumentProcessor.cs @@ -1,6 +1,10 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; +using Microsoft.VisualStudio.TestPlatform.Common; +using Microsoft.VisualStudio.TestPlatform.Common.Interfaces; + namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors { using System; @@ -54,7 +58,7 @@ public Lazy Executor { if (this.executor == null) { - this.executor = new Lazy(() => new PlatformArgumentExecutor(CommandLineOptions.Instance)); + this.executor = new Lazy(() => new PlatformArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance)); } return this.executor; @@ -94,6 +98,10 @@ internal class PlatformArgumentExecutor : IArgumentExecutor /// private CommandLineOptions commandLineOptions; + private IRunSettingsProvider runSettingsManager; + + private const string RunSettingsPath = "RunConfiguration.TargetPlatform"; + #endregion #region Constructor @@ -101,14 +109,16 @@ internal class PlatformArgumentExecutor : IArgumentExecutor /// /// Default constructor. /// - /// - /// The options. - /// - public PlatformArgumentExecutor(CommandLineOptions options) + /// The options. + /// The runsettings manager. + public PlatformArgumentExecutor(CommandLineOptions options, IRunSettingsProvider runSettingsManager) { Contract.Requires(options != null); + Contract.Requires(runSettingsManager != null); this.commandLineOptions = options; + this.runSettingsManager = runSettingsManager; } + #endregion #region IArgumentExecutor @@ -134,6 +144,7 @@ public void Initialize(string argument) if (validPlatform) { this.commandLineOptions.TargetArchitecture = platform; + RunSettingsUtilities.UpdateRunSettings(this.runSettingsManager, PlatformArgumentExecutor.RunSettingsPath, platform.ToString()); } else { diff --git a/src/vstest.console/Processors/ResultsDirectoryArgumentProcessor.cs b/src/vstest.console/Processors/ResultsDirectoryArgumentProcessor.cs new file mode 100644 index 0000000000..87266d02fc --- /dev/null +++ b/src/vstest.console/Processors/ResultsDirectoryArgumentProcessor.cs @@ -0,0 +1,166 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Xml; +using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; +using Microsoft.VisualStudio.TestPlatform.Common; +using Microsoft.VisualStudio.TestPlatform.Common.Interfaces; +using Microsoft.VisualStudio.TestPlatform.ObjectModel; +using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities; + +namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors +{ + using System; + using System.Collections.Generic; + using System.Diagnostics.Contracts; + using System.Globalization; + using System.IO; + + using Microsoft.VisualStudio.TestPlatform.Client; + using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client; + using Microsoft.VisualStudio.TestPlatform.Utilities; + using CommandLineResources = Microsoft.VisualStudio.TestPlatform.CommandLine.Resources.Resources; + + /// + /// Allows the user to specify a path to save test results. + /// + internal class ResultsDirectoryArgumentProcessor : IArgumentProcessor + { + #region Constants + + /// + /// The name of the command line argument that the ListTestsArgumentExecutor handles. + /// + public const string CommandName = "/ResultsDirectory"; + + private const string RunSettingsPath = "RunConfiguration.ResultsDirectory"; + #endregion + + private Lazy metadata; + + private Lazy executor; + + /// + /// Gets the metadata. + /// + public Lazy Metadata + { + get + { + if (this.metadata == null) + { + this.metadata = new Lazy(() => new ResultsDirectoryArgumentProcessorCapabilities()); + } + + return this.metadata; + } + } + + /// + /// Gets or sets the executor. + /// + public Lazy Executor + { + get + { + if (this.executor == null) + { + this.executor = new Lazy(() => new ResultsDirectoryArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance)); + } + + return this.executor; + } + + set + { + this.executor = value; + } + } + } + + /// + /// The argument capabilities. + /// + internal class ResultsDirectoryArgumentProcessorCapabilities : BaseArgumentProcessorCapabilities + { + public override string CommandName => ResultsDirectoryArgumentProcessor.CommandName; + + public override bool AllowMultiple => false; + + public override bool IsAction => false; + + public override ArgumentProcessorPriority Priority => ArgumentProcessorPriority.AutoUpdateRunSettings; + + public override string HelpContentResourceName => CommandLineResources.ResultsDirectoryArgumentHelp; + + public override HelpContentPriority HelpPriority => HelpContentPriority.ResultsDirectoryArgumentProcessorHelpPriority; + } + + /// + /// The argument executor. + /// + internal class ResultsDirectoryArgumentExecutor : IArgumentExecutor + { + #region Fields + + /// + /// Used for getting sources. + /// + private CommandLineOptions commandLineOptions; + + private IRunSettingsProvider runSettingsManager; + + private const string RunSettingsPath = "RunConfiguration.ResultsDirectory"; + + #endregion + + #region Constructor + + /// + /// Default constructor. + /// + /// The options. + /// The test platform + public ResultsDirectoryArgumentExecutor(CommandLineOptions options, IRunSettingsProvider runSettingsManager) + { + Contract.Requires(options != null); + Contract.Requires(runSettingsManager != null); + + this.commandLineOptions = options; + this.runSettingsManager = runSettingsManager; + } + + #endregion + + #region IArgumentExecutor + + /// + /// Initializes with the argument that was provided with the command. + /// + /// Argument that was provided with the command. + public void Initialize(string argument) + { + if (string.IsNullOrWhiteSpace(argument)) + { + throw new CommandLineException(CommandLineResources.ResultsDirectoryValueRequired); + } + + //TODO validate TestResults Path + this.commandLineOptions.ResultsDirectory = argument; + + RunSettingsUtilities.UpdateRunSettings(this.runSettingsManager, ResultsDirectoryArgumentExecutor.RunSettingsPath, argument); + } + + /// + /// Executes the argument processor. + /// + /// The . + public ArgumentProcessorResult Execute() + { + // Nothing to do since we updated the parameter during initialize parameter + return ArgumentProcessorResult.Success; + } + + #endregion + } +} diff --git a/src/vstest.console/Processors/Utilities/ArgumentProcessorFactory.cs b/src/vstest.console/Processors/Utilities/ArgumentProcessorFactory.cs index e0683c555d..241ab9efbe 100644 --- a/src/vstest.console/Processors/Utilities/ArgumentProcessorFactory.cs +++ b/src/vstest.console/Processors/Utilities/ArgumentProcessorFactory.cs @@ -229,7 +229,8 @@ public IEnumerable GetArgumentProcessorsToAlwaysExecute() new EnableLoggerArgumentProcessor(), new ParallelArgumentProcessor(), new EnableDiagArgumentProcessor(), - new CLIRunSettingsArgumentProcessor() + new CLIRunSettingsArgumentProcessor(), + new ResultsDirectoryArgumentProcessor() }; /// diff --git a/src/vstest.console/Processors/Utilities/ArgumentProcessorPriority.cs b/src/vstest.console/Processors/Utilities/ArgumentProcessorPriority.cs index a551970e8d..6f9c4db1a4 100644 --- a/src/vstest.console/Processors/Utilities/ArgumentProcessorPriority.cs +++ b/src/vstest.console/Processors/Utilities/ArgumentProcessorPriority.cs @@ -33,14 +33,14 @@ internal enum ArgumentProcessorPriority TestAdapterPath = 1, /// - /// Priority of processors that needs to update runsettings. + /// Priority of processors related to Run Settings. /// - AutoUpdateRunSettings = 3, + RunSettings = 3, /// - /// Priority of processors related to Run Settings. + /// Priority of processors that needs to update runsettings. /// - RunSettings = 5, + AutoUpdateRunSettings = 5, /// /// Priority of processors related to CLI Run Settings. diff --git a/src/vstest.console/Processors/Utilities/HelpContentPriority.cs b/src/vstest.console/Processors/Utilities/HelpContentPriority.cs index 16b515bc95..8e5a12521d 100644 --- a/src/vstest.console/Processors/Utilities/HelpContentPriority.cs +++ b/src/vstest.console/Processors/Utilities/HelpContentPriority.cs @@ -68,6 +68,11 @@ internal enum HelpContentPriority /// ParallelArgumentProcessorHelpPriority, + /// + /// ResultsDirectoryArgumentProcessor Help + /// + ResultsDirectoryArgumentProcessorHelpPriority, + /// /// TestCaseFilterArgumentProcessor Help /// diff --git a/src/vstest.console/Processors/Utilities/RunSettingsUtilities.cs b/src/vstest.console/Processors/Utilities/RunSettingsUtilities.cs index 499c74eaa6..66178a551c 100644 --- a/src/vstest.console/Processors/Utilities/RunSettingsUtilities.cs +++ b/src/vstest.console/Processors/Utilities/RunSettingsUtilities.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using Microsoft.VisualStudio.TestPlatform.Common; + namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities { using System.Diagnostics.CodeAnalysis; @@ -34,7 +36,7 @@ internal static string GetRunSettings(IRunSettingsProvider runSettingsProvider, runSettings = EmptyRunSettings; } - runSettings = GetEffectiveRunSettings(runSettings, commandlineOptions); + //runSettings = GetEffectiveRunSettings(runSettings, commandlineOptions); return runSettings; } @@ -62,8 +64,12 @@ private static string GetEffectiveRunSettings(string runSettings, CommandLineOpt { framework = commandLineOptions.TargetFrameworkVersion; } + var resultsDirectory = Path.Combine(Directory.GetCurrentDirectory(), Constants.ResultsDirectoryName); - var defaultResultsDirectory = Path.Combine(Directory.GetCurrentDirectory(), Constants.ResultsDirectoryName); + if (commandLineOptions != null && !string.IsNullOrEmpty(commandLineOptions.ResultsDirectory)) + { + resultsDirectory = commandLineOptions.ResultsDirectory; + } using (var stream = new StringReader(runSettings)) using (var reader = XmlReader.Create(stream, XmlRunSettingsUtilities.ReaderSettings)) @@ -73,7 +79,7 @@ private static string GetEffectiveRunSettings(string runSettings, CommandLineOpt var navigator = document.CreateNavigator(); - InferRunSettingsHelper.UpdateRunSettingsWithUserProvidedSwitches(navigator, architecture, framework, defaultResultsDirectory); + InferRunSettingsHelper.UpdateRunSettingsWithUserProvidedSwitches(navigator, architecture, framework, resultsDirectory); if (commandLineOptions != null && commandLineOptions.Parallel) { @@ -83,5 +89,91 @@ private static string GetEffectiveRunSettings(string runSettings, CommandLineOpt return navigator.OuterXml; } } + + public static XmlNode CreateNode(XmlDocument doc, string[] xPath) + { + XmlNode node = null; + XmlNode parent = doc.DocumentElement; + + for (int i = 0; i < xPath.Length; i++) + { + node = parent.SelectSingleNode(xPath[i]); + + if (node == null) + { + node = parent.AppendChild(doc.CreateElement(xPath[i])); + } + + parent = node; + } + + return node; + } + + public static XmlDocument GetRunSettingXmlDocument(IRunSettingsProvider runSettingsManager) + { + var doc = new XmlDocument(); + + if (runSettingsManager.ActiveRunSettings != null && + !string.IsNullOrEmpty(runSettingsManager.ActiveRunSettings.SettingsXml)) + { + var settingsXml = runSettingsManager.ActiveRunSettings.SettingsXml; + +#if net46 + using (var reader = XmlReader.Create(new StringReader(settingsXml), new XmlReaderSettings() { XmlResolver = null, CloseInput = true, DtdProcessing = DtdProcessing.Prohibit })) + { +#else + using ( + var reader = XmlReader.Create(new StringReader(settingsXml), + new XmlReaderSettings() { CloseInput = true, DtdProcessing = DtdProcessing.Prohibit })) + { +#endif + doc.Load(reader); + } + } + else + { +#if net46 + doc = (XmlDocument)XmlRunSettingsUtilities.CreateDefaultRunSettings(); +#else + using ( + var reader = + XmlReader.Create( + new StringReader(XmlRunSettingsUtilities.CreateDefaultRunSettings().CreateNavigator().OuterXml), + new XmlReaderSettings() { CloseInput = true, DtdProcessing = DtdProcessing.Prohibit })) + { + doc.Load(reader); + } +#endif + } + return doc; + } + + public static void SetRunSettingXmlDocument(IRunSettingsProvider runSettingsManager, XmlDocument xmlDocument) + { + var runSettings = new RunSettings(); + runSettings.LoadSettingsXml(xmlDocument.OuterXml); + runSettingsManager.SetActiveRunSettings(runSettings); + } + + public static void UpdateRunSettingsXmlDocument(XmlDocument xmlDocument, string key, string value) + { + var xPath = key.Replace('.', '/'); + var node = xmlDocument.SelectSingleNode(string.Format("//RunSettings/{0}", xPath)); + + if (node == null) + { + node = RunSettingsUtilities.CreateNode(xmlDocument, key.Split('.')); + } + + node.InnerText = value; + } + + public static void UpdateRunSettings(IRunSettingsProvider runSettingsManager, string key, string value) + { + var xmlDocument = RunSettingsUtilities.GetRunSettingXmlDocument(runSettingsManager); + RunSettingsUtilities.UpdateRunSettingsXmlDocument(xmlDocument, key, value); + RunSettingsUtilities.SetRunSettingXmlDocument(runSettingsManager, xmlDocument); + } } } diff --git a/src/vstest.console/Resources/Resources.Designer.cs b/src/vstest.console/Resources/Resources.Designer.cs index 3cbe0f1e62..22e4df2b1e 100644 --- a/src/vstest.console/Resources/Resources.Designer.cs +++ b/src/vstest.console/Resources/Resources.Designer.cs @@ -1001,7 +1001,32 @@ public static string ProjectPathNotFound { return ResourceManager.GetString("ProjectPathNotFound", resourceCulture); } } - + + /// + /// Looks up a localized string similar to --ResultsDirectory|/ResultsDirectory + /// This makes vstest.console.exe process use custom test results directory + /// from a given path (if any) in the test run. + /// Example /ResultsDirectory:<pathToResultsDirectory>. + /// + public static string ResultsDirectoryArgumentHelp + { + get + { + return ResourceManager.GetString("ResultsDirectoryArgumentHelp", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The /ResultsDirectory parameter requires a value, which is path of a location test results directory. Example: /ResultsDirectory:"c:\path\to\TestResults". + /// + public static string ResultsDirectoryValueRequired + { + get + { + return ResourceManager.GetString("ResultsDirectoryValueRequired", resourceCulture); + } + } + /// /// Looks up a localized string similar to --Settings|/Settings:<Settings File> /// Settings to use when running tests.. diff --git a/src/vstest.console/Resources/Resources.resx b/src/vstest.console/Resources/Resources.resx index f657d11e6d..135c0b633f 100644 --- a/src/vstest.console/Resources/Resources.resx +++ b/src/vstest.console/Resources/Resources.resx @@ -623,4 +623,13 @@ One or more runsettings provided contain invalid token + + --ResultsDirectory|/ResultsDirectory + This makes vstest.console.exe process use custom test results directory + from a given path (if any) in the test run. + Example /ResultsDirectory:<pathToResultsDirectory> + + + The /ResultsDirectory parameter requires a value, which is path of a location test results directory. Example: /ResultsDirectory:"c:\path\to\TestResults" + \ No newline at end of file diff --git a/src/vstest.console/vstest.console.csproj b/src/vstest.console/vstest.console.csproj index d1c31038a8..94fbee354d 100644 --- a/src/vstest.console/vstest.console.csproj +++ b/src/vstest.console/vstest.console.csproj @@ -1,4 +1,4 @@ - + ..\..\ diff --git a/test/Microsoft.TestPlatform.AcceptanceTests/AcceptanceTestBase.cs b/test/Microsoft.TestPlatform.AcceptanceTests/AcceptanceTestBase.cs index ef3837f583..477f15a088 100644 --- a/test/Microsoft.TestPlatform.AcceptanceTests/AcceptanceTestBase.cs +++ b/test/Microsoft.TestPlatform.AcceptanceTests/AcceptanceTestBase.cs @@ -74,5 +74,27 @@ protected string GetTargetFramworkForRunsettings() return targetFramework; } + + protected string GetTestHostProcessName(string targetPlatform) + { + var testHostProcessName = string.Empty; + if (this.IsDesktopTargetFramework()) + { + if (string.Equals(targetPlatform, "x86", StringComparison.OrdinalIgnoreCase)) + { + testHostProcessName = "testhost.x86"; + } + else + { + testHostProcessName = "testhost"; + } + } + else + { + testHostProcessName = "dotnet"; + } + + return testHostProcessName; + } } } diff --git a/test/Microsoft.TestPlatform.AcceptanceTests/RunsettingsTests.cs b/test/Microsoft.TestPlatform.AcceptanceTests/RunsettingsTests.cs index 484cf7db8d..67d97b5e70 100644 --- a/test/Microsoft.TestPlatform.AcceptanceTests/RunsettingsTests.cs +++ b/test/Microsoft.TestPlatform.AcceptanceTests/RunsettingsTests.cs @@ -18,112 +18,62 @@ public class RunsettingsTests : AcceptanceTestBase [CustomDataTestMethod] [NET46TargetFramework] [NETCORETargetFramework] - public void RunTestExecutionWithRunSettingsWithoutParallelAndPlatformX86(string runnerFramework, string targetFramework, string targetRuntime) + public void RunSettingsWithoutParallelAndPlatformX86(string runnerFramework, string targetFramework, string targetRuntime) { AcceptanceTestBase.SetTestEnvironment(this.testEnvironment, runnerFramework, targetFramework, targetRuntime); - string testhostProcessName; - int expectedProcessCreated; - if (this.IsDesktopTargetFramework()) - { - testhostProcessName = "testhost.x86"; - expectedProcessCreated = 1; - } - else - { - testhostProcessName = "dotnet"; - if (this.IsDesktopRunner()) - { - expectedProcessCreated = 2; - } - else - { - // includes launcher dotnet process - expectedProcessCreated = 3; - } - } + var targetPlatform = "x86"; + var testhostProcessName = this.GetTestHostProcessName(targetPlatform); + var expectedNumOfProcessCreated = GetExpectedNumOfProcessCreatedForWithoutParallel(); var runConfigurationDictionary = new Dictionary { { "MaxCpuCount", "1" }, - { "TargetPlatform", "x86" }, + { "TargetPlatform", targetPlatform }, { "TargetFrameworkVersion", this.GetTargetFramworkForRunsettings() }, { "TestAdaptersPaths", this.GetTestAdapterPath() } }; - this.RunTestWithRunSettings(runConfigurationDictionary, testhostProcessName, expectedProcessCreated); + this.RunTestWithRunSettings(runConfigurationDictionary, testhostProcessName, expectedNumOfProcessCreated); } [CustomDataTestMethod] [NET46TargetFramework] [NETCORETargetFramework] - public void RunTestExecutionWithRunSettingsParamsAsArguments(string runnerFramework, string targetFramework, string targetRuntime) + public void RunSettingsParamsAsArguments(string runnerFramework, string targetFramework, string targetRuntime) { AcceptanceTestBase.SetTestEnvironment(this.testEnvironment, runnerFramework, targetFramework, targetRuntime); - string testhostProcessName; - int expectedProcessCreated; - if (this.IsDesktopTargetFramework()) - { - testhostProcessName = "testhost.x86"; - expectedProcessCreated = 1; - } - else - { - testhostProcessName = "dotnet"; - if (this.IsDesktopRunner()) - { - expectedProcessCreated = 2; - } - else - { - // includes launcher dotnet process - expectedProcessCreated = 3; - } - } + var targetPlatform = "x86"; + var testhostProcessName = this.GetTestHostProcessName(targetPlatform); + var expectedNumOfProcessCreated = GetExpectedNumOfProcessCreatedForWithoutParallel(); var runSettingsArgs = String.Join( " ", new string[] { - "RunConfiguration.MaxCpuCount=1", "RunConfiguration.TargetPlatform=x86", + "RunConfiguration.MaxCpuCount=1", + string.Concat("RunConfiguration.TargetPlatform=",targetPlatform), string.Concat("RunConfiguration.TargetFrameworkVersion=" , this.GetTargetFramworkForRunsettings()), string.Concat("RunConfiguration.TestAdaptersPaths=" , this.GetTestAdapterPath()) }); - this.RunTestWithRunSettingsAndRunSettingsParamsAsArguments(null, runSettingsArgs, testhostProcessName, expectedProcessCreated); + this.RunTestWithRunSettingsAndRunSettingsParamsAsArguments(null, runSettingsArgs, testhostProcessName, expectedNumOfProcessCreated); } [CustomDataTestMethod] [NET46TargetFramework] [NETCORETargetFramework] - public void RunTestExecutionWithRunSettingsAndRunSettingsParamsAsArguments(string runnerFramework, string targetFramework, string targetRuntime) + public void RunSettingsAndRunSettingsParamsAsArguments(string runnerFramework, string targetFramework, string targetRuntime) { AcceptanceTestBase.SetTestEnvironment(this.testEnvironment, runnerFramework, targetFramework, targetRuntime); - string testhostProcessName; - int expectedProcessCreated; - if (this.IsDesktopTargetFramework()) - { - testhostProcessName = "testhost.x86"; - expectedProcessCreated = 1; - } - else - { - testhostProcessName = "dotnet"; - if (this.IsDesktopRunner()) - { - expectedProcessCreated = 2; - } - else - { - // includes launcher dotnet process - expectedProcessCreated = 3; - } - } + var targetPlatform = "x86"; + var testhostProcessName = this.GetTestHostProcessName(targetPlatform); + var expectedNumOfProcessCreated = GetExpectedNumOfProcessCreatedForWithoutParallel(); var runConfigurationDictionary = new Dictionary { { "MaxCpuCount", "2" }, - { "TargetPlatform", "x86" }, + { "TargetPlatform", targetPlatform }, { "TargetFrameworkVersion", this.GetTargetFramworkForRunsettings() }, { "TestAdaptersPaths", this.GetTestAdapterPath() } }; @@ -132,40 +82,48 @@ public void RunTestExecutionWithRunSettingsAndRunSettingsParamsAsArguments(strin " ", new string[] { - "RunConfiguration.MaxCpuCount=1", "RunConfiguration.TargetPlatform=x86", + "RunConfiguration.MaxCpuCount=1", + string.Concat("RunConfiguration.TargetPlatform=",targetPlatform), string.Concat("RunConfiguration.TargetFrameworkVersion=" , this.GetTargetFramworkForRunsettings()), string.Concat("RunConfiguration.TestAdaptersPaths=" , this.GetTestAdapterPath()) }); - this.RunTestWithRunSettingsAndRunSettingsParamsAsArguments(runConfigurationDictionary, runSettingsArgs, testhostProcessName, expectedProcessCreated); + this.RunTestWithRunSettingsAndRunSettingsParamsAsArguments(runConfigurationDictionary, runSettingsArgs, testhostProcessName, expectedNumOfProcessCreated); + } + + private int GetExpectedNumOfProcessCreatedForWithoutParallel() + { + int expectedNumOfProcessCreated; + if (this.IsDesktopTargetFramework()) + { + expectedNumOfProcessCreated = 1; + } + else + { + expectedNumOfProcessCreated = this.IsDesktopRunner() ? 2 : 3; + } + return expectedNumOfProcessCreated; } [CustomDataTestMethod] [NET46TargetFramework] [NETCORETargetFramework] - public void RunTestExecutionWithRunSettingsWithParallelAndPlatformX64(string runnerFramework, string targetFramework, string targetRuntime) + public void RunSettingsWithParallelAndPlatformX64(string runnerFramework, string targetFramework, string targetRuntime) { AcceptanceTestBase.SetTestEnvironment(this.testEnvironment, runnerFramework, targetFramework, targetRuntime); - string testhostProcessName; - int expectedProcessCreated = 2; - if (this.IsDesktopTargetFramework()) - { - testhostProcessName = "testhost"; - } - else + var targetPlatform = "x64"; + var testhostProcessName = this.GetTestHostProcessName(targetPlatform); + var expectedProcessCreated = 2; + if (!this.IsDesktopTargetFramework() && !this.IsDesktopRunner()) { - testhostProcessName = "dotnet"; - if (!this.IsDesktopRunner()) - { - expectedProcessCreated = 3; - } + expectedProcessCreated = 3; } var runConfigurationDictionary = new Dictionary { { "MaxCpuCount", "2" }, - { "TargetPlatform", "x64" }, + { "TargetPlatform", targetPlatform }, { "TargetFrameworkVersion", this.GetTargetFramworkForRunsettings()}, { "TestAdaptersPaths", this.GetTestAdapterPath() } }; @@ -177,7 +135,7 @@ public void RunTestExecutionWithRunSettingsWithParallelAndPlatformX64(string run [CustomDataTestMethod] [NET46TargetFramework] [NETCORETargetFramework] - public void RunTestExecutionWithTestAdapterPathFromRunSettings(string runnerFramework, string targetFramework, string targetRuntime) + public void TestAdapterPathFromRunSettings(string runnerFramework, string targetFramework, string targetRuntime) { AcceptanceTestBase.SetTestEnvironment(this.testEnvironment, runnerFramework, targetFramework, targetRuntime); @@ -191,7 +149,6 @@ public void RunTestExecutionWithTestAdapterPathFromRunSettings(string runnerFram string.Empty, runsettingsFilePath, this.FrameworkArgValue); - this.InvokeVsTest(arguments); this.ValidateSummaryStatus(1, 1, 1); File.Delete(runsettingsFilePath); @@ -209,7 +166,7 @@ private string GetRunsettingsFilePath(Dictionary runConfiguratio private void RunTestWithRunSettings( Dictionary runConfigurationDictionary, string testhostProcessName, - int expectedProcessCreated) + int expectedNumOfProcessCreated) { var assemblyPaths = this.BuildMultipleAssemblyPath("SimpleTestProject.dll", "SimpleTestProject2.dll").Trim('\"'); @@ -224,9 +181,9 @@ private void RunTestWithRunSettings( cts.Cancel(); Assert.AreEqual( - expectedProcessCreated, + expectedNumOfProcessCreated, numOfProcessCreatedTask.Result, - $"Number of {testhostProcessName} process created, expected: {expectedProcessCreated} actual: {numOfProcessCreatedTask.Result} args: {arguments} runner path: {this.testEnvironment.GetConsoleRunnerPath()}"); + $"Number of {testhostProcessName} process created, expected: {expectedNumOfProcessCreated} actual: {numOfProcessCreatedTask.Result} args: {arguments} runner path: {this.testEnvironment.GetConsoleRunnerPath()}"); this.ValidateSummaryStatus(2, 2, 2); File.Delete(runsettingsPath); } @@ -235,11 +192,11 @@ private void RunTestWithRunSettingsAndRunSettingsParamsAsArguments( Dictionary runConfigurationDictionary, string runSettingsArgs, string testhostProcessName, - int expectedProcessCreated) + int expectedNumOfProcessCreated) { var assemblyPaths = this.BuildMultipleAssemblyPath("SimpleTestProject.dll", "SimpleTestProject2.dll").Trim('\"'); - string runsettingsPath = string.Empty; + var runsettingsPath = string.Empty; if (runConfigurationDictionary != null) { @@ -257,10 +214,12 @@ private void RunTestWithRunSettingsAndRunSettingsParamsAsArguments( cts.Cancel(); Assert.AreEqual( - expectedProcessCreated, + expectedNumOfProcessCreated, numOfProcessCreatedTask.Result, - $"Number of {testhostProcessName} process created, expected: {expectedProcessCreated} actual: {numOfProcessCreatedTask.Result} args: {arguments} runner path: {this.testEnvironment.GetConsoleRunnerPath()}"); + $"Number of {testhostProcessName} process created, expected: {expectedNumOfProcessCreated} actual: {numOfProcessCreatedTask.Result} args: {arguments} runner path: {this.testEnvironment.GetConsoleRunnerPath()}"); this.ValidateSummaryStatus(2, 2, 2); } + + // runsetting with framework } } diff --git a/test/Microsoft.TestPlatform.Common.UnitTests/Microsoft.TestPlatform.Common.UnitTests.csproj b/test/Microsoft.TestPlatform.Common.UnitTests/Microsoft.TestPlatform.Common.UnitTests.csproj index 702177b810..e61506db0b 100644 --- a/test/Microsoft.TestPlatform.Common.UnitTests/Microsoft.TestPlatform.Common.UnitTests.csproj +++ b/test/Microsoft.TestPlatform.Common.UnitTests/Microsoft.TestPlatform.Common.UnitTests.csproj @@ -1,22 +1,18 @@ - + $(MSBuildThisFileDirectory)../../ true - - Exe netcoreapp1.0;net46 Microsoft.TestPlatform.Common.UnitTests $(PackageTargetFallback);dnxcore50;portable-net45+win8 - - @@ -31,26 +27,25 @@ - 1.0.0 - - + + + - - + \ No newline at end of file diff --git a/test/Microsoft.TestPlatform.CommunicationUtilities.UnitTests/Microsoft.TestPlatform.CommunicationUtilities.UnitTests.csproj b/test/Microsoft.TestPlatform.CommunicationUtilities.UnitTests/Microsoft.TestPlatform.CommunicationUtilities.UnitTests.csproj index 765ce55e9e..9342224eae 100644 --- a/test/Microsoft.TestPlatform.CommunicationUtilities.UnitTests/Microsoft.TestPlatform.CommunicationUtilities.UnitTests.csproj +++ b/test/Microsoft.TestPlatform.CommunicationUtilities.UnitTests/Microsoft.TestPlatform.CommunicationUtilities.UnitTests.csproj @@ -4,18 +4,15 @@ true - Exe netcoreapp1.0;net46 Microsoft.TestPlatform.CommunicationUtilities.UnitTests $(PackageTargetFallback);dnxcore50;portable-net45+win8 - - @@ -28,19 +25,19 @@ true - 1.0.0 - - + + + - + \ No newline at end of file diff --git a/test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/Microsoft.TestPlatform.CrossPlatEngine.UnitTests.csproj b/test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/Microsoft.TestPlatform.CrossPlatEngine.UnitTests.csproj index cfddfa336d..c33a0cd024 100644 --- a/test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/Microsoft.TestPlatform.CrossPlatEngine.UnitTests.csproj +++ b/test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/Microsoft.TestPlatform.CrossPlatEngine.UnitTests.csproj @@ -4,18 +4,15 @@ true - Exe netcoreapp1.0;net46 Microsoft.TestPlatform.CrossPlatEngine.UnitTests $(PackageTargetFallback);dnxcore50;portable-net45+win8 - - @@ -28,19 +25,19 @@ true - 1.0.0 - + - - + + + - + \ No newline at end of file diff --git a/test/vstest.console.UnitTests/Processors/FrameworkArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/FrameworkArgumentProcessorTests.cs index c495f7e215..494ef3b62b 100644 --- a/test/vstest.console.UnitTests/Processors/FrameworkArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/FrameworkArgumentProcessorTests.cs @@ -3,6 +3,7 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors { + using Microsoft.VisualStudio.TestPlatform.Common; using Microsoft.VisualStudio.TestTools.UnitTesting; using TestPlatform.CommandLine.Processors; @@ -54,7 +55,7 @@ public void CapabilitiesShouldReturnAppropriateProperties() [TestMethod] public void InitializeShouldThrowIfArgumentIsNull() { - var executor = new FrameworkArgumentExecutor(CommandLineOptions.Instance); + var executor = new FrameworkArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); ExceptionUtilities.ThrowsException( () => executor.Initialize(null), @@ -64,7 +65,7 @@ public void InitializeShouldThrowIfArgumentIsNull() [TestMethod] public void InitializeShouldThrowIfArgumentIsEmpty() { - var executor = new FrameworkArgumentExecutor(CommandLineOptions.Instance); + var executor = new FrameworkArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); ExceptionUtilities.ThrowsException( () => executor.Initialize(" "), @@ -74,7 +75,7 @@ public void InitializeShouldThrowIfArgumentIsEmpty() [TestMethod] public void InitializeShouldThrowIfArgumentIsInvalid() { - var executor = new FrameworkArgumentExecutor(CommandLineOptions.Instance); + var executor = new FrameworkArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); ExceptionUtilities.ThrowsException( () => executor.Initialize("foo"), @@ -85,7 +86,7 @@ public void InitializeShouldThrowIfArgumentIsInvalid() [TestMethod] public void InitializeShouldSetCommandLineOptionsFramework() { - var executor = new FrameworkArgumentExecutor(CommandLineOptions.Instance); + var executor = new FrameworkArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); executor.Initialize(".NETCoreApp,Version=v1.0"); Assert.AreEqual(".NETCoreApp,Version=v1.0", CommandLineOptions.Instance.TargetFrameworkVersion.Name); @@ -94,7 +95,7 @@ public void InitializeShouldSetCommandLineOptionsFramework() [TestMethod] public void InitializeShouldSetCommandLineOptionsFrameworkForOlderFrameworks() { - var executor = new FrameworkArgumentExecutor(CommandLineOptions.Instance); + var executor = new FrameworkArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); executor.Initialize("Framework35"); Assert.AreEqual(".NETFramework,Version=v3.5", CommandLineOptions.Instance.TargetFrameworkVersion.Name); @@ -103,7 +104,7 @@ public void InitializeShouldSetCommandLineOptionsFrameworkForOlderFrameworks() [TestMethod] public void InitializeShouldSetCommandLineOptionsFrameworkForCaseInsensitiveFramework() { - var executor = new FrameworkArgumentExecutor(CommandLineOptions.Instance); + var executor = new FrameworkArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); executor.Initialize(".netcoreApp,Version=v1.0"); Assert.AreEqual(".netcoreApp,Version=v1.0", CommandLineOptions.Instance.TargetFrameworkVersion.Name); @@ -116,7 +117,7 @@ public void InitializeShouldSetCommandLineOptionsFrameworkForCaseInsensitiveFram [TestMethod] public void ExecuteShouldReturnSuccess() { - var executor = new FrameworkArgumentExecutor(CommandLineOptions.Instance); + var executor = new FrameworkArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); Assert.AreEqual(ArgumentProcessorResult.Success, executor.Execute()); } diff --git a/test/vstest.console.UnitTests/Processors/ParallelArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/ParallelArgumentProcessorTests.cs index f9a0f1c834..fd131840fd 100644 --- a/test/vstest.console.UnitTests/Processors/ParallelArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/ParallelArgumentProcessorTests.cs @@ -5,6 +5,7 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors { using Microsoft.VisualStudio.TestTools.UnitTesting; using TestPlatform.CommandLine.Processors; + using Microsoft.VisualStudio.TestPlatform.Common; [TestClass] public class ParallelArgumentProcessorTests @@ -54,7 +55,7 @@ public void CapabilitiesShouldReturnAppropriateProperties() [TestMethod] public void InitializeShouldThrowIfArgumentIsNonNull() { - var executor = new ParallelArgumentExecutor(CommandLineOptions.Instance); + var executor = new ParallelArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); // Parallel should not have any values or arguments ExceptionUtilities.ThrowsException( @@ -65,7 +66,7 @@ public void InitializeShouldThrowIfArgumentIsNonNull() [TestMethod] public void InitializeShouldSetParallelValue() { - var executor = new ParallelArgumentExecutor(CommandLineOptions.Instance); + var executor = new ParallelArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); executor.Initialize(null); Assert.IsTrue(CommandLineOptions.Instance.Parallel, "Parallel option must be set to true."); } @@ -77,7 +78,7 @@ public void InitializeShouldSetParallelValue() [TestMethod] public void ExecuteShouldReturnSuccess() { - var executor = new ParallelArgumentExecutor(CommandLineOptions.Instance); + var executor = new ParallelArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); Assert.AreEqual(ArgumentProcessorResult.Success, executor.Execute()); } diff --git a/test/vstest.console.UnitTests/Processors/PlatformArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/PlatformArgumentProcessorTests.cs index a38d159680..43b52f1ac6 100644 --- a/test/vstest.console.UnitTests/Processors/PlatformArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/PlatformArgumentProcessorTests.cs @@ -5,6 +5,7 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors { using Microsoft.VisualStudio.TestTools.UnitTesting; using TestPlatform.CommandLine.Processors; + using Microsoft.VisualStudio.TestPlatform.Common; [TestClass] public class PlatformArgumentProcessorTests @@ -54,7 +55,7 @@ public void CapabilitiesShouldReturnAppropriateProperties() [TestMethod] public void InitializeShouldThrowIfArgumentIsNull() { - var executor = new PlatformArgumentExecutor(CommandLineOptions.Instance); + var executor = new PlatformArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); ExceptionUtilities.ThrowsException( () => executor.Initialize(null), @@ -64,7 +65,7 @@ public void InitializeShouldThrowIfArgumentIsNull() [TestMethod] public void InitializeShouldThrowIfArgumentIsEmpty() { - var executor = new PlatformArgumentExecutor(CommandLineOptions.Instance); + var executor = new PlatformArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); ExceptionUtilities.ThrowsException( () => executor.Initialize(" "), @@ -74,7 +75,7 @@ public void InitializeShouldThrowIfArgumentIsEmpty() [TestMethod] public void InitializeShouldThrowIfArgumentIsNotAnArchitecture() { - var executor = new PlatformArgumentExecutor(CommandLineOptions.Instance); + var executor = new PlatformArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); ExceptionUtilities.ThrowsException( () => executor.Initialize("foo"), @@ -85,7 +86,7 @@ public void InitializeShouldThrowIfArgumentIsNotAnArchitecture() [TestMethod] public void InitializeShouldThrowIfArgumentIsNotASupportedArchitecture() { - var executor = new PlatformArgumentExecutor(CommandLineOptions.Instance); + var executor = new PlatformArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); ExceptionUtilities.ThrowsException( () => executor.Initialize("AnyCPU"), @@ -96,7 +97,7 @@ public void InitializeShouldThrowIfArgumentIsNotASupportedArchitecture() [TestMethod] public void InitializeShouldSetCommandLineOptionsArchitecture() { - var executor = new PlatformArgumentExecutor(CommandLineOptions.Instance); + var executor = new PlatformArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); executor.Initialize("x64"); Assert.AreEqual(ObjectModel.Architecture.X64, CommandLineOptions.Instance.TargetArchitecture); @@ -105,7 +106,7 @@ public void InitializeShouldSetCommandLineOptionsArchitecture() [TestMethod] public void InitializeShouldNotConsiderCaseSensitivityOfTheArgumentPassed() { - var executor = new PlatformArgumentExecutor(CommandLineOptions.Instance); + var executor = new PlatformArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); executor.Initialize("ArM"); Assert.AreEqual(ObjectModel.Architecture.ARM, CommandLineOptions.Instance.TargetArchitecture); @@ -118,7 +119,7 @@ public void InitializeShouldNotConsiderCaseSensitivityOfTheArgumentPassed() [TestMethod] public void ExecuteShouldReturnSuccess() { - var executor = new PlatformArgumentExecutor(CommandLineOptions.Instance); + var executor = new PlatformArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); Assert.AreEqual(ArgumentProcessorResult.Success, executor.Execute()); } diff --git a/test/vstest.console.UnitTests/vstest.console.UnitTests.csproj b/test/vstest.console.UnitTests/vstest.console.UnitTests.csproj index 995ede1750..3551a02a31 100644 --- a/test/vstest.console.UnitTests/vstest.console.UnitTests.csproj +++ b/test/vstest.console.UnitTests/vstest.console.UnitTests.csproj @@ -4,18 +4,15 @@ true - Exe netcoreapp1.0;net46 vstest.console.UnitTests $(PackageTargetFallback);dnxcore50;portable-net45+win8 - - @@ -38,13 +35,11 @@ true - 1.0.0 - + - @@ -52,6 +47,8 @@ - + + + - + \ No newline at end of file From 6084f8e991ce0933fe4e62d78a82455387d7095a Mon Sep 17 00:00:00 2001 From: samadala Date: Tue, 3 Jan 2017 21:51:00 +0530 Subject: [PATCH 02/10] Add runsettings precedence acceptance test --- .../RunsettingsTests.cs | 187 +++++++++++++----- 1 file changed, 138 insertions(+), 49 deletions(-) diff --git a/test/Microsoft.TestPlatform.AcceptanceTests/RunsettingsTests.cs b/test/Microsoft.TestPlatform.AcceptanceTests/RunsettingsTests.cs index 67d97b5e70..7497a3acc6 100644 --- a/test/Microsoft.TestPlatform.AcceptanceTests/RunsettingsTests.cs +++ b/test/Microsoft.TestPlatform.AcceptanceTests/RunsettingsTests.cs @@ -15,6 +15,107 @@ namespace Microsoft.TestPlatform.AcceptanceTests [TestClass] public class RunsettingsTests : AcceptanceTestBase { + #region Runsettings precedence tests + /// + /// Command line run settings should have high precedence among settings file, cli runsettings and cli switches + /// + [CustomDataTestMethod] + [NET46TargetFramework] + [NETCORETargetFramework] + public void CommandLineRunSettingsShouldWinAmongAllOptions(string runnerFramework, string targetFramework, string targetRuntime) + { + AcceptanceTestBase.SetTestEnvironment(this.testEnvironment, runnerFramework, targetFramework, targetRuntime); + + var targetPlatform = "x86"; + var testhostProcessName = this.GetTestHostProcessName(targetPlatform); + var expectedNumOfProcessCreated = GetExpectedNumOfProcessCreatedForWithoutParallel(); + + // passing parallel + var runConfigurationDictionary = new Dictionary + { + { "MaxCpuCount", "0" }, + { "TargetFrameworkVersion", this.GetTargetFramworkForRunsettings() }, + { "TestAdaptersPaths", this.GetTestAdapterPath() } + }; + // passing different platform + var additionalArgs = "/Platform:x64"; + + var runSettingsArgs = String.Join( + " ", + new string[] + { + "RunConfiguration.MaxCpuCount=1", + string.Concat("RunConfiguration.TargetPlatform=",targetPlatform), + string.Concat("RunConfiguration.TargetFrameworkVersion=" , this.GetTargetFramworkForRunsettings()), + string.Concat("RunConfiguration.TestAdaptersPaths=" , this.GetTestAdapterPath()) + }); + + this.RunTestWithRunSettings(runConfigurationDictionary, runSettingsArgs, additionalArgs, testhostProcessName, expectedNumOfProcessCreated); + } + + /// + /// Command line run settings should have high precedence btween cli runsettings and cli switches. + /// + [CustomDataTestMethod] + [NET46TargetFramework] + [NETCORETargetFramework] + public void CLIRunsettingsShouldWinBetweenCLISwitchesAndCLIRunsettings(string runnerFramework, string targetFramework, string targetRuntime) + { + AcceptanceTestBase.SetTestEnvironment(this.testEnvironment, runnerFramework, targetFramework, targetRuntime); + + var targetPlatform = "x86"; + var testhostProcessName = this.GetTestHostProcessName(targetPlatform); + var expectedNumOfProcessCreated = GetExpectedNumOfProcessCreatedForWithoutParallel(); + + // Pass parallel + var additionalArgs = "/Parallel"; + + // Pass non parallel + var runSettingsArgs = String.Join( + " ", + new string[] + { + "RunConfiguration.MaxCpuCount=1", + string.Concat("RunConfiguration.TargetPlatform=",targetPlatform), + string.Concat("RunConfiguration.TargetFrameworkVersion=" , this.GetTargetFramworkForRunsettings()), + string.Concat("RunConfiguration.TestAdaptersPaths=" , this.GetTestAdapterPath()) + }); + + this.RunTestWithRunSettings(null, runSettingsArgs, additionalArgs, testhostProcessName, expectedNumOfProcessCreated); + } + + /// + /// Command line switches should have high precedence if runsetting file and commandline switch specified + /// + /// + /// + /// + [CustomDataTestMethod] + [NET46TargetFramework] + [NETCORETargetFramework] + public void CommandLineSwitchesShouldWinBetweenSettingsFileAndCommandLineSwitches(string runnerFramework, string targetFramework, string targetRuntime) + { + AcceptanceTestBase.SetTestEnvironment(this.testEnvironment, runnerFramework, targetFramework, targetRuntime); + + var targetPlatform = "x86"; + var testhostProcessName = this.GetTestHostProcessName(targetPlatform); + var expectedNumOfProcessCreated = GetExpectedNumOfProcessCreatedForWithoutParallel(); + + // passing different platform + var runConfigurationDictionary = new Dictionary + { + { "MaxCpuCount", "1" }, + { "TargetPlatform", "x64" }, + { "TargetFrameworkVersion", this.GetTargetFramworkForRunsettings() }, + { "TestAdaptersPaths", this.GetTestAdapterPath() } + }; + var additionalArgs = "/Platform:x86"; + + this.RunTestWithRunSettings(runConfigurationDictionary, null, additionalArgs, testhostProcessName, expectedNumOfProcessCreated); + } + + #endregion + [CustomDataTestMethod] [NET46TargetFramework] [NETCORETargetFramework] @@ -33,7 +134,7 @@ public void RunSettingsWithoutParallelAndPlatformX86(string runnerFramework, str { "TargetFrameworkVersion", this.GetTargetFramworkForRunsettings() }, { "TestAdaptersPaths", this.GetTestAdapterPath() } }; - this.RunTestWithRunSettings(runConfigurationDictionary, testhostProcessName, expectedNumOfProcessCreated); + this.RunTestWithRunSettings(runConfigurationDictionary, null, null, testhostProcessName, expectedNumOfProcessCreated); } [CustomDataTestMethod] @@ -57,7 +158,7 @@ public void RunSettingsParamsAsArguments(string runnerFramework, string targetFr string.Concat("RunConfiguration.TestAdaptersPaths=" , this.GetTestAdapterPath()) }); - this.RunTestWithRunSettingsAndRunSettingsParamsAsArguments(null, runSettingsArgs, testhostProcessName, expectedNumOfProcessCreated); + this.RunTestWithRunSettings(null, runSettingsArgs, null, testhostProcessName, expectedNumOfProcessCreated); } [CustomDataTestMethod] @@ -88,21 +189,7 @@ public void RunSettingsAndRunSettingsParamsAsArguments(string runnerFramework, s string.Concat("RunConfiguration.TestAdaptersPaths=" , this.GetTestAdapterPath()) }); - this.RunTestWithRunSettingsAndRunSettingsParamsAsArguments(runConfigurationDictionary, runSettingsArgs, testhostProcessName, expectedNumOfProcessCreated); - } - - private int GetExpectedNumOfProcessCreatedForWithoutParallel() - { - int expectedNumOfProcessCreated; - if (this.IsDesktopTargetFramework()) - { - expectedNumOfProcessCreated = 1; - } - else - { - expectedNumOfProcessCreated = this.IsDesktopRunner() ? 2 : 3; - } - return expectedNumOfProcessCreated; + this.RunTestWithRunSettings(runConfigurationDictionary, runSettingsArgs, null, testhostProcessName, expectedNumOfProcessCreated); } [CustomDataTestMethod] @@ -127,7 +214,7 @@ public void RunSettingsWithParallelAndPlatformX64(string runnerFramework, string { "TargetFrameworkVersion", this.GetTargetFramworkForRunsettings()}, { "TestAdaptersPaths", this.GetTestAdapterPath() } }; - this.RunTestWithRunSettings(runConfigurationDictionary, testhostProcessName, expectedProcessCreated); + this.RunTestWithRunSettings(runConfigurationDictionary, null, null, testhostProcessName, expectedProcessCreated); } // Known issue https://github.com/Microsoft/vstest/issues/135 @@ -163,39 +250,12 @@ private string GetRunsettingsFilePath(Dictionary runConfiguratio return runsettingsPath; } - private void RunTestWithRunSettings( - Dictionary runConfigurationDictionary, - string testhostProcessName, - int expectedNumOfProcessCreated) + private void RunTestWithRunSettings(Dictionary runConfigurationDictionary, + string runSettingsArgs, string additionalArgs, string testhostProcessName, int expectedNumOfProcessCreated) { var assemblyPaths = this.BuildMultipleAssemblyPath("SimpleTestProject.dll", "SimpleTestProject2.dll").Trim('\"'); - var runsettingsPath = this.GetRunsettingsFilePath(runConfigurationDictionary); - var arguments = PrepareArguments(assemblyPaths, this.GetTestAdapterPath(), runsettingsPath, this.FrameworkArgValue); - var cts = new CancellationTokenSource(); - var numOfProcessCreatedTask = NumberOfProcessLaunchedUtility.NumberOfProcessCreated( - cts, - testhostProcessName); - - this.InvokeVsTest(arguments); - cts.Cancel(); - Assert.AreEqual( - expectedNumOfProcessCreated, - numOfProcessCreatedTask.Result, - $"Number of {testhostProcessName} process created, expected: {expectedNumOfProcessCreated} actual: {numOfProcessCreatedTask.Result} args: {arguments} runner path: {this.testEnvironment.GetConsoleRunnerPath()}"); - this.ValidateSummaryStatus(2, 2, 2); - File.Delete(runsettingsPath); - } - - private void RunTestWithRunSettingsAndRunSettingsParamsAsArguments( - Dictionary runConfigurationDictionary, - string runSettingsArgs, - string testhostProcessName, - int expectedNumOfProcessCreated) - { - var assemblyPaths = - this.BuildMultipleAssemblyPath("SimpleTestProject.dll", "SimpleTestProject2.dll").Trim('\"'); var runsettingsPath = string.Empty; if (runConfigurationDictionary != null) @@ -204,7 +264,17 @@ private void RunTestWithRunSettingsAndRunSettingsParamsAsArguments( } var arguments = PrepareArguments(assemblyPaths, this.GetTestAdapterPath(), runsettingsPath, this.FrameworkArgValue); - arguments = string.Concat(arguments, " -- ", runSettingsArgs); + + if (!string.IsNullOrWhiteSpace(runSettingsArgs)) + { + arguments = string.Concat(arguments, " -- ", runSettingsArgs); + } + + if (!string.IsNullOrWhiteSpace(additionalArgs)) + { + arguments = string.Concat(arguments, " ", additionalArgs); + } + var cts = new CancellationTokenSource(); var numOfProcessCreatedTask = NumberOfProcessLaunchedUtility.NumberOfProcessCreated( cts, @@ -213,13 +283,32 @@ private void RunTestWithRunSettingsAndRunSettingsParamsAsArguments( this.InvokeVsTest(arguments); cts.Cancel(); + // assert Assert.AreEqual( expectedNumOfProcessCreated, numOfProcessCreatedTask.Result, $"Number of {testhostProcessName} process created, expected: {expectedNumOfProcessCreated} actual: {numOfProcessCreatedTask.Result} args: {arguments} runner path: {this.testEnvironment.GetConsoleRunnerPath()}"); this.ValidateSummaryStatus(2, 2, 2); + + //cleanup + if (!string.IsNullOrWhiteSpace(runsettingsPath)) + { + File.Delete(runsettingsPath); + } } - // runsetting with framework + private int GetExpectedNumOfProcessCreatedForWithoutParallel() + { + int expectedNumOfProcessCreated; + if (this.IsDesktopTargetFramework()) + { + expectedNumOfProcessCreated = 1; + } + else + { + expectedNumOfProcessCreated = this.IsDesktopRunner() ? 2 : 3; + } + return expectedNumOfProcessCreated; + } } } From c5f59c75987c0e04a3fca9e7692ecfa9afac22f9 Mon Sep 17 00:00:00 2001 From: samadala Date: Wed, 4 Jan 2017 21:12:36 +0530 Subject: [PATCH 03/10] * Create default RunSettings before any argument process initialize * Add Acceptance test for ResultsDirectory argument * Update Unittest of runsettings related Argument process --- .../RunSettingsManager.cs | 5 + src/vstest.console/CommandLine/Executor.cs | 5 + .../CLIRunSettingsArgumentProcessor.cs | 2 +- .../Processors/FrameworkArgumentProcessor.cs | 4 +- .../Processors/ListTestsArgumentProcessor.cs | 3 +- .../Processors/ParallelArgumentProcessor.cs | 4 +- .../Processors/PlatformArgumentProcessor.cs | 4 +- .../ResultsDirectoryArgumentProcessor.cs | 21 +- .../RunSettingsArgumentProcessor.cs | 12 +- .../RunSpecificTestsArgumentProcessor.cs | 3 +- .../Processors/RunTestsArgumentProcessor.cs | 3 +- .../Utilities/RunSettingsUtilities.cs | 163 ++++++-------- .../ResultsDirectoryTests.cs | 61 ++++++ .../FrameworkArgumentProcessorTests.cs | 11 +- .../ParallelArgumentProcessorTests.cs | 5 +- .../PlatformArgumentProcessorTests.cs | 9 +- .../ResultsDirectoryArgumentProcessorTests.cs | 199 ++++++++++++++++++ .../RunSpecificTestsArgumentProcessorTests.cs | 17 ++ .../Utilities/RunSettingsUtilitiesTests.cs | 4 +- 19 files changed, 413 insertions(+), 122 deletions(-) create mode 100644 test/Microsoft.TestPlatform.AcceptanceTests/ResultsDirectoryTests.cs create mode 100644 test/vstest.console.UnitTests/Processors/ResultsDirectoryArgumentProcessorTests.cs diff --git a/src/Microsoft.TestPlatform.Common/RunSettingsManager.cs b/src/Microsoft.TestPlatform.Common/RunSettingsManager.cs index 89d06ead24..5319845fb5 100644 --- a/src/Microsoft.TestPlatform.Common/RunSettingsManager.cs +++ b/src/Microsoft.TestPlatform.Common/RunSettingsManager.cs @@ -78,6 +78,11 @@ public void SetActiveRunSettings(RunSettings runSettings) this.ActiveRunSettings = runSettings; } + public void Reset() + { + RunSettingsManager.runSettingsManagerInstance = null; + } + #endregion } } diff --git a/src/vstest.console/CommandLine/Executor.cs b/src/vstest.console/CommandLine/Executor.cs index aa9eaef58c..e2656b09e4 100644 --- a/src/vstest.console/CommandLine/Executor.cs +++ b/src/vstest.console/CommandLine/Executor.cs @@ -35,6 +35,8 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine using Microsoft.VisualStudio.TestPlatform.CoreUtilities.Tracing.Interfaces; using Microsoft.VisualStudio.TestPlatform.ObjectModel; using Microsoft.VisualStudio.TestPlatform.Utilities; + using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; + using Microsoft.VisualStudio.TestPlatform.Common; using CommandLineResources = Microsoft.VisualStudio.TestPlatform.CommandLine.Resources.Resources; @@ -190,6 +192,9 @@ private int GetArgumentProcessors(string[] args, out List pr var processorsToAlwaysExecute = processorFactory.GetArgumentProcessorsToAlwaysExecute(); processors.AddRange(processorsToAlwaysExecute); + // Initialize Runsettings with defaults + RunSettingsUtilities.AddDefaultRunSettings(RunSettingsManager.Instance); + // Ensure we have an action argument. this.EnsureActionArgumentIsPresent(processors, processorFactory); diff --git a/src/vstest.console/Processors/CLIRunSettingsArgumentProcessor.cs b/src/vstest.console/Processors/CLIRunSettingsArgumentProcessor.cs index 3638335706..4e03423d9a 100644 --- a/src/vstest.console/Processors/CLIRunSettingsArgumentProcessor.cs +++ b/src/vstest.console/Processors/CLIRunSettingsArgumentProcessor.cs @@ -121,7 +121,7 @@ public void Initialize(string[] arguments) CreateOrOverwriteRunSettings(doc, arguments); // Set Active Run Settings. - RunSettingsUtilities.SetRunSettingXmlDocument(this.runSettingsManager, doc); + RunSettingsUtilities.UpdateRunSettings(this.runSettingsManager, doc.OuterXml); } catch (XPathException exception) { diff --git a/src/vstest.console/Processors/FrameworkArgumentProcessor.cs b/src/vstest.console/Processors/FrameworkArgumentProcessor.cs index 00ea40395a..952b543c95 100644 --- a/src/vstest.console/Processors/FrameworkArgumentProcessor.cs +++ b/src/vstest.console/Processors/FrameworkArgumentProcessor.cs @@ -100,7 +100,7 @@ internal class FrameworkArgumentExecutor : IArgumentExecutor private IRunSettingsProvider runSettingsManager; - private const string RunSettingsPath = "RunConfiguration.TargetFrameworkVersion"; + public const string RunSettingsPath = "RunConfiguration.TargetFrameworkVersion"; #endregion @@ -141,7 +141,7 @@ public void Initialize(string argument) string.Format(CultureInfo.CurrentCulture, CommandLineResources.InvalidFrameworkVersion, argument)); } this.commandLineOptions.TargetFrameworkVersion = validFramework; - RunSettingsUtilities.UpdateRunSettings(this.runSettingsManager, FrameworkArgumentExecutor.RunSettingsPath, validFramework.ToString()); + RunSettingsUtilities.UpdateRunSettingsNode(this.runSettingsManager, FrameworkArgumentExecutor.RunSettingsPath, validFramework.ToString()); if (EqtTrace.IsInfoEnabled) { diff --git a/src/vstest.console/Processors/ListTestsArgumentProcessor.cs b/src/vstest.console/Processors/ListTestsArgumentProcessor.cs index 08a26c9fe5..770f4fc18f 100644 --- a/src/vstest.console/Processors/ListTestsArgumentProcessor.cs +++ b/src/vstest.console/Processors/ListTestsArgumentProcessor.cs @@ -199,6 +199,7 @@ public ArgumentProcessorResult Execute() { Contract.Assert(this.output != null); Contract.Assert(this.commandLineOptions != null); + Contract.Assert(!string.IsNullOrWhiteSpace(this.runSettingsManager.ActiveRunSettings.SettingsXml)); if (this.commandLineOptions.Sources.Count() <= 0) { @@ -207,7 +208,7 @@ public ArgumentProcessorResult Execute() this.output.WriteLine(CommandLineResources.ListTestsHeaderMessage, OutputLevel.Information); - var runSettings = RunSettingsUtilities.GetRunSettings(this.runSettingsManager, this.commandLineOptions); + var runSettings = this.runSettingsManager.ActiveRunSettings.SettingsXml; var success = this.testRequestManager.DiscoverTests( new DiscoveryRequestPayload() { Sources = this.commandLineOptions.Sources, RunSettings = runSettings }, diff --git a/src/vstest.console/Processors/ParallelArgumentProcessor.cs b/src/vstest.console/Processors/ParallelArgumentProcessor.cs index 8668113f4b..4f683a7b57 100644 --- a/src/vstest.console/Processors/ParallelArgumentProcessor.cs +++ b/src/vstest.console/Processors/ParallelArgumentProcessor.cs @@ -95,7 +95,7 @@ internal class ParallelArgumentExecutor : IArgumentExecutor private IRunSettingsProvider runSettingsManager; - private const string RunSettingsPath = "RunConfiguration.MaxCpuCount"; + public const string RunSettingsPath = "RunConfiguration.MaxCpuCount"; #endregion @@ -132,7 +132,7 @@ public void Initialize(string argument) } commandLineOptions.Parallel = true; - RunSettingsUtilities.UpdateRunSettings(this.runSettingsManager, ParallelArgumentExecutor.RunSettingsPath, "0"); + RunSettingsUtilities.UpdateRunSettingsNode(this.runSettingsManager, ParallelArgumentExecutor.RunSettingsPath, "0"); } /// diff --git a/src/vstest.console/Processors/PlatformArgumentProcessor.cs b/src/vstest.console/Processors/PlatformArgumentProcessor.cs index fe617c49c7..64a296291e 100644 --- a/src/vstest.console/Processors/PlatformArgumentProcessor.cs +++ b/src/vstest.console/Processors/PlatformArgumentProcessor.cs @@ -100,7 +100,7 @@ internal class PlatformArgumentExecutor : IArgumentExecutor private IRunSettingsProvider runSettingsManager; - private const string RunSettingsPath = "RunConfiguration.TargetPlatform"; + public const string RunSettingsPath = "RunConfiguration.TargetPlatform"; #endregion @@ -144,7 +144,7 @@ public void Initialize(string argument) if (validPlatform) { this.commandLineOptions.TargetArchitecture = platform; - RunSettingsUtilities.UpdateRunSettings(this.runSettingsManager, PlatformArgumentExecutor.RunSettingsPath, platform.ToString()); + RunSettingsUtilities.UpdateRunSettingsNode(this.runSettingsManager, PlatformArgumentExecutor.RunSettingsPath, platform.ToString()); } else { diff --git a/src/vstest.console/Processors/ResultsDirectoryArgumentProcessor.cs b/src/vstest.console/Processors/ResultsDirectoryArgumentProcessor.cs index 87266d02fc..5479878ed2 100644 --- a/src/vstest.console/Processors/ResultsDirectoryArgumentProcessor.cs +++ b/src/vstest.console/Processors/ResultsDirectoryArgumentProcessor.cs @@ -110,7 +110,7 @@ internal class ResultsDirectoryArgumentExecutor : IArgumentExecutor private IRunSettingsProvider runSettingsManager; - private const string RunSettingsPath = "RunConfiguration.ResultsDirectory"; + public const string RunSettingsPath = "RunConfiguration.ResultsDirectory"; #endregion @@ -145,10 +145,23 @@ public void Initialize(string argument) throw new CommandLineException(CommandLineResources.ResultsDirectoryValueRequired); } - //TODO validate TestResults Path - this.commandLineOptions.ResultsDirectory = argument; + try + { + // to check valid directory path + var di = new DirectoryInfo(argument); - RunSettingsUtilities.UpdateRunSettings(this.runSettingsManager, ResultsDirectoryArgumentExecutor.RunSettingsPath, argument); + if (!Path.IsPathRooted(argument)) + { + argument = Path.GetFullPath(argument); + } + } + catch (Exception) + { + throw new CommandLineException(CommandLineResources.ResultsDirectoryValueRequired); + } + + this.commandLineOptions.ResultsDirectory = argument; + RunSettingsUtilities.UpdateRunSettingsNode(this.runSettingsManager, ResultsDirectoryArgumentExecutor.RunSettingsPath, argument); } /// diff --git a/src/vstest.console/Processors/RunSettingsArgumentProcessor.cs b/src/vstest.console/Processors/RunSettingsArgumentProcessor.cs index 0c217e860a..bdc6a2e156 100644 --- a/src/vstest.console/Processors/RunSettingsArgumentProcessor.cs +++ b/src/vstest.console/Processors/RunSettingsArgumentProcessor.cs @@ -17,6 +17,7 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors using Microsoft.VisualStudio.TestPlatform.Utilities; using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers; using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces; + using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; using CommandLineResources = Microsoft.VisualStudio.TestPlatform.CommandLine.Resources.Resources; @@ -119,18 +120,15 @@ public void Initialize(string argument) } Contract.EndContractBlock(); - + // Load up the run settings and set it as the active run settings. try { IXPathNavigable document = this.GetRunSettingsDocument(argument); - - var runSettings = new RunSettings(); - - // Currently do not see the need to load the settings providers in the console process. - runSettings.LoadSettingsXml(document.CreateNavigator().OuterXml); + RunSettingsUtilities.UpdateRunSettings(this.runSettingsManager, document.CreateNavigator().OuterXml); - this.runSettingsManager.SetActiveRunSettings(runSettings); + //Add default runsettings values if not exists in given runsettings file. + RunSettingsUtilities.AddDefaultRunSettings(this.runSettingsManager); } catch (XmlException exception) { diff --git a/src/vstest.console/Processors/RunSpecificTestsArgumentProcessor.cs b/src/vstest.console/Processors/RunSpecificTestsArgumentProcessor.cs index 9c4b6df55c..63d4ea67b4 100644 --- a/src/vstest.console/Processors/RunSpecificTestsArgumentProcessor.cs +++ b/src/vstest.console/Processors/RunSpecificTestsArgumentProcessor.cs @@ -190,6 +190,7 @@ public ArgumentProcessorResult Execute() Contract.Assert(this.output != null); Contract.Assert(this.commandLineOptions != null); Contract.Assert(this.testRequestManager != null); + Contract.Assert(!string.IsNullOrWhiteSpace(this.runSettingsManager.ActiveRunSettings.SettingsXml)); if (this.commandLineOptions.Sources.Count() <= 0) { @@ -203,7 +204,7 @@ public ArgumentProcessorResult Execute() bool result = false; - this.effectiveRunSettings = RunSettingsUtilities.GetRunSettings(this.runSettingsManager, this.commandLineOptions); + this.effectiveRunSettings = this.runSettingsManager.ActiveRunSettings.SettingsXml; // Discover tests from sources and filter on every discovery reported. result = this.DiscoverTestsAndSelectSpecified(this.commandLineOptions.Sources); diff --git a/src/vstest.console/Processors/RunTestsArgumentProcessor.cs b/src/vstest.console/Processors/RunTestsArgumentProcessor.cs index cffa9c0837..583f2102fb 100644 --- a/src/vstest.console/Processors/RunTestsArgumentProcessor.cs +++ b/src/vstest.console/Processors/RunTestsArgumentProcessor.cs @@ -146,6 +146,7 @@ public void Initialize(string argument) public ArgumentProcessorResult Execute() { Contract.Assert(this.commandLineOptions != null); + Contract.Assert(!string.IsNullOrWhiteSpace(this.runSettingsManager.ActiveRunSettings.SettingsXml)); // Ensure a test source file was provided var anySource = this.commandLineOptions.Sources.FirstOrDefault(); @@ -173,7 +174,7 @@ private bool RunTests(IEnumerable sources) EqtTrace.Info("RunTestsArgumentProcessor:Execute: Test run is starting."); } - var runSettings = RunSettingsUtilities.GetRunSettings(this.runSettingsManager, this.commandLineOptions); + var runSettings = this.runSettingsManager.ActiveRunSettings.SettingsXml; if (EqtTrace.IsVerboseEnabled) { diff --git a/src/vstest.console/Processors/Utilities/RunSettingsUtilities.cs b/src/vstest.console/Processors/Utilities/RunSettingsUtilities.cs index 66178a551c..b8da65547f 100644 --- a/src/vstest.console/Processors/Utilities/RunSettingsUtilities.cs +++ b/src/vstest.console/Processors/Utilities/RunSettingsUtilities.cs @@ -21,93 +21,17 @@ internal class RunSettingsUtilities { private const string EmptyRunSettings = @""; - /// - /// Gets the run settings to be used for the session. - /// - /// The current provider of run settings. - /// The command line options specified. - /// - internal static string GetRunSettings(IRunSettingsProvider runSettingsProvider, CommandLineOptions commandlineOptions) + public static void AddDefaultRunSettings(IRunSettingsProvider runSettingsProvider) { - var runSettings = runSettingsProvider?.ActiveRunSettings?.SettingsXml; + var runSettingsXml = runSettingsProvider?.ActiveRunSettings?.SettingsXml; - if (string.IsNullOrWhiteSpace(runSettings)) + if (string.IsNullOrWhiteSpace(runSettingsXml)) { - runSettings = EmptyRunSettings; + runSettingsXml = EmptyRunSettings; } - //runSettings = GetEffectiveRunSettings(runSettings, commandlineOptions); - - return runSettings; - } - - /// - /// Gets the effective run settings adding the commandline options to the run settings if not already present. - /// - /// The run settings XML. - /// The command line options. - /// Effective run settings. - [SuppressMessage("Microsoft.Security.Xml", "CA3053:UseXmlSecureResolver", - Justification = "XmlDocument.XmlResolver is not available in core. Suppress until fxcop issue is fixed.")] - private static string GetEffectiveRunSettings(string runSettings, CommandLineOptions commandLineOptions) - { - var architecture = Constants.DefaultPlatform; - - if (commandLineOptions != null && commandLineOptions.ArchitectureSpecified) - { - architecture = commandLineOptions.TargetArchitecture; - } - - var framework = Framework.DefaultFramework; - - if (commandLineOptions != null && commandLineOptions.FrameworkVersionSpecified) - { - framework = commandLineOptions.TargetFrameworkVersion; - } - var resultsDirectory = Path.Combine(Directory.GetCurrentDirectory(), Constants.ResultsDirectoryName); - - if (commandLineOptions != null && !string.IsNullOrEmpty(commandLineOptions.ResultsDirectory)) - { - resultsDirectory = commandLineOptions.ResultsDirectory; - } - - using (var stream = new StringReader(runSettings)) - using (var reader = XmlReader.Create(stream, XmlRunSettingsUtilities.ReaderSettings)) - { - var document = new XmlDocument(); - document.Load(reader); - - var navigator = document.CreateNavigator(); - - InferRunSettingsHelper.UpdateRunSettingsWithUserProvidedSwitches(navigator, architecture, framework, resultsDirectory); - - if (commandLineOptions != null && commandLineOptions.Parallel) - { - ParallelRunSettingsUtilities.UpdateRunSettingsWithParallelSettingIfNotConfigured(navigator); - } - - return navigator.OuterXml; - } - } - - public static XmlNode CreateNode(XmlDocument doc, string[] xPath) - { - XmlNode node = null; - XmlNode parent = doc.DocumentElement; - - for (int i = 0; i < xPath.Length; i++) - { - node = parent.SelectSingleNode(xPath[i]); - - if (node == null) - { - node = parent.AppendChild(doc.CreateElement(xPath[i])); - } - - parent = node; - } - - return node; + runSettingsXml = AddDefaultRunSettings(runSettingsXml); + RunSettingsUtilities.UpdateRunSettings(runSettingsProvider, runSettingsXml); } public static XmlDocument GetRunSettingXmlDocument(IRunSettingsProvider runSettingsManager) @@ -149,31 +73,84 @@ public static XmlDocument GetRunSettingXmlDocument(IRunSettingsProvider runSetti return doc; } - public static void SetRunSettingXmlDocument(IRunSettingsProvider runSettingsManager, XmlDocument xmlDocument) + public static void UpdateRunSettings(IRunSettingsProvider runSettingsManager, string runsettingsXml) { var runSettings = new RunSettings(); - runSettings.LoadSettingsXml(xmlDocument.OuterXml); + runSettings.LoadSettingsXml(runsettingsXml); runSettingsManager.SetActiveRunSettings(runSettings); } public static void UpdateRunSettingsXmlDocument(XmlDocument xmlDocument, string key, string value) { - var xPath = key.Replace('.', '/'); - var node = xmlDocument.SelectSingleNode(string.Format("//RunSettings/{0}", xPath)); + var node = GetXmlNode(xmlDocument, key) ?? RunSettingsUtilities.CreateNode(xmlDocument, key.Split('.')); + node.InnerText = value; + } + + public static void UpdateRunSettingsNode(IRunSettingsProvider runSettingsManager, string key, string value) + { + var xmlDocument = RunSettingsUtilities.GetRunSettingXmlDocument(runSettingsManager); + RunSettingsUtilities.UpdateRunSettingsXmlDocument(xmlDocument, key, value); + RunSettingsUtilities.UpdateRunSettings(runSettingsManager, xmlDocument.OuterXml); + } + + public static string QueryRunSettingsNode(IRunSettingsProvider runSettingsManager, string key) + { + var xmlDocument = RunSettingsUtilities.GetRunSettingXmlDocument(runSettingsManager); + var node = GetXmlNode(xmlDocument, key); + return node?.InnerText; + } + + /// + /// Gets the effective run settings adding the default run settings if not already present. + /// + /// The run settings XML. + /// Effective run settings. + [SuppressMessage("Microsoft.Security.Xml", "CA3053:UseXmlSecureResolver", + Justification = "XmlDocument.XmlResolver is not available in core. Suppress until fxcop issue is fixed.")] + private static string AddDefaultRunSettings(string runSettings) + { + var architecture = Constants.DefaultPlatform; + var framework = Framework.DefaultFramework; + var defaultResultsDirectory = Path.Combine(Directory.GetCurrentDirectory(), Constants.ResultsDirectoryName); + + using (var stream = new StringReader(runSettings)) + using (var reader = XmlReader.Create(stream, XmlRunSettingsUtilities.ReaderSettings)) + { + var document = new XmlDocument(); + document.Load(reader); + + var navigator = document.CreateNavigator(); + + InferRunSettingsHelper.UpdateRunSettingsWithUserProvidedSwitches(navigator, architecture, framework, defaultResultsDirectory); + return navigator.OuterXml; + } + } - if (node == null) + private static XmlNode CreateNode(XmlDocument doc, string[] xPath) + { + XmlNode node = null; + XmlNode parent = doc.DocumentElement; + + for (int i = 0; i < xPath.Length; i++) { - node = RunSettingsUtilities.CreateNode(xmlDocument, key.Split('.')); + node = parent.SelectSingleNode(xPath[i]); + + if (node == null) + { + node = parent.AppendChild(doc.CreateElement(xPath[i])); + } + + parent = node; } - node.InnerText = value; + return node; } - public static void UpdateRunSettings(IRunSettingsProvider runSettingsManager, string key, string value) + private static XmlNode GetXmlNode(XmlDocument xmlDocument, string key) { - var xmlDocument = RunSettingsUtilities.GetRunSettingXmlDocument(runSettingsManager); - RunSettingsUtilities.UpdateRunSettingsXmlDocument(xmlDocument, key, value); - RunSettingsUtilities.SetRunSettingXmlDocument(runSettingsManager, xmlDocument); + var xPath = key.Replace('.', '/'); + var node = xmlDocument.SelectSingleNode(string.Format("//RunSettings/{0}", xPath)); + return node; } } } diff --git a/test/Microsoft.TestPlatform.AcceptanceTests/ResultsDirectoryTests.cs b/test/Microsoft.TestPlatform.AcceptanceTests/ResultsDirectoryTests.cs new file mode 100644 index 0000000000..4273765092 --- /dev/null +++ b/test/Microsoft.TestPlatform.AcceptanceTests/ResultsDirectoryTests.cs @@ -0,0 +1,61 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Microsoft.TestPlatform.AcceptanceTests +{ + using System.IO; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class ResultsDirectoryTests : AcceptanceTestBase + { + [CustomDataTestMethod] + [NET46TargetFramework] + [NETCORETargetFramework] + public void TrxFileShouldBeCreatedInResultsDirectory(string runnerFramework, string targetFramework, string targetRuntime) + { + AcceptanceTestBase.SetTestEnvironment(this.testEnvironment, runnerFramework, targetFramework, targetRuntime); + var arguments = PrepareArguments(this.GetSampleTestAssembly(), this.GetTestAdapterPath(), string.Empty, this.FrameworkArgValue); + var trxFileName = "TestResults.trx"; + var resultsDir = Path.GetTempPath(); + var trxFilePath = Path.Combine(resultsDir, trxFileName); + arguments = string.Concat(arguments, $" /logger:\"trx;LogFileName={trxFileName}\""); + arguments = string.Concat(arguments, $" /ResultsDirectory:{resultsDir}"); + + // Delete if already exists + File.Delete(trxFilePath); + + this.InvokeVsTest(arguments); + + Assert.IsTrue(File.Exists(trxFilePath), $"Expexted Trx file: {trxFilePath} not created in results directory"); + } + + //Getting Current directory C:\Windows\system32 https://github.com/Microsoft/vstest/issues/311 + //Failed create relative directory + [Ignore] + [CustomDataTestMethod] + [NET46TargetFramework] + [NETCORETargetFramework] + public void ResultsDirectoryRelativePathShouldWork(string runnerFramework, string targetFramework, string targetRuntime) + { + AcceptanceTestBase.SetTestEnvironment(this.testEnvironment, runnerFramework, targetFramework, targetRuntime); + var arguments = PrepareArguments(this.GetSampleTestAssembly(), this.GetTestAdapterPath(), string.Empty, this.FrameworkArgValue); + var trxFileName = "TestResults.trx"; + var relativeDirectory = @"relative\directory"; + var resultsDirectory = Path.Combine(Directory.GetCurrentDirectory(), relativeDirectory); + + var trxFilePath = Path.Combine(resultsDirectory , trxFileName); + arguments = string.Concat(arguments, $" /logger:\"trx;LogFileName={trxFileName}\""); + arguments = string.Concat(arguments, $" /ResultsDirectory:{relativeDirectory}"); + + if (Directory.Exists(resultsDirectory)) + { + Directory.Delete(resultsDirectory); + } + + this.InvokeVsTest(arguments); + + Assert.IsTrue(File.Exists(trxFilePath), $"Expexted Trx file: {trxFilePath} not created in results directory"); + } + } +} diff --git a/test/vstest.console.UnitTests/Processors/FrameworkArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/FrameworkArgumentProcessorTests.cs index 494ef3b62b..b18005d5e2 100644 --- a/test/vstest.console.UnitTests/Processors/FrameworkArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/FrameworkArgumentProcessorTests.cs @@ -6,6 +6,7 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors using Microsoft.VisualStudio.TestPlatform.Common; using Microsoft.VisualStudio.TestTools.UnitTesting; using TestPlatform.CommandLine.Processors; + using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; [TestClass] public class FrameworkArgumentProcessorTests @@ -14,6 +15,7 @@ public class FrameworkArgumentProcessorTests public void TestCleanup() { CommandLineOptions.Instance.Reset(); + RunSettingsManager.Instance.Reset(); } [TestMethod] @@ -24,7 +26,7 @@ public void GetMetadataShouldReturnFrameworkArgumentProcessorCapabilities() } [TestMethod] - public void GetExecuterShouldReturnFrameworkArgumentProcessorCapabilities() + public void GetExecuterShouldReturnFrameworkArgumentExecutor() { var processor = new FrameworkArgumentProcessor(); Assert.IsTrue(processor.Executor.Value is FrameworkArgumentExecutor); @@ -82,14 +84,15 @@ public void InitializeShouldThrowIfArgumentIsInvalid() "Invalid .Net Framework version:{0}. Please give the fullname of the TargetFramework. Other supported .Net Framework versions are Framework35, Framework40 and Framework45.", "foo"); } - + [TestMethod] - public void InitializeShouldSetCommandLineOptionsFramework() + public void InitializeShouldSetCommandLineOptionsAndRunSettingsFramework() { var executor = new FrameworkArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); executor.Initialize(".NETCoreApp,Version=v1.0"); Assert.AreEqual(".NETCoreApp,Version=v1.0", CommandLineOptions.Instance.TargetFrameworkVersion.Name); + Assert.AreEqual(".NETCoreApp,Version=v1.0", RunSettingsUtilities.QueryRunSettingsNode(RunSettingsManager.Instance, FrameworkArgumentExecutor.RunSettingsPath)); } [TestMethod] @@ -99,6 +102,7 @@ public void InitializeShouldSetCommandLineOptionsFrameworkForOlderFrameworks() executor.Initialize("Framework35"); Assert.AreEqual(".NETFramework,Version=v3.5", CommandLineOptions.Instance.TargetFrameworkVersion.Name); + Assert.AreEqual(".NETFramework,Version=v3.5", RunSettingsUtilities.QueryRunSettingsNode(RunSettingsManager.Instance, FrameworkArgumentExecutor.RunSettingsPath)); } [TestMethod] @@ -108,6 +112,7 @@ public void InitializeShouldSetCommandLineOptionsFrameworkForCaseInsensitiveFram executor.Initialize(".netcoreApp,Version=v1.0"); Assert.AreEqual(".netcoreApp,Version=v1.0", CommandLineOptions.Instance.TargetFrameworkVersion.Name); + Assert.AreEqual(".netcoreApp,Version=v1.0", RunSettingsUtilities.QueryRunSettingsNode(RunSettingsManager.Instance, FrameworkArgumentExecutor.RunSettingsPath)); } #endregion diff --git a/test/vstest.console.UnitTests/Processors/ParallelArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/ParallelArgumentProcessorTests.cs index fd131840fd..a3d5a4184c 100644 --- a/test/vstest.console.UnitTests/Processors/ParallelArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/ParallelArgumentProcessorTests.cs @@ -6,6 +6,7 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors using Microsoft.VisualStudio.TestTools.UnitTesting; using TestPlatform.CommandLine.Processors; using Microsoft.VisualStudio.TestPlatform.Common; + using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; [TestClass] public class ParallelArgumentProcessorTests @@ -14,6 +15,7 @@ public class ParallelArgumentProcessorTests public void TestCleanup() { CommandLineOptions.Instance.Reset(); + RunSettingsManager.Instance.Reset(); } [TestMethod] @@ -24,7 +26,7 @@ public void GetMetadataShouldReturnParallelArgumentProcessorCapabilities() } [TestMethod] - public void GetExecuterShouldReturnPlatformArgumentProcessorCapabilities() + public void GetExecuterShouldReturnParallelArgumentExecutor() { var processor = new ParallelArgumentProcessor(); Assert.IsTrue(processor.Executor.Value is ParallelArgumentExecutor); @@ -69,6 +71,7 @@ public void InitializeShouldSetParallelValue() var executor = new ParallelArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); executor.Initialize(null); Assert.IsTrue(CommandLineOptions.Instance.Parallel, "Parallel option must be set to true."); + Assert.AreEqual("0", RunSettingsUtilities.QueryRunSettingsNode(RunSettingsManager.Instance, ParallelArgumentExecutor.RunSettingsPath)); } #endregion diff --git a/test/vstest.console.UnitTests/Processors/PlatformArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/PlatformArgumentProcessorTests.cs index 43b52f1ac6..629ed4e53c 100644 --- a/test/vstest.console.UnitTests/Processors/PlatformArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/PlatformArgumentProcessorTests.cs @@ -3,9 +3,11 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors { + using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using TestPlatform.CommandLine.Processors; using Microsoft.VisualStudio.TestPlatform.Common; + using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; [TestClass] public class PlatformArgumentProcessorTests @@ -14,6 +16,7 @@ public class PlatformArgumentProcessorTests public void TestCleanup() { CommandLineOptions.Instance.Reset(); + RunSettingsManager.Instance.Reset(); } [TestMethod] @@ -24,7 +27,7 @@ public void GetMetadataShouldReturnPlatformArgumentProcessorCapabilities() } [TestMethod] - public void GetExecuterShouldReturnPlatformArgumentProcessorCapabilities() + public void GetExecuterShouldReturnPlatformArgumentExecutor() { var processor = new PlatformArgumentProcessor(); Assert.IsTrue(processor.Executor.Value is PlatformArgumentExecutor); @@ -37,7 +40,7 @@ public void CapabilitiesShouldReturnAppropriateProperties() { var capabilities = new PlatformArgumentProcessorCapabilities(); Assert.AreEqual("/Platform", capabilities.CommandName); - Assert.AreEqual("--Platform|/Platform:\n Target platform architecture to be used for test execution. \n Valid values are x86, x64 and ARM.", capabilities.HelpContentResourceName); + Assert.AreEqual("--Platform|/Platform:" + Environment.NewLine + " Target platform architecture to be used for test execution. " + Environment.NewLine + " Valid values are x86, x64 and ARM.", capabilities.HelpContentResourceName); Assert.AreEqual(HelpContentPriority.PlatformArgumentProcessorHelpPriority, capabilities.HelpPriority); Assert.AreEqual(false, capabilities.IsAction); @@ -101,6 +104,7 @@ public void InitializeShouldSetCommandLineOptionsArchitecture() executor.Initialize("x64"); Assert.AreEqual(ObjectModel.Architecture.X64, CommandLineOptions.Instance.TargetArchitecture); + Assert.AreEqual(ObjectModel.Architecture.X64.ToString(), RunSettingsUtilities.QueryRunSettingsNode(RunSettingsManager.Instance, PlatformArgumentExecutor.RunSettingsPath)); } [TestMethod] @@ -110,6 +114,7 @@ public void InitializeShouldNotConsiderCaseSensitivityOfTheArgumentPassed() executor.Initialize("ArM"); Assert.AreEqual(ObjectModel.Architecture.ARM, CommandLineOptions.Instance.TargetArchitecture); + Assert.AreEqual(ObjectModel.Architecture.ARM.ToString(), RunSettingsUtilities.QueryRunSettingsNode(RunSettingsManager.Instance, PlatformArgumentExecutor.RunSettingsPath)); } #endregion diff --git a/test/vstest.console.UnitTests/Processors/ResultsDirectoryArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/ResultsDirectoryArgumentProcessorTests.cs new file mode 100644 index 0000000000..a8ebbaa89f --- /dev/null +++ b/test/vstest.console.UnitTests/Processors/ResultsDirectoryArgumentProcessorTests.cs @@ -0,0 +1,199 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + + +namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors +{ + using Microsoft.VisualStudio.TestPlatform.Common; + using Microsoft.VisualStudio.TestTools.UnitTesting; + using TestPlatform.CommandLine.Processors; + using System; + using System.IO; + using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; + + [TestClass] + public class ResultsDirectoryArgumentProcessorTests + { + [TestCleanup] + public void TestCleanup() + { + CommandLineOptions.Instance.Reset(); + RunSettingsManager.Instance.Reset(); + } + + [TestMethod] + public void GetMetadataShouldReturnResultsDirectoryArgumentProcessorCapabilities() + { + var processor = new ResultsDirectoryArgumentProcessor(); + Assert.IsTrue(processor.Metadata.Value is ResultsDirectoryArgumentProcessorCapabilities); + } + + [TestMethod] + public void GetExecuterShouldReturnResultsDirectoryArgumentExecutor() + { + var processor = new ResultsDirectoryArgumentProcessor(); + Assert.IsTrue(processor.Executor.Value is ResultsDirectoryArgumentExecutor); + } + + #region ResultsDirectoryArgumentProcessorCapabilities tests + + [TestMethod] + public void CapabilitiesShouldReturnAppropriateProperties() + { + var capabilities = new ResultsDirectoryArgumentProcessorCapabilities(); + Assert.AreEqual("/ResultsDirectory", capabilities.CommandName); + //StringAssert.Contains(capabilities.HelpContentResourceName, "Test results directory will be created from a given path"); + + Assert.AreEqual(HelpContentPriority.ResultsDirectoryArgumentProcessorHelpPriority, capabilities.HelpPriority); + Assert.AreEqual(false, capabilities.IsAction); + Assert.AreEqual(ArgumentProcessorPriority.AutoUpdateRunSettings, capabilities.Priority); + + Assert.AreEqual(false, capabilities.AllowMultiple); + Assert.AreEqual(false, capabilities.AlwaysExecute); + Assert.AreEqual(false, capabilities.IsSpecialCommand); + } + + #endregion + + #region ResultsDirectoryArgumentExecutor Initialize tests + + [TestMethod] + public void InitializeShouldThrowIfArgumentIsNull() + { + var executor = new ResultsDirectoryArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); + + /*var message = + @"The /ResultsDirectory parameter requires a value, where the test results should be saved. Example: /ResultsDirectory:c:\MyTestResultsDirectory";*/ + + var isExceptionThrown = false; + + try + { + executor.Initialize(null); + } + catch (Exception ex) + { + isExceptionThrown = true; + Assert.IsTrue(ex is CommandLineException); + //Assert.AreEqual(message, ex.Message); + } + + Assert.IsTrue(isExceptionThrown); + } + + [TestMethod] + public void InitializeShouldThrowIfArgumentIsAWhiteSpace() + { + var executor = new ResultsDirectoryArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); + + /*var message = + @"The /ResultsDirectory parameter requires a value, where the test results should be saved. Example: /ResultsDirectory:c:\MyTestResultsDirectory";*/ + + var isExceptionThrown = false; + + try + { + executor.Initialize(" "); + } + catch (Exception ex) + { + isExceptionThrown = true; + Assert.IsTrue(ex is CommandLineException); + //Assert.AreEqual(message, ex.Message); + } + + Assert.IsTrue(isExceptionThrown); + } + + [TestMethod] + public void InitializeShouldThrowIfRelativePathIsInvalid() + { + var executor = new ResultsDirectoryArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); + + var folder = @".\path\to\in:valid"; + + /*var message = string.Format( + @"The path '{0}' specified in the 'ResultsDirectory' is invalid. Error: {1}", + folder, + "Please provide valid path");*/ + + var isExceptionThrown = false; + + try + { + executor.Initialize(folder); + } + catch (Exception ex) + { + isExceptionThrown = true; + Assert.IsTrue(ex is CommandLineException); + //Assert.AreEqual(message, ex.Message); + } + + Assert.IsTrue(isExceptionThrown); + } + + [TestMethod] + public void InitializeShouldThrowIfPathIsInvalid() + { + var executor = new ResultsDirectoryArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); + + var folder = @"c:\path\to\in:valid"; + + /*var message = string.Format( + @"The path '{0}' specified in the 'ResultsDirectory' is invalid. Error: {1}", + folder, + "Please provide valid path");*/ + + var isExceptionThrown = false; + + try + { + executor.Initialize(folder); + } + catch (Exception ex) + { + isExceptionThrown = true; + Assert.IsTrue(ex is CommandLineException); + //Assert.AreEqual(message, ex.Message); + } + + Assert.IsTrue(isExceptionThrown); + } + + [TestMethod] + public void InitializeShouldSetCommandLineOptionsAndRunSettingsFrameworkForRelativePathValue() + { + var executor = new ResultsDirectoryArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); + var relativePath = @".\relative\path"; + var absolutePath = Path.GetFullPath(relativePath); + executor.Initialize(relativePath); + Assert.AreEqual(absolutePath, CommandLineOptions.Instance.ResultsDirectory); + Assert.AreEqual(absolutePath, RunSettingsUtilities.QueryRunSettingsNode(RunSettingsManager.Instance, ResultsDirectoryArgumentExecutor.RunSettingsPath)); + } + + [TestMethod] + public void InitializeShouldSetCommandLineOptionsAndRunSettingsFrameworkForAbsolutePathValue() + { + var executor = new ResultsDirectoryArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); + var absolutePath = @"c:\Users\someone\testresults"; + executor.Initialize(absolutePath); + Assert.AreEqual(absolutePath, CommandLineOptions.Instance.ResultsDirectory); + Assert.AreEqual(absolutePath, RunSettingsUtilities.QueryRunSettingsNode(RunSettingsManager.Instance, ResultsDirectoryArgumentExecutor.RunSettingsPath)); + } + + #endregion + + #region ResultsDirectoryArgumentExecutor Execute tests + + [TestMethod] + public void ExecuteShouldReturnSuccess() + { + var executor = new ResultsDirectoryArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); + + Assert.AreEqual(ArgumentProcessorResult.Success, executor.Execute()); + } + + #endregion + } +} diff --git a/test/vstest.console.UnitTests/Processors/RunSpecificTestsArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/RunSpecificTestsArgumentProcessorTests.cs index 6db1cb3665..67e2e39e2c 100644 --- a/test/vstest.console.UnitTests/Processors/RunSpecificTestsArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/RunSpecificTestsArgumentProcessorTests.cs @@ -1,6 +1,10 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; +using Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors.Utilities; +using Microsoft.VisualStudio.TestPlatform.Common; + namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors { using System; @@ -29,6 +33,19 @@ public class RunSpecificTestsArgumentProcessorTests private Mock mockTestPlatformEventSource; + [TestInitialize] + public void Init() + { + RunSettingsUtilities.AddDefaultRunSettings(RunSettingsManager.Instance); + } + + [TestCleanup] + public void Cleanup() + { + RunSettingsUtilities.AddDefaultRunSettings(RunSettingsManager.Instance); + } + + public RunSpecificTestsArgumentProcessorTests() { this.mockFileHelper = new Mock(); diff --git a/test/vstest.console.UnitTests/Processors/Utilities/RunSettingsUtilitiesTests.cs b/test/vstest.console.UnitTests/Processors/Utilities/RunSettingsUtilitiesTests.cs index 258ec7e51b..7cad1dfa09 100644 --- a/test/vstest.console.UnitTests/Processors/Utilities/RunSettingsUtilitiesTests.cs +++ b/test/vstest.console.UnitTests/Processors/Utilities/RunSettingsUtilitiesTests.cs @@ -23,7 +23,7 @@ public void TestCleanup() { CommandLineOptions.Instance.Reset(); } - + /* [TestMethod] public void GetRunSettingsShouldReturnDefaultRunSettingsIfProviderIsNull() { @@ -130,7 +130,7 @@ public void GetRunSettingsShouldReturnWithoutChangeIfUserProvidesBothParallelSwi var parallelValue = Environment.ProcessorCount; StringAssert.Contains(runSettings, "2", "RunSettings Parallel value should take precendence over parallel switch."); } - + */ #region Testable Implementations From c790e6dc745baf9d86befa979b25b6f715139f3f Mon Sep 17 00:00:00 2001 From: samadala Date: Thu, 5 Jan 2017 18:17:56 +0530 Subject: [PATCH 04/10] Fix unit tests --- .../InferRunSettingsHelper.cs | 6 +++ .../CommandLine/CommandLineOptions.cs | 8 ++++ .../Processors/FrameworkArgumentProcessor.cs | 11 +++++- .../Processors/ListTestsArgumentProcessor.cs | 2 +- .../RunSettingsArgumentProcessor.cs | 19 ++++------ .../Processors/RunTestsArgumentProcessor.cs | 2 +- .../ListTestsArgumentProcessorTests.cs | 28 ++++++++++---- .../RunSettingsArgumentProcessortTests.cs | 38 ++++++++++++++++++- .../RunSpecificTestsArgumentProcessorTests.cs | 4 +- .../RunTestsArgumentProcessorTests.cs | 29 ++++++++++---- 10 files changed, 114 insertions(+), 33 deletions(-) diff --git a/src/Microsoft.TestPlatform.Utilities/InferRunSettingsHelper.cs b/src/Microsoft.TestPlatform.Utilities/InferRunSettingsHelper.cs index 1f6fc5da42..184711799c 100644 --- a/src/Microsoft.TestPlatform.Utilities/InferRunSettingsHelper.cs +++ b/src/Microsoft.TestPlatform.Utilities/InferRunSettingsHelper.cs @@ -209,6 +209,12 @@ private static bool TryGetFrameworkXml(XPathNavigator runSettingsNavigator, out Func validator = (string xml) => { + if (Framework.FromString(xml) != null) + { + // Allow values like .NETFramework,Version=v4.5, ".NETCoreApp,Version=v1.0 + return false; + } + var value = (FrameworkVersion)Enum.Parse(typeof(FrameworkVersion), xml, true); if (!Enum.IsDefined(typeof(FrameworkVersion), value) || value == FrameworkVersion.None) diff --git a/src/vstest.console/CommandLine/CommandLineOptions.cs b/src/vstest.console/CommandLine/CommandLineOptions.cs index b7af0eb5d9..d14aeb4214 100644 --- a/src/vstest.console/CommandLine/CommandLineOptions.cs +++ b/src/vstest.console/CommandLine/CommandLineOptions.cs @@ -227,8 +227,16 @@ internal Framework TargetFrameworkVersion /// internal bool FrameworkVersionSpecified { get; private set; } + /// + /// Gets or sets the results directory for test run. + /// internal string ResultsDirectory { get; set; } + /// + /// Gets or sets the /setting switch value. i.e path to settings file. + /// + internal string SettingsFile { get; set; } + #endregion #region Public Methods diff --git a/src/vstest.console/Processors/FrameworkArgumentProcessor.cs b/src/vstest.console/Processors/FrameworkArgumentProcessor.cs index 952b543c95..0b15a354e3 100644 --- a/src/vstest.console/Processors/FrameworkArgumentProcessor.cs +++ b/src/vstest.console/Processors/FrameworkArgumentProcessor.cs @@ -11,6 +11,7 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; using Microsoft.VisualStudio.TestPlatform.Common; using Microsoft.VisualStudio.TestPlatform.Common.Interfaces; + using Microsoft.VisualStudio.TestPlatform.Utilities; using CommandLineResources = Microsoft.VisualStudio.TestPlatform.CommandLine.Resources.Resources; @@ -136,7 +137,7 @@ public void Initialize(string argument) var validFramework = Framework.FromString(argument); if (validFramework == null) - { + { throw new CommandLineException( string.Format(CultureInfo.CurrentCulture, CommandLineResources.InvalidFrameworkVersion, argument)); } @@ -147,6 +148,14 @@ public void Initialize(string argument) { EqtTrace.Info("Using .Net Framework version:{0}", commandLineOptions.TargetFrameworkVersion); } + + if (this.commandLineOptions.TargetFrameworkVersion != Framework.DefaultFramework + && !string.IsNullOrWhiteSpace(this.commandLineOptions.SettingsFile) + && MSTestSettingsUtilities.IsLegacyTestSettingsFile(this.commandLineOptions.SettingsFile)) + { + IOutput output = ConsoleOutput.Instance; + output.Warning(CommandLineResources.TestSettingsFrameworkMismatch, this.commandLineOptions.TargetFrameworkVersion.ToString(), Framework.DefaultFramework.ToString()); + } } /// diff --git a/src/vstest.console/Processors/ListTestsArgumentProcessor.cs b/src/vstest.console/Processors/ListTestsArgumentProcessor.cs index 770f4fc18f..f3180c5222 100644 --- a/src/vstest.console/Processors/ListTestsArgumentProcessor.cs +++ b/src/vstest.console/Processors/ListTestsArgumentProcessor.cs @@ -199,7 +199,7 @@ public ArgumentProcessorResult Execute() { Contract.Assert(this.output != null); Contract.Assert(this.commandLineOptions != null); - Contract.Assert(!string.IsNullOrWhiteSpace(this.runSettingsManager.ActiveRunSettings.SettingsXml)); + Contract.Assert(!string.IsNullOrWhiteSpace(this.runSettingsManager?.ActiveRunSettings?.SettingsXml)); if (this.commandLineOptions.Sources.Count() <= 0) { diff --git a/src/vstest.console/Processors/RunSettingsArgumentProcessor.cs b/src/vstest.console/Processors/RunSettingsArgumentProcessor.cs index bdc6a2e156..b4ed9f0420 100644 --- a/src/vstest.console/Processors/RunSettingsArgumentProcessor.cs +++ b/src/vstest.console/Processors/RunSettingsArgumentProcessor.cs @@ -129,6 +129,8 @@ public void Initialize(string argument) //Add default runsettings values if not exists in given runsettings file. RunSettingsUtilities.AddDefaultRunSettings(this.runSettingsManager); + + this.commandLineOptions.SettingsFile = argument; } catch (XmlException exception) { @@ -171,19 +173,12 @@ private IXPathNavigable GetRunSettingsDocument(string runSettingsFile) { runSettingsDocument = XmlRunSettingsUtilities.CreateDefaultRunSettings(); +#if NET46 FrameworkVersion frameworkVersion = FrameworkVersion.Framework45; - - if (this.commandLineOptions.FrameworkVersionSpecified && this.commandLineOptions.TargetFrameworkVersion != Framework.DefaultFramework) - { - IOutput output = ConsoleOutput.Instance; - output.Warning(CommandLineResources.TestSettingsFrameworkMismatch, this.commandLineOptions.TargetFrameworkVersion.ToString(), Framework.DefaultFramework.ToString()); - } - - var architecture = this.commandLineOptions.ArchitectureSpecified - ? this.commandLineOptions.TargetArchitecture - : Architecture.X86; - - runSettingsDocument = MSTestSettingsUtilities.Import(runSettingsFile, runSettingsDocument, architecture, frameworkVersion); +#else + FrameworkVersion frameworkVersion = FrameworkVersion.FrameworkCore10; +#endif + runSettingsDocument = MSTestSettingsUtilities.Import(runSettingsFile, runSettingsDocument, Architecture.X86, frameworkVersion); } if (this.commandLineOptions.EnableCodeCoverage == true) diff --git a/src/vstest.console/Processors/RunTestsArgumentProcessor.cs b/src/vstest.console/Processors/RunTestsArgumentProcessor.cs index 583f2102fb..c549a9679a 100644 --- a/src/vstest.console/Processors/RunTestsArgumentProcessor.cs +++ b/src/vstest.console/Processors/RunTestsArgumentProcessor.cs @@ -146,7 +146,7 @@ public void Initialize(string argument) public ArgumentProcessorResult Execute() { Contract.Assert(this.commandLineOptions != null); - Contract.Assert(!string.IsNullOrWhiteSpace(this.runSettingsManager.ActiveRunSettings.SettingsXml)); + Contract.Assert(!string.IsNullOrWhiteSpace(this.runSettingsManager?.ActiveRunSettings?.SettingsXml)); // Ensure a test source file was provided var anySource = this.commandLineOptions.Sources.FirstOrDefault(); diff --git a/test/vstest.console.UnitTests/Processors/ListTestsArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/ListTestsArgumentProcessorTests.cs index 03ee3aaf06..b8def16506 100644 --- a/test/vstest.console.UnitTests/Processors/ListTestsArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/ListTestsArgumentProcessorTests.cs @@ -10,6 +10,8 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors using Microsoft.VisualStudio.TestPlatform.Client; using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors; using Microsoft.VisualStudio.TestTools.UnitTesting; + using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; + using Microsoft.VisualStudio.TestPlatform.Common; using Moq; using ObjectModel; @@ -33,6 +35,18 @@ public class ListTestsArgumentProcessorTests private readonly Mock mockTestPlatformEventSource; + [TestInitialize] + public void Init() + { + RunSettingsUtilities.AddDefaultRunSettings(RunSettingsManager.Instance); + } + + [TestCleanup] + public void Cleanup() + { + RunSettingsUtilities.AddDefaultRunSettings(RunSettingsManager.Instance); + } + public ListTestsArgumentProcessorTests() { this.mockFileHelper = new Mock(); @@ -92,7 +106,7 @@ public void ExecutorInitializeWithValidSourceShouldAddItToTestSources() var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, new TestPlatform(), TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); ListTestsArgumentExecutor executor = new ListTestsArgumentExecutor( CommandLineOptions.Instance, - null, + RunSettingsManager.Instance, testRequestManager); executor.Initialize(this.dummyTestFilePath); @@ -108,7 +122,7 @@ public void ExecutorExecuteForNoSourcesShouldReturnFail() var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, new TestPlatform(), TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); ListTestsArgumentExecutor executor = new ListTestsArgumentExecutor( CommandLineOptions.Instance, - null, + RunSettingsManager.Instance, testRequestManager); Assert.ThrowsException(() => executor.Execute()); @@ -129,7 +143,7 @@ public void ExecutorExecuteShouldCatchTestPlatformExceptionAndReturnFail() ListTestsArgumentExecutor listTestsArgumentExecutor = new ListTestsArgumentExecutor( CommandLineOptions.Instance, - null, + RunSettingsManager.Instance, testRequestManager); ArgumentProcessorResult argumentProcessorResult = listTestsArgumentExecutor.Execute(); @@ -151,7 +165,7 @@ public void ExecutorExecuteShouldCatchSettingsExceptionAndReturnFail() ListTestsArgumentExecutor listTestsArgumentExecutor = new ListTestsArgumentExecutor( CommandLineOptions.Instance, - null, + RunSettingsManager.Instance, testRequestManager); ArgumentProcessorResult argumentProcessorResult = listTestsArgumentExecutor.Execute(); @@ -173,7 +187,7 @@ public void ExecutorExecuteShouldCatchInvalidOperationExceptionAndReturnFail() ListTestsArgumentExecutor listTestsArgumentExecutor = new ListTestsArgumentExecutor( CommandLineOptions.Instance, - null, + RunSettingsManager.Instance, testRequestManager); ArgumentProcessorResult argumentProcessorResult = listTestsArgumentExecutor.Execute(); @@ -194,7 +208,7 @@ public void ExecutorExecuteShouldThrowOtherExceptions() var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); var executor = new ListTestsArgumentExecutor( CommandLineOptions.Instance, - null, + RunSettingsManager.Instance, testRequestManager); Assert.ThrowsException(() => executor.Execute()); @@ -253,7 +267,7 @@ private void RunListTestArgumentProcessorExecuteWithMockSetup(Mock\r\n"); + } + + [TestMethod] + public void InitializeShouldAddDefaultSettingsIfNotPresent() + { + // Arrange. + var fileName = "C:\\temp\\r.runsettings"; + var settingsXml = ""; + + var settingsProvider = new TestableRunSettingsProvider(); + + var executor = new TestableRunSettingsArgumentExecutor( + CommandLineOptions.Instance, + settingsProvider, + settingsXml); + + // Setup mocks. + var mockFileHelper = new Mock(); + mockFileHelper.Setup(fh => fh.Exists(It.IsAny())).Returns(true); + executor.FileHelper = mockFileHelper.Object; + + // Act. + executor.Initialize(fileName); + + // Assert. + Assert.IsNotNull(settingsProvider.ActiveRunSettings); + RunConfiguration runConfiguration = + XmlRunSettingsUtilities.GetRunConfigurationNode(settingsProvider.ActiveRunSettings.SettingsXml); + Assert.AreEqual(runConfiguration.ResultsDirectory, Constants.DefaultResultsDirectory); + Assert.AreEqual(runConfiguration.TargetFrameworkVersion.ToString(), Framework.DefaultFramework.ToString()); + Assert.AreEqual(runConfiguration.TargetPlatform, Constants.DefaultPlatform); + } [TestMethod] @@ -180,6 +213,7 @@ public void InitializeShouldSetActiveRunSettingsForTestSettingsFiles() StringAssert.Contains(settingsProvider.ActiveRunSettings.SettingsXml, "\r\n \r\n X86\r\n Framework45\r\n \r\n \r\n C:\\temp\\r.testsettings\r\n true\r\n \r\n \r\n \r\n \r\n"); } + [Ignore] // Code coverage yet to add [TestMethod] public void InitializeShouldSetActiveRunSettingsWithCodeCoverageData() { diff --git a/test/vstest.console.UnitTests/Processors/RunSpecificTestsArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/RunSpecificTestsArgumentProcessorTests.cs index 67e2e39e2c..2d02caaf61 100644 --- a/test/vstest.console.UnitTests/Processors/RunSpecificTestsArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/RunSpecificTestsArgumentProcessorTests.cs @@ -62,7 +62,7 @@ public void GetMetadataShouldReturnRunSpecificTestsArgumentProcessorCapabilities } [TestMethod] - public void GetExecutorShouldReturnRunSpecificTestsArgumentProcessorCapabilities() + public void GetExecutorShouldReturnRunSpecificTestsArgumentExecutor() { RunSpecificTestsArgumentProcessor processor = new RunSpecificTestsArgumentProcessor(); @@ -76,7 +76,7 @@ public void CapabilitiesShouldReturnAppropriateProperties() { RunSpecificTestsArgumentProcessorCapabilities capabilities = new RunSpecificTestsArgumentProcessorCapabilities(); Assert.AreEqual("/Tests", capabilities.CommandName); - StringAssert.Contains(capabilities.HelpContentResourceName,"/Tests:\n Run tests with names that match the provided values."); + StringAssert.Contains(capabilities.HelpContentResourceName, "/Tests:\n Run tests with names that match the provided values."); Assert.AreEqual(HelpContentPriority.RunSpecificTestsArgumentProcessorHelpPriority, capabilities.HelpPriority); Assert.AreEqual(true, capabilities.IsAction); diff --git a/test/vstest.console.UnitTests/Processors/RunTestsArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/RunTestsArgumentProcessorTests.cs index 7cec41e3a3..4ed2f8132d 100644 --- a/test/vstest.console.UnitTests/Processors/RunTestsArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/RunTestsArgumentProcessorTests.cs @@ -22,6 +22,8 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors using Microsoft.VisualStudio.TestPlatform.Utilities; using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces; using Microsoft.VisualStudio.TestTools.UnitTesting; + using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; + using Microsoft.VisualStudio.TestPlatform.Common; using Moq; @@ -36,6 +38,18 @@ public class RunTestsArgumentProcessorTests private Mock mockTestPlatformEventSource; + [TestInitialize] + public void Init() + { + RunSettingsUtilities.AddDefaultRunSettings(RunSettingsManager.Instance); + } + + [TestCleanup] + public void Cleanup() + { + RunSettingsUtilities.AddDefaultRunSettings(RunSettingsManager.Instance); + } + public RunTestsArgumentProcessorTests() { this.mockFileHelper = new Mock(); @@ -63,9 +77,10 @@ public void GetExecuterShouldReturnRunTestsArgumentProcessorCapabilities() [TestMethod] public void CapabilitiesShouldReturnAppropriateProperties() { + System.Diagnostics.Debugger.Launch(); RunTestsArgumentProcessorCapabilities capabilities = new RunTestsArgumentProcessorCapabilities(); Assert.AreEqual("/RunTests", capabilities.CommandName); - Assert.AreEqual("[TestFileNames]\n Run tests from the specified files. Separate multiple test file names\n by spaces.\n Examples: mytestproject.dll\n mytestproject.dll myothertestproject.exe", capabilities.HelpContentResourceName); + Assert.AreEqual("[TestFileNames]" + Environment.NewLine + " Run tests from the specified files. Separate multiple test file names" + Environment.NewLine + " by spaces." + Environment.NewLine + " Examples: mytestproject.dll" + Environment.NewLine + " mytestproject.dll myothertestproject.exe", capabilities.HelpContentResourceName); Assert.AreEqual(HelpContentPriority.RunTestsArgumentProcessorHelpPriority, capabilities.HelpPriority); Assert.AreEqual(true, capabilities.IsAction); @@ -86,7 +101,7 @@ public void ExecutorExecuteForNoSourcesShouldThrowCommandLineException() var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, new TestPlatform(), TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); - var executor = new RunTestsArgumentExecutor(CommandLineOptions.Instance, null, testRequestManager); + var executor = new RunTestsArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance, testRequestManager); Assert.ThrowsException(() => executor.Execute()); } @@ -103,7 +118,7 @@ public void ExecutorExecuteShouldCatchTestPlatformExceptionAndReturnFail() this.ResetAndAddSourceToCommandLineOptions(); var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); - var executor = new RunTestsArgumentExecutor(CommandLineOptions.Instance, null, testRequestManager); + var executor = new RunTestsArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance, testRequestManager); ArgumentProcessorResult argumentProcessorResult = executor.Execute(); Assert.AreEqual(ArgumentProcessorResult.Fail, argumentProcessorResult); @@ -121,7 +136,7 @@ public void ExecutorExecuteShouldCatchSettingsExceptionAndReturnFail() this.ResetAndAddSourceToCommandLineOptions(); var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); - var executor = new RunTestsArgumentExecutor(CommandLineOptions.Instance, null, testRequestManager); + var executor = new RunTestsArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance, testRequestManager); ArgumentProcessorResult argumentProcessorResult = executor.Execute(); Assert.AreEqual(ArgumentProcessorResult.Fail, argumentProcessorResult); @@ -139,7 +154,7 @@ public void ExecutorExecuteShouldCatchInvalidOperationExceptionAndReturnFail() this.ResetAndAddSourceToCommandLineOptions(); var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); - var executor = new RunTestsArgumentExecutor(CommandLineOptions.Instance, null, testRequestManager); + var executor = new RunTestsArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance, testRequestManager); ArgumentProcessorResult argumentProcessorResult = executor.Execute(); Assert.AreEqual(ArgumentProcessorResult.Fail, argumentProcessorResult); @@ -157,7 +172,7 @@ public void ExecutorExecuteShouldThrowOtherExceptions() this.ResetAndAddSourceToCommandLineOptions(); var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); - var executor = new RunTestsArgumentExecutor(CommandLineOptions.Instance, null, testRequestManager); + var executor = new RunTestsArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance, testRequestManager); Assert.ThrowsException(() => executor.Execute()); } @@ -212,7 +227,7 @@ private ArgumentProcessorResult RunRunArgumentProcessorExecuteWithMockSetup(ITes this.ResetAndAddSourceToCommandLineOptions(); var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); - var executor = new RunTestsArgumentExecutor(CommandLineOptions.Instance, null, testRequestManager); + var executor = new RunTestsArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance, testRequestManager); return executor.Execute(); } From 33622f092a927e8dd208942802fc30f208ebbebb Mon Sep 17 00:00:00 2001 From: samadala Date: Thu, 5 Jan 2017 21:35:11 +0530 Subject: [PATCH 05/10] Update resources and tests --- .../RunSettingsManager.cs | 5 ---- .../InferRunSettingsHelper.cs | 4 +-- .../ResultsDirectoryArgumentProcessor.cs | 4 +-- .../Resources/Resources.Designer.cs | 15 ++++++++-- src/vstest.console/Resources/Resources.resx | 5 +++- .../LoggerTests.cs | 2 +- .../InferRunSettingsHelperTests.cs | 15 ++++++++++ .../ExecutorUnitTests.cs | 17 +++++++++++ .../FrameworkArgumentProcessorTests.cs | 2 +- .../ParallelArgumentProcessorTests.cs | 2 +- .../PlatformArgumentProcessorTests.cs | 2 +- .../ResultsDirectoryArgumentProcessorTests.cs | 28 +++++++++---------- .../RunSettingsArgumentProcessortTests.cs | 27 ++++++++++++++++++ 13 files changed, 98 insertions(+), 30 deletions(-) diff --git a/src/Microsoft.TestPlatform.Common/RunSettingsManager.cs b/src/Microsoft.TestPlatform.Common/RunSettingsManager.cs index 5319845fb5..89d06ead24 100644 --- a/src/Microsoft.TestPlatform.Common/RunSettingsManager.cs +++ b/src/Microsoft.TestPlatform.Common/RunSettingsManager.cs @@ -78,11 +78,6 @@ public void SetActiveRunSettings(RunSettings runSettings) this.ActiveRunSettings = runSettings; } - public void Reset() - { - RunSettingsManager.runSettingsManagerInstance = null; - } - #endregion } } diff --git a/src/Microsoft.TestPlatform.Utilities/InferRunSettingsHelper.cs b/src/Microsoft.TestPlatform.Utilities/InferRunSettingsHelper.cs index 184711799c..906b401c8f 100644 --- a/src/Microsoft.TestPlatform.Utilities/InferRunSettingsHelper.cs +++ b/src/Microsoft.TestPlatform.Utilities/InferRunSettingsHelper.cs @@ -211,8 +211,8 @@ private static bool TryGetFrameworkXml(XPathNavigator runSettingsNavigator, out { if (Framework.FromString(xml) != null) { - // Allow values like .NETFramework,Version=v4.5, ".NETCoreApp,Version=v1.0 - return false; + // Allow TargetFrameworkMoniker values like .NETFramework,Version=v4.5, ".NETCoreApp,Version=v1.0 + return true; } var value = (FrameworkVersion)Enum.Parse(typeof(FrameworkVersion), xml, true); diff --git a/src/vstest.console/Processors/ResultsDirectoryArgumentProcessor.cs b/src/vstest.console/Processors/ResultsDirectoryArgumentProcessor.cs index 5479878ed2..771a98b72f 100644 --- a/src/vstest.console/Processors/ResultsDirectoryArgumentProcessor.cs +++ b/src/vstest.console/Processors/ResultsDirectoryArgumentProcessor.cs @@ -155,9 +155,9 @@ public void Initialize(string argument) argument = Path.GetFullPath(argument); } } - catch (Exception) + catch (Exception ex) { - throw new CommandLineException(CommandLineResources.ResultsDirectoryValueRequired); + throw new CommandLineException( String.Format(CommandLineResources.InvalidResultsDirectoryPathCommand, argument, ex.Message)); } this.commandLineOptions.ResultsDirectory = argument; diff --git a/src/vstest.console/Resources/Resources.Designer.cs b/src/vstest.console/Resources/Resources.Designer.cs index 22e4df2b1e..58b5e0e9a4 100644 --- a/src/vstest.console/Resources/Resources.Designer.cs +++ b/src/vstest.console/Resources/Resources.Designer.cs @@ -646,7 +646,18 @@ public static string InvalidPortArgument { return ResourceManager.GetString("InvalidPortArgument", resourceCulture); } } - + + /// + /// Looks up a localized string similar to The path '{0}' specified in the 'ResultsDirectory' is invalid. Error: {1}. + /// + public static string InvalidResultsDirectoryPathCommand + { + get + { + return ResourceManager.GetString("InvalidResultsDirectoryPathCommand", resourceCulture); + } + } + /// /// Looks up a localized string similar to The path '{0}' specified in the 'TestAdapterPath' is invalid. Error: {1}. /// @@ -1017,7 +1028,7 @@ public static string ResultsDirectoryArgumentHelp } /// - /// Looks up a localized string similar to The /ResultsDirectory parameter requires a value, which is path of a location test results directory. Example: /ResultsDirectory:"c:\path\to\TestResults". + /// Looks up a localized string similar to The /ResultsDirectory parameter requires a value, where the test results should be saved. Example: /ResultsDirectory:c:\MyTestResultsDirectory. /// public static string ResultsDirectoryValueRequired { diff --git a/src/vstest.console/Resources/Resources.resx b/src/vstest.console/Resources/Resources.resx index 135c0b633f..1a04f13f0c 100644 --- a/src/vstest.console/Resources/Resources.resx +++ b/src/vstest.console/Resources/Resources.resx @@ -630,6 +630,9 @@ Example /ResultsDirectory:<pathToResultsDirectory> - The /ResultsDirectory parameter requires a value, which is path of a location test results directory. Example: /ResultsDirectory:"c:\path\to\TestResults" + The /ResultsDirectory parameter requires a value, where the test results should be saved. Example: /ResultsDirectory:c:\MyTestResultsDirectory + + + The path '{0}' specified in the 'ResultsDirectory' is invalid. Error: {1} \ No newline at end of file diff --git a/test/Microsoft.TestPlatform.AcceptanceTests/LoggerTests.cs b/test/Microsoft.TestPlatform.AcceptanceTests/LoggerTests.cs index 34ac55692e..e409a8825a 100644 --- a/test/Microsoft.TestPlatform.AcceptanceTests/LoggerTests.cs +++ b/test/Microsoft.TestPlatform.AcceptanceTests/LoggerTests.cs @@ -28,7 +28,7 @@ public void TrxLoggerShouldProperlyOverwriteFile(string runnerFramework, string arguments = string.Concat(arguments, " /testcasefilter:Name~Pass"); this.InvokeVsTest(arguments); - var trxLogFilePath = Path.Combine(AppContext.BaseDirectory, "TestResults", trxFileName); + var trxLogFilePath = Path.Combine(Directory.GetCurrentDirectory(), "TestResults", trxFileName); Assert.IsTrue(IsValidXml(trxLogFilePath), "Invalid content in Trx log file"); } diff --git a/test/Microsoft.TestPlatform.Utilities.UnitTests/InferRunSettingsHelperTests.cs b/test/Microsoft.TestPlatform.Utilities.UnitTests/InferRunSettingsHelperTests.cs index 81725f5d75..0ef6078d69 100644 --- a/test/Microsoft.TestPlatform.Utilities.UnitTests/InferRunSettingsHelperTests.cs +++ b/test/Microsoft.TestPlatform.Utilities.UnitTests/InferRunSettingsHelperTests.cs @@ -122,6 +122,21 @@ public void UpdateRunSettingsShouldNotUpdateFrameworkIfRunSettingsAlreadyHasIt() StringAssert.Contains(xml, ".NETFramework,Version=v4.0"); } + //TargetFrameworkMoniker + + [TestMethod] + public void UpdateRunSettingsShouldAllowTargetFrameworkMonikerValue() + { + + var settings = @".NETFramework,Version=v4.0"; + var navigator = this.GetNavigator(settings); + + InferRunSettingsHelper.UpdateRunSettingsWithUserProvidedSwitches(navigator, Architecture.X64, Framework.DefaultFramework, "temp"); + + var xml = navigator.OuterXml; + + StringAssert.Contains(xml, ".NETFramework,Version=v4.0"); + } [TestMethod] public void UpdateRunSettingsShouldNotUpdateResultsDirectoryIfRunSettingsAlreadyHasIt() diff --git a/test/vstest.console.UnitTests/ExecutorUnitTests.cs b/test/vstest.console.UnitTests/ExecutorUnitTests.cs index 45250cac62..3ea428153f 100644 --- a/test/vstest.console.UnitTests/ExecutorUnitTests.cs +++ b/test/vstest.console.UnitTests/ExecutorUnitTests.cs @@ -10,6 +10,9 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests using CoreUtilities.Tracing.Interfaces; using Microsoft.VisualStudio.TestTools.UnitTesting; + using Microsoft.VisualStudio.TestPlatform.Common; + using Microsoft.VisualStudio.TestPlatform.ObjectModel; + using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities; using Moq; @@ -73,6 +76,20 @@ public void ExecutorEmptyArgsCallRunTestsProcessor() // "First Printed message must be Microsoft Copyright"); } + /// + /// Executor should set default runsettings value even there is no processor + /// + [TestMethod] + public void ExecuteShouldInitializeDefaultRunsettings() + { + var mockOutput = new MockOutput(); + var exitCode = new Executor(mockOutput, this.mockTestPlatformEventSource.Object).Execute(null); + RunConfiguration runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(RunSettingsManager.Instance.ActiveRunSettings.SettingsXml); + Assert.AreEqual(runConfiguration.ResultsDirectory, Constants.DefaultResultsDirectory); + Assert.AreEqual(runConfiguration.TargetFrameworkVersion.ToString(), Framework.DefaultFramework.ToString()); + Assert.AreEqual(runConfiguration.TargetPlatform, Constants.DefaultPlatform); + } + [TestMethod] public void ExecuteShouldInstrumentVsTestConsoleStart() { diff --git a/test/vstest.console.UnitTests/Processors/FrameworkArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/FrameworkArgumentProcessorTests.cs index b18005d5e2..5f3e47f378 100644 --- a/test/vstest.console.UnitTests/Processors/FrameworkArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/FrameworkArgumentProcessorTests.cs @@ -15,7 +15,7 @@ public class FrameworkArgumentProcessorTests public void TestCleanup() { CommandLineOptions.Instance.Reset(); - RunSettingsManager.Instance.Reset(); + RunSettingsManager.Instance = null; } [TestMethod] diff --git a/test/vstest.console.UnitTests/Processors/ParallelArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/ParallelArgumentProcessorTests.cs index a3d5a4184c..838b40e464 100644 --- a/test/vstest.console.UnitTests/Processors/ParallelArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/ParallelArgumentProcessorTests.cs @@ -15,7 +15,7 @@ public class ParallelArgumentProcessorTests public void TestCleanup() { CommandLineOptions.Instance.Reset(); - RunSettingsManager.Instance.Reset(); + RunSettingsManager.Instance = null; } [TestMethod] diff --git a/test/vstest.console.UnitTests/Processors/PlatformArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/PlatformArgumentProcessorTests.cs index 629ed4e53c..53f35f0370 100644 --- a/test/vstest.console.UnitTests/Processors/PlatformArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/PlatformArgumentProcessorTests.cs @@ -16,7 +16,7 @@ public class PlatformArgumentProcessorTests public void TestCleanup() { CommandLineOptions.Instance.Reset(); - RunSettingsManager.Instance.Reset(); + RunSettingsManager.Instance = null; } [TestMethod] diff --git a/test/vstest.console.UnitTests/Processors/ResultsDirectoryArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/ResultsDirectoryArgumentProcessorTests.cs index a8ebbaa89f..fa612045cc 100644 --- a/test/vstest.console.UnitTests/Processors/ResultsDirectoryArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/ResultsDirectoryArgumentProcessorTests.cs @@ -18,7 +18,7 @@ public class ResultsDirectoryArgumentProcessorTests public void TestCleanup() { CommandLineOptions.Instance.Reset(); - RunSettingsManager.Instance.Reset(); + RunSettingsManager.Instance = null; } [TestMethod] @@ -42,7 +42,7 @@ public void CapabilitiesShouldReturnAppropriateProperties() { var capabilities = new ResultsDirectoryArgumentProcessorCapabilities(); Assert.AreEqual("/ResultsDirectory", capabilities.CommandName); - //StringAssert.Contains(capabilities.HelpContentResourceName, "Test results directory will be created from a given path"); + Assert.AreEqual("--ResultsDirectory|/ResultsDirectory\n Test results directory will be created from a given path \n Example: /ResultsDirectory:", capabilities.HelpContentResourceName); Assert.AreEqual(HelpContentPriority.ResultsDirectoryArgumentProcessorHelpPriority, capabilities.HelpPriority); Assert.AreEqual(false, capabilities.IsAction); @@ -62,8 +62,8 @@ public void InitializeShouldThrowIfArgumentIsNull() { var executor = new ResultsDirectoryArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); - /*var message = - @"The /ResultsDirectory parameter requires a value, where the test results should be saved. Example: /ResultsDirectory:c:\MyTestResultsDirectory";*/ + var message = + @"The /ResultsDirectory parameter requires a value, where the test results should be saved. Example: /ResultsDirectory:c:\MyTestResultsDirectory"; var isExceptionThrown = false; @@ -75,7 +75,7 @@ public void InitializeShouldThrowIfArgumentIsNull() { isExceptionThrown = true; Assert.IsTrue(ex is CommandLineException); - //Assert.AreEqual(message, ex.Message); + Assert.AreEqual(message, ex.Message); } Assert.IsTrue(isExceptionThrown); @@ -86,8 +86,8 @@ public void InitializeShouldThrowIfArgumentIsAWhiteSpace() { var executor = new ResultsDirectoryArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); - /*var message = - @"The /ResultsDirectory parameter requires a value, where the test results should be saved. Example: /ResultsDirectory:c:\MyTestResultsDirectory";*/ + var message = + @"The /ResultsDirectory parameter requires a value, where the test results should be saved. Example: /ResultsDirectory:c:\MyTestResultsDirectory"; var isExceptionThrown = false; @@ -99,7 +99,7 @@ public void InitializeShouldThrowIfArgumentIsAWhiteSpace() { isExceptionThrown = true; Assert.IsTrue(ex is CommandLineException); - //Assert.AreEqual(message, ex.Message); + Assert.AreEqual(message, ex.Message); } Assert.IsTrue(isExceptionThrown); @@ -112,10 +112,10 @@ public void InitializeShouldThrowIfRelativePathIsInvalid() var folder = @".\path\to\in:valid"; - /*var message = string.Format( + var message = string.Format( @"The path '{0}' specified in the 'ResultsDirectory' is invalid. Error: {1}", folder, - "Please provide valid path");*/ + "The given path's format is not supported."); var isExceptionThrown = false; @@ -127,7 +127,7 @@ public void InitializeShouldThrowIfRelativePathIsInvalid() { isExceptionThrown = true; Assert.IsTrue(ex is CommandLineException); - //Assert.AreEqual(message, ex.Message); + Assert.AreEqual(message, ex.Message); } Assert.IsTrue(isExceptionThrown); @@ -140,10 +140,10 @@ public void InitializeShouldThrowIfPathIsInvalid() var folder = @"c:\path\to\in:valid"; - /*var message = string.Format( + var message = string.Format( @"The path '{0}' specified in the 'ResultsDirectory' is invalid. Error: {1}", folder, - "Please provide valid path");*/ + "The given path's format is not supported."); var isExceptionThrown = false; @@ -155,7 +155,7 @@ public void InitializeShouldThrowIfPathIsInvalid() { isExceptionThrown = true; Assert.IsTrue(ex is CommandLineException); - //Assert.AreEqual(message, ex.Message); + Assert.AreEqual(message, ex.Message); } Assert.IsTrue(isExceptionThrown); diff --git a/test/vstest.console.UnitTests/Processors/RunSettingsArgumentProcessortTests.cs b/test/vstest.console.UnitTests/Processors/RunSettingsArgumentProcessortTests.cs index a21a25e27d..e254ec8836 100644 --- a/test/vstest.console.UnitTests/Processors/RunSettingsArgumentProcessortTests.cs +++ b/test/vstest.console.UnitTests/Processors/RunSettingsArgumentProcessortTests.cs @@ -152,6 +152,33 @@ public void InitializeShouldSetActiveRunSettings() // Assert. Assert.IsNotNull(settingsProvider.ActiveRunSettings); + Assert.AreEqual(fileName, CommandLineOptions.Instance.SettingsFile); + } + + [TestMethod] + public void InitializeShouldSetSettingsFileForCommandLineOptions() + { + // Arrange. + var fileName = "C:\\temp\\r.runsettings"; + var settingsXml = ""; + + var settingsProvider = new TestableRunSettingsProvider(); + + var executor = new TestableRunSettingsArgumentExecutor( + CommandLineOptions.Instance, + settingsProvider, + settingsXml); + + // Setup mocks. + var mockFileHelper = new Mock(); + mockFileHelper.Setup(fh => fh.Exists(It.IsAny())).Returns(true); + executor.FileHelper = mockFileHelper.Object; + + // Act. + executor.Initialize(fileName); + + // Assert. + Assert.AreEqual(fileName, CommandLineOptions.Instance.SettingsFile); } [TestMethod] From 2fd9b859bc425c550e2f0cac1f016419c5697881 Mon Sep 17 00:00:00 2001 From: samadala Date: Fri, 6 Jan 2017 11:29:38 +0530 Subject: [PATCH 06/10] Add unit test for RunSettingsUtilities --- .../Utilities/RunSettingsUtilities.cs | 41 ++++--- .../ResultsDirectoryArgumentProcessorTests.cs | 4 +- .../RunSpecificTestsArgumentProcessorTests.cs | 6 +- .../Utilities/RunSettingsUtilitiesTests.cs | 109 +++++------------- 4 files changed, 56 insertions(+), 104 deletions(-) diff --git a/src/vstest.console/Processors/Utilities/RunSettingsUtilities.cs b/src/vstest.console/Processors/Utilities/RunSettingsUtilities.cs index b8da65547f..d52c043faa 100644 --- a/src/vstest.console/Processors/Utilities/RunSettingsUtilities.cs +++ b/src/vstest.console/Processors/Utilities/RunSettingsUtilities.cs @@ -1,8 +1,6 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using Microsoft.VisualStudio.TestPlatform.Common; - namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities { using System.Diagnostics.CodeAnalysis; @@ -13,6 +11,7 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities using Microsoft.VisualStudio.TestPlatform.Common.Interfaces; using Microsoft.VisualStudio.TestPlatform.ObjectModel; using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities; + using Microsoft.VisualStudio.TestPlatform.Common; /// /// Utilities to get the run settings from the provider and the commandline options specified. @@ -21,8 +20,17 @@ internal class RunSettingsUtilities { private const string EmptyRunSettings = @""; + public static void UpdateRunSettings(IRunSettingsProvider runSettingsManager, string runsettingsXml) + { + var runSettings = new RunSettings(); + runSettings.LoadSettingsXml(runsettingsXml); + runSettingsManager.SetActiveRunSettings(runSettings); + } + public static void AddDefaultRunSettings(IRunSettingsProvider runSettingsProvider) { + ValidateArg.NotNull(runSettingsProvider, nameof(runSettingsProvider)); + var runSettingsXml = runSettingsProvider?.ActiveRunSettings?.SettingsXml; if (string.IsNullOrWhiteSpace(runSettingsXml)) @@ -34,14 +42,16 @@ public static void AddDefaultRunSettings(IRunSettingsProvider runSettingsProvide RunSettingsUtilities.UpdateRunSettings(runSettingsProvider, runSettingsXml); } - public static XmlDocument GetRunSettingXmlDocument(IRunSettingsProvider runSettingsManager) + public static XmlDocument GetRunSettingXmlDocument(IRunSettingsProvider runSettingsProvider) { + ValidateArg.NotNull(runSettingsProvider, nameof(runSettingsProvider)); + var doc = new XmlDocument(); - if (runSettingsManager.ActiveRunSettings != null && - !string.IsNullOrEmpty(runSettingsManager.ActiveRunSettings.SettingsXml)) + if (runSettingsProvider.ActiveRunSettings != null && + !string.IsNullOrEmpty(runSettingsProvider.ActiveRunSettings.SettingsXml)) { - var settingsXml = runSettingsManager.ActiveRunSettings.SettingsXml; + var settingsXml = runSettingsProvider.ActiveRunSettings.SettingsXml; #if net46 using (var reader = XmlReader.Create(new StringReader(settingsXml), new XmlReaderSettings() { XmlResolver = null, CloseInput = true, DtdProcessing = DtdProcessing.Prohibit })) @@ -73,29 +83,24 @@ public static XmlDocument GetRunSettingXmlDocument(IRunSettingsProvider runSetti return doc; } - public static void UpdateRunSettings(IRunSettingsProvider runSettingsManager, string runsettingsXml) - { - var runSettings = new RunSettings(); - runSettings.LoadSettingsXml(runsettingsXml); - runSettingsManager.SetActiveRunSettings(runSettings); - } - public static void UpdateRunSettingsXmlDocument(XmlDocument xmlDocument, string key, string value) { var node = GetXmlNode(xmlDocument, key) ?? RunSettingsUtilities.CreateNode(xmlDocument, key.Split('.')); node.InnerText = value; } - public static void UpdateRunSettingsNode(IRunSettingsProvider runSettingsManager, string key, string value) + public static void UpdateRunSettingsNode(IRunSettingsProvider runSettingsProvider, string key, string value) { - var xmlDocument = RunSettingsUtilities.GetRunSettingXmlDocument(runSettingsManager); + ValidateArg.NotNull(runSettingsProvider, nameof(runSettingsProvider)); + var xmlDocument = RunSettingsUtilities.GetRunSettingXmlDocument(runSettingsProvider); RunSettingsUtilities.UpdateRunSettingsXmlDocument(xmlDocument, key, value); - RunSettingsUtilities.UpdateRunSettings(runSettingsManager, xmlDocument.OuterXml); + RunSettingsUtilities.UpdateRunSettings(runSettingsProvider, xmlDocument.OuterXml); } - public static string QueryRunSettingsNode(IRunSettingsProvider runSettingsManager, string key) + public static string QueryRunSettingsNode(IRunSettingsProvider runSettingsProvider, string key) { - var xmlDocument = RunSettingsUtilities.GetRunSettingXmlDocument(runSettingsManager); + ValidateArg.NotNull(runSettingsProvider, nameof(runSettingsProvider)); + var xmlDocument = RunSettingsUtilities.GetRunSettingXmlDocument(runSettingsProvider); var node = GetXmlNode(xmlDocument, key); return node?.InnerText; } diff --git a/test/vstest.console.UnitTests/Processors/ResultsDirectoryArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/ResultsDirectoryArgumentProcessorTests.cs index fa612045cc..12f130c006 100644 --- a/test/vstest.console.UnitTests/Processors/ResultsDirectoryArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/ResultsDirectoryArgumentProcessorTests.cs @@ -162,7 +162,7 @@ public void InitializeShouldThrowIfPathIsInvalid() } [TestMethod] - public void InitializeShouldSetCommandLineOptionsAndRunSettingsFrameworkForRelativePathValue() + public void InitializeShouldSetCommandLineOptionsAndRunSettingsForRelativePathValue() { var executor = new ResultsDirectoryArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); var relativePath = @".\relative\path"; @@ -173,7 +173,7 @@ public void InitializeShouldSetCommandLineOptionsAndRunSettingsFrameworkForRelat } [TestMethod] - public void InitializeShouldSetCommandLineOptionsAndRunSettingsFrameworkForAbsolutePathValue() + public void InitializeShouldSetCommandLineOptionsAndRunSettingsForAbsolutePathValue() { var executor = new ResultsDirectoryArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); var absolutePath = @"c:\Users\someone\testresults"; diff --git a/test/vstest.console.UnitTests/Processors/RunSpecificTestsArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/RunSpecificTestsArgumentProcessorTests.cs index 2d02caaf61..c1cf327393 100644 --- a/test/vstest.console.UnitTests/Processors/RunSpecificTestsArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/RunSpecificTestsArgumentProcessorTests.cs @@ -1,10 +1,6 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; -using Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors.Utilities; -using Microsoft.VisualStudio.TestPlatform.Common; - namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors { using System; @@ -18,6 +14,8 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client; using Microsoft.VisualStudio.TestPlatform.Utilities; using Microsoft.VisualStudio.TestTools.UnitTesting; + using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; + using Microsoft.VisualStudio.TestPlatform.Common; using Moq; using CoreUtilities.Tracing; diff --git a/test/vstest.console.UnitTests/Processors/Utilities/RunSettingsUtilitiesTests.cs b/test/vstest.console.UnitTests/Processors/Utilities/RunSettingsUtilitiesTests.cs index 7cad1dfa09..99370e398e 100644 --- a/test/vstest.console.UnitTests/Processors/Utilities/RunSettingsUtilitiesTests.cs +++ b/test/vstest.console.UnitTests/Processors/Utilities/RunSettingsUtilitiesTests.cs @@ -10,6 +10,7 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors.U using Microsoft.VisualStudio.TestPlatform.Common; using Microsoft.VisualStudio.TestPlatform.Common.Interfaces; using Microsoft.VisualStudio.TestTools.UnitTesting; + using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities; using ObjectModel; [TestClass] @@ -22,115 +23,63 @@ public class RunSettingsUtilitiesTests public void TestCleanup() { CommandLineOptions.Instance.Reset(); - } - /* - [TestMethod] - public void GetRunSettingsShouldReturnDefaultRunSettingsIfProviderIsNull() - { - var runSettings = RunSettingsUtilities.GetRunSettings(null, null); - - Assert.AreEqual(this.GetDefaultRunSettings(), runSettings); - } - - [TestMethod] - public void GetRunSettingsShouldReturnDefaultRunSettingsIfActiveRunSettingsIsNull() - { - var settingsProvider = new TestableRunSettingsProvider(); - settingsProvider.SetActiveRunSettings(runSettings: null); - - var runSettings = RunSettingsUtilities.GetRunSettings(settingsProvider, null); - - Assert.AreEqual(this.GetDefaultRunSettings(), runSettings); - } - - [TestMethod] - public void GetRunSettingsShouldReturnRunSettingsFromTheProvider() - { - var settingsProvider = new TestableRunSettingsProvider(); - - var settings = "\r\n \r\n 432423\r\n \r\n"; - settingsProvider.SetActiveRunSettings(settings); - - var receivedRunSettings = RunSettingsUtilities.GetRunSettings(settingsProvider, null); - - StringAssert.Contains(receivedRunSettings, "432423"); + RunSettingsManager.Instance = null; } [TestMethod] - public void GetRunSettingsShouldReturnSettingsWithPlatformSpecifiedInCommandLineOptions() + public void UpdateRunSettingsShouldUpdateGivenSettingsXml() { - var settingsProvider = new TestableRunSettingsProvider(); - settingsProvider.SetActiveRunSettings(runSettings: null); - - CommandLineOptions.Instance.TargetArchitecture = ObjectModel.Architecture.X64; + var runSettingsManager = RunSettingsManager.Instance; + const string runSettingsXml = "\r\n \r\n X86\r\n \r\n"; - var runSettings = RunSettingsUtilities.GetRunSettings(settingsProvider, CommandLineOptions.Instance); + RunSettingsUtilities.UpdateRunSettings(runSettingsManager, runSettingsXml); - var defaultRunSettings = this.GetDefaultRunSettings(); - //Replace with the platform specified. - var expectedSettings = defaultRunSettings.Replace("X86", "X64"); - Assert.AreEqual(expectedSettings, runSettings); + StringAssert.Contains(runSettingsManager.ActiveRunSettings.SettingsXml, runSettingsXml); } [TestMethod] - public void GetRunSettingsShouldReturnSettingsWithFrameworkSpecifiedInCommandLineOptions() + public void AddDefaultRunSettingsShouldSetDefaultSettingsForEmptySettings() { - var settingsProvider = new TestableRunSettingsProvider(); - settingsProvider.SetActiveRunSettings(runSettings: null); + var runSettingsManager = RunSettingsManager.Instance; - CommandLineOptions.Instance.TargetFrameworkVersion = Framework.FromString(".NETFramework,Version=v3.5"); + RunSettingsUtilities.AddDefaultRunSettings(runSettingsManager); - var runSettings = RunSettingsUtilities.GetRunSettings(settingsProvider, CommandLineOptions.Instance); - - var defaultRunSettings = this.GetDefaultRunSettings(); - - //Replace with the framework specified. - var expectedSettings = defaultRunSettings.Replace(Framework.DefaultFramework.Name, ".NETFramework,Version=v3.5"); - Assert.AreEqual(expectedSettings, runSettings); + RunConfiguration runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(RunSettingsManager.Instance.ActiveRunSettings.SettingsXml); + Assert.AreEqual(runConfiguration.ResultsDirectory, Constants.DefaultResultsDirectory); + Assert.AreEqual(runConfiguration.TargetFrameworkVersion.ToString(), Framework.DefaultFramework.ToString()); + Assert.AreEqual(runConfiguration.TargetPlatform, Constants.DefaultPlatform); } [TestMethod] - public void GetRunSettingsShouldReturnSettingsWithoutParallelOptionWhenParallelIsOff() + public void AddDefaultRunSettingsShouldAddUnspecifiedSettings() { - var settingsProvider = new TestableRunSettingsProvider(); - settingsProvider.SetActiveRunSettings(runSettings: null); - - // Do not have to explicitly set - but for readability - CommandLineOptions.Instance.Parallel = false; + var runSettingsManager = RunSettingsManager.Instance; + RunSettingsUtilities.UpdateRunSettings(runSettingsManager, "\r\n \r\n X86\r\n \r\n"); - var runSettings = RunSettingsUtilities.GetRunSettings(settingsProvider, CommandLineOptions.Instance); + RunSettingsUtilities.AddDefaultRunSettings(runSettingsManager); - Assert.IsTrue(!runSettings.Contains("MaxCpuCount"), "MaxCpuCount must not be set if parallel setting is false."); + RunConfiguration runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(RunSettingsManager.Instance.ActiveRunSettings.SettingsXml); + Assert.AreEqual(runConfiguration.ResultsDirectory, Constants.DefaultResultsDirectory); + Assert.AreEqual(runConfiguration.TargetFrameworkVersion.ToString(), Framework.DefaultFramework.ToString()); } - [TestMethod] - public void GetRunSettingsShouldReturnSettingsWithParallelOptionWhenParallelIsOn() + public void AddDefaultRunSettingsShouldNotChangeSpecifiedSettings() { - var settingsProvider = new TestableRunSettingsProvider(); - settingsProvider.SetActiveRunSettings(runSettings: null); + var runSettingsManager = RunSettingsManager.Instance; + RunSettingsUtilities.UpdateRunSettings(runSettingsManager, "\r\n \r\n X64\r\n \r\n"); - CommandLineOptions.Instance.Parallel = true; + RunSettingsUtilities.AddDefaultRunSettings(runSettingsManager); - var runSettings = RunSettingsUtilities.GetRunSettings(settingsProvider, CommandLineOptions.Instance); - StringAssert.Contains(runSettings, "0", "MaxCpuCount must be set to 0 if Parallel Enabled."); + RunConfiguration runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(RunSettingsManager.Instance.ActiveRunSettings.SettingsXml); + Assert.AreEqual(runConfiguration.TargetPlatform, Architecture.X64); } [TestMethod] - public void GetRunSettingsShouldReturnWithoutChangeIfUserProvidesBothParallelSwitchAndSettings() + public void AddDefaultRunSettingsShouldThrowExceptionIfArgumentIsNull() { - string settingXml = @"2"; - var settingsProvider = new TestableRunSettingsProvider(); - settingsProvider.SetActiveRunSettings(settingXml); - - CommandLineOptions.Instance.Parallel = true; - - var runSettings = RunSettingsUtilities.GetRunSettings(settingsProvider, CommandLineOptions.Instance); - - var parallelValue = Environment.ProcessorCount; - StringAssert.Contains(runSettings, "2", "RunSettings Parallel value should take precendence over parallel switch."); + Assert.ThrowsException(() => RunSettingsUtilities.AddDefaultRunSettings(null)); } - */ #region Testable Implementations From ba83348d122a5cfd51d4ae5eb117e2a99de458b1 Mon Sep 17 00:00:00 2001 From: samadala Date: Sun, 8 Jan 2017 22:47:27 +0530 Subject: [PATCH 07/10] * Remove RunSettingsManager singleton instance in tests * Add tests for RunSettingsProviderExtensions *Nit fixes --- .../ValidateArg.cs | 26 +++ src/vstest.console/CommandLine/Executor.cs | 2 +- .../CLIRunSettingsArgumentProcessor.cs | 11 +- .../Processors/FrameworkArgumentProcessor.cs | 21 ++- .../Processors/ParallelArgumentProcessor.cs | 2 +- .../Processors/PlatformArgumentProcessor.cs | 9 +- .../ResultsDirectoryArgumentProcessor.cs | 22 +-- .../RunSettingsArgumentProcessor.cs | 12 +- .../RunSpecificTestsArgumentProcessor.cs | 2 +- .../Processors/RunTestsArgumentProcessor.cs | 2 +- ...es.cs => RunSettingsProviderExtensions.cs} | 140 +++++++------- .../Resources/Resources.Designer.cs | 5 +- src/vstest.console/Resources/Resources.resx | 3 +- .../ResultsDirectoryTests.cs | 6 +- .../IntegrationTestEnvironment.cs | 6 +- .../CLIRunSettingsArgumentProcessorTests.cs | 112 ++++------- .../FrameworkArgumentProcessorTests.cs | 55 +++--- .../ListTestsArgumentProcessorTests.cs | 76 ++++---- .../ParallelArgumentProcessorTests.cs | 24 ++- .../PlatformArgumentProcessorTests.cs | 45 ++--- .../ResultsDirectoryArgumentProcessorTests.cs | 107 ++++------- ...s => RunSettingsArgumentProcessorTests.cs} | 67 +++---- .../RunSpecificTestsArgumentProcessorTests.cs | 37 ++-- .../RunTestsArgumentProcessorTests.cs | 41 ++--- .../Processors/TestableRunSettingsProvider.cs | 22 +++ .../RunSettingsProviderExtensionsTests.cs | 174 ++++++++++++++++++ .../Utilities/RunSettingsUtilitiesTests.cs | 112 ----------- 27 files changed, 572 insertions(+), 569 deletions(-) rename src/vstest.console/Processors/Utilities/{RunSettingsUtilities.cs => RunSettingsProviderExtensions.cs} (61%) rename test/vstest.console.UnitTests/Processors/{RunSettingsArgumentProcessortTests.cs => RunSettingsArgumentProcessorTests.cs} (60%) create mode 100644 test/vstest.console.UnitTests/Processors/TestableRunSettingsProvider.cs create mode 100644 test/vstest.console.UnitTests/Processors/Utilities/RunSettingsProviderExtensionsTests.cs delete mode 100644 test/vstest.console.UnitTests/Processors/Utilities/RunSettingsUtilitiesTests.cs diff --git a/src/Microsoft.TestPlatform.CoreUtilities/ValidateArg.cs b/src/Microsoft.TestPlatform.CoreUtilities/ValidateArg.cs index aad87d6d51..e911a9117a 100644 --- a/src/Microsoft.TestPlatform.CoreUtilities/ValidateArg.cs +++ b/src/Microsoft.TestPlatform.CoreUtilities/ValidateArg.cs @@ -74,6 +74,32 @@ public static string NotNullOrEmpty([ValidatedNotNull]string arg, string paramet return arg; } + /// + /// Validate a string is not null, empty or consists only of white-space characters. + /// + /// + /// Input string. + /// + /// + /// Name of the parameter to validate. + /// + /// + /// Validated string. + /// + /// + /// Thrown if the input string is null null, empty or consists only of white-space characters. + /// + [DebuggerStepThrough] + public static string NotNullOrWhiteSpace([ValidatedNotNull]string arg, string parameterName) + { + if (string.IsNullOrWhiteSpace(arg)) + { + throw new ArgumentNullException(parameterName); + } + + return arg; + } + /// /// Throws ArgumentOutOfRangeException if the argument is less than zero. /// diff --git a/src/vstest.console/CommandLine/Executor.cs b/src/vstest.console/CommandLine/Executor.cs index e2656b09e4..63ffce4a69 100644 --- a/src/vstest.console/CommandLine/Executor.cs +++ b/src/vstest.console/CommandLine/Executor.cs @@ -193,7 +193,7 @@ private int GetArgumentProcessors(string[] args, out List pr processors.AddRange(processorsToAlwaysExecute); // Initialize Runsettings with defaults - RunSettingsUtilities.AddDefaultRunSettings(RunSettingsManager.Instance); + RunSettingsManager.Instance.AddDefaultRunSettings(); // Ensure we have an action argument. this.EnsureActionArgumentIsPresent(processors, processorFactory); diff --git a/src/vstest.console/Processors/CLIRunSettingsArgumentProcessor.cs b/src/vstest.console/Processors/CLIRunSettingsArgumentProcessor.cs index 4e03423d9a..610f32ad3c 100644 --- a/src/vstest.console/Processors/CLIRunSettingsArgumentProcessor.cs +++ b/src/vstest.console/Processors/CLIRunSettingsArgumentProcessor.cs @@ -115,13 +115,8 @@ public void Initialize(string[] arguments) // Load up the run settings and set it as the active run settings. try { - var doc = RunSettingsUtilities.GetRunSettingXmlDocument(this.runSettingsManager); - // Append / Override run settings supplied in CLI - CreateOrOverwriteRunSettings(doc, arguments); - - // Set Active Run Settings. - RunSettingsUtilities.UpdateRunSettings(this.runSettingsManager, doc.OuterXml); + CreateOrOverwriteRunSettings(this.runSettingsManager, arguments); } catch (XPathException exception) { @@ -139,7 +134,7 @@ public ArgumentProcessorResult Execute() return ArgumentProcessorResult.Success; } - private void CreateOrOverwriteRunSettings(XmlDocument xmlDoc, string[] args) + private void CreateOrOverwriteRunSettings(IRunSettingsProvider runSettingsProvider, string[] args) { var length = args.Length; @@ -159,7 +154,7 @@ private void CreateOrOverwriteRunSettings(XmlDocument xmlDoc, string[] args) continue; } - RunSettingsUtilities.UpdateRunSettingsXmlDocument(xmlDoc, key, value); + runSettingsProvider.UpdateRunSettingsNode(key, value); } } } diff --git a/src/vstest.console/Processors/FrameworkArgumentProcessor.cs b/src/vstest.console/Processors/FrameworkArgumentProcessor.cs index 0b15a354e3..cf29800818 100644 --- a/src/vstest.console/Processors/FrameworkArgumentProcessor.cs +++ b/src/vstest.console/Processors/FrameworkArgumentProcessor.cs @@ -141,20 +141,27 @@ public void Initialize(string argument) throw new CommandLineException( string.Format(CultureInfo.CurrentCulture, CommandLineResources.InvalidFrameworkVersion, argument)); } - this.commandLineOptions.TargetFrameworkVersion = validFramework; - RunSettingsUtilities.UpdateRunSettingsNode(this.runSettingsManager, FrameworkArgumentExecutor.RunSettingsPath, validFramework.ToString()); - if (EqtTrace.IsInfoEnabled) - { - EqtTrace.Info("Using .Net Framework version:{0}", commandLineOptions.TargetFrameworkVersion); - } + this.commandLineOptions.TargetFrameworkVersion = validFramework; if (this.commandLineOptions.TargetFrameworkVersion != Framework.DefaultFramework && !string.IsNullOrWhiteSpace(this.commandLineOptions.SettingsFile) && MSTestSettingsUtilities.IsLegacyTestSettingsFile(this.commandLineOptions.SettingsFile)) { + // Legacy testsettings file support only default target framework. IOutput output = ConsoleOutput.Instance; - output.Warning(CommandLineResources.TestSettingsFrameworkMismatch, this.commandLineOptions.TargetFrameworkVersion.ToString(), Framework.DefaultFramework.ToString()); + output.Warning(CommandLineResources.TestSettingsFrameworkMismatch, + this.commandLineOptions.TargetFrameworkVersion.ToString(), Framework.DefaultFramework.ToString()); + } + else + { + this.runSettingsManager.UpdateRunSettingsNode(FrameworkArgumentExecutor.RunSettingsPath, + validFramework.ToString()); + } + + if (EqtTrace.IsInfoEnabled) + { + EqtTrace.Info("Using .Net Framework version:{0}", commandLineOptions.TargetFrameworkVersion); } } diff --git a/src/vstest.console/Processors/ParallelArgumentProcessor.cs b/src/vstest.console/Processors/ParallelArgumentProcessor.cs index 4f683a7b57..55cb54b1e4 100644 --- a/src/vstest.console/Processors/ParallelArgumentProcessor.cs +++ b/src/vstest.console/Processors/ParallelArgumentProcessor.cs @@ -132,7 +132,7 @@ public void Initialize(string argument) } commandLineOptions.Parallel = true; - RunSettingsUtilities.UpdateRunSettingsNode(this.runSettingsManager, ParallelArgumentExecutor.RunSettingsPath, "0"); + this.runSettingsManager.UpdateRunSettingsNode(ParallelArgumentExecutor.RunSettingsPath, "0"); } /// diff --git a/src/vstest.console/Processors/PlatformArgumentProcessor.cs b/src/vstest.console/Processors/PlatformArgumentProcessor.cs index 64a296291e..1337f5518d 100644 --- a/src/vstest.console/Processors/PlatformArgumentProcessor.cs +++ b/src/vstest.console/Processors/PlatformArgumentProcessor.cs @@ -1,16 +1,15 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; -using Microsoft.VisualStudio.TestPlatform.Common; -using Microsoft.VisualStudio.TestPlatform.Common.Interfaces; - namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors { using System; using System.Diagnostics.Contracts; using System.Globalization; + using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; + using Microsoft.VisualStudio.TestPlatform.Common; + using Microsoft.VisualStudio.TestPlatform.Common.Interfaces; using Microsoft.VisualStudio.TestPlatform.ObjectModel; using CommandLineResources = Microsoft.VisualStudio.TestPlatform.CommandLine.Resources.Resources; @@ -144,7 +143,7 @@ public void Initialize(string argument) if (validPlatform) { this.commandLineOptions.TargetArchitecture = platform; - RunSettingsUtilities.UpdateRunSettingsNode(this.runSettingsManager, PlatformArgumentExecutor.RunSettingsPath, platform.ToString()); + this.runSettingsManager.UpdateRunSettingsNode(PlatformArgumentExecutor.RunSettingsPath, platform.ToString()); } else { diff --git a/src/vstest.console/Processors/ResultsDirectoryArgumentProcessor.cs b/src/vstest.console/Processors/ResultsDirectoryArgumentProcessor.cs index 771a98b72f..7213972650 100644 --- a/src/vstest.console/Processors/ResultsDirectoryArgumentProcessor.cs +++ b/src/vstest.console/Processors/ResultsDirectoryArgumentProcessor.cs @@ -1,24 +1,16 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using System.Xml; -using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; -using Microsoft.VisualStudio.TestPlatform.Common; -using Microsoft.VisualStudio.TestPlatform.Common.Interfaces; -using Microsoft.VisualStudio.TestPlatform.ObjectModel; -using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities; - namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors { using System; - using System.Collections.Generic; using System.Diagnostics.Contracts; - using System.Globalization; using System.IO; + using System.Security; - using Microsoft.VisualStudio.TestPlatform.Client; - using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client; - using Microsoft.VisualStudio.TestPlatform.Utilities; + using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; + using Microsoft.VisualStudio.TestPlatform.Common; + using Microsoft.VisualStudio.TestPlatform.Common.Interfaces; using CommandLineResources = Microsoft.VisualStudio.TestPlatform.CommandLine.Resources.Resources; /// @@ -155,13 +147,13 @@ public void Initialize(string argument) argument = Path.GetFullPath(argument); } } - catch (Exception ex) + catch (Exception ex) when( ex is NotSupportedException || ex is SecurityException || ex is ArgumentException || ex is PathTooLongException) { - throw new CommandLineException( String.Format(CommandLineResources.InvalidResultsDirectoryPathCommand, argument, ex.Message)); + throw new CommandLineException(string.Format(CommandLineResources.InvalidResultsDirectoryPathCommand, argument, ex.Message)); } this.commandLineOptions.ResultsDirectory = argument; - RunSettingsUtilities.UpdateRunSettingsNode(this.runSettingsManager, ResultsDirectoryArgumentExecutor.RunSettingsPath, argument); + this.runSettingsManager.UpdateRunSettingsNode(ResultsDirectoryArgumentExecutor.RunSettingsPath, argument); } /// diff --git a/src/vstest.console/Processors/RunSettingsArgumentProcessor.cs b/src/vstest.console/Processors/RunSettingsArgumentProcessor.cs index b4ed9f0420..4998fa4eff 100644 --- a/src/vstest.console/Processors/RunSettingsArgumentProcessor.cs +++ b/src/vstest.console/Processors/RunSettingsArgumentProcessor.cs @@ -125,10 +125,10 @@ public void Initialize(string argument) try { IXPathNavigable document = this.GetRunSettingsDocument(argument); - RunSettingsUtilities.UpdateRunSettings(this.runSettingsManager, document.CreateNavigator().OuterXml); + this.runSettingsManager.UpdateRunSettings(document.CreateNavigator().OuterXml); //Add default runsettings values if not exists in given runsettings file. - RunSettingsUtilities.AddDefaultRunSettings(this.runSettingsManager); + this.runSettingsManager.AddDefaultRunSettings(); this.commandLineOptions.SettingsFile = argument; } @@ -172,13 +172,7 @@ private IXPathNavigable GetRunSettingsDocument(string runSettingsFile) else { runSettingsDocument = XmlRunSettingsUtilities.CreateDefaultRunSettings(); - -#if NET46 - FrameworkVersion frameworkVersion = FrameworkVersion.Framework45; -#else - FrameworkVersion frameworkVersion = FrameworkVersion.FrameworkCore10; -#endif - runSettingsDocument = MSTestSettingsUtilities.Import(runSettingsFile, runSettingsDocument, Architecture.X86, frameworkVersion); + runSettingsDocument = MSTestSettingsUtilities.Import(runSettingsFile, runSettingsDocument, Architecture.X86, FrameworkVersion.Framework45); } if (this.commandLineOptions.EnableCodeCoverage == true) diff --git a/src/vstest.console/Processors/RunSpecificTestsArgumentProcessor.cs b/src/vstest.console/Processors/RunSpecificTestsArgumentProcessor.cs index 63d4ea67b4..da70550ac0 100644 --- a/src/vstest.console/Processors/RunSpecificTestsArgumentProcessor.cs +++ b/src/vstest.console/Processors/RunSpecificTestsArgumentProcessor.cs @@ -152,7 +152,7 @@ public RunSpecificTestsArgumentExecutor( this.commandLineOptions = options; this.testRequestManager = testRequestManager; - this.runSettingsManager = RunSettingsManager.Instance; + this.runSettingsManager = runSettingsProvider; this.output = ConsoleOutput.Instance; this.discoveryEventsRegistrar = new DiscoveryEventsRegistrar(this.discoveryRequest_OnDiscoveredTests); } diff --git a/src/vstest.console/Processors/RunTestsArgumentProcessor.cs b/src/vstest.console/Processors/RunTestsArgumentProcessor.cs index c549a9679a..d71603f54b 100644 --- a/src/vstest.console/Processors/RunTestsArgumentProcessor.cs +++ b/src/vstest.console/Processors/RunTestsArgumentProcessor.cs @@ -128,7 +128,7 @@ public RunTestsArgumentExecutor( this.output = ConsoleOutput.Instance; - this.runSettingsManager = RunSettingsManager.Instance; + this.runSettingsManager = runSettingsProvider; this.testRequestManager = testRequestManager; this.testRunEventsRegistrar = new TestRunRequestEventsRegistrar(); } diff --git a/src/vstest.console/Processors/Utilities/RunSettingsUtilities.cs b/src/vstest.console/Processors/Utilities/RunSettingsProviderExtensions.cs similarity index 61% rename from src/vstest.console/Processors/Utilities/RunSettingsUtilities.cs rename to src/vstest.console/Processors/Utilities/RunSettingsProviderExtensions.cs index d52c043faa..3d89867df6 100644 --- a/src/vstest.console/Processors/Utilities/RunSettingsUtilities.cs +++ b/src/vstest.console/Processors/Utilities/RunSettingsProviderExtensions.cs @@ -3,9 +3,11 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities { + using System; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Xml; + using System.Diagnostics.Contracts; using Microsoft.VisualStudio.TestPlatform.Utilities; using Microsoft.VisualStudio.TestPlatform.Common.Interfaces; @@ -16,22 +18,25 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities /// /// Utilities to get the run settings from the provider and the commandline options specified. /// - internal class RunSettingsUtilities + internal static class RunSettingsProviderExtensions { - private const string EmptyRunSettings = @""; + public const string EmptyRunSettings = @""; - public static void UpdateRunSettings(IRunSettingsProvider runSettingsManager, string runsettingsXml) + public static void UpdateRunSettings(this IRunSettingsProvider runSettingsProvider, string runsettingsXml) { + ValidateArg.NotNull(runSettingsProvider, nameof(runSettingsProvider)); + ValidateArg.NotNullOrWhiteSpace(runsettingsXml, nameof(runsettingsXml)); + var runSettings = new RunSettings(); runSettings.LoadSettingsXml(runsettingsXml); - runSettingsManager.SetActiveRunSettings(runSettings); + runSettingsProvider.SetActiveRunSettings(runSettings); } - public static void AddDefaultRunSettings(IRunSettingsProvider runSettingsProvider) + public static void AddDefaultRunSettings(this IRunSettingsProvider runSettingsProvider) { ValidateArg.NotNull(runSettingsProvider, nameof(runSettingsProvider)); - var runSettingsXml = runSettingsProvider?.ActiveRunSettings?.SettingsXml; + var runSettingsXml = runSettingsProvider.ActiveRunSettings?.SettingsXml; if (string.IsNullOrWhiteSpace(runSettingsXml)) { @@ -39,68 +44,26 @@ public static void AddDefaultRunSettings(IRunSettingsProvider runSettingsProvide } runSettingsXml = AddDefaultRunSettings(runSettingsXml); - RunSettingsUtilities.UpdateRunSettings(runSettingsProvider, runSettingsXml); + runSettingsProvider.UpdateRunSettings(runSettingsXml); } - public static XmlDocument GetRunSettingXmlDocument(IRunSettingsProvider runSettingsProvider) + public static void UpdateRunSettingsNode(this IRunSettingsProvider runSettingsProvider, string key, string data) { ValidateArg.NotNull(runSettingsProvider, nameof(runSettingsProvider)); + ValidateArg.NotNullOrWhiteSpace(key, nameof(key)); + ValidateArg.NotNull(data, nameof(data)); - var doc = new XmlDocument(); - - if (runSettingsProvider.ActiveRunSettings != null && - !string.IsNullOrEmpty(runSettingsProvider.ActiveRunSettings.SettingsXml)) - { - var settingsXml = runSettingsProvider.ActiveRunSettings.SettingsXml; - -#if net46 - using (var reader = XmlReader.Create(new StringReader(settingsXml), new XmlReaderSettings() { XmlResolver = null, CloseInput = true, DtdProcessing = DtdProcessing.Prohibit })) - { -#else - using ( - var reader = XmlReader.Create(new StringReader(settingsXml), - new XmlReaderSettings() { CloseInput = true, DtdProcessing = DtdProcessing.Prohibit })) - { -#endif - doc.Load(reader); - } - } - else - { -#if net46 - doc = (XmlDocument)XmlRunSettingsUtilities.CreateDefaultRunSettings(); -#else - using ( - var reader = - XmlReader.Create( - new StringReader(XmlRunSettingsUtilities.CreateDefaultRunSettings().CreateNavigator().OuterXml), - new XmlReaderSettings() { CloseInput = true, DtdProcessing = DtdProcessing.Prohibit })) - { - doc.Load(reader); - } -#endif - } - return doc; - } - - public static void UpdateRunSettingsXmlDocument(XmlDocument xmlDocument, string key, string value) - { - var node = GetXmlNode(xmlDocument, key) ?? RunSettingsUtilities.CreateNode(xmlDocument, key.Split('.')); - node.InnerText = value; + var xmlDocument = runSettingsProvider.GetRunSettingXmlDocument(); + RunSettingsProviderExtensions.UpdateRunSettingsXmlDocument(xmlDocument, key, data); + runSettingsProvider.UpdateRunSettings(xmlDocument.OuterXml); } - public static void UpdateRunSettingsNode(IRunSettingsProvider runSettingsProvider, string key, string value) + public static string QueryRunSettingsNode(this IRunSettingsProvider runSettingsProvider, string key) { ValidateArg.NotNull(runSettingsProvider, nameof(runSettingsProvider)); - var xmlDocument = RunSettingsUtilities.GetRunSettingXmlDocument(runSettingsProvider); - RunSettingsUtilities.UpdateRunSettingsXmlDocument(xmlDocument, key, value); - RunSettingsUtilities.UpdateRunSettings(runSettingsProvider, xmlDocument.OuterXml); - } + ValidateArg.NotNullOrWhiteSpace(key, nameof(key)); - public static string QueryRunSettingsNode(IRunSettingsProvider runSettingsProvider, string key) - { - ValidateArg.NotNull(runSettingsProvider, nameof(runSettingsProvider)); - var xmlDocument = RunSettingsUtilities.GetRunSettingXmlDocument(runSettingsProvider); + var xmlDocument = runSettingsProvider.GetRunSettingXmlDocument(); var node = GetXmlNode(xmlDocument, key); return node?.InnerText; } @@ -131,20 +94,15 @@ private static string AddDefaultRunSettings(string runSettings) } } - private static XmlNode CreateNode(XmlDocument doc, string[] xPath) + private static XmlNode CreateNode(XmlDocument doc, string xPath) { + var path = xPath.Split('.'); XmlNode node = null; XmlNode parent = doc.DocumentElement; - for (int i = 0; i < xPath.Length; i++) + for (var i = 0; i < path.Length; i++) { - node = parent.SelectSingleNode(xPath[i]); - - if (node == null) - { - node = parent.AppendChild(doc.CreateElement(xPath[i])); - } - + node = parent.SelectSingleNode(path[i]) ?? parent.AppendChild(doc.CreateElement(path[i])); parent = node; } @@ -157,5 +115,53 @@ private static XmlNode GetXmlNode(XmlDocument xmlDocument, string key) var node = xmlDocument.SelectSingleNode(string.Format("//RunSettings/{0}", xPath)); return node; } + + private static XmlDocument GetRunSettingXmlDocument(this IRunSettingsProvider runSettingsProvider) + { + ValidateArg.NotNull(runSettingsProvider, nameof(runSettingsProvider)); + + var doc = new XmlDocument(); + + if (runSettingsProvider.ActiveRunSettings != null && + !string.IsNullOrEmpty(runSettingsProvider.ActiveRunSettings.SettingsXml)) + { + var settingsXml = runSettingsProvider.ActiveRunSettings.SettingsXml; + +#if NET46 + using (var reader = XmlReader.Create(new StringReader(settingsXml), new XmlReaderSettings() { XmlResolver = null, CloseInput = true, DtdProcessing = DtdProcessing.Prohibit })) + { +#else + using ( + var reader = XmlReader.Create(new StringReader(settingsXml), + new XmlReaderSettings() {CloseInput = true, DtdProcessing = DtdProcessing.Prohibit})) + { +#endif + doc.Load(reader); + } + } + else + { +#if NET46 + doc = (XmlDocument) XmlRunSettingsUtilities.CreateDefaultRunSettings(); +#else + using ( + var reader = + XmlReader.Create( + new StringReader( + XmlRunSettingsUtilities.CreateDefaultRunSettings().CreateNavigator().OuterXml), + new XmlReaderSettings() {CloseInput = true, DtdProcessing = DtdProcessing.Prohibit})) + { + doc.Load(reader); + } +#endif + } + return doc; + } + + private static void UpdateRunSettingsXmlDocument(XmlDocument xmlDocument, string key, string data) + { + var node = GetXmlNode(xmlDocument, key) ?? RunSettingsProviderExtensions.CreateNode(xmlDocument, key); + node.InnerText = data; + } } } diff --git a/src/vstest.console/Resources/Resources.Designer.cs b/src/vstest.console/Resources/Resources.Designer.cs index 58b5e0e9a4..2a1db4379e 100644 --- a/src/vstest.console/Resources/Resources.Designer.cs +++ b/src/vstest.console/Resources/Resources.Designer.cs @@ -1015,11 +1015,10 @@ public static string ProjectPathNotFound { /// /// Looks up a localized string similar to --ResultsDirectory|/ResultsDirectory - /// This makes vstest.console.exe process use custom test results directory - /// from a given path (if any) in the test run. + /// Test results directory will be created from a given path. /// Example /ResultsDirectory:<pathToResultsDirectory>. /// - public static string ResultsDirectoryArgumentHelp + internal static string ResultsDirectoryArgumentHelp { get { diff --git a/src/vstest.console/Resources/Resources.resx b/src/vstest.console/Resources/Resources.resx index 1a04f13f0c..8bf5a7eff5 100644 --- a/src/vstest.console/Resources/Resources.resx +++ b/src/vstest.console/Resources/Resources.resx @@ -625,8 +625,7 @@ --ResultsDirectory|/ResultsDirectory - This makes vstest.console.exe process use custom test results directory - from a given path (if any) in the test run. + Test results directory will be created from a given path. Example /ResultsDirectory:<pathToResultsDirectory> diff --git a/test/Microsoft.TestPlatform.AcceptanceTests/ResultsDirectoryTests.cs b/test/Microsoft.TestPlatform.AcceptanceTests/ResultsDirectoryTests.cs index 4273765092..5a4a3c9de9 100644 --- a/test/Microsoft.TestPlatform.AcceptanceTests/ResultsDirectoryTests.cs +++ b/test/Microsoft.TestPlatform.AcceptanceTests/ResultsDirectoryTests.cs @@ -27,11 +27,11 @@ public void TrxFileShouldBeCreatedInResultsDirectory(string runnerFramework, str this.InvokeVsTest(arguments); - Assert.IsTrue(File.Exists(trxFilePath), $"Expexted Trx file: {trxFilePath} not created in results directory"); + Assert.IsTrue(File.Exists(trxFilePath), $"Expected Trx file: {trxFilePath} not created in results directory"); } //Getting Current directory C:\Windows\system32 https://github.com/Microsoft/vstest/issues/311 - //Failed create relative directory + //Failed to create relative directory [Ignore] [CustomDataTestMethod] [NET46TargetFramework] @@ -55,7 +55,7 @@ public void ResultsDirectoryRelativePathShouldWork(string runnerFramework, strin this.InvokeVsTest(arguments); - Assert.IsTrue(File.Exists(trxFilePath), $"Expexted Trx file: {trxFilePath} not created in results directory"); + Assert.IsTrue(File.Exists(trxFilePath), $"Expected Trx file: {trxFilePath} not created in results directory"); } } } diff --git a/test/Microsoft.TestPlatform.TestUtilities/IntegrationTestEnvironment.cs b/test/Microsoft.TestPlatform.TestUtilities/IntegrationTestEnvironment.cs index e83c10a6fa..cfd608caa0 100644 --- a/test/Microsoft.TestPlatform.TestUtilities/IntegrationTestEnvironment.cs +++ b/test/Microsoft.TestPlatform.TestUtilities/IntegrationTestEnvironment.cs @@ -42,8 +42,8 @@ public IntegrationTestEnvironment() { // Running in VS/IDE. Use artifacts directory as root. this.runningInCli = false; - this.testPlatformRootDirectory = Path.GetFullPath(@"..\..\.."); - this.TestAssetsPath = Path.Combine(this.testPlatformRootDirectory, @"artifacts\test\TestAssets"); + this.testPlatformRootDirectory = "C:/Users/samadala/src/vstest"; //Path.GetFullPath(@"..\..\.."); + this.TestAssetsPath = Path.Combine(this.testPlatformRootDirectory, @"test\TestAssets"); } else { @@ -91,7 +91,7 @@ public string PublishDirectory get { string value = string.Empty; - if (this.runningInCli) + if (this.runningInCli || true) { value = Path.Combine( this.testPlatformRootDirectory, diff --git a/test/vstest.console.UnitTests/Processors/CLIRunSettingsArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/CLIRunSettingsArgumentProcessorTests.cs index 1896d368ac..329ca5b5c6 100644 --- a/test/vstest.console.UnitTests/Processors/CLIRunSettingsArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/CLIRunSettingsArgumentProcessorTests.cs @@ -12,17 +12,24 @@ namespace vstest.console.UnitTests.Processors using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors; using Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests; using Microsoft.VisualStudio.TestPlatform.Common; - using Microsoft.VisualStudio.TestPlatform.Common.Interfaces; - using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] public class CLIRunSettingsArgumentProcessorTests { + private TestableRunSettingsProvider settingsProvider; + private CLIRunSettingsArgumentExecutor executor; private const string DefaultRunSettings = "\r\n\r\n \r\n \r\n \r\n"; private const string RunSettingsWithDeploymentDisabled = "\r\n\r\n \r\n \r\n \r\n \r\n False\r\n \r\n"; private const string RunSettingsWithDeploymentEnabled = "\r\n\r\n \r\n \r\n \r\n \r\n True\r\n \r\n"; + [TestInitialize] + public void Init() + { + this.settingsProvider = new TestableRunSettingsProvider(); + this.executor = new CLIRunSettingsArgumentExecutor(this.settingsProvider); + } + [TestMethod] public void GetMetadataShouldReturnRunSettingsArgumentProcessorCapabilities() { @@ -63,45 +70,35 @@ public void CapabilitiesShouldReturnAppropriateProperties() [TestMethod] public void InitializeShouldNotThrowExceptionIfArgumentIsNull() { - var settingsProvider = new TestableRunSettingsProvider(); - var executor = new CLIRunSettingsArgumentExecutor(null); + this.executor.Initialize((string[])null); - executor.Initialize((string[])null); - - Assert.IsNull(settingsProvider.ActiveRunSettings); + Assert.IsNull(this.settingsProvider.ActiveRunSettings); } [TestMethod] public void InitializeShouldNotThrowExceptionIfArgumentIsEmpty() { - var settingsProvider = new TestableRunSettingsProvider(); - var executor = new CLIRunSettingsArgumentExecutor(settingsProvider); - - executor.Initialize(new string[0]); + this.executor.Initialize(new string[0]); - Assert.IsNull(settingsProvider.ActiveRunSettings); + Assert.IsNull(this.settingsProvider.ActiveRunSettings); } [TestMethod] - public void InitializeShouldCreateDefaultRunSettingsIfArgumentsHasOnlyWhiteSpace() + public void InitializeShouldCreateEmptyRunSettingsIfArgumentsHasOnlyWhiteSpace() { - var settingsProvider = new TestableRunSettingsProvider(); - var executor = new CLIRunSettingsArgumentExecutor(settingsProvider); + this.executor.Initialize(new string[] { " " }); - executor.Initialize(new string[] { " " }); - - Assert.AreEqual(DefaultRunSettings, settingsProvider.ActiveRunSettings.SettingsXml); + Assert.IsNull(this.settingsProvider.ActiveRunSettings); } [TestMethod] public void InitializeShouldSetValueInRunSettings() { var args = new string[] { "MSTest.DeploymentEnabled=False" }; - var settingsProvider = new TestableRunSettingsProvider(); - var executor = new CLIRunSettingsArgumentExecutor(settingsProvider); - executor.Initialize(args); - Assert.IsNotNull(settingsProvider.ActiveRunSettings); + this.executor.Initialize(args); + + Assert.IsNotNull(this.settingsProvider.ActiveRunSettings); Assert.AreEqual(RunSettingsWithDeploymentDisabled, settingsProvider.ActiveRunSettings.SettingsXml); } @@ -109,12 +106,10 @@ public void InitializeShouldSetValueInRunSettings() public void InitializeShouldIgnoreKeyIfValueIsNotPassed() { var args = new string[] { "MSTest.DeploymentEnabled=False", "MSTest1" }; - var settingsProvider = new TestableRunSettingsProvider(); - var executor = new CLIRunSettingsArgumentExecutor(settingsProvider); - executor.Initialize(args); + this.executor.Initialize(args); - Assert.IsNotNull(settingsProvider.ActiveRunSettings); + Assert.IsNotNull(this.settingsProvider.ActiveRunSettings); Assert.AreEqual(RunSettingsWithDeploymentDisabled, settingsProvider.ActiveRunSettings.SettingsXml); } @@ -122,12 +117,10 @@ public void InitializeShouldIgnoreKeyIfValueIsNotPassed() public void InitializeShouldIgnoreWhiteSpaceInBeginningOrEndOfKey() { var args = new string[] { " MSTest.DeploymentEnabled =False" }; - var settingsProvider = new TestableRunSettingsProvider(); - var executor = new CLIRunSettingsArgumentExecutor(settingsProvider); - executor.Initialize(args); + this.executor.Initialize(args); - Assert.IsNotNull(settingsProvider.ActiveRunSettings); + Assert.IsNotNull(this.settingsProvider.ActiveRunSettings); Assert.AreEqual(RunSettingsWithDeploymentDisabled, settingsProvider.ActiveRunSettings.SettingsXml); } @@ -135,10 +128,8 @@ public void InitializeShouldIgnoreWhiteSpaceInBeginningOrEndOfKey() public void InitializeShouldIgnoreThrowExceptionIfKeyHasWhiteSpace() { var args = new string[] { "MST est.DeploymentEnabled=False" }; - var settingsProvider = new TestableRunSettingsProvider(); - var executor = new CLIRunSettingsArgumentExecutor(settingsProvider); - Action action = () => executor.Initialize(args); + Action action = () => this.executor.Initialize(args); ExceptionUtilities.ThrowsException( action, @@ -149,12 +140,10 @@ public void InitializeShouldIgnoreThrowExceptionIfKeyHasWhiteSpace() public void InitializeShouldEncodeXMLIfInvalidXMLCharsArePresent() { var args = new string[] { "MSTest.DeploymentEnabled=F>a>\r\n\r\n \r\n \r\n \r\n \r\n F>a><l<se\r\n \r\n", settingsProvider.ActiveRunSettings.SettingsXml); } @@ -162,44 +151,40 @@ public void InitializeShouldEncodeXMLIfInvalidXMLCharsArePresent() public void InitializeShouldIgnoreIfKeyIsNotPassed() { var args = new string[] { "MSTest.DeploymentEnabled=False", "=value" }; - var settingsProvider = new TestableRunSettingsProvider(); - var executor = new CLIRunSettingsArgumentExecutor(settingsProvider); - executor.Initialize(args); + this.executor.Initialize(args); - Assert.IsNotNull(settingsProvider.ActiveRunSettings); + Assert.IsNotNull(this.settingsProvider.ActiveRunSettings); Assert.AreEqual(RunSettingsWithDeploymentDisabled, settingsProvider.ActiveRunSettings.SettingsXml); } [TestMethod] public void InitializeShouldIgnoreIfEmptyValueIsPassed() { - var settingsProvider = new TestableRunSettingsProvider(); + var runSettings = new RunSettings(); runSettings.LoadSettingsXml(DefaultRunSettings); - settingsProvider.SetActiveRunSettings(runSettings); + this.settingsProvider.SetActiveRunSettings(runSettings); var args = new string[] { "MSTest.DeploymentEnabled=" }; - var executor = new CLIRunSettingsArgumentExecutor(settingsProvider); - executor.Initialize(args); + this.executor.Initialize(args); - Assert.IsNotNull(settingsProvider.ActiveRunSettings); - Assert.AreEqual(DefaultRunSettings, settingsProvider.ActiveRunSettings.SettingsXml); + Assert.IsNotNull(this.settingsProvider.ActiveRunSettings); + Assert.AreEqual(DefaultRunSettings, this.settingsProvider.ActiveRunSettings.SettingsXml); } [TestMethod] public void InitializeShouldOverwriteValueIfNodeAlreadyExists() { - var settingsProvider = new TestableRunSettingsProvider(); + var runSettings = new RunSettings(); runSettings.LoadSettingsXml(DefaultRunSettings); settingsProvider.SetActiveRunSettings(runSettings); var args = new string[] { "MSTest.DeploymentEnabled=True" }; - var executor = new CLIRunSettingsArgumentExecutor(settingsProvider); - executor.Initialize(args); + this.executor.Initialize(args); - Assert.IsNotNull(settingsProvider.ActiveRunSettings); + Assert.IsNotNull(this.settingsProvider.ActiveRunSettings); Assert.AreEqual(RunSettingsWithDeploymentEnabled, settingsProvider.ActiveRunSettings.SettingsXml); } @@ -207,37 +192,18 @@ public void InitializeShouldOverwriteValueIfNodeAlreadyExists() [TestMethod] public void InitializeShouldOverwriteValueIfWhitSpaceIsPassedAndNodeAlreadyExists() { - var settingsProvider = new TestableRunSettingsProvider(); + var runSettings = new RunSettings(); runSettings.LoadSettingsXml(DefaultRunSettings); settingsProvider.SetActiveRunSettings(runSettings); var args = new string[] { "MSTest.DeploymentEnabled= " }; - var executor = new CLIRunSettingsArgumentExecutor(settingsProvider); - executor.Initialize(args); + this.executor.Initialize(args); - Assert.IsNotNull(settingsProvider.ActiveRunSettings); + Assert.IsNotNull(this.settingsProvider.ActiveRunSettings); Assert.AreEqual("\r\n\r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n", settingsProvider.ActiveRunSettings.SettingsXml); } #endregion - - #region private - - private class TestableRunSettingsProvider : IRunSettingsProvider - { - public RunSettings ActiveRunSettings - { - get; - set; - } - - public void SetActiveRunSettings(RunSettings runSettings) - { - this.ActiveRunSettings = runSettings; - } - } - - #endregion } } diff --git a/test/vstest.console.UnitTests/Processors/FrameworkArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/FrameworkArgumentProcessorTests.cs index 5f3e47f378..82560c891f 100644 --- a/test/vstest.console.UnitTests/Processors/FrameworkArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/FrameworkArgumentProcessorTests.cs @@ -3,19 +3,28 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors { - using Microsoft.VisualStudio.TestPlatform.Common; using Microsoft.VisualStudio.TestTools.UnitTesting; using TestPlatform.CommandLine.Processors; using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; + using vstest.console.UnitTests.Processors; + using Microsoft.VisualStudio.TestPlatform.ObjectModel; [TestClass] public class FrameworkArgumentProcessorTests { + private FrameworkArgumentExecutor executor; + private TestableRunSettingsProvider runSettingsProvider; + + [TestInitialize] + public void Init() + { + this.runSettingsProvider = new TestableRunSettingsProvider(); + this.executor = new FrameworkArgumentExecutor(CommandLineOptions.Instance, runSettingsProvider); + } [TestCleanup] public void TestCleanup() { CommandLineOptions.Instance.Reset(); - RunSettingsManager.Instance = null; } [TestMethod] @@ -57,18 +66,15 @@ public void CapabilitiesShouldReturnAppropriateProperties() [TestMethod] public void InitializeShouldThrowIfArgumentIsNull() { - var executor = new FrameworkArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); ExceptionUtilities.ThrowsException( - () => executor.Initialize(null), + () => this.executor.Initialize(null), "The /Framework argument requires the target .Net Framework version for the test run. Example: /Framework:\".NETFramework,Version=v4.6\""); } [TestMethod] public void InitializeShouldThrowIfArgumentIsEmpty() { - var executor = new FrameworkArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); - ExceptionUtilities.ThrowsException( () => executor.Initialize(" "), "The /Framework argument requires the target .Net Framework version for the test run. Example: /Framework:\".NETFramework,Version=v4.6\""); @@ -77,10 +83,8 @@ public void InitializeShouldThrowIfArgumentIsEmpty() [TestMethod] public void InitializeShouldThrowIfArgumentIsInvalid() { - var executor = new FrameworkArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); - ExceptionUtilities.ThrowsException( - () => executor.Initialize("foo"), + () => this.executor.Initialize("foo"), "Invalid .Net Framework version:{0}. Please give the fullname of the TargetFramework. Other supported .Net Framework versions are Framework35, Framework40 and Framework45.", "foo"); } @@ -88,31 +92,35 @@ public void InitializeShouldThrowIfArgumentIsInvalid() [TestMethod] public void InitializeShouldSetCommandLineOptionsAndRunSettingsFramework() { - var executor = new FrameworkArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); - - executor.Initialize(".NETCoreApp,Version=v1.0"); + this.executor.Initialize(".NETCoreApp,Version=v1.0"); Assert.AreEqual(".NETCoreApp,Version=v1.0", CommandLineOptions.Instance.TargetFrameworkVersion.Name); - Assert.AreEqual(".NETCoreApp,Version=v1.0", RunSettingsUtilities.QueryRunSettingsNode(RunSettingsManager.Instance, FrameworkArgumentExecutor.RunSettingsPath)); + Assert.AreEqual(".NETCoreApp,Version=v1.0", this.runSettingsProvider.QueryRunSettingsNode(FrameworkArgumentExecutor.RunSettingsPath)); } [TestMethod] public void InitializeShouldSetCommandLineOptionsFrameworkForOlderFrameworks() { - var executor = new FrameworkArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); - - executor.Initialize("Framework35"); + this.executor.Initialize("Framework35"); Assert.AreEqual(".NETFramework,Version=v3.5", CommandLineOptions.Instance.TargetFrameworkVersion.Name); - Assert.AreEqual(".NETFramework,Version=v3.5", RunSettingsUtilities.QueryRunSettingsNode(RunSettingsManager.Instance, FrameworkArgumentExecutor.RunSettingsPath)); + Assert.AreEqual(".NETFramework,Version=v3.5", this.runSettingsProvider.QueryRunSettingsNode(FrameworkArgumentExecutor.RunSettingsPath)); } [TestMethod] public void InitializeShouldSetCommandLineOptionsFrameworkForCaseInsensitiveFramework() { - var executor = new FrameworkArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); - - executor.Initialize(".netcoreApp,Version=v1.0"); + this.executor.Initialize(".netcoreApp,Version=v1.0"); Assert.AreEqual(".netcoreApp,Version=v1.0", CommandLineOptions.Instance.TargetFrameworkVersion.Name); - Assert.AreEqual(".netcoreApp,Version=v1.0", RunSettingsUtilities.QueryRunSettingsNode(RunSettingsManager.Instance, FrameworkArgumentExecutor.RunSettingsPath)); + Assert.AreEqual(".netcoreApp,Version=v1.0", this.runSettingsProvider.QueryRunSettingsNode(FrameworkArgumentExecutor.RunSettingsPath)); + } + + [TestMethod] + public void InitializeShouldNotSetFrameworkIfSettingsFileIsLegacy() + { + this.runSettingsProvider.UpdateRunSettingsNode(FrameworkArgumentExecutor.RunSettingsPath, FrameworkVersion.Framework45.ToString()); + CommandLineOptions.Instance.SettingsFile = @"c:\tmp\settings.testsettings"; + this.executor.Initialize(".NETFramework,Version=v3.5"); + Assert.AreEqual(".NETFramework,Version=v3.5", CommandLineOptions.Instance.TargetFrameworkVersion.Name); + Assert.AreEqual(FrameworkVersion.Framework45.ToString(), this.runSettingsProvider.QueryRunSettingsNode(FrameworkArgumentExecutor.RunSettingsPath)); } #endregion @@ -122,11 +130,10 @@ public void InitializeShouldSetCommandLineOptionsFrameworkForCaseInsensitiveFram [TestMethod] public void ExecuteShouldReturnSuccess() { - var executor = new FrameworkArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); - - Assert.AreEqual(ArgumentProcessorResult.Success, executor.Execute()); + Assert.AreEqual(ArgumentProcessorResult.Success, this.executor.Execute()); } #endregion + } } diff --git a/test/vstest.console.UnitTests/Processors/ListTestsArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/ListTestsArgumentProcessorTests.cs index b8def16506..59c1c26851 100644 --- a/test/vstest.console.UnitTests/Processors/ListTestsArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/ListTestsArgumentProcessorTests.cs @@ -12,6 +12,9 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; using Microsoft.VisualStudio.TestPlatform.Common; + using Microsoft.VisualStudio.TestPlatform.Client.RequestHelper; + using Microsoft.VisualStudio.TestPlatform.Common.Interfaces; + using vstest.console.UnitTests.Processors; using Moq; using ObjectModel; @@ -35,16 +38,23 @@ public class ListTestsArgumentProcessorTests private readonly Mock mockTestPlatformEventSource; - [TestInitialize] - public void Init() + private static ListTestsArgumentExecutor GetExecutor(ITestRequestManager testRequestManager, IOutput output) { - RunSettingsUtilities.AddDefaultRunSettings(RunSettingsManager.Instance); + var runSettingsProvider = new TestableRunSettingsProvider(); + runSettingsProvider.AddDefaultRunSettings(); + var listTestsArgumentExecutor = + new ListTestsArgumentExecutor( + CommandLineOptions.Instance, + runSettingsProvider, + testRequestManager, + output ?? ConsoleOutput.Instance); + return listTestsArgumentExecutor; } [TestCleanup] public void Cleanup() { - RunSettingsUtilities.AddDefaultRunSettings(RunSettingsManager.Instance); + CommandLineOptions.Instance.Reset(); } public ListTestsArgumentProcessorTests() @@ -60,7 +70,7 @@ public ListTestsArgumentProcessorTests() [TestMethod] public void GetMetadataShouldReturnListTestsArgumentProcessorCapabilities() { - ListTestsArgumentProcessor processor = new ListTestsArgumentProcessor(); + var processor = new ListTestsArgumentProcessor(); Assert.IsTrue(processor.Metadata.Value is ListTestsArgumentProcessorCapabilities); } @@ -70,7 +80,7 @@ public void GetMetadataShouldReturnListTestsArgumentProcessorCapabilities() [TestMethod] public void GetExecuterShouldReturnListTestsArgumentProcessorCapabilities() { - ListTestsArgumentProcessor processor = new ListTestsArgumentProcessor(); + var processor = new ListTestsArgumentProcessor(); Assert.IsTrue(processor.Executor.Value is ListTestsArgumentExecutor); } @@ -79,7 +89,7 @@ public void GetExecuterShouldReturnListTestsArgumentProcessorCapabilities() [TestMethod] public void CapabilitiesShouldReturnAppropriateProperties() { - ListTestsArgumentProcessorCapabilities capabilities = new ListTestsArgumentProcessorCapabilities(); + var capabilities = new ListTestsArgumentProcessorCapabilities(); Assert.AreEqual("/ListTests", capabilities.CommandName); Assert.AreEqual("/lt", capabilities.ShortCommandName); Assert.AreEqual("-lt|--ListTests|/lt|/ListTests:\n Lists discovered tests from the given test container.", capabilities.HelpContentResourceName); @@ -99,15 +109,10 @@ public void CapabilitiesShouldReturnAppropriateProperties() [TestMethod] public void ExecutorInitializeWithValidSourceShouldAddItToTestSources() { - CommandLineOptions.Instance.Reset(); - CommandLineOptions.Instance.FileHelper = this.mockFileHelper.Object; var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, new TestPlatform(), TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); - ListTestsArgumentExecutor executor = new ListTestsArgumentExecutor( - CommandLineOptions.Instance, - RunSettingsManager.Instance, - testRequestManager); + var executor = GetExecutor(testRequestManager, null); executor.Initialize(this.dummyTestFilePath); @@ -120,10 +125,7 @@ public void ExecutorExecuteForNoSourcesShouldReturnFail() CommandLineOptions.Instance.Reset(); var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, new TestPlatform(), TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); - ListTestsArgumentExecutor executor = new ListTestsArgumentExecutor( - CommandLineOptions.Instance, - RunSettingsManager.Instance, - testRequestManager); + var executor = GetExecutor(testRequestManager, null); Assert.ThrowsException(() => executor.Execute()); } @@ -138,15 +140,11 @@ public void ExecutorExecuteShouldCatchTestPlatformExceptionAndReturnFail() mockTestPlatform.Setup(tp => tp.CreateDiscoveryRequest(It.IsAny())).Returns(mockDiscoveryRequest.Object); this.ResetAndAddSourceToCommandLineOptions(); - + var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); - ListTestsArgumentExecutor listTestsArgumentExecutor = - new ListTestsArgumentExecutor( - CommandLineOptions.Instance, - RunSettingsManager.Instance, - testRequestManager); + var executor = GetExecutor(testRequestManager, null); - ArgumentProcessorResult argumentProcessorResult = listTestsArgumentExecutor.Execute(); + var argumentProcessorResult = executor.Execute(); Assert.AreEqual(ArgumentProcessorResult.Fail, argumentProcessorResult); } @@ -162,13 +160,9 @@ public void ExecutorExecuteShouldCatchSettingsExceptionAndReturnFail() this.ResetAndAddSourceToCommandLineOptions(); var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); - ListTestsArgumentExecutor listTestsArgumentExecutor = - new ListTestsArgumentExecutor( - CommandLineOptions.Instance, - RunSettingsManager.Instance, - testRequestManager); + var listTestsArgumentExecutor = GetExecutor(testRequestManager, null); - ArgumentProcessorResult argumentProcessorResult = listTestsArgumentExecutor.Execute(); + var argumentProcessorResult = listTestsArgumentExecutor.Execute(); Assert.AreEqual(ArgumentProcessorResult.Fail, argumentProcessorResult); } @@ -184,13 +178,9 @@ public void ExecutorExecuteShouldCatchInvalidOperationExceptionAndReturnFail() this.ResetAndAddSourceToCommandLineOptions(); var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); - ListTestsArgumentExecutor listTestsArgumentExecutor = - new ListTestsArgumentExecutor( - CommandLineOptions.Instance, - RunSettingsManager.Instance, - testRequestManager); + var listTestsArgumentExecutor = GetExecutor(testRequestManager, null); - ArgumentProcessorResult argumentProcessorResult = listTestsArgumentExecutor.Execute(); + var argumentProcessorResult = listTestsArgumentExecutor.Execute(); Assert.AreEqual(ArgumentProcessorResult.Fail, argumentProcessorResult); } @@ -202,14 +192,11 @@ public void ExecutorExecuteShouldThrowOtherExceptions() mockDiscoveryRequest.Setup(dr => dr.DiscoverAsync()).Throws(new Exception("DummyException")); mockTestPlatform.Setup(tp => tp.CreateDiscoveryRequest(It.IsAny())).Returns(mockDiscoveryRequest.Object); - + this.ResetAndAddSourceToCommandLineOptions(); var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); - var executor = new ListTestsArgumentExecutor( - CommandLineOptions.Instance, - RunSettingsManager.Instance, - testRequestManager); + var executor = GetExecutor(testRequestManager, null); Assert.ThrowsException(() => executor.Execute()); } @@ -217,7 +204,6 @@ public void ExecutorExecuteShouldThrowOtherExceptions() [TestMethod] public void ExecutorExecuteShouldOutputDiscoveredTestsAndReturnSuccess() { - var mockDiscoveryRequest = new Mock(); var mockConsoleOutput = new Mock(); @@ -225,7 +211,7 @@ public void ExecutorExecuteShouldOutputDiscoveredTestsAndReturnSuccess() // Assert mockDiscoveryRequest.Verify(dr => dr.DiscoverAsync(), Times.Once); - + mockConsoleOutput.Verify((IOutput co) => co.WriteLine(" Test1", OutputLevel.Information)); mockConsoleOutput.Verify((IOutput co) => co.WriteLine(" Test2", OutputLevel.Information)); } @@ -256,7 +242,7 @@ public void ListTestArgumentProcessorExecuteShouldInstrumentDiscoveryRequestStop private void RunListTestArgumentProcessorExecuteWithMockSetup(Mock mockDiscoveryRequest, Mock mockConsoleOutput) { var mockTestPlatform = new Mock(); - List list = new List(); + var list = new List(); list.Add(new TestCase("Test1", new Uri("http://FooTestUri1"), "Source1")); list.Add(new TestCase("Test2", new Uri("http://FooTestUri2"), "Source2")); mockDiscoveryRequest.Setup(dr => dr.DiscoverAsync()).Raises(dr => dr.OnDiscoveredTests += null, new DiscoveredTestsEventArgs(list)); @@ -267,7 +253,7 @@ private void RunListTestArgumentProcessorExecuteWithMockSetup(Mock( - () => executor.Initialize("123"), + () => this.executor.Initialize("123"), "Argument " + 123 + " is not expected in the 'Parallel' command. Specify the command without the argument (Example: vstest.console.exe myTests.dll /Parallel) and try again."); } [TestMethod] public void InitializeShouldSetParallelValue() { - var executor = new ParallelArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); - executor.Initialize(null); + this.executor.Initialize(null); Assert.IsTrue(CommandLineOptions.Instance.Parallel, "Parallel option must be set to true."); - Assert.AreEqual("0", RunSettingsUtilities.QueryRunSettingsNode(RunSettingsManager.Instance, ParallelArgumentExecutor.RunSettingsPath)); + Assert.AreEqual("0", this.runSettingsProvider.QueryRunSettingsNode(ParallelArgumentExecutor.RunSettingsPath)); } #endregion @@ -81,9 +87,7 @@ public void InitializeShouldSetParallelValue() [TestMethod] public void ExecuteShouldReturnSuccess() { - var executor = new ParallelArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); - - Assert.AreEqual(ArgumentProcessorResult.Success, executor.Execute()); + Assert.AreEqual(ArgumentProcessorResult.Success, this.executor.Execute()); } #endregion diff --git a/test/vstest.console.UnitTests/Processors/PlatformArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/PlatformArgumentProcessorTests.cs index 53f35f0370..1309ca5aba 100644 --- a/test/vstest.console.UnitTests/Processors/PlatformArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/PlatformArgumentProcessorTests.cs @@ -6,17 +6,26 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using TestPlatform.CommandLine.Processors; - using Microsoft.VisualStudio.TestPlatform.Common; using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; + using vstest.console.UnitTests.Processors; [TestClass] public class PlatformArgumentProcessorTests { + private PlatformArgumentExecutor executor; + private TestableRunSettingsProvider runSettingsProvider; + + [TestInitialize] + public void Init() + { + this.runSettingsProvider = new TestableRunSettingsProvider(); + this.executor = new PlatformArgumentExecutor(CommandLineOptions.Instance, runSettingsProvider); + } + [TestCleanup] public void TestCleanup() { CommandLineOptions.Instance.Reset(); - RunSettingsManager.Instance = null; } [TestMethod] @@ -40,7 +49,7 @@ public void CapabilitiesShouldReturnAppropriateProperties() { var capabilities = new PlatformArgumentProcessorCapabilities(); Assert.AreEqual("/Platform", capabilities.CommandName); - Assert.AreEqual("--Platform|/Platform:" + Environment.NewLine + " Target platform architecture to be used for test execution. " + Environment.NewLine + " Valid values are x86, x64 and ARM.", capabilities.HelpContentResourceName); + Assert.AreEqual("--Platform|/Platform:\n Target platform architecture to be used for test execution. \n Valid values are x86, x64 and ARM.", capabilities.HelpContentResourceName); Assert.AreEqual(HelpContentPriority.PlatformArgumentProcessorHelpPriority, capabilities.HelpPriority); Assert.AreEqual(false, capabilities.IsAction); @@ -58,30 +67,24 @@ public void CapabilitiesShouldReturnAppropriateProperties() [TestMethod] public void InitializeShouldThrowIfArgumentIsNull() { - var executor = new PlatformArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); - ExceptionUtilities.ThrowsException( - () => executor.Initialize(null), + () => this.executor.Initialize(null), "The /Platform argument requires the target platform type for the test run to be provided. Example: /Platform:x86"); } [TestMethod] public void InitializeShouldThrowIfArgumentIsEmpty() { - var executor = new PlatformArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); - ExceptionUtilities.ThrowsException( - () => executor.Initialize(" "), + () => this.executor.Initialize(" "), "The /Platform argument requires the target platform type for the test run to be provided. Example: /Platform:x86"); } [TestMethod] public void InitializeShouldThrowIfArgumentIsNotAnArchitecture() { - var executor = new PlatformArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); - ExceptionUtilities.ThrowsException( - () => executor.Initialize("foo"), + () => this.executor.Initialize("foo"), "Invalid platform type:{0}. Valid platform types are x86, x64 and Arm.", "foo"); } @@ -89,10 +92,8 @@ public void InitializeShouldThrowIfArgumentIsNotAnArchitecture() [TestMethod] public void InitializeShouldThrowIfArgumentIsNotASupportedArchitecture() { - var executor = new PlatformArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); - ExceptionUtilities.ThrowsException( - () => executor.Initialize("AnyCPU"), + () => this.executor.Initialize("AnyCPU"), "Invalid platform type:{0}. Valid platform types are x86, x64 and Arm.", "AnyCPU"); } @@ -100,21 +101,17 @@ public void InitializeShouldThrowIfArgumentIsNotASupportedArchitecture() [TestMethod] public void InitializeShouldSetCommandLineOptionsArchitecture() { - var executor = new PlatformArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); - - executor.Initialize("x64"); + this.executor.Initialize("x64"); Assert.AreEqual(ObjectModel.Architecture.X64, CommandLineOptions.Instance.TargetArchitecture); - Assert.AreEqual(ObjectModel.Architecture.X64.ToString(), RunSettingsUtilities.QueryRunSettingsNode(RunSettingsManager.Instance, PlatformArgumentExecutor.RunSettingsPath)); + Assert.AreEqual(ObjectModel.Architecture.X64.ToString(), this.runSettingsProvider.QueryRunSettingsNode(PlatformArgumentExecutor.RunSettingsPath)); } [TestMethod] public void InitializeShouldNotConsiderCaseSensitivityOfTheArgumentPassed() { - var executor = new PlatformArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); - executor.Initialize("ArM"); Assert.AreEqual(ObjectModel.Architecture.ARM, CommandLineOptions.Instance.TargetArchitecture); - Assert.AreEqual(ObjectModel.Architecture.ARM.ToString(), RunSettingsUtilities.QueryRunSettingsNode(RunSettingsManager.Instance, PlatformArgumentExecutor.RunSettingsPath)); + Assert.AreEqual(ObjectModel.Architecture.ARM.ToString(), this.runSettingsProvider.QueryRunSettingsNode(PlatformArgumentExecutor.RunSettingsPath)); } #endregion @@ -124,9 +121,7 @@ public void InitializeShouldNotConsiderCaseSensitivityOfTheArgumentPassed() [TestMethod] public void ExecuteShouldReturnSuccess() { - var executor = new PlatformArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); - - Assert.AreEqual(ArgumentProcessorResult.Success, executor.Execute()); + Assert.AreEqual(ArgumentProcessorResult.Success, this.executor.Execute()); } #endregion diff --git a/test/vstest.console.UnitTests/Processors/ResultsDirectoryArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/ResultsDirectoryArgumentProcessorTests.cs index 12f130c006..01159e8c4d 100644 --- a/test/vstest.console.UnitTests/Processors/ResultsDirectoryArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/ResultsDirectoryArgumentProcessorTests.cs @@ -1,24 +1,32 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. - namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors { - using Microsoft.VisualStudio.TestPlatform.Common; using Microsoft.VisualStudio.TestTools.UnitTesting; using TestPlatform.CommandLine.Processors; using System; using System.IO; using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; + using vstest.console.UnitTests.Processors; [TestClass] public class ResultsDirectoryArgumentProcessorTests { + private ResultsDirectoryArgumentExecutor executor; + private TestableRunSettingsProvider runSettingsProvider; + + [TestInitialize] + public void Init() + { + this.runSettingsProvider = new TestableRunSettingsProvider(); + this.executor = new ResultsDirectoryArgumentExecutor(CommandLineOptions.Instance, this.runSettingsProvider); + } + [TestCleanup] public void TestCleanup() { CommandLineOptions.Instance.Reset(); - RunSettingsManager.Instance = null; } [TestMethod] @@ -60,83 +68,46 @@ public void CapabilitiesShouldReturnAppropriateProperties() [TestMethod] public void InitializeShouldThrowIfArgumentIsNull() { - var executor = new ResultsDirectoryArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); - + string folder = null; var message = @"The /ResultsDirectory parameter requires a value, where the test results should be saved. Example: /ResultsDirectory:c:\MyTestResultsDirectory"; - - var isExceptionThrown = false; - - try - { - executor.Initialize(null); - } - catch (Exception ex) - { - isExceptionThrown = true; - Assert.IsTrue(ex is CommandLineException); - Assert.AreEqual(message, ex.Message); - } - - Assert.IsTrue(isExceptionThrown); + this.InitializeExceptionTestTemplate(folder, message); } [TestMethod] public void InitializeShouldThrowIfArgumentIsAWhiteSpace() { - var executor = new ResultsDirectoryArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); - + var folder = " "; var message = @"The /ResultsDirectory parameter requires a value, where the test results should be saved. Example: /ResultsDirectory:c:\MyTestResultsDirectory"; - - var isExceptionThrown = false; - - try - { - executor.Initialize(" "); - } - catch (Exception ex) - { - isExceptionThrown = true; - Assert.IsTrue(ex is CommandLineException); - Assert.AreEqual(message, ex.Message); - } - - Assert.IsTrue(isExceptionThrown); + this.InitializeExceptionTestTemplate(folder, message); } [TestMethod] - public void InitializeShouldThrowIfRelativePathIsInvalid() + public void InitializeShouldThrowIfRelativePathIsTooLong() { - var executor = new ResultsDirectoryArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); - - var folder = @".\path\to\in:valid"; - + var folder = @".\this\path\is\too\longgggggggggggggggggggggggggggggggggggggggggggggggggggglonggggggggggggggggggggggggggggggggggggggggggggggggggggglonggggggggggggggggggggggggggggggggggggggggggggggggggggglonggggggggggggggggggggggggggggggggggggggggggggggggggggglonggggggggggggggggggggggggggggggggggggggggggggggggggggglonggggggggggggggggggggggggggggggggggggggggggggggggggggglonggggggggggggggggggggggggggggggggggggggggggggggggggggg"; var message = string.Format( @"The path '{0}' specified in the 'ResultsDirectory' is invalid. Error: {1}", folder, - "The given path's format is not supported."); - - var isExceptionThrown = false; - - try - { - executor.Initialize(folder); - } - catch (Exception ex) - { - isExceptionThrown = true; - Assert.IsTrue(ex is CommandLineException); - Assert.AreEqual(message, ex.Message); - } + "The specified file name or path is too long, or a component of the specified path is too long."); + this.InitializeExceptionTestTemplate(folder, message); + } - Assert.IsTrue(isExceptionThrown); + [TestMethod] + public void InitializeShouldThrowIfGivenPathisIllegal() + { + var folder = @"c:\som<\illegal\path\"; + var message = string.Format( + @"The path '{0}' specified in the 'ResultsDirectory' is invalid. Error: {1}", + folder, + "Illegal characters in path."); + this.InitializeExceptionTestTemplate(folder, message); } [TestMethod] - public void InitializeShouldThrowIfPathIsInvalid() + public void InitializeShouldThrowIfPathIsNotSupported() { - var executor = new ResultsDirectoryArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); var folder = @"c:\path\to\in:valid"; @@ -144,12 +115,16 @@ public void InitializeShouldThrowIfPathIsInvalid() @"The path '{0}' specified in the 'ResultsDirectory' is invalid. Error: {1}", folder, "The given path's format is not supported."); + this.InitializeExceptionTestTemplate(folder, message); + } + private void InitializeExceptionTestTemplate(string folder, string message) + { var isExceptionThrown = false; try { - executor.Initialize(folder); + this.executor.Initialize(folder); } catch (Exception ex) { @@ -164,22 +139,20 @@ public void InitializeShouldThrowIfPathIsInvalid() [TestMethod] public void InitializeShouldSetCommandLineOptionsAndRunSettingsForRelativePathValue() { - var executor = new ResultsDirectoryArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); var relativePath = @".\relative\path"; var absolutePath = Path.GetFullPath(relativePath); - executor.Initialize(relativePath); + this.executor.Initialize(relativePath); Assert.AreEqual(absolutePath, CommandLineOptions.Instance.ResultsDirectory); - Assert.AreEqual(absolutePath, RunSettingsUtilities.QueryRunSettingsNode(RunSettingsManager.Instance, ResultsDirectoryArgumentExecutor.RunSettingsPath)); + Assert.AreEqual(absolutePath, this.runSettingsProvider.QueryRunSettingsNode(ResultsDirectoryArgumentExecutor.RunSettingsPath)); } [TestMethod] public void InitializeShouldSetCommandLineOptionsAndRunSettingsForAbsolutePathValue() { - var executor = new ResultsDirectoryArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); var absolutePath = @"c:\Users\someone\testresults"; - executor.Initialize(absolutePath); + this.executor.Initialize(absolutePath); Assert.AreEqual(absolutePath, CommandLineOptions.Instance.ResultsDirectory); - Assert.AreEqual(absolutePath, RunSettingsUtilities.QueryRunSettingsNode(RunSettingsManager.Instance, ResultsDirectoryArgumentExecutor.RunSettingsPath)); + Assert.AreEqual(absolutePath, this.runSettingsProvider.QueryRunSettingsNode(ResultsDirectoryArgumentExecutor.RunSettingsPath)); } #endregion @@ -189,8 +162,6 @@ public void InitializeShouldSetCommandLineOptionsAndRunSettingsForAbsolutePathVa [TestMethod] public void ExecuteShouldReturnSuccess() { - var executor = new ResultsDirectoryArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance); - Assert.AreEqual(ArgumentProcessorResult.Success, executor.Execute()); } diff --git a/test/vstest.console.UnitTests/Processors/RunSettingsArgumentProcessortTests.cs b/test/vstest.console.UnitTests/Processors/RunSettingsArgumentProcessorTests.cs similarity index 60% rename from test/vstest.console.UnitTests/Processors/RunSettingsArgumentProcessortTests.cs rename to test/vstest.console.UnitTests/Processors/RunSettingsArgumentProcessorTests.cs index e254ec8836..6a9d704dd9 100644 --- a/test/vstest.console.UnitTests/Processors/RunSettingsArgumentProcessortTests.cs +++ b/test/vstest.console.UnitTests/Processors/RunSettingsArgumentProcessorTests.cs @@ -1,7 +1,6 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. - namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors { using System; @@ -16,12 +15,21 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors; using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces; using Microsoft.VisualStudio.TestPlatform.ObjectModel; + using vstest.console.UnitTests.Processors; using Moq; [TestClass] - public class RunSettingsArgumentProcessortTests + public class RunSettingsArgumentProcessorTests { + private IRunSettingsProvider settingsProvider; + + [TestInitialize] + public void Init() + { + this.settingsProvider = new TestableRunSettingsProvider(); + } + [TestCleanup] public void TestCleanup() { @@ -108,12 +116,10 @@ public void InitializeShouldThrowIfRunSettingsSchemaDoesNotMatch() var fileName = "C:\\temp\\r.runsettings"; var settingsXml = ""; - var settingsProvider = new TestableRunSettingsProvider(); - CommandLineOptions.Instance.EnableCodeCoverage = true; var executor = new TestableRunSettingsArgumentExecutor( CommandLineOptions.Instance, - settingsProvider, + this.settingsProvider, settingsXml); // Setup mocks. @@ -135,13 +141,11 @@ public void InitializeShouldSetActiveRunSettings() var fileName = "C:\\temp\\r.runsettings"; var settingsXml = ""; - var settingsProvider = new TestableRunSettingsProvider(); - var executor = new TestableRunSettingsArgumentExecutor( CommandLineOptions.Instance, - settingsProvider, + this.settingsProvider, settingsXml); - + // Setup mocks. var mockFileHelper = new Mock(); mockFileHelper.Setup(fh => fh.Exists(It.IsAny())).Returns(true); @@ -151,7 +155,7 @@ public void InitializeShouldSetActiveRunSettings() executor.Initialize(fileName); // Assert. - Assert.IsNotNull(settingsProvider.ActiveRunSettings); + Assert.IsNotNull(this.settingsProvider.ActiveRunSettings); Assert.AreEqual(fileName, CommandLineOptions.Instance.SettingsFile); } @@ -162,11 +166,9 @@ public void InitializeShouldSetSettingsFileForCommandLineOptions() var fileName = "C:\\temp\\r.runsettings"; var settingsXml = ""; - var settingsProvider = new TestableRunSettingsProvider(); - var executor = new TestableRunSettingsArgumentExecutor( CommandLineOptions.Instance, - settingsProvider, + this.settingsProvider, settingsXml); // Setup mocks. @@ -188,11 +190,9 @@ public void InitializeShouldAddDefaultSettingsIfNotPresent() var fileName = "C:\\temp\\r.runsettings"; var settingsXml = ""; - var settingsProvider = new TestableRunSettingsProvider(); - var executor = new TestableRunSettingsArgumentExecutor( CommandLineOptions.Instance, - settingsProvider, + this.settingsProvider, settingsXml); // Setup mocks. @@ -204,9 +204,9 @@ public void InitializeShouldAddDefaultSettingsIfNotPresent() executor.Initialize(fileName); // Assert. - Assert.IsNotNull(settingsProvider.ActiveRunSettings); + Assert.IsNotNull(this.settingsProvider.ActiveRunSettings); RunConfiguration runConfiguration = - XmlRunSettingsUtilities.GetRunConfigurationNode(settingsProvider.ActiveRunSettings.SettingsXml); + XmlRunSettingsUtilities.GetRunConfigurationNode(this.settingsProvider.ActiveRunSettings.SettingsXml); Assert.AreEqual(runConfiguration.ResultsDirectory, Constants.DefaultResultsDirectory); Assert.AreEqual(runConfiguration.TargetFrameworkVersion.ToString(), Framework.DefaultFramework.ToString()); Assert.AreEqual(runConfiguration.TargetPlatform, Constants.DefaultPlatform); @@ -220,11 +220,9 @@ public void InitializeShouldSetActiveRunSettingsForTestSettingsFiles() var fileName = "C:\\temp\\r.testsettings"; var settingsXml = ""; - var settingsProvider = new TestableRunSettingsProvider(); - var executor = new TestableRunSettingsArgumentExecutor( CommandLineOptions.Instance, - settingsProvider, + this.settingsProvider, settingsXml); // Setup mocks. @@ -236,11 +234,10 @@ public void InitializeShouldSetActiveRunSettingsForTestSettingsFiles() executor.Initialize(fileName); // Assert. - Assert.IsNotNull(settingsProvider.ActiveRunSettings); - StringAssert.Contains(settingsProvider.ActiveRunSettings.SettingsXml, "\r\n \r\n X86\r\n Framework45\r\n \r\n \r\n C:\\temp\\r.testsettings\r\n true\r\n \r\n \r\n \r\n \r\n"); + Assert.IsNotNull(this.settingsProvider.ActiveRunSettings); + StringAssert.Contains(this.settingsProvider.ActiveRunSettings.SettingsXml, $"\r\n \r\n {Constants.DefaultPlatform}\r\n {Framework.FromString(FrameworkVersion.Framework45.ToString()).Name}\r\n {Constants.DefaultResultsDirectory}\r\n \r\n \r\n C:\\temp\\r.testsettings\r\n true\r\n \r\n \r\n \r\n \r\n"); } - [Ignore] // Code coverage yet to add [TestMethod] public void InitializeShouldSetActiveRunSettingsWithCodeCoverageData() { @@ -248,12 +245,10 @@ public void InitializeShouldSetActiveRunSettingsWithCodeCoverageData() var fileName = "C:\\temp\\r.runsettings"; var settingsXml = ""; - var settingsProvider = new TestableRunSettingsProvider(); - CommandLineOptions.Instance.EnableCodeCoverage = true; var executor = new TestableRunSettingsArgumentExecutor( CommandLineOptions.Instance, - settingsProvider, + this.settingsProvider, settingsXml); // Setup mocks. @@ -265,8 +260,8 @@ public void InitializeShouldSetActiveRunSettingsWithCodeCoverageData() executor.Initialize(fileName); // Assert. - Assert.IsNotNull(settingsProvider.ActiveRunSettings); - StringAssert.Contains(settingsProvider.ActiveRunSettings.SettingsXml, "\r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n .*CPPUnitTestFramework.*\r\n .*vstest.console.*\r\n .*microsoft.intellitrace.*\r\n .*vstest.executionengine.*\r\n .*vstest.discoveryengine.*\r\n .*microsoft.teamfoundation.testplatform.*\r\n .*microsoft.visualstudio.testplatform.*\r\n .*microsoft.visualstudio.testwindow.*\r\n .*microsoft.visualstudio.mstest.*\r\n .*microsoft.visualstudio.qualitytools.*\r\n .*microsoft.vssdk.testhostadapter.*\r\n .*microsoft.vssdk.testhostframework.*\r\n .*qtagent32.*\r\n .*msvcr.*dll$\r\n .*msvcp.*dll$\r\n .*clr.dll$\r\n .*clr.ni.dll$\r\n .*clrjit.dll$\r\n .*clrjit.ni.dll$\r\n .*mscoree.dll$\r\n .*mscoreei.dll$\r\n .*mscoreei.ni.dll$\r\n .*mscorlib.dll$\r\n .*mscorlib.ni.dll$\r\n \r\n \r\n True\r\n True\r\n True\r\n false\r\n \r\n \r\n \r\n ^std::.*\r\n ^ATL::.*\r\n .*::__GetTestMethodInfo.*\r\n .*__CxxPureMSILEntry.*\r\n ^Microsoft::VisualStudio::CppCodeCoverageFramework::.*\r\n ^Microsoft::VisualStudio::CppUnitTestFramework::.*\r\n .*::YOU_CAN_ONLY_DESIGNATE_ONE_.*\r\n ^__.*\r\n .*::__.*\r\n \r\n \r\n \r\n \r\n ^System.Diagnostics.DebuggerHiddenAttribute$\r\n ^System.Diagnostics.DebuggerNonUserCodeAttribute$\r\n ^System.Runtime.CompilerServices.CompilerGeneratedAttribute$\r\n ^System.CodeDom.Compiler.GeneratedCodeAttribute$\r\n ^System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute$\r\n \r\n \r\n \r\n \r\n .*\\\\atlmfc\\\\.*\r\n .*\\\\vctools\\\\.*\r\n .*\\\\public\\\\sdk\\\\.*\r\n .*\\\\externalapis\\\\.*\r\n .*\\\\microsoft sdks\\\\.*\r\n .*\\\\vc\\\\include\\\\.*\r\n .*\\\\msclr\\\\.*\r\n .*\\\\ucrt\\\\.*\r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n"); + Assert.IsNotNull(this.settingsProvider.ActiveRunSettings); + StringAssert.Contains(this.settingsProvider.ActiveRunSettings.SettingsXml, $"\r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n .*CPPUnitTestFramework.*\r\n .*vstest.console.*\r\n .*microsoft.intellitrace.*\r\n .*vstest.executionengine.*\r\n .*vstest.discoveryengine.*\r\n .*microsoft.teamfoundation.testplatform.*\r\n .*microsoft.visualstudio.testplatform.*\r\n .*microsoft.visualstudio.testwindow.*\r\n .*microsoft.visualstudio.mstest.*\r\n .*microsoft.visualstudio.qualitytools.*\r\n .*microsoft.vssdk.testhostadapter.*\r\n .*microsoft.vssdk.testhostframework.*\r\n .*qtagent32.*\r\n .*msvcr.*dll$\r\n .*msvcp.*dll$\r\n .*clr.dll$\r\n .*clr.ni.dll$\r\n .*clrjit.dll$\r\n .*clrjit.ni.dll$\r\n .*mscoree.dll$\r\n .*mscoreei.dll$\r\n .*mscoreei.ni.dll$\r\n .*mscorlib.dll$\r\n .*mscorlib.ni.dll$\r\n \r\n \r\n True\r\n True\r\n True\r\n false\r\n \r\n \r\n \r\n ^std::.*\r\n ^ATL::.*\r\n .*::__GetTestMethodInfo.*\r\n .*__CxxPureMSILEntry.*\r\n ^Microsoft::VisualStudio::CppCodeCoverageFramework::.*\r\n ^Microsoft::VisualStudio::CppUnitTestFramework::.*\r\n .*::YOU_CAN_ONLY_DESIGNATE_ONE_.*\r\n ^__.*\r\n .*::__.*\r\n \r\n \r\n \r\n \r\n ^System.Diagnostics.DebuggerHiddenAttribute$\r\n ^System.Diagnostics.DebuggerNonUserCodeAttribute$\r\n ^System.Runtime.CompilerServices.CompilerGeneratedAttribute$\r\n ^System.CodeDom.Compiler.GeneratedCodeAttribute$\r\n ^System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute$\r\n \r\n \r\n \r\n \r\n .*\\\\atlmfc\\\\.*\r\n .*\\\\vctools\\\\.*\r\n .*\\\\public\\\\sdk\\\\.*\r\n .*\\\\externalapis\\\\.*\r\n .*\\\\microsoft sdks\\\\.*\r\n .*\\\\vc\\\\include\\\\.*\r\n .*\\\\msclr\\\\.*\r\n .*\\\\ucrt\\\\.*\r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n {Constants.DefaultResultsDirectory}\r\n {Constants.DefaultPlatform}\r\n {Framework.DefaultFramework.Name}\r\n \r\n"); } #endregion @@ -301,20 +296,6 @@ protected override XmlReader GetReaderForFile(string runSettingsFile) } } - private class TestableRunSettingsProvider : IRunSettingsProvider - { - public RunSettings ActiveRunSettings - { - get; - set; - } - - public void SetActiveRunSettings(RunSettings runSettings) - { - this.ActiveRunSettings = runSettings; - } - } - #endregion } } diff --git a/test/vstest.console.UnitTests/Processors/RunSpecificTestsArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/RunSpecificTestsArgumentProcessorTests.cs index c1cf327393..1bafa2a6de 100644 --- a/test/vstest.console.UnitTests/Processors/RunSpecificTestsArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/RunSpecificTestsArgumentProcessorTests.cs @@ -12,13 +12,12 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors using Microsoft.VisualStudio.TestPlatform.Common.Logging; using Microsoft.VisualStudio.TestPlatform.ObjectModel; using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client; - using Microsoft.VisualStudio.TestPlatform.Utilities; using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; - using Microsoft.VisualStudio.TestPlatform.Common; + using Microsoft.VisualStudio.TestPlatform.Client.RequestHelper; + using vstest.console.UnitTests.Processors; using Moq; - using CoreUtilities.Tracing; using CoreUtilities.Tracing.Interfaces; using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces; @@ -31,19 +30,13 @@ public class RunSpecificTestsArgumentProcessorTests private Mock mockTestPlatformEventSource; - [TestInitialize] - public void Init() + private static RunSpecificTestsArgumentExecutor GetExecutor(ITestRequestManager testRequestManager) { - RunSettingsUtilities.AddDefaultRunSettings(RunSettingsManager.Instance); + var runSettingsProvider = new TestableRunSettingsProvider(); + runSettingsProvider.AddDefaultRunSettings(); + return new RunSpecificTestsArgumentExecutor(CommandLineOptions.Instance, runSettingsProvider, testRequestManager); } - [TestCleanup] - public void Cleanup() - { - RunSettingsUtilities.AddDefaultRunSettings(RunSettingsManager.Instance); - } - - public RunSpecificTestsArgumentProcessorTests() { this.mockFileHelper = new Mock(); @@ -94,7 +87,7 @@ public void ExecutorExecuteForNoSourcesShouldThrowCommandLineException() CommandLineOptions.Instance.Reset(); var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, new TestPlatform(), TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); - RunSpecificTestsArgumentExecutor executor = new RunSpecificTestsArgumentExecutor(CommandLineOptions.Instance, null, testRequestManager); + var executor = GetExecutor(testRequestManager); Assert.ThrowsException(() => executor.Execute()); } @@ -104,7 +97,7 @@ public void ExecutorExecuteForValidSourceWithTestCaseFilterShouldThrowCommandLin this.ResetAndAddSourceToCommandLineOptions(); var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, new TestPlatform(), TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); - var executor = new RunSpecificTestsArgumentExecutor(CommandLineOptions.Instance, null, testRequestManager); + var executor = GetExecutor(testRequestManager); CommandLineOptions.Instance.TestCaseFilterValue = "Filter"; Assert.ThrowsException(() => executor.Execute()); } @@ -123,7 +116,7 @@ public void ExecutorExecuteShouldCatchTestPlatformExceptionThrownDuringDiscovery this.ResetAndAddSourceToCommandLineOptions(); var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); - var executor = new RunSpecificTestsArgumentExecutor(CommandLineOptions.Instance, null, testRequestManager); + var executor = GetExecutor(testRequestManager); ArgumentProcessorResult argumentProcessorResult = executor.Execute(); Assert.AreEqual(ArgumentProcessorResult.Fail, argumentProcessorResult); @@ -143,7 +136,7 @@ public void ExecutorExecuteShouldCatchInvalidOperationExceptionThrownDuringDisco this.ResetAndAddSourceToCommandLineOptions(); var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); - var executor = new RunSpecificTestsArgumentExecutor(CommandLineOptions.Instance, null, testRequestManager); + var executor = GetExecutor(testRequestManager); ArgumentProcessorResult argumentProcessorResult = executor.Execute(); Assert.AreEqual(ArgumentProcessorResult.Fail, argumentProcessorResult); @@ -163,7 +156,7 @@ public void ExecutorExecuteShouldCatchSettingsExceptionThrownDuringDiscoveryAndR this.ResetAndAddSourceToCommandLineOptions(); var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); - var executor = new RunSpecificTestsArgumentExecutor(CommandLineOptions.Instance, null, testRequestManager); + var executor = GetExecutor(testRequestManager); ArgumentProcessorResult argumentProcessorResult = executor.Execute(); Assert.AreEqual(ArgumentProcessorResult.Fail, argumentProcessorResult); @@ -188,7 +181,7 @@ public void ExecutorExecuteShouldCatchTestPlatformExceptionThrownDuringExecution this.ResetAndAddSourceToCommandLineOptions(); var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); - var executor = new RunSpecificTestsArgumentExecutor(CommandLineOptions.Instance, null, testRequestManager); + var executor = GetExecutor(testRequestManager); executor.Initialize("Test1"); @@ -215,7 +208,7 @@ public void ExecutorExecuteShouldCatchSettingsExceptionThrownDuringExecutionAndR this.ResetAndAddSourceToCommandLineOptions(); var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); - var executor = new RunSpecificTestsArgumentExecutor(CommandLineOptions.Instance, null, testRequestManager); + var executor = GetExecutor(testRequestManager); executor.Initialize("Test1"); @@ -242,7 +235,7 @@ public void ExecutorExecuteShouldCatchInvalidOperationExceptionThrownDuringExecu this.ResetAndAddSourceToCommandLineOptions(); var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); - var executor = new RunSpecificTestsArgumentExecutor(CommandLineOptions.Instance, null, testRequestManager); + var executor = GetExecutor(testRequestManager); executor.Initialize("Test1"); @@ -267,7 +260,7 @@ public void ExecutorExecuteShouldForValidSourcesAndValidSelectedTestsRunsTestsAn mockTestPlatform.Setup(tp => tp.CreateDiscoveryRequest(It.IsAny())).Returns(mockDiscoveryRequest.Object); var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); - var executor = new RunSpecificTestsArgumentExecutor(CommandLineOptions.Instance, null, testRequestManager); + var executor = GetExecutor(testRequestManager); executor.Initialize("Test1"); diff --git a/test/vstest.console.UnitTests/Processors/RunTestsArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/RunTestsArgumentProcessorTests.cs index 4ed2f8132d..99ae5e1fce 100644 --- a/test/vstest.console.UnitTests/Processors/RunTestsArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/RunTestsArgumentProcessorTests.cs @@ -11,6 +11,7 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors using Microsoft.VisualStudio.TestPlatform.Client; using Microsoft.VisualStudio.TestPlatform.CommandLine.Internal; using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors; + using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; using Microsoft.VisualStudio.TestPlatform.CommandLine.TestPlatformHelpers; using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework; using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilities; @@ -22,8 +23,8 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors using Microsoft.VisualStudio.TestPlatform.Utilities; using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces; using Microsoft.VisualStudio.TestTools.UnitTesting; - using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; - using Microsoft.VisualStudio.TestPlatform.Common; + using Microsoft.VisualStudio.TestPlatform.Client.RequestHelper; + using vstest.console.UnitTests.Processors; using Moq; @@ -38,18 +39,6 @@ public class RunTestsArgumentProcessorTests private Mock mockTestPlatformEventSource; - [TestInitialize] - public void Init() - { - RunSettingsUtilities.AddDefaultRunSettings(RunSettingsManager.Instance); - } - - [TestCleanup] - public void Cleanup() - { - RunSettingsUtilities.AddDefaultRunSettings(RunSettingsManager.Instance); - } - public RunTestsArgumentProcessorTests() { this.mockFileHelper = new Mock(); @@ -77,7 +66,6 @@ public void GetExecuterShouldReturnRunTestsArgumentProcessorCapabilities() [TestMethod] public void CapabilitiesShouldReturnAppropriateProperties() { - System.Diagnostics.Debugger.Launch(); RunTestsArgumentProcessorCapabilities capabilities = new RunTestsArgumentProcessorCapabilities(); Assert.AreEqual("/RunTests", capabilities.CommandName); Assert.AreEqual("[TestFileNames]" + Environment.NewLine + " Run tests from the specified files. Separate multiple test file names" + Environment.NewLine + " by spaces." + Environment.NewLine + " Examples: mytestproject.dll" + Environment.NewLine + " mytestproject.dll myothertestproject.exe", capabilities.HelpContentResourceName); @@ -101,11 +89,22 @@ public void ExecutorExecuteForNoSourcesShouldThrowCommandLineException() var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, new TestPlatform(), TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); - var executor = new RunTestsArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance, testRequestManager); + var executor = GetExecutor(testRequestManager); Assert.ThrowsException(() => executor.Execute()); } + private static RunTestsArgumentExecutor GetExecutor(ITestRequestManager testRequestManager) + { + var runSettingsProvider = new TestableRunSettingsProvider(); + runSettingsProvider.AddDefaultRunSettings(); + var executor = new RunTestsArgumentExecutor( + CommandLineOptions.Instance, + runSettingsProvider, + testRequestManager); + return executor; + } + [TestMethod] public void ExecutorExecuteShouldCatchTestPlatformExceptionAndReturnFail() { @@ -118,7 +117,7 @@ public void ExecutorExecuteShouldCatchTestPlatformExceptionAndReturnFail() this.ResetAndAddSourceToCommandLineOptions(); var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); - var executor = new RunTestsArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance, testRequestManager); + var executor = GetExecutor(testRequestManager); ArgumentProcessorResult argumentProcessorResult = executor.Execute(); Assert.AreEqual(ArgumentProcessorResult.Fail, argumentProcessorResult); @@ -136,7 +135,7 @@ public void ExecutorExecuteShouldCatchSettingsExceptionAndReturnFail() this.ResetAndAddSourceToCommandLineOptions(); var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); - var executor = new RunTestsArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance, testRequestManager); + var executor = GetExecutor(testRequestManager); ArgumentProcessorResult argumentProcessorResult = executor.Execute(); Assert.AreEqual(ArgumentProcessorResult.Fail, argumentProcessorResult); @@ -154,7 +153,7 @@ public void ExecutorExecuteShouldCatchInvalidOperationExceptionAndReturnFail() this.ResetAndAddSourceToCommandLineOptions(); var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); - var executor = new RunTestsArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance, testRequestManager); + var executor = GetExecutor(testRequestManager); ArgumentProcessorResult argumentProcessorResult = executor.Execute(); Assert.AreEqual(ArgumentProcessorResult.Fail, argumentProcessorResult); @@ -172,7 +171,7 @@ public void ExecutorExecuteShouldThrowOtherExceptions() this.ResetAndAddSourceToCommandLineOptions(); var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); - var executor = new RunTestsArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance, testRequestManager); + var executor = GetExecutor(testRequestManager); Assert.ThrowsException(() => executor.Execute()); } @@ -227,7 +226,7 @@ private ArgumentProcessorResult RunRunArgumentProcessorExecuteWithMockSetup(ITes this.ResetAndAddSourceToCommandLineOptions(); var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object); - var executor = new RunTestsArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance, testRequestManager); + var executor = GetExecutor(testRequestManager); return executor.Execute(); } diff --git a/test/vstest.console.UnitTests/Processors/TestableRunSettingsProvider.cs b/test/vstest.console.UnitTests/Processors/TestableRunSettingsProvider.cs new file mode 100644 index 0000000000..169443da7d --- /dev/null +++ b/test/vstest.console.UnitTests/Processors/TestableRunSettingsProvider.cs @@ -0,0 +1,22 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace vstest.console.UnitTests.Processors +{ + using Microsoft.VisualStudio.TestPlatform.Common; + using Microsoft.VisualStudio.TestPlatform.Common.Interfaces; + + internal class TestableRunSettingsProvider : IRunSettingsProvider + { + public RunSettings ActiveRunSettings + { + get; + set; + } + + public void SetActiveRunSettings(RunSettings runSettings) + { + this.ActiveRunSettings = runSettings; + } + } +} diff --git a/test/vstest.console.UnitTests/Processors/Utilities/RunSettingsProviderExtensionsTests.cs b/test/vstest.console.UnitTests/Processors/Utilities/RunSettingsProviderExtensionsTests.cs new file mode 100644 index 0000000000..4d6d7463a6 --- /dev/null +++ b/test/vstest.console.UnitTests/Processors/Utilities/RunSettingsProviderExtensionsTests.cs @@ -0,0 +1,174 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors.Utilities +{ + using System; + using System.IO; + + using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; + 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 vstest.console.UnitTests.Processors; + + [TestClass] + public class RunSettingsProviderExtensionsTests + { + private const string DefaultRunSettingsTemplate = + "\r\n \r\n %ResultsDirectory%\r\n X86\r\n %DefaultFramework%\r\n \r\n"; + private IRunSettingsProvider runSettingsProvider; + + [TestInitialize] + public void Init() + { + runSettingsProvider = new TestableRunSettingsProvider(); + } + + [TestCleanup] + public void TestCleanup() + { + CommandLineOptions.Instance.Reset(); + } + + [TestMethod] + public void UpdateRunSettingsShouldUpdateGivenSettingsXml() + { + const string runSettingsXml = "\r\n \r\n X86\r\n \r\n"; + + this.runSettingsProvider.UpdateRunSettings(runSettingsXml); + + StringAssert.Contains(this.runSettingsProvider.ActiveRunSettings.SettingsXml, runSettingsXml); + } + + [TestMethod] + public void UpdateRunSettingsShouldThrownExceptionIfRunSettingsProviderIsNull() + { + Assert.ThrowsException( + () => RunSettingsProviderExtensions.UpdateRunSettings(null, "")); + } + + [TestMethod] + public void UpdateRunSettingsShouldThrownExceptionIfSettingsXmlIsNull() + { + Assert.ThrowsException( + () => this.runSettingsProvider.UpdateRunSettings(null)); + } + + [TestMethod] + public void UpdateRunSettingsShouldThrownExceptionIfSettingsXmlIsEmptyOrWhiteSpace() + { + Assert.ThrowsException( + () => this.runSettingsProvider.UpdateRunSettings(" ")); + } + + [TestMethod] + public void AddDefaultRunSettingsShouldSetDefaultSettingsForEmptySettings() + { + this.runSettingsProvider.AddDefaultRunSettings(); + + var runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(this.runSettingsProvider.ActiveRunSettings.SettingsXml); + Assert.AreEqual(runConfiguration.ResultsDirectory, Constants.DefaultResultsDirectory); + Assert.AreEqual(runConfiguration.TargetFrameworkVersion.ToString(), Framework.DefaultFramework.ToString()); + Assert.AreEqual(runConfiguration.TargetPlatform, Constants.DefaultPlatform); + } + + [TestMethod] + public void AddDefaultRunSettingsShouldAddUnspecifiedSettings() + { + this.runSettingsProvider.UpdateRunSettings("\r\n \r\n X86\r\n \r\n"); + + this.runSettingsProvider.AddDefaultRunSettings(); + + var runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(this.runSettingsProvider.ActiveRunSettings.SettingsXml); + Assert.AreEqual(runConfiguration.ResultsDirectory, Constants.DefaultResultsDirectory); + Assert.AreEqual(runConfiguration.TargetFrameworkVersion.ToString(), Framework.DefaultFramework.ToString()); + } + + [TestMethod] + public void AddDefaultRunSettingsShouldNotChangeSpecifiedSettings() + { + + this.runSettingsProvider.UpdateRunSettings("\r\n \r\n X64\r\n \r\n"); + + this.runSettingsProvider.AddDefaultRunSettings(); + + var runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(this.runSettingsProvider.ActiveRunSettings.SettingsXml); + Assert.AreEqual(runConfiguration.TargetPlatform, Architecture.X64); + } + + [TestMethod] + public void AddDefaultRunSettingsShouldThrowExceptionIfArgumentIsNull() + { + Assert.ThrowsException(() => RunSettingsProviderExtensions.AddDefaultRunSettings(null)); + } + + [TestMethod] + public void UpdateRunSettingsNodeShouldThrowExceptionIfKeyIsNull() + { + Assert.ThrowsException(() => this.runSettingsProvider.UpdateRunSettingsNode(null, "data")); + } + + [TestMethod] + public void UpdateRunSettingsNodeShouldThrowExceptionIfKeyIsEmptyOrWhiteSpace() + { + Assert.ThrowsException(() => this.runSettingsProvider.UpdateRunSettingsNode(" ", "data")); + } + + [TestMethod] + public void UpdateRunSettingsNodeShouldThrowExceptionIfDataIsNull() + { + Assert.ThrowsException(() => this.runSettingsProvider.UpdateRunSettingsNode("Key", null)); + } + + [TestMethod] + public void UpdateRunSettingsNodeShouldThrowExceptionIfRunSettingsProviderIsNull() + { + Assert.ThrowsException(() => RunSettingsProviderExtensions.UpdateRunSettingsNode(null, "Key", "data")); + } + + [TestMethod] + public void UpdateRunSettingsNodeShouldAddNewKeyIfNotPresent() + { + this.runSettingsProvider.UpdateRunSettings(" "); + this.runSettingsProvider.UpdateRunSettingsNode("Key.Path", "data"); + + Assert.AreEqual("data", this.runSettingsProvider.QueryRunSettingsNode("Key.Path")); + } + + [TestMethod] + public void UpdateRunSettingsNodeShouldUpdateKeyIfAlreadyPresent() + { + this.runSettingsProvider.UpdateRunSettings(" 1 "); + this.runSettingsProvider.UpdateRunSettingsNode("RunConfiguration.MaxCpuCount", "0"); + Assert.AreEqual("0", this.runSettingsProvider.QueryRunSettingsNode("RunConfiguration.MaxCpuCount")); + } + + [TestMethod] + public void QueryRunSettingsNodeShouldThrowIfKeyIsNull() + { + Assert.ThrowsException(() => this.runSettingsProvider.QueryRunSettingsNode(null)); + } + + [TestMethod] + public void QueryRunSettingsNodeShouldThrowIfKeyIsEmptyOrWhiteSpace() + { + Assert.ThrowsException(() => this.runSettingsProvider.QueryRunSettingsNode(" ")); + } + + [TestMethod] + public void QueryRunSettingsNodeShouldReturnNullForNotExistKey() + { + Assert.IsNull(this.runSettingsProvider.QueryRunSettingsNode("RunConfiguration.TargetPlatform")); + } + + [TestMethod] + public void QueryRunSettingsNodeShouldReturnCorrectValue() + { + this.runSettingsProvider.UpdateRunSettings(" x86 "); + Assert.AreEqual("x86", this.runSettingsProvider.QueryRunSettingsNode("RunConfiguration.TargetPlatform")); + } + } +} diff --git a/test/vstest.console.UnitTests/Processors/Utilities/RunSettingsUtilitiesTests.cs b/test/vstest.console.UnitTests/Processors/Utilities/RunSettingsUtilitiesTests.cs deleted file mode 100644 index 99370e398e..0000000000 --- a/test/vstest.console.UnitTests/Processors/Utilities/RunSettingsUtilitiesTests.cs +++ /dev/null @@ -1,112 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors.Utilities -{ - using System; - using System.IO; - - using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities; - using Microsoft.VisualStudio.TestPlatform.Common; - using Microsoft.VisualStudio.TestPlatform.Common.Interfaces; - using Microsoft.VisualStudio.TestTools.UnitTesting; - using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities; - using ObjectModel; - - [TestClass] - public class RunSettingsUtilitiesTests - { - private const string DefaultRunSettingsTemplate = - "\r\n \r\n %ResultsDirectory%\r\n X86\r\n %DefaultFramework%\r\n \r\n"; - - [TestCleanup] - public void TestCleanup() - { - CommandLineOptions.Instance.Reset(); - RunSettingsManager.Instance = null; - } - - [TestMethod] - public void UpdateRunSettingsShouldUpdateGivenSettingsXml() - { - var runSettingsManager = RunSettingsManager.Instance; - const string runSettingsXml = "\r\n \r\n X86\r\n \r\n"; - - RunSettingsUtilities.UpdateRunSettings(runSettingsManager, runSettingsXml); - - StringAssert.Contains(runSettingsManager.ActiveRunSettings.SettingsXml, runSettingsXml); - } - - [TestMethod] - public void AddDefaultRunSettingsShouldSetDefaultSettingsForEmptySettings() - { - var runSettingsManager = RunSettingsManager.Instance; - - RunSettingsUtilities.AddDefaultRunSettings(runSettingsManager); - - RunConfiguration runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(RunSettingsManager.Instance.ActiveRunSettings.SettingsXml); - Assert.AreEqual(runConfiguration.ResultsDirectory, Constants.DefaultResultsDirectory); - Assert.AreEqual(runConfiguration.TargetFrameworkVersion.ToString(), Framework.DefaultFramework.ToString()); - Assert.AreEqual(runConfiguration.TargetPlatform, Constants.DefaultPlatform); - } - - [TestMethod] - public void AddDefaultRunSettingsShouldAddUnspecifiedSettings() - { - var runSettingsManager = RunSettingsManager.Instance; - RunSettingsUtilities.UpdateRunSettings(runSettingsManager, "\r\n \r\n X86\r\n \r\n"); - - RunSettingsUtilities.AddDefaultRunSettings(runSettingsManager); - - RunConfiguration runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(RunSettingsManager.Instance.ActiveRunSettings.SettingsXml); - Assert.AreEqual(runConfiguration.ResultsDirectory, Constants.DefaultResultsDirectory); - Assert.AreEqual(runConfiguration.TargetFrameworkVersion.ToString(), Framework.DefaultFramework.ToString()); - } - - [TestMethod] - public void AddDefaultRunSettingsShouldNotChangeSpecifiedSettings() - { - var runSettingsManager = RunSettingsManager.Instance; - RunSettingsUtilities.UpdateRunSettings(runSettingsManager, "\r\n \r\n X64\r\n \r\n"); - - RunSettingsUtilities.AddDefaultRunSettings(runSettingsManager); - - RunConfiguration runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(RunSettingsManager.Instance.ActiveRunSettings.SettingsXml); - Assert.AreEqual(runConfiguration.TargetPlatform, Architecture.X64); - } - - [TestMethod] - public void AddDefaultRunSettingsShouldThrowExceptionIfArgumentIsNull() - { - Assert.ThrowsException(() => RunSettingsUtilities.AddDefaultRunSettings(null)); - } - - #region Testable Implementations - - private string GetDefaultRunSettings() - { - var defaultResultsDirectory = Path.Combine(Directory.GetCurrentDirectory(), "TestResults"); - return DefaultRunSettingsTemplate.Replace("%ResultsDirectory%", defaultResultsDirectory).Replace("%DefaultFramework%", Framework.DefaultFramework.Name); - } - - private class TestableRunSettingsProvider : IRunSettingsProvider - { - public RunSettings ActiveRunSettings { get; private set; } - - public void SetActiveRunSettings(RunSettings runSettings) - { - this.ActiveRunSettings = runSettings; - } - - public void SetActiveRunSettings(string settingsXml) - { - var runSettings = new RunSettings(); - runSettings.LoadSettingsXml(settingsXml); - - SetActiveRunSettings(runSettings); - } - } - - #endregion - } -} From e474688104a2423c985765b9632227f30f89f8c2 Mon Sep 17 00:00:00 2001 From: samadala Date: Sun, 8 Jan 2017 22:58:59 +0530 Subject: [PATCH 08/10] revert IntegrationTestEnvironment local changes --- .../IntegrationTestEnvironment.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/Microsoft.TestPlatform.TestUtilities/IntegrationTestEnvironment.cs b/test/Microsoft.TestPlatform.TestUtilities/IntegrationTestEnvironment.cs index cfd608caa0..376ea89c19 100644 --- a/test/Microsoft.TestPlatform.TestUtilities/IntegrationTestEnvironment.cs +++ b/test/Microsoft.TestPlatform.TestUtilities/IntegrationTestEnvironment.cs @@ -42,8 +42,8 @@ public IntegrationTestEnvironment() { // Running in VS/IDE. Use artifacts directory as root. this.runningInCli = false; - this.testPlatformRootDirectory = "C:/Users/samadala/src/vstest"; //Path.GetFullPath(@"..\..\.."); - this.TestAssetsPath = Path.Combine(this.testPlatformRootDirectory, @"test\TestAssets"); + this.testPlatformRootDirectory = Path.GetFullPath(@"..\..\.."); + this.TestAssetsPath = Path.Combine(this.testPlatformRootDirectory, @"artifacts\test\TestAssets"); } else { From 0675d9d21a8fac28dba98b1f9893553492babf61 Mon Sep 17 00:00:00 2001 From: samadala Date: Mon, 9 Jan 2017 15:58:53 +0530 Subject: [PATCH 09/10] update unit test and revert Integration test local changes --- .../IntegrationTestEnvironment.cs | 2 +- .../Processors/ResultsDirectoryArgumentProcessorTests.cs | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/test/Microsoft.TestPlatform.TestUtilities/IntegrationTestEnvironment.cs b/test/Microsoft.TestPlatform.TestUtilities/IntegrationTestEnvironment.cs index 376ea89c19..e83c10a6fa 100644 --- a/test/Microsoft.TestPlatform.TestUtilities/IntegrationTestEnvironment.cs +++ b/test/Microsoft.TestPlatform.TestUtilities/IntegrationTestEnvironment.cs @@ -91,7 +91,7 @@ public string PublishDirectory get { string value = string.Empty; - if (this.runningInCli || true) + if (this.runningInCli) { value = Path.Combine( this.testPlatformRootDirectory, diff --git a/test/vstest.console.UnitTests/Processors/ResultsDirectoryArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/ResultsDirectoryArgumentProcessorTests.cs index 01159e8c4d..aa2879fb5d 100644 --- a/test/vstest.console.UnitTests/Processors/ResultsDirectoryArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/ResultsDirectoryArgumentProcessorTests.cs @@ -86,11 +86,18 @@ public void InitializeShouldThrowIfArgumentIsAWhiteSpace() [TestMethod] public void InitializeShouldThrowIfRelativePathIsTooLong() { +#if NET46 + var errorMessage = + "The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters"; +#else + var errorMessage = "The specified file name or path is too long, or a component of the specified path is too long."; +#endif + var folder = @".\this\path\is\too\longgggggggggggggggggggggggggggggggggggggggggggggggggggglonggggggggggggggggggggggggggggggggggggggggggggggggggggglonggggggggggggggggggggggggggggggggggggggggggggggggggggglonggggggggggggggggggggggggggggggggggggggggggggggggggggglonggggggggggggggggggggggggggggggggggggggggggggggggggggglonggggggggggggggggggggggggggggggggggggggggggggggggggggglonggggggggggggggggggggggggggggggggggggggggggggggggggggg"; var message = string.Format( @"The path '{0}' specified in the 'ResultsDirectory' is invalid. Error: {1}", folder, - "The specified file name or path is too long, or a component of the specified path is too long."); + errorMessage); this.InitializeExceptionTestTemplate(folder, message); } From b8a782a820dca8f02b4ceffc16d57c63c36f8dcc Mon Sep 17 00:00:00 2001 From: samadala Date: Mon, 9 Jan 2017 18:57:54 +0530 Subject: [PATCH 10/10] * Rearrange order of help content * Update results directory help content --- scripts/build.ps1 | 2 +- .../Utilities/HelpContentPriority.cs | 121 +++++++++++------- .../Resources/Resources.Designer.cs | 2 +- src/vstest.console/Resources/Resources.resx | 2 +- .../ResultsDirectoryArgumentProcessorTests.cs | 20 +-- 5 files changed, 79 insertions(+), 68 deletions(-) diff --git a/scripts/build.ps1 b/scripts/build.ps1 index b05081f0db..74df19cec3 100644 --- a/scripts/build.ps1 +++ b/scripts/build.ps1 @@ -59,7 +59,7 @@ $env:DOTNET_SKIP_FIRST_TIME_EXPERIENCE = 1 # Dotnet build doesn't support --packages yet. See https://github.com/dotnet/cli/issues/2712 $env:NUGET_PACKAGES = $env:TP_PACKAGES_DIR $env:NUGET_EXE_Version = "3.4.3" -$env:DOTNET_CLI_VERSION = "latest" +$env:DOTNET_CLI_VERSION = "1.0.0-preview5-004384" # # Build configuration diff --git a/src/vstest.console/Processors/Utilities/HelpContentPriority.cs b/src/vstest.console/Processors/Utilities/HelpContentPriority.cs index 8e5a12521d..61af351a0a 100644 --- a/src/vstest.console/Processors/Utilities/HelpContentPriority.cs +++ b/src/vstest.console/Processors/Utilities/HelpContentPriority.cs @@ -5,6 +5,35 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors { /// /// Attribute to be used for displaying the help content in required order. + /// + /// Order of settings display (based on lifecycle at https://blogs.msdn.microsoft.com/visualstudioalm/2016/07/25/evolving-the-visual-studio-test-platform-part-1/). + /// + /// Selection + /// --Tests + /// --TestCaseFilter + /// + /// Configure + /// --Framework + /// --Platform + /// --Settings + /// --CLI runsettings + /// + /// Run/Discover + /// --ListTests + /// --Parallel + /// --TestAdapterPath + /// + /// Diagnose/Report + /// --Diag + /// --Logger + /// --ResultsDirectory + /// + /// IDE Automation + /// --ParentProcessId + /// --Port + /// + /// Help + /// -–Help /// internal enum HelpContentPriority { @@ -19,118 +48,118 @@ internal enum HelpContentPriority RunTestsArgumentProcessorHelpPriority, /// - /// RunSettingsArgumentProcessor Help + /// RunSpecificTestsArgumentProcessor Help /// - RunSettingsArgumentProcessorHelpPriority, + RunSpecificTestsArgumentProcessorHelpPriority, /// - /// RunSpecificTestsArgumentProcessor Help + /// TestCaseFilterArgumentProcessor Help /// - RunSpecificTestsArgumentProcessorHelpPriority, + TestCaseFilterArgumentProcessorHelpPriority, /// - /// EnableCodeCoverageArgumentProcessor Help + /// FrameworkArgumentProcessor Help /// - EnableCodeCoverageArgumentProcessorHelpPriority, + FrameworkArgumentProcessorHelpPriority, /// - /// InIsolationArgumentProcessor Help + /// PlatformArgumentProcessor Help /// - InIsolationArgumentProcessorHelpPriority, + PlatformArgumentProcessorHelpPriority, /// - /// DisableAutoFakesArgumentProcessor Help + /// RunSettingsArgumentProcessor Help /// - DisableAutoFakesArgumentProcessorHelpPriority, + RunSettingsArgumentProcessorHelpPriority, /// - /// UseVsixArgumentProcessor Help + /// CLIRunSettingsArgumentProcessor Help /// - UseVsixArgumentProcessorHelpPriority, + CLIRunSettingsArgumentProcessorHelpPriority, /// - /// TestAdapterPathArgumentProcessor Help + /// ListTestsArgumentExecutor Help /// - TestAdapterPathArgumentProcessorHelpPriority, + ListTestsArgumentProcessorHelpPriority, /// - /// PlatformArgumentProcessor Help + /// ParallelArgumentProcessor Help /// - PlatformArgumentProcessorHelpPriority, + ParallelArgumentProcessorHelpPriority, /// - /// FrameworkArgumentProcessor Help + /// TestAdapterPathArgumentProcessor Help /// - FrameworkArgumentProcessorHelpPriority, + TestAdapterPathArgumentProcessorHelpPriority, /// - /// ParallelArgumentProcessor Help + /// EnableDiagArgumentProcessor Help /// - ParallelArgumentProcessorHelpPriority, + EnableDiagArgumentProcessorHelpPriority, /// - /// ResultsDirectoryArgumentProcessor Help + /// EnableLoggerArgumentProcessor Help /// - ResultsDirectoryArgumentProcessorHelpPriority, + EnableLoggerArgumentProcessorHelpPriority, /// - /// TestCaseFilterArgumentProcessor Help + /// ResultsDirectoryArgumentProcessor Help /// - TestCaseFilterArgumentProcessorHelpPriority, + ResultsDirectoryArgumentProcessorHelpPriority, /// - /// HelpArgumentExecutor + /// PortArgumentProcessor Help /// - HelpArgumentProcessorHelpPriority, + ParentProcessIdArgumentProcessorHelpPriority, /// - /// EnableLoggerArgumentProcessor Help + /// PortArgumentProcessor Help /// - EnableLoggerArgumentProcessorHelpPriority, + PortArgumentProcessorHelpPriority, /// - /// ListTestsArgumentExecutor Help + /// HelpArgumentExecutor /// - ListTestsArgumentProcessorHelpPriority, + HelpArgumentProcessorHelpPriority, /// - /// ListDiscoverersArgumentProcessor Help + /// EnableCodeCoverageArgumentProcessor Help /// - ListDiscoverersArgumentProcessorHelpPriority, + EnableCodeCoverageArgumentProcessorHelpPriority, /// - /// ListExecutorsArgumentProcessor Help + /// InIsolationArgumentProcessor Help /// - ListExecutorsArgumentProcessorHelpPriority, + InIsolationArgumentProcessorHelpPriority, /// - /// ListLoggersArgumentProcessor Help + /// DisableAutoFakesArgumentProcessor Help /// - ListLoggersArgumentProcessorHelpPriority, + DisableAutoFakesArgumentProcessorHelpPriority, /// - /// ListSettingProviderArgumentProcessor Help + /// UseVsixArgumentProcessor Help /// - ListSettingsProvidersArgumentProcessorHelpPriority, + UseVsixArgumentProcessorHelpPriority, /// - /// PortArgumentProcessor Help + /// ListDiscoverersArgumentProcessor Help /// - ParentProcessIdArgumentProcessorHelpPriority, + ListDiscoverersArgumentProcessorHelpPriority, /// - /// PortArgumentProcessor Help + /// ListExecutorsArgumentProcessor Help /// - PortArgumentProcessorHelpPriority, + ListExecutorsArgumentProcessorHelpPriority, /// - /// EnableDiagArgumentProcessor Help + /// ListLoggersArgumentProcessor Help /// - EnableDiagArgumentProcessorHelpPriority, + ListLoggersArgumentProcessorHelpPriority, /// - /// CLIRunSettingsArgumentProcessor Help + /// ListSettingProviderArgumentProcessor Help /// - CLIRunSettingsArgumentProcessorHelpPriority + ListSettingsProvidersArgumentProcessorHelpPriority } } diff --git a/src/vstest.console/Resources/Resources.Designer.cs b/src/vstest.console/Resources/Resources.Designer.cs index 2a1db4379e..0f55d62a8b 100644 --- a/src/vstest.console/Resources/Resources.Designer.cs +++ b/src/vstest.console/Resources/Resources.Designer.cs @@ -1015,7 +1015,7 @@ public static string ProjectPathNotFound { /// /// Looks up a localized string similar to --ResultsDirectory|/ResultsDirectory - /// Test results directory will be created from a given path. + /// Test results directory will be created in specified path if not exists. /// Example /ResultsDirectory:<pathToResultsDirectory>. /// internal static string ResultsDirectoryArgumentHelp diff --git a/src/vstest.console/Resources/Resources.resx b/src/vstest.console/Resources/Resources.resx index 8bf5a7eff5..7165b68a1c 100644 --- a/src/vstest.console/Resources/Resources.resx +++ b/src/vstest.console/Resources/Resources.resx @@ -625,7 +625,7 @@ --ResultsDirectory|/ResultsDirectory - Test results directory will be created from a given path. + Test results directory will be created in specified path if not exists. Example /ResultsDirectory:<pathToResultsDirectory> diff --git a/test/vstest.console.UnitTests/Processors/ResultsDirectoryArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/ResultsDirectoryArgumentProcessorTests.cs index aa2879fb5d..73ffc53fb2 100644 --- a/test/vstest.console.UnitTests/Processors/ResultsDirectoryArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/ResultsDirectoryArgumentProcessorTests.cs @@ -50,7 +50,7 @@ public void CapabilitiesShouldReturnAppropriateProperties() { var capabilities = new ResultsDirectoryArgumentProcessorCapabilities(); Assert.AreEqual("/ResultsDirectory", capabilities.CommandName); - Assert.AreEqual("--ResultsDirectory|/ResultsDirectory\n Test results directory will be created from a given path \n Example: /ResultsDirectory:", capabilities.HelpContentResourceName); + Assert.AreEqual("--ResultsDirectory|/ResultsDirectory\n Test results directory will be created in specified path if not exists. \n Example: /ResultsDirectory:", capabilities.HelpContentResourceName); Assert.AreEqual(HelpContentPriority.ResultsDirectoryArgumentProcessorHelpPriority, capabilities.HelpPriority); Assert.AreEqual(false, capabilities.IsAction); @@ -83,24 +83,6 @@ public void InitializeShouldThrowIfArgumentIsAWhiteSpace() this.InitializeExceptionTestTemplate(folder, message); } - [TestMethod] - public void InitializeShouldThrowIfRelativePathIsTooLong() - { -#if NET46 - var errorMessage = - "The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters"; -#else - var errorMessage = "The specified file name or path is too long, or a component of the specified path is too long."; -#endif - - var folder = @".\this\path\is\too\longgggggggggggggggggggggggggggggggggggggggggggggggggggglonggggggggggggggggggggggggggggggggggggggggggggggggggggglonggggggggggggggggggggggggggggggggggggggggggggggggggggglonggggggggggggggggggggggggggggggggggggggggggggggggggggglonggggggggggggggggggggggggggggggggggggggggggggggggggggglonggggggggggggggggggggggggggggggggggggggggggggggggggggglonggggggggggggggggggggggggggggggggggggggggggggggggggggg"; - var message = string.Format( - @"The path '{0}' specified in the 'ResultsDirectory' is invalid. Error: {1}", - folder, - errorMessage); - this.InitializeExceptionTestTemplate(folder, message); - } - [TestMethod] public void InitializeShouldThrowIfGivenPathisIllegal() {