Skip to content

Commit

Permalink
Always look for adapters in Test Source directory (#1574)
Browse files Browse the repository at this point in the history
* Always look for adapters in Test Source directory

* test Name

* Allow multiple Loggers / TAP from dotnet CLI

* PR Comments

* test fix
  • Loading branch information
mayankbansal018 committed May 7, 2018
1 parent 3bd2fee commit e6b5743
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 23 deletions.
32 changes: 18 additions & 14 deletions src/Microsoft.TestPlatform.Build/Tasks/VSTestTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public string VSTestSetting
set;
}

public string VSTestTestAdapterPath
public string[] VSTestTestAdapterPath
{
get;
set;
Expand All @@ -54,7 +54,7 @@ public string VSTestTestCaseFilter
get;
set;
}
public string VSTestLogger
public string[] VSTestLogger
{
get;
set;
Expand Down Expand Up @@ -131,6 +131,7 @@ public void Cancel()

internal IEnumerable<string> CreateArgument()
{
var isConsoleLoggerEnabled = true;
var allArgs = new List<string>();

// TODO log arguments in task
Expand All @@ -139,16 +140,11 @@ internal IEnumerable<string> CreateArgument()
allArgs.Add("--settings:" + ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(this.VSTestSetting));
}

if (!string.IsNullOrEmpty(this.VSTestTestAdapterPath))
if (this.VSTestTestAdapterPath != null && this.VSTestTestAdapterPath.Length > 0)
{
allArgs.Add("--testAdapterPath:" + ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(this.VSTestTestAdapterPath));
}
else
{
// For Full CLR, add source directory as test adapter path.
if (this.VSTestFramework.StartsWith(".NETFramework", StringComparison.OrdinalIgnoreCase))
foreach (var arg in this.VSTestTestAdapterPath)
{
allArgs.Add("--testAdapterPath:" + ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(Path.GetDirectoryName(this.TestFileFullPath)));
allArgs.Add("--testAdapterPath:" + ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(arg));
}
}

Expand All @@ -168,9 +164,17 @@ internal IEnumerable<string> CreateArgument()
allArgs.Add("--testCaseFilter:" + ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(this.VSTestTestCaseFilter));
}

if (!string.IsNullOrEmpty(this.VSTestLogger))
if (this.VSTestLogger != null && this.VSTestLogger.Length > 0)
{
allArgs.Add("--logger:" + ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(this.VSTestLogger));
foreach (var arg in this.VSTestLogger)
{
allArgs.Add("--logger:" + ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(arg));

if (arg.StartsWith("console", StringComparison.OrdinalIgnoreCase))
{
isConsoleLoggerEnabled = false;
}
}
}

if (!string.IsNullOrEmpty(this.VSTestResultsDirectory))
Expand All @@ -197,8 +201,8 @@ internal IEnumerable<string> CreateArgument()
allArgs.Add(ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(this.TestFileFullPath));
}

