Skip to content

Commit

Permalink
Add GitHub coverage summaries (#1142)
Browse files Browse the repository at this point in the history
- Add a code coverage summary to the Actions logs.
- Use the MSBuild version of ReportGenerator.
- Simplify PowerShell script.
- Rename build script to lowercase.
  • Loading branch information
martincostello authored Mar 10, 2023
1 parent 844bdaf commit edf0dd5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 35 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ jobs:
- name: Build, Test and Package
if: ${{ runner.os != 'linux' }}
shell: pwsh
run: ./Build.ps1
run: ./build.ps1

- name: Build, Test, IntegrationTest and Package
if: ${{ runner.os == 'linux' }}
shell: pwsh
run: ./Build.ps1 -EnableIntegrationTests
run: ./build.ps1 -EnableIntegrationTests

- uses: codecov/codecov-action@v3
name: Upload coverage to Codecov
Expand Down
14 changes: 14 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +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>
3 changes: 2 additions & 1 deletion JustSaying.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.editorconfig = .editorconfig
.gitattributes = .gitattributes
.gitignore = .gitignore
Build.ps1 = Build.ps1
build.ps1 = build.ps1
build.sh = build.sh
CHANGELOG.md = CHANGELOG.md
CODE_OF_CONDUCT.md = CODE_OF_CONDUCT.md
CodeAnalysisRules.ruleset = CodeAnalysisRules.ruleset
Directory.Build.props = Directory.Build.props
Directory.Build.targets = Directory.Build.targets
global.json = global.json
justeat-oss.snk = justeat-oss.snk
LICENSE = LICENSE
Expand Down
48 changes: 16 additions & 32 deletions Build.ps1 → build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,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 @@ -51,7 +51,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 @@ -92,12 +92,15 @@ if (($installDotNetSdk -eq $true) -And ($null -eq $env:TF_BUILD)) {
function DotNetPack {
param([string]$Project)

$additionalArgs = @()

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

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

if ($LASTEXITCODE -ne 0) {
throw "dotnet pack failed with exit code $LASTEXITCODE"
}
Expand All @@ -106,37 +109,18 @@ function DotNetPack {
function DotNetTest {
param([string]$Project)

$nugetPath = Join-Path ($env:USERPROFILE ?? "~") ".nuget\packages"
$propsFile = Join-Path $solutionPath "Directory.Build.props"

$reportGeneratorVersion = (Select-Xml -Path $propsFile -XPath "//PackageReference[@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)) {
& $dotnet test $Project --output $OutputPath --configuration $Configuration
if (![string]::IsNullOrEmpty($env:GITHUB_SHA)) {
$additionalArgs += "--logger"
$additionalArgs += "GitHubActions;report-warnings=false"
}
else {
& $dotnet test $Project --output $OutputPath --configuration $Configuration --logger GitHubActions
}

$dotNetTestExitCode = $LASTEXITCODE

if ((Test-Path $coverageOutput)) {
& $dotnet `
$reportGeneratorPath `
`"-reports:$coverageOutput`" `
`"-targetdir:$reportOutput`" `
-reporttypes:HTML `
-verbosity:Warning
}
& $dotnet test $Project --output $OutputPath --configuration $Configuration $additionalArgs

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

}

Write-Host "Creating packages..." -ForegroundColor Green
Expand Down

0 comments on commit edf0dd5

Please sign in to comment.