Skip to content

Commit

Permalink
Added support for new CC merging library for netstandard2.0 (#2598)
Browse files Browse the repository at this point in the history
* Added support for new CC merging library

Co-authored-by: dotnet-maestro[bot] <42748379+dotnet-maestro[bot]@users.noreply.github.com>
Co-authored-by: dotnet-maestro[bot] <dotnet-maestro[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 20, 2020
1 parent 9af8eef commit 74c64d8
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 106 deletions.
4 changes: 2 additions & 2 deletions scripts/build/TestPlatform.Dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
<NuGetFrameworksVersion>5.0.0</NuGetFrameworksVersion>
<JsonNetVersion>9.0.1</JsonNetVersion>
<MoqVersion>4.7.63</MoqVersion>
<TestPlatformExternalsVersion>16.9.0-preview-4189539</TestPlatformExternalsVersion>
<CodeCoverageExternalsVersion>16.9.0-beta.20563.1</CodeCoverageExternalsVersion>
<TestPlatformExternalsVersion>16.9.0-preview-4243816</TestPlatformExternalsVersion>
<CodeCoverageExternalsVersion>16.9.0-beta.20570.2</CodeCoverageExternalsVersion>
<MicrosoftFakesVersion>16.8.0-beta.20420.2</MicrosoftFakesVersion>

<MicrosoftBuildPackageVersion>16.0.461</MicrosoftBuildPackageVersion>
Expand Down
6 changes: 3 additions & 3 deletions scripts/verify-nupkgs.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ function Verify-Nuget-Packages($packageDirectory, $version)
$expectedNumOfFiles = @{
"Microsoft.CodeCoverage" = 44;
"Microsoft.NET.Test.Sdk" = 18;
"Microsoft.TestPlatform" = 488;
"Microsoft.TestPlatform" = 486;
"Microsoft.TestPlatform.Build" = 19;
"Microsoft.TestPlatform.CLI" = 350;
"Microsoft.TestPlatform.CLI" = 353;
"Microsoft.TestPlatform.Extensions.TrxLogger" = 33;
"Microsoft.TestPlatform.ObjectModel" = 178;
"Microsoft.TestPlatform.Portable" = 566;
"Microsoft.TestPlatform.Portable" = 568;
"Microsoft.TestPlatform.TestHost" = 212;
"Microsoft.TestPlatform.TranslationLayer" = 121}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace Microsoft.VisualStudio.TestPlatform.Utilities
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;
Expand All @@ -22,10 +24,6 @@ public class CodeCoverageDataAttachmentsHandler : IDataCollectorAttachmentProces
private const string CoverageFileExtension = ".coverage";
private const string CoverageFriendlyName = "Code Coverage";

private const string CodeCoverageAnalysisAssemblyName = "Microsoft.VisualStudio.Coverage.Analysis";
private const string MergeMethodName = "MergeCoverageFiles";
private const string CoverageInfoTypeName = "CoverageInfo";

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

public bool SupportsIncrementalProcessing => true;
Expand All @@ -35,93 +33,68 @@ public IEnumerable<Uri> GetExtensionUris()
yield return CodeCoverageDataCollectorUri;
}

public Task<ICollection<AttachmentSet>> ProcessAttachmentSetsAsync(ICollection<AttachmentSet> attachments, IProgress<int> progressReporter, IMessageLogger logger, CancellationToken cancellationToken)
public async Task<ICollection<AttachmentSet>> ProcessAttachmentSetsAsync(ICollection<AttachmentSet> attachments, IProgress<int> progressReporter, IMessageLogger logger, CancellationToken cancellationToken)
{
if (attachments != null && attachments.Any())
{
var codeCoverageFiles = attachments.Select(coverageAttachment => coverageAttachment.Attachments[0].Uri.LocalPath).ToArray();
var outputFile = MergeCodeCoverageFiles(codeCoverageFiles, progressReporter, cancellationToken);
var outputFile = await this.MergeCodeCoverageFilesAsync(codeCoverageFiles, progressReporter, cancellationToken).ConfigureAwait(false);
var attachmentSet = new AttachmentSet(CodeCoverageDataCollectorUri, CoverageFriendlyName);

if (!string.IsNullOrEmpty(outputFile))
{
attachmentSet.Attachments.Add(new UriDataAttachment(new Uri(outputFile), CoverageFriendlyName));
return Task.FromResult((ICollection<AttachmentSet>)new Collection<AttachmentSet> { attachmentSet });
return new Collection<AttachmentSet> { attachmentSet };
}

// In case merging fails(esp in dotnet core we cannot merge), so return filtered list of Code Coverage Attachments
return Task.FromResult(attachments);
return attachments;
}

return Task.FromResult((ICollection<AttachmentSet>)new Collection<AttachmentSet>());
return new Collection<AttachmentSet>();
}

private string MergeCodeCoverageFiles(IList<string> files, IProgress<int> progressReporter, CancellationToken cancellationToken)
private async Task<string> MergeCodeCoverageFilesAsync(IList<string> files, IProgress<int> progressReporter, CancellationToken cancellationToken)
{
if(files.Count == 1)
if (files.Count == 1)
{
return files[0];
}

string tempFileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + CoverageFileExtension);
string outputfileName = files[0];

File.Create(tempFileName).Dispose();
var assemblyPath = Path.Combine(Path.GetDirectoryName(typeof(CodeCoverageDataAttachmentsHandler).GetTypeInfo().Assembly.GetAssemblyLocation()), CodeCoverageAnalysisAssemblyName + ".dll");

try
{
cancellationToken.ThrowIfCancellationRequested();
Assembly assembly = new PlatformAssemblyLoadContext().LoadAssemblyFromPath(assemblyPath);
var type = assembly.GetType(CodeCoverageAnalysisAssemblyName + "." + CoverageInfoTypeName);

var methodInfo = type?.GetMethod(MergeMethodName);

if (methodInfo != null)
{
IList<string> filesToDelete = new List<string>(files.Count) { tempFileName };

for (int i = 1; i < files.Count; i++)
{
cancellationToken.ThrowIfCancellationRequested();
progressReporter?.Report(100 * i / files.Count);

cancellationToken.ThrowIfCancellationRequested();
methodInfo.Invoke(null, new object[] { files[i], outputfileName, tempFileName, true });

cancellationToken.ThrowIfCancellationRequested();
File.Copy(tempFileName, outputfileName, true);

filesToDelete.Add(files[i]);
}

cancellationToken.ThrowIfCancellationRequested();
foreach (string fileName in filesToDelete)
{
File.Delete(fileName);
}
}

// Warning: Don't remove this method call.
//
// We took a dependency on Coverage.CoreLib.Net. In the unlikely case it cannot be
// resolved, this method call will throw an exception that will be caught and
// absorbed here.
await this.MergeCodeCoverageFilesAsync(files, cancellationToken).ConfigureAwait(false);
progressReporter?.Report(100);
return outputfileName;
}
catch (OperationCanceledException)
{
if (EqtTrace.IsWarningEnabled)
{
EqtTrace.Warning("CodeCoverageDataCollectorAttachmentsHandler: operation was cancelled.");
}
// Occurs due to cancellation, ok to re-throw.
throw;
}
catch (Exception ex)
{
if (EqtTrace.IsErrorEnabled)
{
EqtTrace.Error("CodeCoverageDataCollectorAttachmentsHandler: Failed to load datacollector of type : {0} from location : {1}. Error : {2}", CodeCoverageAnalysisAssemblyName, assemblyPath, ex.ToString());
}
EqtTrace.Error(
"CodeCoverageDataCollectorAttachmentsHandler: Failed to load datacollector. Error: {0}",
ex.ToString());
}

return string.Empty;
return files[0];
}

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

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

