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

Pass coverlet codebase in runsettings for inproc data collector initialization #2288

Merged
merged 11 commits into from
Jan 30, 2020
Prev Previous commit
Next Next commit
move get coverlet codebase path to init
  • Loading branch information
MarcoRossignoli committed Jan 16, 2020
commit f7ec6085a4e94ab8b182efb074d0b2c8c868daa5
39 changes: 14 additions & 25 deletions src/vstest.console/Processors/CollectArgumentProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,41 +122,30 @@ public void Initialize(string argument)
{
throw new SettingsException(string.Format(CommandLineResources.CollectWithTestSettingErrorMessage, argument));
}
AddDataCollectorToRunSettings(argument, this.runSettingsManager);
AddDataCollectorToRunSettings(argument, this.runSettingsManager, this.fileHelper);
}

/// <summary>
/// We try to fix inproc coverlet codebase searching coverlet.collector.dll assembly inside adaptersPaths
/// Returns coverlet codebase searching coverlet.collector.dll assembly inside adaptersPaths
/// </summary>
private void FixCoverletInProcessCollectorCodeBase()
private static string GetCoverletCodeBasePath(IRunSettingsProvider runSettingProvider, IFileHelper fileHelper)
{
DataCollectionRunSettings inProcDataCollectionRunSettings = XmlRunSettingsUtilities.GetInProcDataCollectionRunSettings(this.runSettingsManager.ActiveRunSettings.SettingsXml);

if (inProcDataCollectionRunSettings is null)
foreach (string adapterPath in RunSettingsUtilities.GetTestAdaptersPaths(runSettingProvider.ActiveRunSettings.SettingsXml))
{
return;
}

if (DoesDataCollectorSettingsExist(CoverletConstants.CoverletDataCollectorFriendlyName, inProcDataCollectionRunSettings, out DataCollectorSettings inProcDataCollector))
{
foreach (string adapterPath in RunSettingsUtilities.GetTestAdaptersPaths(this.runSettingsManager.ActiveRunSettings.SettingsXml))
string collectorPath = Path.Combine(adapterPath, CoverletConstants.CoverletDataCollectorCodebase);
if (fileHelper.Exists(collectorPath))
{
string collectorPath = Path.Combine(adapterPath, CoverletConstants.CoverletDataCollectorCodebase);
if (fileHelper.Exists(collectorPath))
{
inProcDataCollector.CodeBase = collectorPath;
runSettingsManager.UpdateRunSettingsNodeInnerXml(Constants.InProcDataCollectionRunSettingsName, inProcDataCollectionRunSettings.ToXml().InnerXml);
EqtTrace.Verbose("CoverletDataCollector in-process codeBase updated to '{0}'", inProcDataCollector.CodeBase);
break;
}
EqtTrace.Verbose("CoverletDataCollector in-process codeBase path '{0}'", collectorPath);
return collectorPath;
}
}

return null;
}

/// <inheritdoc />
public ArgumentProcessorResult Execute()
{
FixCoverletInProcessCollectorCodeBase();
return ArgumentProcessorResult.Success;
}