if (!string.IsNullOrWhiteSpace(this.VSTestVerbosity) &&
(string.IsNullOrEmpty(this.VSTestLogger) || !this.VSTestLogger.StartsWith("console", StringComparison.OrdinalIgnoreCase)))
// Console logger was not specified by user, but verbosity was, hence add default console logger with verbosity as specified
if (!string.IsNullOrWhiteSpace(this.VSTestVerbosity) && isConsoleLoggerEnabled)
{
var normalTestLogging = new List<string>() { "n", "normal", "d", "detailed", "diag", "diagnostic" };
var quietTestLogging = new List<string>() { "q", "quiet" };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public class DefaultTestHostManager : ITestRuntimeProvider
private StringBuilder testHostProcessStdError;
private IMessageLogger messageLogger;
private bool hostExitedEventRaised;
private bool projectOutputExtensionsRequired;

/// <summary>
/// Initializes a new instance of the <see cref="DefaultTestHostManager"/> class.
Expand Down Expand Up @@ -192,7 +191,7 @@ public virtual TestProcessStartInfo GetTestHostProcessStartInfo(
/// <inheritdoc/>
public IEnumerable<string> GetTestPlatformExtensions(IEnumerable<string> sources, IEnumerable<string> extensions)
{
if (sources != null && sources.Any() && this.projectOutputExtensionsRequired)
if (sources != null && sources.Any())
{
extensions = extensions.Concat(sources.SelectMany(s => this.fileHelper.EnumerateFiles(Path.GetDirectoryName(s), SearchOption.TopDirectoryOnly, TestAdapterEndsWithPattern)));
}
Expand Down Expand Up @@ -248,7 +247,6 @@ public void Initialize(IMessageLogger logger, string runsettingsXml)
this.testHostProcess = null;

this.Shared = !runConfiguration.DisableAppDomain;
this.projectOutputExtensionsRequired = !(runConfiguration.TestAdaptersPaths?.Length > 0);
this.hostExitedEventRaised = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void ExecuteTestsWithDataCollection(RunnerInfo runnerInfo)
"bin",
IntegrationTestEnvironment.BuildConfiguration,
this.testEnvironment.RunnerFramework);
var arguments = PrepareArguments(assemblyPaths, this.GetTestAdapterPath(), runSettings, this.FrameworkArgValue, runnerInfo.InIsolationValue);
var arguments = PrepareArguments(assemblyPaths, null, runSettings, this.FrameworkArgValue, runnerInfo.InIsolationValue);
arguments = string.Concat(arguments, $" /ResultsDirectory:{resultsDir}", $" /Diag:{diagFileName}", $" /TestAdapterPath:{extensionsPath}");

this.InvokeVsTest(arguments);
Expand All @@ -73,7 +73,7 @@ public void ExecuteTestsWithDataCollectionUsingCollectArgument(RunnerInfo runner
IntegrationTestEnvironment.BuildConfiguration,
this.testEnvironment.RunnerFramework);

var arguments = PrepareArguments(assemblyPaths, this.GetTestAdapterPath(), null, this.FrameworkArgValue, runnerInfo.InIsolationValue);
var arguments = PrepareArguments(assemblyPaths, null, null, this.FrameworkArgValue, runnerInfo.InIsolationValue);
arguments = string.Concat(arguments, $" /ResultsDirectory:{resultsDir}", $" /Diag:{diagFileName}", $" /Collect:SampleDataCollector", $" /TestAdapterPath:{extensionsPath}");

this.InvokeVsTest(arguments);
Expand Down
39 changes: 37 additions & 2 deletions test/Microsoft.TestPlatform.Build.UnitTests/VsTestTaskTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void CreateArgumentShouldNotSetConsoleLoggerVerbosityIfConsoleLoggerIsGiv
// Add values for required properties.
vstestTask.TestFileFullPath = "abc";
vstestTask.VSTestFramework = "abc";
vstestTask.VSTestLogger = "Console;Verbosity=quiet";
vstestTask.VSTestLogger = new string[] { "Console;Verbosity=quiet" };

var allArguments = vstestTask.CreateArgument().ToArray();

Expand Down Expand Up @@ -209,7 +209,7 @@ public void CreateArgumentShouldPreserveWhiteSpaceInLogger()
// Add values for required properties.
vstestTask.TestFileFullPath = "abc";
vstestTask.VSTestFramework = "abc";
vstestTask.VSTestLogger = "trx;LogFileName=foo bar.trx";
vstestTask.VSTestLogger = new string[] { "trx;LogFileName=foo bar.trx" };


var allArguments = vstestTask.CreateArgument().ToArray();
Expand All @@ -234,5 +234,40 @@ public void CreateArgumentShouldAddOneCollectArgumentForEachCollect()
Assert.IsNotNull(allArguments.FirstOrDefault(arg => arg.Contains("--collect:name1")));
Assert.IsNotNull(allArguments.FirstOrDefault(arg => arg.Contains("--collect:\"name 2\"")));
}

[TestMethod]
public void CreateArgumentShouldAddMultipleTestAdapterPaths()
{
var vstestTask = new VSTestTask();

// Add values for required properties.
vstestTask.TestFileFullPath = "abc";
vstestTask.VSTestFramework = "abc";

vstestTask.VSTestTestAdapterPath = new string[] { "path1", "path2" };

var allArguments = vstestTask.CreateArgument().ToArray();

Assert.IsNotNull(allArguments.FirstOrDefault(arg => arg.Contains("--testAdapterPath:path1")));
Assert.IsNotNull(allArguments.FirstOrDefault(arg => arg.Contains("--testAdapterPath:path2")));
}

[TestMethod]
public void CreateArgumentShouldAddMultipleLoggers()
{
var vstestTask = new VSTestTask();

// Add values for required properties.
vstestTask.TestFileFullPath = "abc";
vstestTask.VSTestFramework = "abc";

vstestTask.VSTestLogger = new string[] { "trx;LogFileName=foo bar.trx", "console" };


var allArguments = vstestTask.CreateArgument().ToArray();

Assert.IsNotNull(allArguments.FirstOrDefault(arg => arg.Contains("--logger:\"trx;LogFileName=foo bar.trx\"")));
Assert.IsNotNull(allArguments.FirstOrDefault(arg => arg.Contains("--logger:console")));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,19 @@ public void GetTestPlatformExtensionsShouldReturnExtensionsListAsIsIfSourcesList
}

[TestMethod]
public void GetTestPlatformExtensionsShouldExcludeOutputDirectoryExtensionsIfTestAdapterPathIsSet()
public void GetTestPlatformExtensionsShouldNotExcludeOutputDirectoryExtensionsIfTestAdapterPathIsSet()
{
List<string> sourcesDir = new List<string> { @"C:\Source1" };
List<string> sources = new List<string> { @"C:\Source1\source1.dll" };

List<string> extensionsList1 = new List<string> { @"C:\Source1\ext1.TestAdapter.dll", @"C:\Source1\ext2.TestAdapter.dll" };
this.mockFileHelper.Setup(fh => fh.EnumerateFiles(sourcesDir[0], SearchOption.TopDirectoryOnly, "TestAdapter.dll")).Returns(extensionsList1);

this.mockFileHelper.Setup(fh => fh.GetFileVersion(extensionsList1[0])).Returns(new Version(2, 0));
this.mockFileHelper.Setup(fh => fh.GetFileVersion(extensionsList1[1])).Returns(new Version(5, 5));

this.testHostManager.Initialize(this.mockMessageLogger.Object, $"<?xml version=\"1.0\" encoding=\"utf-8\"?><RunSettings> <RunConfiguration><TestAdaptersPaths>C:\\Foo</TestAdaptersPaths></RunConfiguration> </RunSettings>");
List<string> currentList = new List<string> { @"FooExtension.dll" };
List<string> currentList = new List<string> { @"FooExtension.dll", @"C:\Source1\ext1.TestAdapter.dll", @"C:\Source1\ext2.TestAdapter.dll" };

// Act
var resultExtensions = this.testHostManager.GetTestPlatformExtensions(sources, currentList).ToList();
Expand Down

0 comments on commit e6b5743

Please sign in to comment.