Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Implement a VSTest data collector to write coverage data to disk #329

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions coverlet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "coverlet.core.performancete
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "coverlet.template", "src\coverlet.template\coverlet.template.csproj", "{FF16BD00-4BE7-41F3-95AE-F69B56E5E254}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "coverlet.datacollector", "src\coverlet.datacollector\coverlet.datacollector.csproj", "{A207B2FD-9533-4F89-BCB9-86512E8743CB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -115,6 +117,18 @@ Global
{FF16BD00-4BE7-41F3-95AE-F69B56E5E254}.Release|x64.Build.0 = Release|Any CPU
{FF16BD00-4BE7-41F3-95AE-F69B56E5E254}.Release|x86.ActiveCfg = Release|Any CPU
{FF16BD00-4BE7-41F3-95AE-F69B56E5E254}.Release|x86.Build.0 = Release|Any CPU
{A207B2FD-9533-4F89-BCB9-86512E8743CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A207B2FD-9533-4F89-BCB9-86512E8743CB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A207B2FD-9533-4F89-BCB9-86512E8743CB}.Debug|x64.ActiveCfg = Debug|Any CPU
{A207B2FD-9533-4F89-BCB9-86512E8743CB}.Debug|x64.Build.0 = Debug|Any CPU
{A207B2FD-9533-4F89-BCB9-86512E8743CB}.Debug|x86.ActiveCfg = Debug|Any CPU
{A207B2FD-9533-4F89-BCB9-86512E8743CB}.Debug|x86.Build.0 = Debug|Any CPU
{A207B2FD-9533-4F89-BCB9-86512E8743CB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A207B2FD-9533-4F89-BCB9-86512E8743CB}.Release|Any CPU.Build.0 = Release|Any CPU
{A207B2FD-9533-4F89-BCB9-86512E8743CB}.Release|x64.ActiveCfg = Release|Any CPU
{A207B2FD-9533-4F89-BCB9-86512E8743CB}.Release|x64.Build.0 = Release|Any CPU
{A207B2FD-9533-4F89-BCB9-86512E8743CB}.Release|x86.ActiveCfg = Release|Any CPU
{A207B2FD-9533-4F89-BCB9-86512E8743CB}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -127,6 +141,7 @@ Global
{AE117FAA-C21D-4F23-917E-0C8050614750} = {2FEBDE1B-83E3-445B-B9F8-5644B0E0E134}
{C68CF6DE-F86C-4BCF-BAB9-7A60C320E1F9} = {2FEBDE1B-83E3-445B-B9F8-5644B0E0E134}
{FF16BD00-4BE7-41F3-95AE-F69B56E5E254} = {E877EBA4-E78B-4F7D-A2D3-1E070FED04CD}
{A207B2FD-9533-4F89-BCB9-86512E8743CB} = {E877EBA4-E78B-4F7D-A2D3-1E070FED04CD}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9CA57C02-97B0-4C38-A027-EA61E8741F10}
Expand Down
73 changes: 73 additions & 0 deletions src/coverlet.datacollector/CoverletDataCollector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using Coverlet.Core.Instrumentation;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollector.InProcDataCollector;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.InProcDataCollector;
using System;
using System.Reflection;

namespace Coverlet.DataCollector
{
public class CoverletDataCollector : InProcDataCollection
{
public void Initialize(IDataCollectionSink dataCollectionSink)
{
}

public void TestCaseEnd(TestCaseEndArgs testCaseEndArgs)
{
}

public void TestCaseStart(TestCaseStartArgs testCaseStartArgs)
{
}

public void TestSessionEnd(TestSessionEndArgs testSessionEndArgs)
{
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
Type injectedInstrumentationClass = GetInstrumentationClass(assembly);
if (injectedInstrumentationClass is null)
continue;

var unloadModule = injectedInstrumentationClass.GetMethod(nameof(ModuleTrackerTemplate.UnloadModule), new[] { typeof(object), typeof(EventArgs) });
if (unloadModule is null)
continue;
Copy link
Collaborator

@MarcoRossignoli MarcoRossignoli Jan 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this possible?Maybe this is "in process" code...binding flag etc...


try
{
unloadModule.Invoke(null, new[] { null, EventArgs.Empty });
}
catch
{
// Ignore exceptions and continue with the unload
}
}
}

public void TestSessionStart(TestSessionStartArgs testSessionStartArgs)
{
}

private static Type GetInstrumentationClass(Assembly assembly)
{
try
{
foreach (var type in assembly.GetTypes())
{
if (type.Namespace == "Coverlet.Core.Instrumentation.Tracker"
&& type.Name.StartsWith(assembly.GetName().Name + "_"))
{
return type;
}
}

return null;
}
catch
{
// Avoid crashing if reflection fails
return null;
}
}
}
}
1 change: 1 addition & 0 deletions src/coverlet.datacollector/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[assembly: System.Reflection.AssemblyKeyFileAttribute("coverlet.datacollector.snk")]
28 changes: 28 additions & 0 deletions src/coverlet.datacollector/coverlet.datacollector.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<AssemblyVersion>1.4.1</AssemblyVersion>
<Authors>tonerdo</Authors>
<PackageId>$(AssemblyTitle)</PackageId>
<Title>$(AssemblyTitle)</Title>
<Description>Coverlet is a cross platform code coverage tool for .NET Core, with support for line, branch and method coverage.</Description>
<PackageVersion>$(AssemblyVersion)</PackageVersion>
<PackageTags>coverage;testing;unit-test;lcov;opencover;quality</PackageTags>
<PackageIconUrl>https://raw.githubusercontent.com/tonerdo/coverlet/master/_assets/coverlet-icon.svg?sanitize=true</PackageIconUrl>
<PackageProjectUrl>https://github.com/tonerdo/coverlet</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/tonerdo/coverlet/blob/master/LICENSE</PackageLicenseUrl>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/tonerdo/coverlet</RepositoryUrl>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.TestPlatform.ObjectModel" Version="15.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\coverlet.core\coverlet.core.csproj" PrivateAssets="all" />
</ItemGroup>

</Project>
Binary file not shown.