Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add ResultsDirectory arg to cli #322

Merged
merged 12 commits into from
Jan 9, 2017
26 changes: 26 additions & 0 deletions src/Microsoft.TestPlatform.CoreUtilities/ValidateArg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,32 @@ public static string NotNullOrEmpty([ValidatedNotNull]string arg, string paramet
return arg;
}

/// <summary>
/// Validate a string is not null, empty or consists only of white-space characters.
/// </summary>
/// <param name="arg">
/// Input string.
/// </param>
/// <param name="parameterName">
/// Name of the parameter to validate.
/// </param>
/// <returns>
/// Validated string.
/// </returns>
/// <exception cref="ArgumentNullException">
/// Thrown if the input string is null null, empty or consists only of white-space characters.
/// </exception>
[DebuggerStepThrough]
public static string NotNullOrWhiteSpace([ValidatedNotNull]string arg, string parameterName)
{
if (string.IsNullOrWhiteSpace(arg))
{
throw new ArgumentNullException(parameterName);
}

return arg;
}

/// <summary>
/// Throws ArgumentOutOfRangeException if the argument is less than zero.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ private static bool TryGetFrameworkXml(XPathNavigator runSettingsNavigator, out

Func<string, bool> validator = (string xml) =>
{
if (Framework.FromString(xml) != null)
{
// Allow TargetFrameworkMoniker values like .NETFramework,Version=v4.5, ".NETCoreApp,Version=v1.0
return true;
}
var value = (FrameworkVersion)Enum.Parse(typeof(FrameworkVersion), xml, true);
if (!Enum.IsDefined(typeof(FrameworkVersion), value) || value == FrameworkVersion.None)
Expand Down
12 changes: 11 additions & 1 deletion src/vstest.console/CommandLine/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,22 @@ internal Framework TargetFrameworkVersion
this.FrameworkVersionSpecified = true;
}
}

/// <summary>
/// Gets a value indicating whether /Framework has been specified on command line or not.
/// </summary>
internal bool FrameworkVersionSpecified { get; private set; }

/// <summary>
/// Gets or sets the results directory for test run.
/// </summary>
internal string ResultsDirectory { get; set; }

/// <summary>
/// Gets or sets the /setting switch value. i.e path to settings file.
/// </summary>
internal string SettingsFile { get; set; }

#endregion

#region Public Methods
Expand Down
5 changes: 5 additions & 0 deletions src/vstest.console/CommandLine/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -190,6 +192,9 @@ private int GetArgumentProcessors(string[] args, out List<IArgumentProcessor> pr
var processorsToAlwaysExecute = processorFactory.GetArgumentProcessorsToAlwaysExecute();
processors.AddRange(processorsToAlwaysExecute);

// Initialize Runsettings with defaults
RunSettingsManager.Instance.AddDefaultRunSettings();

// Ensure we have an action argument.
this.EnsureActionArgumentIsPresent(processors, processorFactory);

Expand Down
69 changes: 4 additions & 65 deletions src/vstest.console/Processors/CLIRunSettingsArgumentProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -114,41 +115,8 @@ 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
}

// 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);
CreateOrOverwriteRunSettings(this.runSettingsManager, arguments);
}
catch (XPathException exception)
{
Expand All @@ -166,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;

Expand All @@ -186,37 +154,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;
runSettingsProvider.UpdateRunSettingsNode(key, 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;
}

return node;
}
}
}
41 changes: 34 additions & 7 deletions src/vstest.console/Processors/FrameworkArgumentProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ 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 Microsoft.VisualStudio.TestPlatform.Utilities;

using CommandLineResources = Microsoft.VisualStudio.TestPlatform.CommandLine.Resources.Resources;

Expand Down Expand Up @@ -54,7 +59,7 @@ public Lazy<IArgumentExecutor> Executor
{
if (this.executor == null)
{
this.executor = new Lazy<IArgumentExecutor>(() => new FrameworkArgumentExecutor(CommandLineOptions.Instance));
this.executor = new Lazy<IArgumentExecutor>(() => new FrameworkArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance));
}

