Skip to content

Commit 34fa5b5

Browse files
fhnaseerJakub Chocholowicz
andauthored
Dynamic corelib.net loading (#2762)
* 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 <[email protected]>
1 parent 436523e commit 34fa5b5

File tree

5 files changed

+45
-15
lines changed

5 files changed

+45
-15
lines changed

scripts/build.ps1

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,14 @@ function Publish-Package
578578
Copy-Loc-Files $eventLogDataCollectorNetFull $coreCLRExtensionsDir "Microsoft.TestPlatform.Extensions.EventLogCollector.resources.dll"
579579
}
580580

581+
# Copy Coverage.CoreLib.Net dlls
582+
$codeCoverageExternalsVersion = ([xml](Get-Content $env:TP_ROOT_DIR\scripts\build\TestPlatform.Dependencies.props)).Project.PropertyGroup.CodeCoverageExternalsVersion
583+
$codeCoverageCoreLibPackagesDir = Join-Path $env:TP_PACKAGES_DIR "microsoft.visualstudio.coverage.corelib.net\$codeCoverageExternalsVersion\lib\$TPB_TargetFrameworkStandard"
584+
Copy-Item $codeCoverageCoreLibPackagesDir\Microsoft.VisualStudio.Coverage.CoreLib.Net.dll $coreCLR20PackageDir -Force
585+
if($TPB_LocalizedBuild) {
586+
Copy-Loc-Files $codeCoverageCoreLibPackagesDir $coreCLR20PackageDir "Microsoft.VisualStudio.Coverage.CoreLib.Net.resources.dll"
587+
}
588+
581589
# If there are some dependencies for the TestHostRuntimeProvider assemblies, those need to be moved too.
582590
$runtimeproviders = @("Microsoft.TestPlatform.TestHostRuntimeProvider.dll", "Microsoft.TestPlatform.TestHostRuntimeProvider.pdb")
583591
foreach($file in $runtimeproviders) {
@@ -741,6 +749,13 @@ function Create-VsixPackage
741749
if($TPB_LocalizedBuild) {
742750
Copy-Loc-Files $traceDataCollectorPackageDirectory $extensionsPackageDir "Microsoft.VisualStudio.TraceDataCollector.resources.dll"
743751
}
752+
753+
# Copy Microsoft.VisualStudio.CoreLib.Net
754+
$codeCoverageCoreLibPackagesDir = Join-Path $env:TP_PACKAGES_DIR "microsoft.visualstudio.coverage.corelib.net\$codeCoverageExternalsVersion\lib\$TPB_TargetFramework451"
755+
Copy-Item $codeCoverageCoreLibPackagesDir\Microsoft.VisualStudio.Coverage.CoreLib.Net.dll $packageDir -Force
756+
if($TPB_LocalizedBuild) {
757+
Copy-Loc-Files $codeCoverageCoreLibPackagesDir $packageDir "Microsoft.VisualStudio.Coverage.CoreLib.Net.resources.dll"
758+
}
744759

745760
$codeCoverageInterprocessPackageDirectory = Join-Path $env:TP_PACKAGES_DIR "Microsoft.VisualStudio.Coverage.Interprocess\$codeCoverageExternalsVersion\lib\$TPB_TargetFrameworkNS20"
746761
Copy-Item $codeCoverageInterprocessPackageDirectory\Microsoft.VisualStudio.Coverage.Interprocess.dll $extensionsPackageDir -Force

scripts/build/TestPlatform.Dependencies.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<JsonNetVersion>9.0.1</JsonNetVersion>
3434
<MoqVersion>4.7.63</MoqVersion>
3535
<TestPlatformExternalsVersion>16.9.0-preview-4267359</TestPlatformExternalsVersion>
36-
<CodeCoverageExternalsVersion>16.10.0-beta.21105.1</CodeCoverageExternalsVersion>
36+
<CodeCoverageExternalsVersion>16.10.0-beta.21119.2</CodeCoverageExternalsVersion>
3737
<MicrosoftFakesVersion>16.9.0-beta.20628.1</MicrosoftFakesVersion>
3838

3939
<MicrosoftBuildPackageVersion>16.0.461</MicrosoftBuildPackageVersion>

scripts/verify-nupkgs.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function Verify-Nuget-Packages($packageDirectory, $version)
1616
"Microsoft.NET.Test.Sdk" = 27;
1717
"Microsoft.TestPlatform" = 515;
1818
"Microsoft.TestPlatform.Build" = 21;
19-
"Microsoft.TestPlatform.CLI" = 381;
19+
"Microsoft.TestPlatform.CLI" = 366;
2020
"Microsoft.TestPlatform.Extensions.TrxLogger" = 35;
2121
"Microsoft.TestPlatform.ObjectModel" = 180;
2222
"Microsoft.TestPlatform.AdapterUtilities" = 62;

src/Microsoft.TestPlatform.Utilities/CodeCoverageDataAttachmentsHandler.cs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,34 @@ namespace Microsoft.VisualStudio.TestPlatform.Utilities
88
using System.Collections.ObjectModel;
99
using System.IO;
1010
using System.Linq;
11+
using System.Reflection;
1112
using System.Threading;
1213
using System.Threading.Tasks;
1314

14-
using Microsoft.VisualStudio.Coverage.CoreLib.Net;
1515
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
1616
using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;
1717
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
18+
using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions;
1819

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

26+
private const string CodeCoverageCoreLibNetAssemblyName = "Microsoft.VisualStudio.Coverage.CoreLib.Net";
27+
private const string CoverageFileUtilityTypeName = "CoverageFileUtility";
28+
private const string MergeMethodName = "MergeCoverageFilesAsync";
29+
private const string WriteMethodName = "WriteCoverageFile";
30+
2531
private static readonly Uri CodeCoverageDataCollectorUri = new Uri(CoverageUri);
2632

2733
public bool SupportsIncrementalProcessing => true;
2834

2935
public IEnumerable<Uri> GetExtensionUris()
3036
{
3137
yield return CodeCoverageDataCollectorUri;
32-
}
38+
}
3339

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

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