Expand All @@ -180,7 +169,7 @@ internal static void EnableDataCollectorUsingFriendlyName(string argument, DataC
/// <summary>
/// Enables coverlet inproc datacollector
/// </summary>
internal static void EnableCoverletInProcDataCollector(string argument, DataCollectionRunSettings dataCollectionRunSettings)
internal static void EnableCoverletInProcDataCollector(string argument, DataCollectionRunSettings dataCollectionRunSettings, IRunSettingsProvider runSettingProvider, IFileHelper fileHelper)
{
DataCollectorSettings dataCollectorSettings = null;

Expand All @@ -190,7 +179,7 @@ internal static void EnableCoverletInProcDataCollector(string argument, DataColl
dataCollectorSettings = new DataCollectorSettings();
dataCollectorSettings.FriendlyName = argument;
dataCollectorSettings.AssemblyQualifiedName = CoverletConstants.CoverletDataCollectorAssemblyQualifiedName;
dataCollectorSettings.CodeBase = CoverletConstants.CoverletDataCollectorCodebase;
dataCollectorSettings.CodeBase = GetCoverletCodeBasePath(runSettingProvider, fileHelper) ?? CoverletConstants.CoverletDataCollectorCodebase;
dataCollectorSettings.IsEnabled = true;
dataCollectionRunSettings.DataCollectorSettingsList.Add(dataCollectorSettings);
}
Expand Down Expand Up @@ -220,7 +209,7 @@ private static bool DoesDataCollectorSettingsExist(string friendlyName,
return false;
}

internal static void AddDataCollectorToRunSettings(string argument, IRunSettingsProvider runSettingsManager)
internal static void AddDataCollectorToRunSettings(string argument, IRunSettingsProvider runSettingsManager, IFileHelper fileHelper)
{
EnabledDataCollectors.Add(argument.ToLower());

Expand All @@ -246,7 +235,7 @@ internal static void AddDataCollectorToRunSettings(string argument, IRunSettings
if (string.Equals(argument, CoverletConstants.CoverletDataCollectorFriendlyName, StringComparison.OrdinalIgnoreCase))
{
// Add inproc data collector to runsetings if coverlet code coverage is enabled
EnableCoverletInProcDataCollector(argument, inProcDataCollectionRunSettings);
EnableCoverletInProcDataCollector(argument, inProcDataCollectionRunSettings, runSettingsManager, fileHelper);
runSettingsManager.UpdateRunSettingsNodeInnerXml(Constants.InProcDataCollectionRunSettingsName, inProcDataCollectionRunSettings.ToXml().InnerXml);
}
}
Expand Down
15 changes: 11 additions & 4 deletions src/vstest.console/Processors/EnableBlameArgumentProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors
using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions;
using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions.Interfaces;
using Microsoft.VisualStudio.TestPlatform.Utilities;

using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;
using CommandLineResources = Microsoft.VisualStudio.TestPlatform.CommandLine.Resources.Resources;

internal class EnableBlameArgumentProcessor : IArgumentProcessor
Expand Down Expand Up @@ -61,7 +62,7 @@ public Lazy<IArgumentExecutor> Executor
{
if (this.executor == null)
{
this.executor = new Lazy<IArgumentExecutor>(() => new EnableBlameArgumentExecutor(RunSettingsManager.Instance, new PlatformEnvironment()));
this.executor = new Lazy<IArgumentExecutor>(() => new EnableBlameArgumentExecutor(RunSettingsManager.Instance, new PlatformEnvironment(),new FileHelper()));
}

return this.executor;
Expand Down Expand Up @@ -111,13 +112,19 @@ internal class EnableBlameArgumentExecutor : IArgumentExecutor
/// </summary>
private IEnvironment environment;

/// <summary>
/// For file related operation
/// </summary>
private readonly IFileHelper fileHelper;

#region Constructor

internal EnableBlameArgumentExecutor(IRunSettingsProvider runSettingsManager, IEnvironment environment)
internal EnableBlameArgumentExecutor(IRunSettingsProvider runSettingsManager, IEnvironment environment, IFileHelper fileHelper)
{
this.runSettingsManager = runSettingsManager;
this.environment = environment;
this.Output = ConsoleOutput.Instance;
this.fileHelper = fileHelper;
}

#endregion
Expand Down Expand Up @@ -182,7 +189,7 @@ private void InitializeBlame(bool enableDump, Dictionary<string, string> collect
LoggerUtilities.AddLoggerToRunSettings(BlameFriendlyName, null, this.runSettingsManager);

// Add Blame Data Collector
CollectArgumentExecutor.AddDataCollectorToRunSettings(BlameFriendlyName, this.runSettingsManager);
CollectArgumentExecutor.AddDataCollectorToRunSettings(BlameFriendlyName, this.runSettingsManager, this.fileHelper);


// Add default run settings if required.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors
using Microsoft.VisualStudio.TestPlatform.Common.Utilities;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities;
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;

/// <summary>
/// The argument processor for enabling data collectors.
Expand Down Expand Up @@ -57,7 +59,7 @@ public Lazy<IArgumentExecutor> Executor
{
if (this.executor == null)
{
this.executor = new Lazy<IArgumentExecutor>(() => new EnableCodeCoverageArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance));
this.executor = new Lazy<IArgumentExecutor>(() => new EnableCodeCoverageArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance, new FileHelper()));
}

return this.executor;
Expand Down Expand Up @@ -95,6 +97,7 @@ internal class EnableCodeCoverageArgumentExecutor : IArgumentExecutor

private IRunSettingsProvider runSettingsManager;
private CommandLineOptions commandLineOptions;
private IFileHelper fileHelper;

private const string FriendlyName = "Code Coverage";

Expand Down Expand Up @@ -185,10 +188,11 @@ internal class EnableCodeCoverageArgumentExecutor : IArgumentExecutor

#endregion

internal EnableCodeCoverageArgumentExecutor(CommandLineOptions options, IRunSettingsProvider runSettingsManager)
internal EnableCodeCoverageArgumentExecutor(CommandLineOptions options, IRunSettingsProvider runSettingsManager, IFileHelper fileHelper)
{
this.commandLineOptions = options;
this.runSettingsManager = runSettingsManager;
this.fileHelper = fileHelper;
}

/// <inheritdoc />
Expand Down Expand Up @@ -242,7 +246,7 @@ private void UpdateWithCodeCoverageSettingsIfNotConfigured()
if (ContainsDataCollectorWithFriendlyName(runSettingsNavigator, FriendlyName))
{
// runsettings already has Code coverage data collector, just enable it.
CollectArgumentExecutor.AddDataCollectorToRunSettings(FriendlyName, this.runSettingsManager);
CollectArgumentExecutor.AddDataCollectorToRunSettings(FriendlyName, this.runSettingsManager, this.fileHelper);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ internal class TestAdapterPathArgumentProcessorCapabilities : BaseArgumentProces

public override bool IsAction => false;

public override ArgumentProcessorPriority Priority => ArgumentProcessorPriority.AutoUpdateRunSettings;
public override ArgumentProcessorPriority Priority => ArgumentProcessorPriority.TestAdapterPath;

public override string HelpContentResourceName => CommandLineResources.TestAdapterPathHelp;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace vstest.console.UnitTests.Processors
using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions;
using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions.Interfaces;
using Microsoft.VisualStudio.TestPlatform.Utilities;
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using CommandLineResources = Microsoft.VisualStudio.TestPlatform.CommandLine.Resources.Resources;
Expand Down Expand Up @@ -206,7 +207,7 @@ public void InitializeShouldCreateEntryForBlameAlongWithCollectDumpParametersIfE
internal class TestableEnableBlameArgumentExecutor : EnableBlameArgumentExecutor
{
internal TestableEnableBlameArgumentExecutor(IRunSettingsProvider runSettingsManager, IEnvironment environment, IOutput output)
: base(runSettingsManager, environment)
: base(runSettingsManager, environment, new Mock<IFileHelper>().Object)
{
this.Output = output;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ namespace vstest.console.UnitTests.Processors
using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors;
using Microsoft.VisualStudio.TestPlatform.Common;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities;
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

[TestClass]
public class EnableCodeCoverageArgumentProcessorTests
Expand All @@ -19,7 +21,7 @@ public class EnableCodeCoverageArgumentProcessorTests
public EnableCodeCoverageArgumentProcessorTests()
{
this.settingsProvider = new TestableRunSettingsProvider();
this.executor = new EnableCodeCoverageArgumentExecutor(CommandLineOptions.Instance, this.settingsProvider);
this.executor = new EnableCodeCoverageArgumentExecutor(CommandLineOptions.Instance, this.settingsProvider, new Mock<IFileHelper>().Object);
CollectArgumentExecutor.EnabledDataCollectors.Clear();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void CapabilitiesShouldReturnAppropriateProperties()

Assert.AreEqual(HelpContentPriority.TestAdapterPathArgumentProcessorHelpPriority, capabilities.HelpPriority);
Assert.AreEqual(false, capabilities.IsAction);
Assert.AreEqual(ArgumentProcessorPriority.AutoUpdateRunSettings, capabilities.Priority);
Assert.AreEqual(ArgumentProcessorPriority.TestAdapterPath, capabilities.Priority);

Assert.AreEqual(true, capabilities.AllowMultiple);
Assert.AreEqual(false, capabilities.AlwaysExecute);
Expand Down