Skip to content

Commit

Permalink
Support multiple paths in TestAdapterPath argument (#1320)
Browse files Browse the repository at this point in the history
Also honor environment variables in test adapter paths
  • Loading branch information
dtretyakov authored and singhsarab committed Dec 6, 2017
1 parent 77f9820 commit ec73594
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 22 deletions.
60 changes: 38 additions & 22 deletions src/vstest.console/Processors/TestAdapterPathArgumentProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ internal class TestAdapterPathArgumentExecutor : IArgumentExecutor
/// For file related operation
/// </summary>
private IFileHelper fileHelper;

/// <summary>
/// Separators for multiple paths in argument.
/// </summary>
private readonly char[] argumentSeparators = new [] { ';' };

#endregion

Expand Down Expand Up @@ -161,44 +166,40 @@ public void Initialize(string argument)

try
{
var testAdapterPaths = new List<string>();
var testAdapterFullPaths = new List<string>();

// VSTS task add double quotes around TestAdapterpath. For example if user has given TestAdapter path C:\temp,
// Then VSTS task will add TestAdapterPath as "/TestAdapterPath:\"C:\Temp\"".
// Remove leading and trailing ' " ' chars...
argument = argument.Trim().Trim(new char[] { '\"' });
customAdaptersPath = Path.GetFullPath(argument);
if (!fileHelper.DirectoryExists(customAdaptersPath))
{
throw new DirectoryNotFoundException(CommandLineResources.TestAdapterPathDoesNotExist);
}

// Get testadapter paths from RunSettings.
var testAdapterPathsInRunSettings = this.runSettingsManager.QueryRunSettingsNode("RunConfiguration.TestAdaptersPaths");

if (!string.IsNullOrWhiteSpace(testAdapterPathsInRunSettings))
{
var testAdapterFullPaths = new List<string>();
var testAdapterPathsInRunSettingsArray = testAdapterPathsInRunSettings.Split(
new[] { ';' },
StringSplitOptions.RemoveEmptyEntries);
testAdapterPaths.AddRange(SplitPaths(testAdapterPathsInRunSettings));
}

testAdapterPaths.AddRange(SplitPaths(argument));

foreach (var testadapterPath in testAdapterPaths)
{
// TestAdaptersPaths could contain environment variables
var testAdapterFullPath = Path.GetFullPath(Environment.ExpandEnvironmentVariables(testadapterPath));

foreach (var testadapterPath in testAdapterPathsInRunSettingsArray)
if (!this.fileHelper.DirectoryExists(testAdapterFullPath))
{
var testAdapterFullPath = Path.GetFullPath(testadapterPath);

if (!this.fileHelper.DirectoryExists(testAdapterFullPath))
{
invalidAdapterPathArgument = testadapterPath;
throw new DirectoryNotFoundException(CommandLineResources.TestAdapterPathDoesNotExist);
}

testAdapterFullPaths.Add(testAdapterFullPath);
invalidAdapterPathArgument = testadapterPath;
throw new DirectoryNotFoundException(CommandLineResources.TestAdapterPathDoesNotExist);
}

testAdapterFullPaths.Add(customAdaptersPath);
testAdapterFullPaths = testAdapterFullPaths.Distinct().ToList();
customAdaptersPath = string.Join(";", testAdapterFullPaths.ToArray());
testAdapterFullPaths.Add(testAdapterFullPath);
}

customAdaptersPath = string.Join(";", testAdapterFullPaths.Distinct().ToArray());

this.runSettingsManager.UpdateRunSettingsNode("RunConfiguration.TestAdaptersPaths", customAdaptersPath);
}
catch (Exception e)
Expand All @@ -210,6 +211,21 @@ public void Initialize(string argument)
this.commandLineOptions.TestAdapterPath = customAdaptersPath;
}

/// <summary>
/// Splits provided paths into array.
/// </summary>
/// <param name="paths">Source paths joined by semicolons.</param>
/// <returns>Paths.</returns>
private string[] SplitPaths(string paths)
{
if (string.IsNullOrWhiteSpace(paths))
{
return new string[] { };
}

return paths.Split(argumentSeparators, StringSplitOptions.RemoveEmptyEntries);
}

/// <summary>
/// Executes the argument processor.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,44 @@ public void InitializeShouldMergeTestAdapterPathsInRunSettingsIgnoringDuplicateP
Assert.AreEqual("d:\\users;c:\\users", runConfiguration.TestAdaptersPaths);
}

[TestMethod]
public void InitializeShouldMergeMultipleTestAdapterPathsWithPathsInRunSettings()
{
var runSettingsXml = "<RunSettings><RunConfiguration><TestAdaptersPaths>d:\\users;f:\\users</TestAdaptersPaths></RunConfiguration></RunSettings>";
var runSettings = new RunSettings();
runSettings.LoadSettingsXml(runSettingsXml);
RunSettingsManager.Instance.SetActiveRunSettings(runSettings);
var mockFileHelper = new Mock<IFileHelper>();
var mockOutput = new Mock<IOutput>();

mockFileHelper.Setup(x => x.DirectoryExists(It.IsAny<string>())).Returns(true);
var executor = new TestAdapterPathArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance, mockOutput.Object, mockFileHelper.Object);

executor.Initialize("c:\\users;e:\\users");
var runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(RunSettingsManager.Instance.ActiveRunSettings.SettingsXml);
Assert.AreEqual("d:\\users;f:\\users;c:\\users;e:\\users", runConfiguration.TestAdaptersPaths);
}

[TestMethod]
public void InitializeShouldHonorEnvironmentVariablesInTestAdapterPaths()
{
var runSettingsXml = "<RunSettings><RunConfiguration><TestAdaptersPaths>%temp%\\adapters1</TestAdaptersPaths></RunConfiguration></RunSettings>";
var runSettings = new RunSettings();
runSettings.LoadSettingsXml(runSettingsXml);
RunSettingsManager.Instance.SetActiveRunSettings(runSettings);
var mockFileHelper = new Mock<IFileHelper>();
var mockOutput = new Mock<IOutput>();

mockFileHelper.Setup(x => x.DirectoryExists(It.IsAny<string>())).Returns(true);
var executor = new TestAdapterPathArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance, mockOutput.Object, mockFileHelper.Object);

executor.Initialize("%temp%\\adapters2");
var runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(RunSettingsManager.Instance.ActiveRunSettings.SettingsXml);

var tempPath = Path.GetFullPath(Environment.ExpandEnvironmentVariables("%temp%"));
Assert.AreEqual(string.Format("{0}\\adapters1;{0}\\adapters2", tempPath), runConfiguration.TestAdaptersPaths);
}

[TestMethod]
public void InitializeShouldAddRightAdapterPathInErrorMessage()
{
Expand Down

0 comments on commit ec73594

Please sign in to comment.