Skip to content

Commit

Permalink
Dynamic corelib.net loading (#2762)
Browse files Browse the repository at this point in the history
* First working version,

* Calling async method,

* Cleaning code,

* Reverting changes,

* Fixing test,

* Copy to output required libs to fix tests

* Next fix

Co-authored-by: Jakub Chocholowicz <jachocho@microsoft.com>
  • Loading branch information
fhnaseer and Jakub Chocholowicz authored Feb 19, 2021
1 parent 436523e commit 34fa5b5
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 15 deletions.
15 changes: 15 additions & 0 deletions scripts/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,14 @@ function Publish-Package
Copy-Loc-Files $eventLogDataCollectorNetFull $coreCLRExtensionsDir "Microsoft.TestPlatform.Extensions.EventLogCollector.resources.dll"
}

# Copy Coverage.CoreLib.Net dlls
$codeCoverageExternalsVersion = ([xml](Get-Content $env:TP_ROOT_DIR\scripts\build\TestPlatform.Dependencies.props)).Project.PropertyGroup.CodeCoverageExternalsVersion
$codeCoverageCoreLibPackagesDir = Join-Path $env:TP_PACKAGES_DIR "microsoft.visualstudio.coverage.corelib.net\$codeCoverageExternalsVersion\lib\$TPB_TargetFrameworkStandard"
Copy-Item $codeCoverageCoreLibPackagesDir\Microsoft.VisualStudio.Coverage.CoreLib.Net.dll $coreCLR20PackageDir -Force
if($TPB_LocalizedBuild) {
Copy-Loc-Files $codeCoverageCoreLibPackagesDir $coreCLR20PackageDir "Microsoft.VisualStudio.Coverage.CoreLib.Net.resources.dll"
}

