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

Support multiple paths in TestAdapterPath argument (#1319) #1320

Merged
merged 2 commits into from
Dec 6, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 37 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,39 @@ 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)
{
var testAdapterFullPath = Path.GetFullPath(testadapterPath);
Copy link
Contributor

Choose a reason for hiding this comment

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

As environment variables (%temp%\adapters) supported in runsettings file's "TestAdaptersPaths". Consider adding "Environment.ExpandEnvironmentVariables" here and test for same.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@smadala, ok, it was fixed.


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 +210,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,24 @@ 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 InitializeShouldAddRightAdapterPathInErrorMessage()
{
Expand Down