107113
private async Task<string> MergeCodeCoverageFilesAsync(IList<string> files, CancellationToken cancellationToken)
108114
{
109-
var coverageUtility = new CoverageFileUtility();
110-
111-
var coverageData = await coverageUtility.MergeCoverageFilesAsync(
112-
files,
113-
cancellationToken).ConfigureAwait(false);
114-
115-
coverageUtility.WriteCoverageFile(files[0], coverageData);
116-
117-
foreach(var file in files.Skip(1))
115+
cancellationToken.ThrowIfCancellationRequested();
116+
117+
var assemblyPath = Path.Combine(Path.GetDirectoryName(typeof(CodeCoverageDataAttachmentsHandler).GetTypeInfo().Assembly.GetAssemblyLocation()), CodeCoverageCoreLibNetAssemblyName + ".dll");
118+
119+
// Get assembly, type and methods
120+
Assembly assembly = new PlatformAssemblyLoadContext().LoadAssemblyFromPath(assemblyPath);
121+
var classType = assembly.GetType($"{CodeCoverageCoreLibNetAssemblyName}.{CoverageFileUtilityTypeName}");
122+
var classInstance = Activator.CreateInstance(classType);
123+
var mergeMethodInfo = classType?.GetMethod(MergeMethodName, new[] { typeof(IList<string>), typeof(CancellationToken) });
124+
var writeMethodInfo = classType?.GetMethod(WriteMethodName);
125+
126+
// Invoke methods
127+
var task = (Task)mergeMethodInfo.Invoke(classInstance, new object[] { files, cancellationToken });
128+
await task.ConfigureAwait(false);
129+
var coverageData = task.GetType().GetProperty("Result").GetValue(task, null);
130+
writeMethodInfo.Invoke(classInstance, new object[] { files[0], coverageData });
131+
132+
// Delete original files and keep merged file only
133+
foreach (var file in files.Skip(1))
118134
{
119135
try
120136
{

src/Microsoft.TestPlatform.Utilities/Microsoft.TestPlatform.Utilities.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
<ItemGroup>
1616
<ProjectReference Include="..\Microsoft.TestPlatform.CoreUtilities\Microsoft.TestPlatform.CoreUtilities.csproj" />
1717
<ProjectReference Include="..\Microsoft.TestPlatform.ObjectModel\Microsoft.TestPlatform.ObjectModel.csproj" />
18-
<PackageReference Include="Microsoft.VisualStudio.Coverage.CoreLib.Net" Version="$(CodeCoverageExternalsVersion)" />
1918
</ItemGroup>
2019
<ItemGroup Condition="'$(TargetFramework)' == 'net451' ">
2120
<Reference Include="System" />

0 commit comments

Comments
 (0)