Skip to content

Commit

Permalink
[WIP] Coverage test
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienBraillard committed Jul 7, 2024
1 parent 3270177 commit ae7aaf6
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
70 changes: 70 additions & 0 deletions tests/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0"/>
<PackageReference Include="NSubstitute" Version="5.1.0"/>
<PackageReference Include="NSubstitute.Analyzers.CSharp" Version="1.0.16" />
<PackageReference Include="ReportGenerator" Version="5.3.7" PrivateAssets="all" />
<PackageReference Include="System.IO.Abstractions.TestingHelpers" Version="20.0.15" />
<PackageReference Include="TestableIO.System.IO.Abstractions.Extensions" Version="2.2.1" />
<PackageReference Include="Verify.Xunit" Version="23.1.0" />
Expand Down Expand Up @@ -78,4 +79,73 @@
</VSTestLogger>
</PropertyGroup>

<PropertyGroup Label="Test output: All">
<!-- Collect code coverage -->
<CollectCoverage>true</CollectCoverage>
<!-- Save code coverage analysis using opencover format in same folder than the unit test rsults -->
<CoverletOutputFormat>json,lcov,opencover</CoverletOutputFormat>
<CoverletOutput>$([MSBuild]::EnsureTrailingSlash($(VSTestResultsDirectory)))</CoverletOutput>
<!-- Ensure that sequential runs are merged together -->
<MergeWith>$([System.IO.Path]::Combine($(MSBuildThisFileDirectory), 'results', 'coverage.$(TargetFramework).json'))</MergeWith>
<!-- Exclude code marked with specific attributes -->
<ExcludeByAttribute>ObsoleteAttribute,GeneratedCodeAttribute,CompilerGeneratedAttribute,ExcludeFromCodeCoverage</ExcludeByAttribute>
<!-- Prepares the coverage output file -->
<CoverageOutputFile>$([System.IO.Path]::Combine($(VSTestResultsDirectory), 'coverage.$(TargetFramework).opencover.xml'))</CoverageOutputFile>
</PropertyGroup>


<!-- Generate coverage report -->
<Target Label="Coverage report: NON CI" Name="GenerateHtmlCoverageReport" AfterTargets="GenerateCoverageResult" Condition="$(ContinuousIntegrationBuild) != 'true'">
<ReportGenerator
ReportFiles="$(CoverageOutputFile)"
TargetDirectory="$([System.IO.Path]::Combine($(VSTestResultsDirectory), 'coverage'))"
ReportTypes="HtmlInline" />
</Target>

<!-- This simple inline task does nothing. -->
<UsingTask
TaskName="SendCoverageToGitHubSummary"
TaskFactory="RoslynCodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
<ParameterGroup>
<Title Required="true"/>
<InputFile ParameterType="Microsoft.Build.Framework.ITaskItem" Required="true" />
<OutputFile ParameterType="Microsoft.Build.Framework.ITaskItem" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System.IO" />
<Using Namespace="System.Text.RegularExpressions" />
<Code Type="Fragment" Language="cs">
<![CDATA[
var content = File.ReadAllLines(InputFile.GetMetadata("FullPath"));
var coverageMatch = Regex.Match(string.Join("\n", content), @"Line coverage.*?([0-9\.]+%)");
var coverage = coverageMatch.Success ? coverageMatch.Groups[1].Value : "?%";
using (var writer = new StreamWriter(OutputFile.GetMetadata("FullPath"), true) { NewLine = "\n" })
{
writer.WriteLine();
writer.WriteLine($"<details><summary><b>{Title}</b>: {coverage}</summary>");
foreach(var line in content.Where(l => !l.StartsWith("#")))
{
writer.WriteLine(line);
}
writer.WriteLine("</details>");
}
]]>
</Code>
</Task>
</UsingTask>

<Target Label="Coverage report: CI" Name="GenerateHtmlCoverageReport" AfterTargets="GenerateCoverageResult" Condition="$(ContinuousIntegrationBuild) == 'true'">
<ReportGenerator
ReportFiles="$(CoverageOutputFile)"
TargetDirectory="$(VSTestResultsDirectory)"
ReportTypes="MarkdownSummary" />
<SendCoverageToGitHubSummary
Title="Code Coverage ($(TargetFramework))"
InputFile="$([System.IO.Path]::Combine($(VSTestResultsDirectory), 'Summary.md'))"
OutputFile="$(GITHUB_STEP_SUMMARY)"/>

</Target>

</Project>
18 changes: 18 additions & 0 deletions tests/Unicorn.LiveLogViewer.Tests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@
"resolved": "1.0.16",
"contentHash": "+sdWbGyEQl22A4fBWo17/T53TMdbxegNjqwCxdBUS5/tp8ESJwfvHvGdIGj83sR0jKxvpUQoHO35qcf9tdgmdw=="
},
"ReportGenerator": {
"type": "Direct",
"requested": "[5.3.7, )",
"resolved": "5.3.7",
"contentHash": "v2QymfyRqwgKECFDd8R9YTIJb0MS/6gKZiEnmc0R2tGU8sCagvxlNL9h0uNz7ETBsIaFWxGFM2pJgPKYjKcBPw=="
},
"System.IO.Abstractions.TestingHelpers": {
"type": "Direct",
"requested": "[20.0.15, )",
Expand Down Expand Up @@ -468,6 +474,12 @@
"resolved": "1.0.16",
"contentHash": "+sdWbGyEQl22A4fBWo17/T53TMdbxegNjqwCxdBUS5/tp8ESJwfvHvGdIGj83sR0jKxvpUQoHO35qcf9tdgmdw=="
},
"ReportGenerator": {
"type": "Direct",
"requested": "[5.3.7, )",
"resolved": "5.3.7",
"contentHash": "v2QymfyRqwgKECFDd8R9YTIJb0MS/6gKZiEnmc0R2tGU8sCagvxlNL9h0uNz7ETBsIaFWxGFM2pJgPKYjKcBPw=="
},
"System.IO.Abstractions.TestingHelpers": {
"type": "Direct",
"requested": "[20.0.15, )",
Expand Down Expand Up @@ -853,6 +865,12 @@
"resolved": "1.0.16",
"contentHash": "+sdWbGyEQl22A4fBWo17/T53TMdbxegNjqwCxdBUS5/tp8ESJwfvHvGdIGj83sR0jKxvpUQoHO35qcf9tdgmdw=="
},
"ReportGenerator": {
"type": "Direct",
"requested": "[5.3.7, )",
"resolved": "5.3.7",
"contentHash": "v2QymfyRqwgKECFDd8R9YTIJb0MS/6gKZiEnmc0R2tGU8sCagvxlNL9h0uNz7ETBsIaFWxGFM2pJgPKYjKcBPw=="
},
"System.IO.Abstractions.TestingHelpers": {
"type": "Direct",
"requested": "[20.0.15, )",
Expand Down

0 comments on commit ae7aaf6

Please sign in to comment.