# If there are some dependencies for the TestHostRuntimeProvider assemblies, those need to be moved too.
$runtimeproviders = @("Microsoft.TestPlatform.TestHostRuntimeProvider.dll", "Microsoft.TestPlatform.TestHostRuntimeProvider.pdb")
foreach($file in $runtimeproviders) {
Expand Down Expand Up @@ -741,6 +749,13 @@ function Create-VsixPackage
if($TPB_LocalizedBuild) {
Copy-Loc-Files $traceDataCollectorPackageDirectory $extensionsPackageDir "Microsoft.VisualStudio.TraceDataCollector.resources.dll"
}

# Copy Microsoft.VisualStudio.CoreLib.Net
$codeCoverageCoreLibPackagesDir = Join-Path $env:TP_PACKAGES_DIR "microsoft.visualstudio.coverage.corelib.net\$codeCoverageExternalsVersion\lib\$TPB_TargetFramework451"
Copy-Item $codeCoverageCoreLibPackagesDir\Microsoft.VisualStudio.Coverage.CoreLib.Net.dll $packageDir -Force
if($TPB_LocalizedBuild) {
Copy-Loc-Files $codeCoverageCoreLibPackagesDir $packageDir "Microsoft.VisualStudio.Coverage.CoreLib.Net.resources.dll"
}

$codeCoverageInterprocessPackageDirectory = Join-Path $env:TP_PACKAGES_DIR "Microsoft.VisualStudio.Coverage.Interprocess\$codeCoverageExternalsVersion\lib\$TPB_TargetFrameworkNS20"
Copy-Item $codeCoverageInterprocessPackageDirectory\Microsoft.VisualStudio.Coverage.Interprocess.dll $extensionsPackageDir -Force
Expand Down
2 changes: 1 addition & 1 deletion scripts/build/TestPlatform.Dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<JsonNetVersion>9.0.1</JsonNetVersion>
<MoqVersion>4.7.63</MoqVersion>
<TestPlatformExternalsVersion>16.9.0-preview-4267359</TestPlatformExternalsVersion>
<CodeCoverageExternalsVersion>16.10.0-beta.21105.1</CodeCoverageExternalsVersion>
<CodeCoverageExternalsVersion>16.10.0-beta.21119.2</CodeCoverageExternalsVersion>
<MicrosoftFakesVersion>16.9.0-beta.20628.1</MicrosoftFakesVersion>

<MicrosoftBuildPackageVersion>16.0.461</MicrosoftBuildPackageVersion>
Expand Down
2 changes: 1 addition & 1 deletion scripts/verify-nupkgs.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function Verify-Nuget-Packages($packageDirectory, $version)
"Microsoft.NET.Test.Sdk" = 27;
"Microsoft.TestPlatform" = 515;
"Microsoft.TestPlatform.Build" = 21;
"Microsoft.TestPlatform.CLI" = 381;
"Microsoft.TestPlatform.CLI" = 366;
"Microsoft.TestPlatform.Extensions.TrxLogger" = 35;
"Microsoft.TestPlatform.ObjectModel" = 180;
"Microsoft.TestPlatform.AdapterUtilities" = 62;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,34 @@ namespace Microsoft.VisualStudio.TestPlatform.Utilities
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

using Microsoft.VisualStudio.Coverage.CoreLib.Net;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions;

public class CodeCoverageDataAttachmentsHandler : IDataCollectorAttachmentProcessor
{
private const string CoverageUri = "datacollector://microsoft/CodeCoverage/2.0";
private const string CoverageFileExtension = ".coverage";
private const string CoverageFriendlyName = "Code Coverage";

private const string CodeCoverageCoreLibNetAssemblyName = "Microsoft.VisualStudio.Coverage.CoreLib.Net";
private const string CoverageFileUtilityTypeName = "CoverageFileUtility";
private const string MergeMethodName = "MergeCoverageFilesAsync";
private const string WriteMethodName = "WriteCoverageFile";

private static readonly Uri CodeCoverageDataCollectorUri = new Uri(CoverageUri);

public bool SupportsIncrementalProcessing => true;

public IEnumerable<Uri> GetExtensionUris()
{
yield return CodeCoverageDataCollectorUri;
}
}

public async Task<ICollection<AttachmentSet>> ProcessAttachmentSetsAsync(ICollection<AttachmentSet> attachments, IProgress<int> progressReporter, IMessageLogger logger, CancellationToken cancellationToken)
{
Expand All @@ -53,7 +59,7 @@ public async Task<ICollection<AttachmentSet>> ProcessAttachmentSetsAsync(ICollec
}
}

if(coverageReportFilePaths.Count > 1)
if (coverageReportFilePaths.Count > 1)
{
var mergedCoverageReportFilePath = await this.MergeCodeCoverageFilesAsync(coverageReportFilePaths, progressReporter, cancellationToken).ConfigureAwait(false);
if (!string.IsNullOrEmpty(mergedCoverageReportFilePath))
Expand Down Expand Up @@ -106,15 +112,25 @@ private async Task<string> MergeCodeCoverageFilesAsync(IList<string> files, IPro

private async Task<string> MergeCodeCoverageFilesAsync(IList<string> files, CancellationToken cancellationToken)
{
var coverageUtility = new CoverageFileUtility();

var coverageData = await coverageUtility.MergeCoverageFilesAsync(
files,
cancellationToken).ConfigureAwait(false);

coverageUtility.WriteCoverageFile(files[0], coverageData);

foreach(var file in files.Skip(1))
cancellationToken.ThrowIfCancellationRequested();

var assemblyPath = Path.Combine(Path.GetDirectoryName(typeof(CodeCoverageDataAttachmentsHandler).GetTypeInfo().Assembly.GetAssemblyLocation()), CodeCoverageCoreLibNetAssemblyName + ".dll");

// Get assembly, type and methods
Assembly assembly = new PlatformAssemblyLoadContext().LoadAssemblyFromPath(assemblyPath);
var classType = assembly.GetType($"{CodeCoverageCoreLibNetAssemblyName}.{CoverageFileUtilityTypeName}");
var classInstance = Activator.CreateInstance(classType);
var mergeMethodInfo = classType?.GetMethod(MergeMethodName, new[] { typeof(IList<string>), typeof(CancellationToken) });
var writeMethodInfo = classType?.GetMethod(WriteMethodName);

// Invoke methods
var task = (Task)mergeMethodInfo.Invoke(classInstance, new object[] { files, cancellationToken });
await task.ConfigureAwait(false);
var coverageData = task.GetType().GetProperty("Result").GetValue(task, null);
writeMethodInfo.Invoke(classInstance, new object[] { files[0], coverageData });

// Delete original files and keep merged file only
foreach (var file in files.Skip(1))
{
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<ItemGroup>
<ProjectReference Include="..\Microsoft.TestPlatform.CoreUtilities\Microsoft.TestPlatform.CoreUtilities.csproj" />
<ProjectReference Include="..\Microsoft.TestPlatform.ObjectModel\Microsoft.TestPlatform.ObjectModel.csproj" />
<PackageReference Include="Microsoft.VisualStudio.Coverage.CoreLib.Net" Version="$(CodeCoverageExternalsVersion)" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net451' ">
<Reference Include="System" />
Expand Down

0 comments on commit 34fa5b5

Please sign in to comment.