Skip to content

Commit

Permalink
Added method to look for new api in fakes datacollector (#2339)
Browse files Browse the repository at this point in the history
* Added method to look for new api in fakes datacollector

* added check for .net runtime version

* refactored code

* enable new configurator for net core

* refactored to remove use of out param

* Removed Microsoft.VisualStudio.TestPlatform.Fakes from the "Microsoft.Internal.TestPlatform.Extensions" nuget pacakge. This would mean that the TP vsix would not drop this dependency any more. The Fakes vsix that builds in the VSUT repo would be dropping it instead.
For the TestPlatform tooling nuget however which ships with the Fakes dependency, I'm pulling in the latest from the vsunittesting myget feed and packaging that up.

* Fixing method resolution.

* updated api call

* fixed pr comments

* Fixing build - this doesn't need to be signed.

* removed obsolete

* Looks like the signed build has a different way of producing nuget packages from the dev build.

* Added method to look for new api in fakes datacollector

* added check for .net runtime version

* refactored code

* enable new configurator for net core

* refactored to remove use of out param

* updated api call

* fixed pr comments

* Removed Microsoft.VisualStudio.TestPlatform.Fakes from the "Microsoft.Internal.TestPlatform.Extensions" nuget pacakge. This would mean that the TP vsix would not drop this dependency any more. The Fakes vsix that builds in the VSUT repo would be dropping it instead.
For the TestPlatform tooling nuget however which ships with the Fakes dependency, I'm pulling in the latest from the vsunittesting myget feed and packaging that up.

* Fixing method resolution.

* Fixing build - this doesn't need to be signed.

* removed obsolete

* removed system.linq reference:

* fixed rebase errors

* Fixing script vars

* Write value to the correct pipeline variable

Co-authored-by: Abhitej John Bandi <aajohn@microsoft.com>
Co-authored-by: nohwnd <me@jakubjares.com>
  • Loading branch information
3 people authored Apr 1, 2020
1 parent d10bcbb commit 97a3802
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 36 deletions.
1 change: 1 addition & 0 deletions NuGet.config
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<add key="xunit.myget" value="https://www.myget.org/F/xunit/api/v3/index.json" />
<add key="mstest.myget" value="https://dotnet.myget.org/F/mstestv2/api/v3/index.json"/>
<add key="pdb2pdb.myget" value="https://dotnet.myget.org/F/symreader-converter/api/v3/index.json" />
<add key="vsunittesting.myget" value="https://dotnet.myget.org/F/vsunittesting/api/v3/index.json"/>
</packageSources>
<fallbackPackageFolders>
<clear />
Expand Down
8 changes: 6 additions & 2 deletions scripts/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ function Create-VsixPackage
$testPlatformExternalsVersion = ([xml](Get-Content $env:TP_ROOT_DIR\scripts\build\TestPlatform.Dependencies.props)).Project.PropertyGroup.TestPlatformExternalsVersion

# Copy legacy dependencies
$legacyDir = Join-Path $env:TP_PACKAGES_DIR "Microsoft.Internal.TestPlatform.Extensions\$testPlatformExternalsVersion-patched4\contentFiles\any\any"
$legacyDir = Join-Path $env:TP_PACKAGES_DIR "Microsoft.Internal.TestPlatform.Extensions\$testPlatformExternalsVersion-patched5\contentFiles\any\any"
Copy-Item -Recurse $legacyDir\* $packageDir -Force

# Copy Microsoft.VisualStudio.ArchitectureTools.PEReader to Extensions
Expand Down Expand Up @@ -687,6 +687,10 @@ function Create-NugetPackages
# Pass Newtonsoft.Json version to nuget pack to keep the version consistent across all nuget packages.
$JsonNetVersion = ([xml](Get-Content $env:TP_ROOT_DIR\scripts\build\TestPlatform.Dependencies.props)).Project.PropertyGroup.JsonNetVersion

# Additional external dependency folders
$microsoftFakesVersion = ([xml](Get-Content $env:TP_ROOT_DIR\scripts\build\TestPlatform.Dependencies.props)).Project.PropertyGroup.MicrosoftFakesVersion
$FakesPackageDir = Join-Path $env:TP_PACKAGES_DIR "Microsoft.VisualStudio.TestPlatform.Fakes\$microsoftFakesVersion\lib"

# package them from stagingDir
foreach ($file in $nuspecFiles) {
$additionalArgs = ""
Expand All @@ -695,7 +699,7 @@ function Create-NugetPackages
}

Write-Verbose "$nugetExe pack $stagingDir\$file -OutputDirectory $packageOutputDir -Version $TPB_Version -Properties Version=$TPB_Version $additionalArgs"
& $nugetExe pack $stagingDir\$file -OutputDirectory $packageOutputDir -Version $TPB_Version -Properties Version=$TPB_Version`;JsonNetVersion=$JsonNetVersion`;Runtime=$TPB_TargetRuntime`;NetCoreTargetFramework=$TPB_TargetFrameworkCore20 $additionalArgs
& $nugetExe pack $stagingDir\$file -OutputDirectory $packageOutputDir -Version $TPB_Version -Properties Version=$TPB_Version`;JsonNetVersion=$JsonNetVersion`;Runtime=$TPB_TargetRuntime`;NetCoreTargetFramework=$TPB_TargetFrameworkCore20`;FakesPackageDir=$FakesPackageDir $additionalArgs

Set-ScriptFailedOnError
}
Expand Down
1 change: 1 addition & 0 deletions scripts/build/TestPlatform.Dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<JsonNetVersion>9.0.1</JsonNetVersion>
<MoqVersion>4.7.63</MoqVersion>
<TestPlatformExternalsVersion>16.0.0-preview-2148743</TestPlatformExternalsVersion>
<MicrosoftFakesVersion>16.6.3-beta.20169.2</MicrosoftFakesVersion>

<MicrosoftBuildPackageVersion>16.0.461</MicrosoftBuildPackageVersion>
<MicrosoftBuildFrameworkPackageVersion>$(MicrosoftBuildPackageVersion)</MicrosoftBuildFrameworkPackageVersion>
Expand Down
6 changes: 5 additions & 1 deletion scripts/vsts-prebuild.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ Write-Host "##vso[task.setvariable variable=PackageVersion;]$packageVersion"
# "Nuget.exe pack" required JsonNetVersion property for creating nuget package.

$JsonNetVersion = ([xml](Get-Content $TP_ROOT_DIR\scripts\build\TestPlatform.Dependencies.props)).Project.PropertyGroup.JsonNetVersion
Write-Host "##vso[task.setvariable variable=JsonNetVersion;]$JsonNetVersion"
Write-Host "##vso[task.setvariable variable=JsonNetVersion;]$JsonNetVersion"

$microsoftFakesVersion = ([xml](Get-Content $TP_ROOT_DIR\scripts\build\TestPlatform.Dependencies.props)).Project.PropertyGroup.MicrosoftFakesVersion
$FakesPackageDir = Join-Path $TP_ROOT_DIR "packages\Microsoft.VisualStudio.TestPlatform.Fakes\$microsoftFakesVersion\lib"
Write-Host "##vso[task.setvariable variable=FakesPackageDir;]$FakesPackageDir"
117 changes: 87 additions & 30 deletions src/Microsoft.TestPlatform.Common/Utilities/FakesUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ namespace Microsoft.VisualStudio.TestPlatform.Common.Utilities
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
#if NET451
using System.Reflection;
#endif
using System.Xml;

using Microsoft.VisualStudio.TestPlatform.ObjectModel;
Expand Down Expand Up @@ -44,12 +42,6 @@ public static string GenerateFakesSettingsForRunConfiguration(string[] sources,
throw new ArgumentNullException(nameof(runSettingsXml));
}

// do not generate fakes for netcore
if (IsNetCoreFramework(runSettingsXml))
{
return runSettingsXml;
}

var doc = new XmlDocument();
using (var xmlReader = XmlReader.Create(
new StringReader(runSettingsXml),
Expand All @@ -58,15 +50,17 @@ public static string GenerateFakesSettingsForRunConfiguration(string[] sources,
doc.Load(xmlReader);
}

return !TryAddFakesDataCollectorSettings(doc, sources) ? runSettingsXml : doc.OuterXml;
return !TryAddFakesDataCollectorSettings(doc, sources, GetFramework(runSettingsXml))
? runSettingsXml
: doc.OuterXml;
}

private static bool IsNetCoreFramework(string runSettingsXml)
private static FrameworkVersion GetFramework(string runSettingsXml)
{
var config = XmlRunSettingsUtilities.GetRunConfigurationNode(runSettingsXml);

return config.TargetFramework.Name.IndexOf("netstandard", StringComparison.OrdinalIgnoreCase) >= 0
|| config.TargetFramework.Name.IndexOf("netcoreapp", StringComparison.OrdinalIgnoreCase) >= 0;
#pragma warning disable CS0618 // Type or member is obsolete
return config.TargetFrameworkVersion;
#pragma warning restore CS0618 // Type or member is obsolete
}

/// <summary>
Expand All @@ -77,24 +71,69 @@ private static bool IsNetCoreFramework(string runSettingsXml)
/// <returns>true if runSettings was modified; false otherwise.</returns>
private static bool TryAddFakesDataCollectorSettings(
XmlDocument runSettings,
IEnumerable<string> sources)
IEnumerable<string> sources,
FrameworkVersion framework)
{
// If user provided fakes settings don't do anything
if (XmlRunSettingsUtilities.ContainsDataCollector(runSettings.CreateNavigator(), FakesMetadata.DataCollectorUri))
{
return false;
}

Func<IEnumerable<string>, string> configurator;
// A new Fakes Congigurator API makes the decision to add the right datacollector uri to the configuration
// There now exist two data collector URIs to support two different scenarios. The new scenario involves
// 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.

// fakes supported?
if (!TryGetFakesDataCollectorConfigurator(out configurator))
var newConfigurator = TryGetFakesNewDataCollectorConfigurator();
if (newConfigurator != null)
{
var sourceTFMMap = CreateDictionary(sources, framework);
var fakesSettings = newConfigurator(sourceTFMMap);
XmlRunSettingsUtilities.InsertDataCollectorsNode(runSettings.CreateNavigator(), fakesSettings);
return true;
}

return AddFallbackFakesSettings(runSettings, sources, framework);
}

private static IDictionary<string, FrameworkVersion> CreateDictionary(IEnumerable<string> sources, FrameworkVersion framework)
{
var dict = new Dictionary<string, FrameworkVersion>();
foreach(var source in sources)
{
if (!dict.ContainsKey(source))
{
dict.Add(source, framework);
}
}

return dict;
}

private static bool AddFallbackFakesSettings(
XmlDocument runSettings,
IEnumerable<string> sources,
FrameworkVersion framework)
{

// The fallback settings is for the old implementation of fakes
// that only supports .Net Framework versions
if (framework != FrameworkVersion.Framework35 &&
framework != FrameworkVersion.Framework40 &&
framework != FrameworkVersion.Framework45)
{
return false;
}

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

// if no fakes, return settings unchanged
var fakesConfiguration = configurator(sources);
var fakesConfiguration = oldConfigurator(sources);
if (fakesConfiguration == null)
{
return false;
Expand All @@ -116,6 +155,7 @@ private static bool TryAddFakesDataCollectorSettings(

fakesSettings.Configuration = doc.DocumentElement;
XmlRunSettingsUtilities.InsertDataCollectorsNode(runSettings.CreateNavigator(), fakesSettings);

return true;
}

Expand All @@ -138,22 +178,17 @@ private static void EnsureSettingsNode(XmlDocument settings, TestRunSettings set
}
}

private static bool TryGetFakesDataCollectorConfigurator(out Func<IEnumerable<string>, string> configurator)
private static Func<IEnumerable<string>, string> TryGetFakesDataCollectorConfigurator()
{
#if NET451
try
{
Assembly assembly = Assembly.Load(FakesConfiguratorAssembly);

var type = assembly?.GetType(ConfiguratorAssemblyQualifiedName, false);
if (type != null)
var method = type?.GetMethod(ConfiguratorMethodName, new Type[] { typeof(IEnumerable<string>) });
if (method != null)
{
var method = type.GetMethod(ConfiguratorMethodName, BindingFlags.Public | BindingFlags.Static);
if (method != null)
{
configurator = (Func<IEnumerable<string>, string>)method.CreateDelegate(typeof(Func<IEnumerable<string>, string>));
return true;
}
return (Func<IEnumerable<string>, string>)method.CreateDelegate(typeof(Func<IEnumerable<string>, string>));
}
}
catch (Exception ex)
Expand All @@ -164,8 +199,30 @@ private static bool TryGetFakesDataCollectorConfigurator(out Func<IEnumerable<st
}
}
#endif
configurator = null;
return false;
return null;
}

private static Func<IDictionary<string, FrameworkVersion>, DataCollectorSettings> TryGetFakesNewDataCollectorConfigurator()
{
try
{
Assembly assembly = Assembly.Load(FakesConfiguratorAssembly);
var type = assembly?.GetType(ConfiguratorAssemblyQualifiedName, false);
var method = type?.GetMethod(ConfiguratorMethodName, new Type[] { typeof(IEnumerable<string>), typeof(FrameworkVersion) });
if (method != null)
{
return (Func<IDictionary<string, FrameworkVersion>, DataCollectorSettings>)method.CreateDelegate(typeof(Func<IDictionary<string, FrameworkVersion>, DataCollectorSettings>));
}
}
catch (Exception ex)
{
if (EqtTrace.IsInfoEnabled)
{
EqtTrace.Info("Failed to create newly implemented Fakes Configurator. Reason:{0} ", ex);
}
}

return null;
}

/// <summary>
Expand Down Expand Up @@ -205,4 +262,4 @@ internal static class FakesMetadata
public const string DataCollectorAssemblyQualifiedName = "Microsoft.VisualStudio.TraceCollector.UnitTestIsolationDataCollector, Microsoft.VisualStudio.TraceCollector, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
}
}
}
}
6 changes: 5 additions & 1 deletion src/package/external/external.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Internal.TestPlatform.Extensions">
<Version>$(TestPlatformExternalsVersion)-patched4</Version>
<Version>$(TestPlatformExternalsVersion)-patched5</Version>
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.QualityTools">
Expand Down Expand Up @@ -81,6 +81,10 @@
<Version>15.6.815-master284DF69C</Version>
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.TestPlatform.Fakes">
<Version>$(MicrosoftFakesVersion)</Version>
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<!-- Required for resolution of desktop dependencies in non windows environment.
We've to set to net46 since the net451 and other packages have assemblies in wrong case.
E.g. System.XML instead of System.Xml. -->
Expand Down
4 changes: 3 additions & 1 deletion src/package/nuspec/Microsoft.TestPlatform.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@
<file src="net451\$Runtime$\Microsoft.VisualStudio.QualityTools.WebTestFramework.dll" target="tools\net451\Common7\IDE\Extensions\TestPlatform\Microsoft.VisualStudio.QualityTools.WebTestFramework.dll" />
<file src="net451\$Runtime$\Microsoft.VisualStudio.TestPlatform.Client.dll" target="tools\net451\Common7\IDE\Extensions\TestPlatform\Microsoft.VisualStudio.TestPlatform.Client.dll" />
<file src="net451\$Runtime$\Microsoft.VisualStudio.TestPlatform.Common.dll" target="tools\net451\Common7\IDE\Extensions\TestPlatform\Microsoft.VisualStudio.TestPlatform.Common.dll" />
<file src="net451\$Runtime$\Microsoft.VisualStudio.TestPlatform.Fakes.dll" target="tools\net451\Common7\IDE\Extensions\TestPlatform\Microsoft.VisualStudio.TestPlatform.Fakes.dll" />
<file src="net451\$Runtime$\Microsoft.VisualStudio.TestPlatform.ObjectModel.dll" target="tools\net451\Common7\IDE\Extensions\TestPlatform\Microsoft.VisualStudio.TestPlatform.ObjectModel.dll" />
<file src="net451\$Runtime$\Microsoft.VisualStudio.TestTools.UITest.Extension.IE.Communication.sxs.manifest" target="tools\net451\Common7\IDE\Extensions\TestPlatform\Microsoft.VisualStudio.TestTools.UITest.Extension.IE.Communication.sxs.manifest" />
<file src="net451\$Runtime$\Microsoft.VisualStudio.TestTools.UITest.Playback.Engine.sxs.manifest" target="tools\net451\Common7\IDE\Extensions\TestPlatform\Microsoft.VisualStudio.TestTools.UITest.Playback.Engine.sxs.manifest" />
Expand Down Expand Up @@ -456,5 +455,8 @@
<file src="net451\$Runtime$\CUITPlugins\Microsoft.VisualStudio.TestTools.UITest.Extension.Uia.dll" target="tools\net451\Common7\IDE\Extensions\TestPlatform\CUITPlugins\Microsoft.VisualStudio.TestTools.UITest.Extension.Uia.dll" />
<file src="net451\$Runtime$\CUITPlugins\Microsoft.VisualStudio.TestTools.UITest.Extension.UiaWidget.dll" target="tools\net451\Common7\IDE\Extensions\TestPlatform\CUITPlugins\Microsoft.VisualStudio.TestTools.UITest.Extension.UiaWidget.dll" />
<file src="net451\$Runtime$\CUITPlugins\Microsoft.VisualStudio.TestTools.UITest.Extension.UiaWidget.UIAHtmlElementUtilities.dll" target="tools\net451\Common7\IDE\Extensions\TestPlatform\CUITPlugins\Microsoft.VisualStudio.TestTools.UITest.Extension.UiaWidget.UIAHtmlElementUtilities.dll" />

<!-- External dependencies-->
<file src="$FakesPackageDir$\net451\Microsoft.VisualStudio.TestPlatform.Fakes.dll" target="tools\net451\Common7\IDE\Extensions\TestPlatform\Microsoft.VisualStudio.TestPlatform.Fakes.dll" />
</files>
</package>
1 change: 0 additions & 1 deletion src/package/sign/sign.proj
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@
<AssembliesToSign Include="$(ArtifactsDirectory)Microsoft.VisualStudio.Diagnostics.Utilities.dll" />
<AssembliesToSign Include="$(ArtifactsDirectory)Microsoft.VisualStudio.Enterprise.AspNetHelper.dll" />
<AssembliesToSign Include="$(ArtifactsDirectory)Microsoft.VisualStudio.ArchitectureTools.PEReader.dll" />
<AssembliesToSign Include="$(ArtifactsDirectory)Microsoft.VisualStudio.TestPlatform.Fakes.dll" />
<AssembliesToSign Include="$(ArtifactsDirectory)Extensions\Microsoft.VisualStudio.TraceDataCollector.dll" />
<AssembliesToSign Include="$(ArtifactsDirectory)Extensions\Microsoft.VisualStudio.Coverage.Interop.dll" />

Expand Down

0 comments on commit 97a3802

Please sign in to comment.