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

Changed new configurator method name #2397

Merged
merged 4 commits into from
Apr 14, 2020
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 48 additions & 19 deletions src/Microsoft.TestPlatform.Common/Utilities/FakesUtilities.cs
Expand Up @@ -20,7 +20,9 @@ public static class FakesUtilities
{
private const string ConfiguratorAssemblyQualifiedName = "Microsoft.VisualStudio.TestPlatform.Fakes.FakesDataCollectorConfiguration";

private const string ConfiguratorMethodName = "GetDataCollectorSettingsOrDefault";
private const string NetFrameworkConfiguratorMethodName = "GetDataCollectorSettingsOrDefault";

private const string CrossPlatformConfiguratorMethodName = "GetCrossPlatformDataCollectorSettings";

private const string FakesConfiguratorAssembly = "Microsoft.VisualStudio.TestPlatform.Fakes, Version=16.0.0.0, Culture=neutral";

Expand Down Expand Up @@ -50,17 +52,44 @@ public static string GenerateFakesSettingsForRunConfiguration(string[] sources,
doc.Load(xmlReader);
}

return !TryAddFakesDataCollectorSettings(doc, sources, GetFramework(runSettingsXml))
? runSettingsXml
: doc.OuterXml;
var frameworkVersion = GetFramework(runSettingsXml);
if (frameworkVersion == null)
{
return runSettingsXml;
vritant24 marked this conversation as resolved.
Show resolved Hide resolved
}

return TryAddFakesDataCollectorSettings(doc, sources, (FrameworkVersion)frameworkVersion)
? doc.OuterXml
: runSettingsXml;
}

private static FrameworkVersion GetFramework(string runSettingsXml)
/// <summary>
/// returns FrameworkVersion contained in the runsettingsXML
/// </summary>
/// <param name="runSettingsXml"></param>
/// <returns></returns>
private static FrameworkVersion? GetFramework(string runSettingsXml)
{
var config = XmlRunSettingsUtilities.GetRunConfigurationNode(runSettingsXml);
#pragma warning disable CS0618 // Type or member is obsolete
return config.TargetFrameworkVersion;
#pragma warning restore CS0618 // Type or member is obsolete
// We assume that only .NET Core, .NET Standard, or .NET Framework projects can have fakes.
var targetFramework = XmlRunSettingsUtilities.GetRunConfigurationNode(runSettingsXml)?.TargetFramework;

if (targetFramework == null)
{
return null;
}

// Since there are no FrameworkVersion values for .Net Core 2.0 +, we check TargetFramework instead
// and default to FrameworkCore10 for .Net Core
if (targetFramework.Name.IndexOf("netstandard", StringComparison.OrdinalIgnoreCase) >= 0 ||
targetFramework.Name.IndexOf("netcoreapp", StringComparison.OrdinalIgnoreCase) >= 0)
{
return FrameworkVersion.FrameworkCore10;
}

// Since the Datacollector is separated on the NetFramework/NetCore line, any value of NETFramework
// can be passed along to the fakes data collector configuration creator.
// We default to Framework40to preserve back compat
vritant24 marked this conversation as resolved.
Show resolved Hide resolved
return FrameworkVersion.Framework40;
}

/// <summary>
Expand All @@ -85,11 +114,11 @@ private static FrameworkVersion GetFramework(string runSettingsXml)
// using the CLRIE profiler, and the old involves using the Intellitrace profiler (which isn't supported in
// .NET Core scenarios). The old API still exists for fallback measures.

var newConfigurator = TryGetFakesNewDataCollectorConfigurator();
if (newConfigurator != null)
var crossPlatformConfigurator = TryGetFakesCrossPlatformDataCollectorConfigurator();
if (crossPlatformConfigurator != null)
{
var sourceTFMMap = CreateDictionary(sources, framework);
var fakesSettings = newConfigurator(sourceTFMMap);
var fakesSettings = crossPlatformConfigurator(sourceTFMMap);
// if no fakes, return settings unchanged
if (fakesSettings == null)
{
Expand Down Expand Up @@ -132,14 +161,14 @@ private static FrameworkVersion GetFramework(string runSettingsXml)
return false;
}

Func<IEnumerable<string>, string> oldConfigurator = TryGetFakesDataCollectorConfigurator();
if (oldConfigurator == null)
Func<IEnumerable<string>, string> netFrameworkConfigurator = TryGetNetFrameworkFakesDataCollectorConfigurator();
if (netFrameworkConfigurator == null)
{
return false;
}

// if no fakes, return settings unchanged
var fakesConfiguration = oldConfigurator(sources);
var fakesConfiguration = netFrameworkConfigurator(sources);
if (fakesConfiguration == null)
{
return false;
Expand Down Expand Up @@ -184,14 +213,14 @@ private static void EnsureSettingsNode(XmlDocument settings, TestRunSettings set
}
}

private static Func<IEnumerable<string>, string> TryGetFakesDataCollectorConfigurator()
private static Func<IEnumerable<string>, string> TryGetNetFrameworkFakesDataCollectorConfigurator()
{
#if NET451
try
{
Assembly assembly = Assembly.Load(FakesConfiguratorAssembly);
var type = assembly?.GetType(ConfiguratorAssemblyQualifiedName, false);
var method = type?.GetMethod(ConfiguratorMethodName, new Type[] { typeof(IEnumerable<string>) });
var method = type?.GetMethod(NetFrameworkConfiguratorMethodName, new Type[] { typeof(IEnumerable<string>) });
if (method != null)
{
return (Func<IEnumerable<string>, string>)method.CreateDelegate(typeof(Func<IEnumerable<string>, string>));
Expand All @@ -208,13 +237,13 @@ private static void EnsureSettingsNode(XmlDocument settings, TestRunSettings set
return null;
}

private static Func<IDictionary<string, FrameworkVersion>, DataCollectorSettings> TryGetFakesNewDataCollectorConfigurator()
private static Func<IDictionary<string, FrameworkVersion>, DataCollectorSettings> TryGetFakesCrossPlatformDataCollectorConfigurator()
{
try
{
Assembly assembly = Assembly.Load(FakesConfiguratorAssembly);
var type = assembly?.GetType(ConfiguratorAssemblyQualifiedName, false);
var method = type?.GetMethod(ConfiguratorMethodName, new Type[] { typeof(IDictionary<string, FrameworkVersion>) });
var method = type?.GetMethod(CrossPlatformConfiguratorMethodName, new Type[] { typeof(IDictionary<string, FrameworkVersion>) });
if (method != null)
{
return (Func<IDictionary<string, FrameworkVersion>, DataCollectorSettings>)method.CreateDelegate(typeof(Func<IDictionary<string, FrameworkVersion>, DataCollectorSettings>));
Expand Down