Skip to content

Commit

Permalink
DataCollector search in output directory (#2015)
Browse files Browse the repository at this point in the history
* DataCollector Search in output directory
  • Loading branch information
vagisha-nidhi committed May 13, 2019
1 parent 06cf5cf commit 7909f50
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollect
using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;

using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;
using CommunicationUtilitiesResources = Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Resources.Resources;
using CoreUtilitiesConstants = Microsoft.VisualStudio.TestPlatform.CoreUtilities.Constants;

Expand All @@ -48,6 +48,8 @@ internal class DataCollectionRequestHandler : IDataCollectionRequestHandler, IDi

private IDataSerializer dataSerializer;

private IFileHelper fileHelper;

/// <summary>
/// Use to cancel data collection test case events monitoring if test run is cancelled.
/// </summary>
Expand All @@ -65,7 +67,8 @@ protected DataCollectionRequestHandler(IMessageSink messageSink)
messageSink,
DataCollectionManager.Create(messageSink),
new DataCollectionTestCaseEventHandler(),
JsonDataSerializer.Instance)
JsonDataSerializer.Instance,
new FileHelper())
{
this.messageSink = messageSink;
}
Expand All @@ -88,19 +91,24 @@ protected DataCollectionRequestHandler(IMessageSink messageSink)
/// <param name="dataSerializer">
/// Serializer for serialization and deserialization of the messages.
/// </param>
/// <param name="fileHelper">
/// File Helper
/// </param>
protected DataCollectionRequestHandler(
ICommunicationManager communicationManager,
IMessageSink messageSink,
IDataCollectionManager dataCollectionManager,
IDataCollectionTestCaseEventHandler dataCollectionTestCaseEventHandler,
IDataSerializer dataSerializer)
IDataSerializer dataSerializer,
IFileHelper fileHelper)
{
this.communicationManager = communicationManager;
this.messageSink = messageSink;
this.dataCollectionManager = dataCollectionManager;
this.dataSerializer = dataSerializer;
this.dataCollectionTestCaseEventHandler = dataCollectionTestCaseEventHandler;
this.cancellationTokenSource = new CancellationTokenSource();
this.fileHelper = fileHelper;
}