return this.executor;
Expand Down Expand Up @@ -94,21 +99,27 @@ internal class FrameworkArgumentExecutor : IArgumentExecutor
/// </summary>
private CommandLineOptions commandLineOptions;

private IRunSettingsProvider runSettingsManager;

public const string RunSettingsPath = "RunConfiguration.TargetFrameworkVersion";

#endregion

#region Constructor

/// <summary>
/// Default constructor.
/// </summary>
/// <param name="options">
/// The options.
/// </param>
public FrameworkArgumentExecutor(CommandLineOptions options)
/// <param name="options"> The options. </param>
/// <param name="runSettingsManager"> The runsettings manager. </param>
public FrameworkArgumentExecutor(CommandLineOptions options, IRunSettingsProvider runSettingsManager)
{
Contract.Requires(options != null);
Contract.Requires(runSettingsManager != null);
this.commandLineOptions = options;
this.runSettingsManager = runSettingsManager;
}

#endregion

#region IArgumentExecutor
Expand All @@ -126,11 +137,27 @@ public void Initialize(string argument)

var validFramework = Framework.FromString(argument);
if (validFramework == null)
{
{
throw new CommandLineException(
string.Format(CultureInfo.CurrentCulture, CommandLineResources.InvalidFrameworkVersion, argument));
}
commandLineOptions.TargetFrameworkVersion = validFramework;

this.commandLineOptions.TargetFrameworkVersion = validFramework;

if (this.commandLineOptions.TargetFrameworkVersion != Framework.DefaultFramework
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get more context on why this warning is interesting?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added Comment.

&& !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());
}
else
{
this.runSettingsManager.UpdateRunSettingsNode(FrameworkArgumentExecutor.RunSettingsPath,
validFramework.ToString());
}

if (EqtTrace.IsInfoEnabled)
{
Expand Down
3 changes: 2 additions & 1 deletion src/vstest.console/Processors/ListTestsArgumentProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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 },
Expand Down
21 changes: 16 additions & 5 deletions src/vstest.console/Processors/ParallelArgumentProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
Expand Down Expand Up @@ -49,7 +53,7 @@ public Lazy<IArgumentExecutor> Executor
{
if (this.executor == null)
{
this.executor = new Lazy<IArgumentExecutor>(() => new ParallelArgumentExecutor(CommandLineOptions.Instance));
this.executor = new Lazy<IArgumentExecutor>(() => new ParallelArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance));
}

return this.executor;
Expand Down Expand Up @@ -89,21 +93,27 @@ internal class ParallelArgumentExecutor : IArgumentExecutor
/// </summary>
private CommandLineOptions commandLineOptions;

private IRunSettingsProvider runSettingsManager;

public const string RunSettingsPath = "RunConfiguration.MaxCpuCount";

#endregion

#region Constructor

/// <summary>
/// Default constructor.
/// </summary>
/// <param name="options">
/// The options.
/// </param>
public ParallelArgumentExecutor(CommandLineOptions options)
/// <param name="options"> The options. </param>
/// <param name="runSettingsManager"> The runsettings manager. </param>
public ParallelArgumentExecutor(CommandLineOptions options, IRunSettingsProvider runSettingsManager)
{
Contract.Requires(options != null);
Contract.Requires(runSettingsManager != null);
this.commandLineOptions = options;
this.runSettingsManager = runSettingsManager;
}

#endregion

#region IArgumentExecutor
Expand All @@ -122,6 +132,7 @@ public void Initialize(string argument)
}

commandLineOptions.Parallel = true;
this.runSettingsManager.UpdateRunSettingsNode(ParallelArgumentExecutor.RunSettingsPath, "0");
}

/// <summary>
Expand Down
Loading