diff --git a/src/Microsoft.TestPlatform.Common/Utilities/FakesUtilities.cs b/src/Microsoft.TestPlatform.Common/Utilities/FakesUtilities.cs index 475b16d956..8c24158902 100644 --- a/src/Microsoft.TestPlatform.Common/Utilities/FakesUtilities.cs +++ b/src/Microsoft.TestPlatform.Common/Utilities/FakesUtilities.cs @@ -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"; @@ -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; + } + + return TryAddFakesDataCollectorSettings(doc, sources, (FrameworkVersion)frameworkVersion) + ? doc.OuterXml + : runSettingsXml; } - private static FrameworkVersion GetFramework(string runSettingsXml) + /// + /// returns FrameworkVersion contained in the runsettingsXML + /// + /// + /// + 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 Framework40 to preserve back compat + return FrameworkVersion.Framework40; } /// @@ -85,11 +114,11 @@ private static bool TryAddFakesDataCollectorSettings( // 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) { @@ -132,14 +161,14 @@ private static bool AddFallbackFakesSettings( return false; } - Func, string> oldConfigurator = TryGetFakesDataCollectorConfigurator(); - if (oldConfigurator == null) + Func, 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; @@ -184,14 +213,14 @@ private static void EnsureSettingsNode(XmlDocument settings, TestRunSettings set } } - private static Func, string> TryGetFakesDataCollectorConfigurator() + private static Func, 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) }); + var method = type?.GetMethod(NetFrameworkConfiguratorMethodName, new Type[] { typeof(IEnumerable) }); if (method != null) { return (Func, string>)method.CreateDelegate(typeof(Func, string>)); @@ -208,13 +237,13 @@ private static Func, string> TryGetFakesDataCollectorConfigu return null; } - private static Func, DataCollectorSettings> TryGetFakesNewDataCollectorConfigurator() + private static Func, DataCollectorSettings> TryGetFakesCrossPlatformDataCollectorConfigurator() { try { Assembly assembly = Assembly.Load(FakesConfiguratorAssembly); var type = assembly?.GetType(ConfiguratorAssemblyQualifiedName, false); - var method = type?.GetMethod(ConfiguratorMethodName, new Type[] { typeof(IDictionary) }); + var method = type?.GetMethod(CrossPlatformConfiguratorMethodName, new Type[] { typeof(IDictionary) }); if (method != null) { return (Func, DataCollectorSettings>)method.CreateDelegate(typeof(Func, DataCollectorSettings>));