/// <summary>
Expand Down Expand Up @@ -137,7 +145,8 @@ protected DataCollectionRequestHandler(IMessageSink messageSink)
messageSink,
DataCollectionManager.Create(messageSink),
new DataCollectionTestCaseEventHandler(),
JsonDataSerializer.Instance);
JsonDataSerializer.Instance,
new FileHelper());
}
}
}
Expand Down Expand Up @@ -228,40 +237,49 @@ public void Close()
/// <summary>
/// Update the test adapter paths provided through run settings to be used by the test plugin cache.
/// </summary>
/// <param name="runSettings">
/// The run Settings.
/// <param name="payload">
/// The before test run start payload
/// </param>
private void AddExtensionAssemblies(string runSettings)
private void AddExtensionAssemblies(BeforeTestRunStartPayload payload)
{
try
{
IEnumerable<string> customTestAdaptersPaths = RunSettingsUtilities.GetTestAdaptersPaths(runSettings);
var customTestAdaptersPaths = RunSettingsUtilities.GetTestAdaptersPaths(payload.SettingsXml);

// In case of dotnet vstest with code coverage, data collector needs to be picked up from publish folder.
// Therefore, adding source dll folders to search datacollectors in these.
var datacollectorSearchPaths = new HashSet<string>();
foreach (var source in payload.Sources)
{
datacollectorSearchPaths.Add(Path.GetDirectoryName(source));
}

if (customTestAdaptersPaths != null)
{
var fileHelper = new FileHelper();
datacollectorSearchPaths.UnionWith(customTestAdaptersPaths);
}

List<string> extensionAssemblies = new List<string>();
foreach (var customTestAdaptersPath in customTestAdaptersPaths)
List<string> extensionAssemblies = new List<string>();
foreach (var datacollectorSearchPath in datacollectorSearchPaths)
{
var adapterPath =
Path.GetFullPath(Environment.ExpandEnvironmentVariables(datacollectorSearchPath));
if (!this.fileHelper.DirectoryExists(adapterPath))
{
var adapterPath =
Path.GetFullPath(Environment.ExpandEnvironmentVariables(customTestAdaptersPath));
if (!fileHelper.DirectoryExists(adapterPath))
{
EqtTrace.Warning(string.Format("AdapterPath Not Found:", adapterPath));
continue;
}

extensionAssemblies.AddRange(
fileHelper.EnumerateFiles(
adapterPath,
SearchOption.AllDirectories,
TestPlatformConstants.DataCollectorEndsWithPattern));
EqtTrace.Warning(string.Format("AdapterPath Not Found:", adapterPath));
continue;
}

if (extensionAssemblies.Count > 0)
{
TestPluginCache.Instance.UpdateExtensions(extensionAssemblies, skipExtensionFilters: false);
}
extensionAssemblies.AddRange(
this.fileHelper.EnumerateFiles(
adapterPath,
SearchOption.AllDirectories,
TestPlatformConstants.DataCollectorEndsWithPattern));
}

if (extensionAssemblies.Count > 0)
{
TestPluginCache.Instance.UpdateExtensions(extensionAssemblies, skipExtensionFilters: false);
}
}
catch (Exception e)
Expand All @@ -278,7 +296,7 @@ private void HandleBeforeTestRunStart(Message message)
{
// Initialize datacollectors and get enviornment variables.
var payload = this.dataSerializer.DeserializePayload<BeforeTestRunStartPayload>(message);
this.AddExtensionAssemblies(payload.SettingsXml);
this.AddExtensionAssemblies(payload);

var envVariables = this.dataCollectionManager.InitializeDataCollectors(payload.SettingsXml);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@
namespace Microsoft.TestPlatform.CommunicationUtilities.UnitTests
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;

using Microsoft.TestPlatform.CommunicationUtilities.UnitTests.TestDoubles;
using Microsoft.VisualStudio.TestPlatform.Common.DataCollection;
using Microsoft.VisualStudio.TestPlatform.Common.DataCollector.Interfaces;
using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection.Interfaces;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Interfaces;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Moq;
Expand All @@ -39,6 +41,7 @@ public class DataCollectionRequestHandlerTests
private Mock<IDataCollectionTestCaseEventHandler> mockDataCollectionTestCaseEventHandler;
private TestableDataCollectionRequestHandler requestHandler;
private Mock<IDataSerializer> mockDataSerializer;
private Mock<IFileHelper> mockFileHelper;
private Message afterTestRunEnd = new Message() { MessageType = MessageType.AfterTestRunEnd, Payload = "false" };
private Message beforeTestRunStart = new Message()
{
Expand All @@ -54,7 +57,8 @@ public DataCollectionRequestHandlerTests()
this.mockDataSerializer = new Mock<IDataSerializer>();
this.mockDataCollectionTestCaseEventHandler = new Mock<IDataCollectionTestCaseEventHandler>();
this.mockDataCollectionTestCaseEventHandler.Setup(x => x.WaitForRequestHandlerConnection(It.IsAny<int>())).Returns(true);
this.requestHandler = new TestableDataCollectionRequestHandler(this.mockCommunicationManager.Object, this.mockMessageSink.Object, this.mockDataCollectionManager.Object, this.mockDataCollectionTestCaseEventHandler.Object, this.mockDataSerializer.Object);
this.mockFileHelper = new Mock<IFileHelper>();
this.requestHandler = new TestableDataCollectionRequestHandler(this.mockCommunicationManager.Object, this.mockMessageSink.Object, this.mockDataCollectionManager.Object, this.mockDataCollectionTestCaseEventHandler.Object, this.mockDataSerializer.Object, this.mockFileHelper.Object);

this.mockCommunicationManager.SetupSequence(x => x.ReceiveMessage()).Returns(this.beforeTestRunStart).Returns(this.afterTestRunEnd);

Expand Down Expand Up @@ -226,6 +230,34 @@ public void ProcessRequestsShouldDisposeDataCollectorsOnAfterTestRunEnd()
this.mockDataCollectionManager.Verify(x => x.Dispose());
}

[TestMethod]
public void ProcessRequestsShouldAddSourceDirectoryToTestPluginCache()
{
var testHostLaunchedPayload = new TestHostLaunchedPayload();
testHostLaunchedPayload.ProcessId = 1234;

string runSettings = "<RunSettings><RunConfiguration><TestAdaptersPaths>d:\\users;f:\\users</TestAdaptersPaths></RunConfiguration></RunSettings>";

this.mockCommunicationManager.SetupSequence(x => x.ReceiveMessage()).Returns(this.beforeTestRunStart)
.Returns(new Message() { MessageType = MessageType.TestHostLaunched, Payload = JToken.FromObject(testHostLaunchedPayload) })
.Returns(this.afterTestRunEnd);

this.mockDataCollectionManager.Setup(x => x.SessionStarted(It.IsAny<SessionStartEventArgs>())).Returns(true);
this.mockDataCollectionManager.Setup(x => x.TestHostLaunched(It.IsAny<int>()));
this.mockDataSerializer.Setup(x => x.DeserializePayload<TestHostLaunchedPayload>(It.Is<Message>(y => y.MessageType == MessageType.TestHostLaunched)))
.Returns(testHostLaunchedPayload);
var beforeTestRunSTartPayload = new BeforeTestRunStartPayload { SettingsXml = runSettings, Sources = new List<string> { @"E:\dir1\test1.dll", @"E:\dir2\test2.dll", @"E:\dir1\test2.dll" } };
this.mockDataSerializer.Setup(x => x.DeserializePayload<BeforeTestRunStartPayload>(It.Is<Message>(y => y.MessageType == MessageType.BeforeTestRunStart)))
.Returns(beforeTestRunSTartPayload);
this.mockFileHelper.Setup(x => x.DirectoryExists(@"E:\dir1")).Returns(true);
this.mockFileHelper.Setup(x => x.EnumerateFiles("E:\\dir1", SearchOption.AllDirectories, @"Collector.dll")).Returns(new List<string> { @"E:\dir1\abc.datacollector.dll" });

this.requestHandler.ProcessRequests();

this.mockFileHelper.Verify(x => x.EnumerateFiles("E:\\dir1", SearchOption.AllDirectories, @"Collector.dll"), Times.Once);
Assert.IsTrue(TestPluginCache.Instance.GetExtensionPaths(@"Collector.dll").Contains(@"E:\dir1\abc.datacollector.dll"));
}

[TestMethod]
public void ProcessRequestsShouldThrowExceptionIfThrownByCommunicationManager()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ namespace Microsoft.TestPlatform.CommunicationUtilities.UnitTests.TestDoubles
using Microsoft.VisualStudio.TestPlatform.Common.DataCollector.Interfaces;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Interfaces;
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;

/// <summary>
/// Testable class for DataCollectionRequestHandler since all constructors of DataCollectionRequestHandler are protected.
/// </summary>
internal class TestableDataCollectionRequestHandler : DataCollectionRequestHandler
{
public TestableDataCollectionRequestHandler(ICommunicationManager communicationManager, IMessageSink messageSink, IDataCollectionManager dataCollectionManager, IDataCollectionTestCaseEventHandler dataCollectionTestCaseEventHandler, IDataSerializer dataSerializer)
: base(communicationManager, messageSink, dataCollectionManager, dataCollectionTestCaseEventHandler, dataSerializer)
public TestableDataCollectionRequestHandler(ICommunicationManager communicationManager, IMessageSink messageSink, IDataCollectionManager dataCollectionManager, IDataCollectionTestCaseEventHandler dataCollectionTestCaseEventHandler, IDataSerializer dataSerializer, IFileHelper fIleHelper)
: base(communicationManager, messageSink, dataCollectionManager, dataCollectionTestCaseEventHandler, dataSerializer, fIleHelper)
{
}
}
Expand Down

0 comments on commit 7909f50

Please sign in to comment.