coverageUtility.WriteCoverageFile(files[0], coverageData);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
<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' ">
<ItemGroup Condition="'$(TargetFramework)' == 'net451' ">
<Reference Include="System" />
<Reference Include="System.Runtime" />
<Reference Include="System.Xml" />
Expand Down
2 changes: 2 additions & 0 deletions src/package/nuspec/Microsoft.TestPlatform.Portable.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<file src="net451\$Runtime$\Microsoft.VisualStudio.TestPlatform.Client.dll" target="tools\net451\Microsoft.VisualStudio.TestPlatform.Client.dll" />
<file src="net451\$Runtime$\Microsoft.VisualStudio.TestPlatform.Common.dll" target="tools\net451\Microsoft.VisualStudio.TestPlatform.Common.dll" />
<file src="net451\$Runtime$\Microsoft.VisualStudio.TestPlatform.ObjectModel.dll" target="tools\net451\Microsoft.VisualStudio.TestPlatform.ObjectModel.dll" />
<file src="net451\$Runtime$\Microsoft.VisualStudio.Coverage.CoreLib.Net.dll" target="tools\net451\Microsoft.VisualStudio.Coverage.CoreLib.Net.dll" />
<file src="net451\$Runtime$\msdia140typelib_clr0200.dll" target="tools\net451\msdia140typelib_clr0200.dll" />
<file src="net451\$Runtime$\Newtonsoft.Json.dll" target="tools\net451\Newtonsoft.Json.dll" />
<file src="net451\$Runtime$\System.Collections.Immutable.dll" target="tools\net451\System.Collections.Immutable.dll" />
Expand Down Expand Up @@ -286,6 +287,7 @@
<file src="netcoreapp2.1\Microsoft.VisualStudio.TestPlatform.Client.dll" target="tools\netcoreapp2.1\Microsoft.VisualStudio.TestPlatform.Client.dll" />
<file src="netcoreapp2.1\Microsoft.VisualStudio.TestPlatform.Common.dll" target="tools\netcoreapp2.1\Microsoft.VisualStudio.TestPlatform.Common.dll" />
<file src="netcoreapp2.1\Microsoft.VisualStudio.TestPlatform.ObjectModel.dll" target="tools\netcoreapp2.1\Microsoft.VisualStudio.TestPlatform.ObjectModel.dll" />
<file src="netcoreapp2.1\Microsoft.VisualStudio.Coverage.CoreLib.Net.dll" target="tools\netcoreapp2.1\Microsoft.VisualStudio.Coverage.CoreLib.Net.dll" />
<file src="netcoreapp2.1\Newtonsoft.Json.dll" target="tools\netcoreapp2.1\Newtonsoft.Json.dll" />
<file src="netcoreapp2.1\package.deps.json" target="tools\netcoreapp2.1\package.deps.json" />
<file src="netcoreapp2.1\vstest.console.deps.json" target="tools\netcoreapp2.1\vstest.console.deps.json" />
Expand Down
4 changes: 1 addition & 3 deletions src/package/nuspec/Microsoft.TestPlatform.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@
<file src="net451\$Runtime$\Microsoft.TestPlatform.VsTestConsole.TranslationLayer.dll" target="tools\net451\Common7\IDE\Extensions\TestPlatform\Microsoft.TestPlatform.VsTestConsole.TranslationLayer.dll" />
<file src="net451\$Runtime$\Microsoft.TestPlatform.VsTestConsole.TranslationLayer.xml" target="tools\net451\Common7\IDE\Extensions\TestPlatform\Microsoft.TestPlatform.VsTestConsole.TranslationLayer.xml" />
<file src="net451\$Runtime$\Microsoft.VisualStudio.ArchitectureTools.PEReader.dll" target="tools\net451\Common7\IDE\Extensions\TestPlatform\Extensions\Microsoft.VisualStudio.ArchitectureTools.PEReader.dll" />
<file src="net451\$Runtime$\Microsoft.VisualStudio.Coverage.Analysis.dll" target="tools\net451\Common7\IDE\Extensions\TestPlatform\Microsoft.VisualStudio.Coverage.Analysis.dll" />
<file src="net451\$Runtime$\Microsoft.VisualStudio.Coverage.Interop.dll" target="tools\net451\Common7\IDE\Extensions\TestPlatform\Microsoft.VisualStudio.Coverage.Interop.dll" />
<file src="net451\$Runtime$\Microsoft.VisualStudio.Coverage.CoreLib.Net.dll" target="tools\net451\Common7\IDE\Extensions\TestPlatform\Extensions\Microsoft.VisualStudio.Coverage.CoreLib.Net.dll" />
<file src="net451\$Runtime$\Microsoft.VisualStudio.Diagnostics.Utilities.dll" target="tools\net451\Common7\IDE\Extensions\TestPlatform\Microsoft.VisualStudio.Diagnostics.Utilities.dll" />
<file src="net451\$Runtime$\Microsoft.VisualStudio.Enterprise.AspNetHelper.dll" target="tools\net451\Common7\IDE\Extensions\TestPlatform\Microsoft.VisualStudio.Enterprise.AspNetHelper.dll" />
<file src="net451\$Runtime$\Microsoft.VisualStudio.QualityTools.AgentObject.dll" target="tools\net451\Common7\IDE\Extensions\TestPlatform\Microsoft.VisualStudio.QualityTools.AgentObject.dll" />
Expand Down Expand Up @@ -268,7 +267,6 @@
<file src="net451\$Runtime$\Extensions\Microsoft.VisualStudio.TestTools.DataCollection.MediaRecorder.Model.dll" target="tools\net451\Common7\IDE\Extensions\TestPlatform\Extensions\Microsoft.VisualStudio.TestTools.DataCollection.MediaRecorder.Model.dll" />
<file src="net451\$Runtime$\Extensions\Microsoft.VisualStudio.TestTools.DataCollection.VideoRecorderCollector.dll" target="tools\net451\Common7\IDE\Extensions\TestPlatform\Extensions\Microsoft.VisualStudio.TestTools.DataCollection.VideoRecorderCollector.dll" />
<file src="net451\$Runtime$\Extensions\Microsoft.VisualStudio.TraceDataCollector.dll" target="tools\net451\Common7\IDE\Extensions\TestPlatform\Extensions\Microsoft.VisualStudio.TraceDataCollector.dll" />
<file src="net451\$Runtime$\Extensions\Microsoft.VisualStudio.Coverage.Interop.dll" target="tools\net451\Common7\IDE\Extensions\TestPlatform\Extensions\Microsoft.VisualStudio.Coverage.Interop.dll" />
<file src="net451\$Runtime$\Extensions\VideoRecorder\VSTestVideoRecorder.exe" target="tools\net451\Common7\IDE\Extensions\TestPlatform\Extensions\VideoRecorder\VSTestVideoRecorder.exe" />
<file src="net451\$Runtime$\Extensions\Cpp\dbghelp.dll" target="tools\net451\Common7\IDE\Extensions\TestPlatform\Extensions\Cpp\dbghelp.dll" />
<file src="net451\$Runtime$\Extensions\Cpp\Microsoft.VisualStudio.TestTools.CppUnitTestFramework.Discoverer.dll" target="tools\net451\Common7\IDE\Extensions\TestPlatform\Extensions\Cpp\Microsoft.VisualStudio.TestTools.CppUnitTestFramework.Discoverer.dll" />
Expand Down
6 changes: 2 additions & 4 deletions src/package/sign/sign.proj
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,11 @@

<!-- DataCollector related dlls -->
<AssembliesToSign Include="$(ArtifactsDirectory)Microsoft.IntelliTrace.Core.dll" />
<AssembliesToSign Include="$(ArtifactsDirectory)Microsoft.VisualStudio.Coverage.Analysis.dll" />
<AssembliesToSign Include="$(ArtifactsDirectory)Microsoft.VisualStudio.Coverage.Interop.dll" />
<AssembliesToSign Include="$(ArtifactsDirectory)Microsoft.VisualStudio.Coverage.CoreLib.Net.dll" />
<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)Extensions\Microsoft.VisualStudio.TraceDataCollector.dll" />
<AssembliesToSign Include="$(ArtifactsDirectory)Extensions\Microsoft.VisualStudio.Coverage.Interop.dll" />

<!-- CUIT related dlls -->
<AssembliesToSign Include="$(ArtifactsDirectory)Microsoft.VisualStudio.Diagnostics.Measurement.dll" />
Expand Down Expand Up @@ -622,4 +620,4 @@
IntermediatesDirectory="$(IntermediatesDirectory)"
Type="$(SignType)" />
</Target>
</Project>
</Project>
Loading

0 comments on commit 74c64d8

Please sign in to comment.