Skip to content

Commit

Permalink
Merge pull request #460 from martincostello/improve-github-coverage-o…
Browse files Browse the repository at this point in the history
…utput

Add GitHub coverage summaries
  • Loading branch information
martincostello authored Mar 10, 2023
2 parents a269a40 + fe47a6f commit 7260679
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup .NET Core SDK
- name: Setup .NET SDK
uses: actions/setup-dotnet@v3

- name: Install StatsD
Expand Down
12 changes: 12 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
<Project>
<PropertyGroup Condition=" '$(CollectCoverage)' == 'true' ">
<ReportGeneratorOutputMarkdown Condition=" '$(ReportGeneratorOutputMarkdown)' == '' AND '$(GITHUB_SHA)' != '' ">true</ReportGeneratorOutputMarkdown>
<ReportGeneratorReportTypes>HTML</ReportGeneratorReportTypes>
<ReportGeneratorReportTypes Condition=" '$(ReportGeneratorOutputMarkdown)' == 'true' ">$(ReportGeneratorReportTypes);MarkdownSummaryGitHub</ReportGeneratorReportTypes>
<ReportGeneratorTargetDirectory>$([System.IO.Path]::Combine($(OutputPath), 'coverage'))</ReportGeneratorTargetDirectory>
<_MarkdownSummaryPrefix>&lt;details&gt;&lt;summary&gt;:chart_with_upwards_trend: &lt;b&gt;$(AssemblyName) Code Coverage report&lt;/b&gt;&lt;/summary&gt;</_MarkdownSummaryPrefix>
<_MarkdownSummarySuffix>&lt;/details&gt;</_MarkdownSummarySuffix>
</PropertyGroup>
<Target Name="GenerateCoverageReports" AfterTargets="GenerateCoverageResultAfterTest" Condition=" '$(CollectCoverage)' == 'true' ">
<ReportGenerator ReportFiles="@(CoverletReport)" ReportTypes="$(ReportGeneratorReportTypes)" Tag="$(Version)" TargetDirectory="$(ReportGeneratorTargetDirectory)" Title="$(AssemblyName)" VerbosityLevel="Warning" />
<Exec Condition=" '$(ReportGeneratorOutputMarkdown)' == 'true' " Command="pwsh -Command %22('$(_MarkdownSummaryPrefix)' + [System.Environment]::NewLine + [System.Environment]::NewLine + (Get-Content $([System.IO.Path]::Combine($(ReportGeneratorTargetDirectory), 'SummaryGithub.md')) | Out-String) + [System.Environment]::NewLine + [System.Environment]::NewLine + '$(_MarkdownSummarySuffix)') >> $(GITHUB_STEP_SUMMARY)%22" />
</Target>
</Project>
44 changes: 17 additions & 27 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ if ($OutputPath -eq "") {
$installDotNetSdk = $false;

if (($null -eq (Get-Command "dotnet" -ErrorAction SilentlyContinue)) -and ($null -eq (Get-Command "dotnet.exe" -ErrorAction SilentlyContinue))) {
Write-Host "The .NET Core SDK is not installed."
Write-Host "The .NET SDK is not installed."
$installDotNetSdk = $true
}
else {
Expand All @@ -43,7 +43,7 @@ else {
}

if ($installedDotNetVersion -ne $dotnetVersion) {
Write-Host "The required version of the .NET Core SDK is not installed. Expected $dotnetVersion."
Write-Host "The required version of the .NET SDK is not installed. Expected $dotnetVersion."
$installDotNetSdk = $true
}
}
Expand Down Expand Up @@ -85,12 +85,21 @@ if ($installDotNetSdk -eq $true) {
function DotNetPack {
param([string]$Project)

$additionalArgs = @()

if ($VersionSuffix) {
& $dotnet pack $Project --output (Join-Path $OutputPath "packages") --configuration $Configuration --version-suffix "$VersionSuffix" --include-symbols --include-source
}
else {
& $dotnet pack $Project --output (Join-Path $OutputPath "packages") --configuration $Configuration --include-symbols --include-source
$additionalArgs += "--version-suffix"
$additionalArgs += $VersionSuffix
}

& $dotnet `
pack $Project `
--output (Join-Path $OutputPath "packages") `
--configuration $Configuration `
--include-symbols `
--include-source `
$additionalArgs

if ($LASTEXITCODE -ne 0) {
throw "dotnet pack failed with exit code $LASTEXITCODE"
}
Expand All @@ -99,14 +108,6 @@ function DotNetPack {
function DotNetTest {
param([string]$Project)

$nugetPath = Join-Path ($env:USERPROFILE ?? "~") ".nuget\packages"
$propsFile = Join-Path $solutionPath "Directory.Packages.props"
$reportGeneratorVersion = (Select-Xml -Path $propsFile -XPath "//PackageVersion[@Include='ReportGenerator']/@Version").Node.'#text'
$reportGeneratorPath = Join-Path $nugetPath "reportgenerator\$reportGeneratorVersion\tools\net6.0\ReportGenerator.dll"

$coverageOutput = Join-Path $OutputPath "coverage.*.cobertura.xml"
$reportOutput = Join-Path $OutputPath "coverage"

$additionalArgs = @()

if (![string]::IsNullOrEmpty($env:GITHUB_SHA)) {
Expand All @@ -116,19 +117,8 @@ function DotNetTest {

& $dotnet test $Project --output $OutputPath --configuration $Configuration $additionalArgs

$dotNetTestExitCode = $LASTEXITCODE

if (Test-Path $coverageOutput) {
& $dotnet `
$reportGeneratorPath `
`"-reports:$coverageOutput`" `
`"-targetdir:$reportOutput`" `
-reporttypes:HTML `
-verbosity:Warning
}

if ($dotNetTestExitCode -ne 0) {
throw "dotnet test failed with exit code $dotNetTestExitCode"
if ($LASTEXITCODE -ne 0) {
throw "dotnet test failed with exit code $LASTEXITCODE"
}
}

Expand Down

0 comments on commit 7260679

Please sign in to comment.