Skip to content

Commit

Permalink
✅ Code coverage (#2)
Browse files Browse the repository at this point in the history
Adds code coverage to the repository. Extends `ci-build.ps1` for local
builds. Adds Report Generator for merging code coverage reports. Publishes
to CodeCov. Updates some test parameters.
  • Loading branch information
connorjs authored Jul 24, 2024
1 parent e3760eb commit 92c3753
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ indent_style = tab
# CSharpier default is 100
max_line_length = 100

[*.{csproj,esproj,props}]
[*.{csproj,esproj,props,runsettings}]
# Increase line length given .NET XML is whitespace sensitive
max_line_length = 999

Expand Down
40 changes: 40 additions & 0 deletions .github/workflows/pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ jobs:
Build:
name: Build
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -30,9 +32,47 @@ jobs:

- name: dotnet test
run: dotnet test -c release --no-build

- name: ReportGenerator
uses: danielpalme/[email protected]
with:
reports: coverage/*.cobertura.xml
targetdir: coverage/report
reporttypes: Cobertura;HtmlInline;JsonSummary;MarkdownSummaryGithub

- name: Check coverage thresholds
shell: pwsh
run: |
$coverage = Get-Content -Raw coverage/report/Summary.json | ConvertFrom-Json
if ($coverage.summary.linecoverage -lt 80 -or $coverage.summary.branchcoverage -lt 80 -or $coverage.summary.methodcoverage -lt 80) {
Write-Error "Coverage does not meet threshold.`n`nCI build failed."; Exit 1
}
# End ci-build.ps1

- name: Add coverage comment to PR
if: github.event_name == 'pull_request' && always() # Still post coverage comment if tests failed
run: gh pr comment $PR_NUMBER --body-file coverage/report/SummaryGithub.md
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.number }}

- name: Publish coverage in build summary
if: always() # Still publish coverage if tests failed
run: cat coverage/report/SummaryGithub.md >> $GITHUB_STEP_SUMMARY

- name: Upload coverage report to Codecov
if: always() # Still upload to CodeCov if tests failed
uses: codecov/codecov-action@v4
with:
disable_search: true
fail_ci_if_error: true
files: coverage/report/Cobertura.xml
flags: unittests
plugins: noop
token: ${{ secrets.CODECOV_TOKEN }}

- name: SonarCloud Scan
if: always() # Still run Sonar if tests failed
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ node_modules/

# Test
coverage/
TestResults/

## IDE ##

Expand Down
10 changes: 6 additions & 4 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<Project>
<PropertyGroup Condition="'$(CI)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>

<!-- MSBuild properties (general) -->
<!-- https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-properties -->
<PropertyGroup>
Expand Down Expand Up @@ -32,11 +36,9 @@
<Deterministic>true</Deterministic>
</PropertyGroup>

<!-- Unit test coverage -->
<!-- Unit test properties -->
<PropertyGroup Condition="$(MSBuildProjectName.EndsWith('test'))">
<!-- Recommended to disable if no C++ native code. -->
<EnableStaticNativeInstrumentation>False</EnableStaticNativeInstrumentation>
<EnableDynamicNativeInstrumentation>False</EnableDynamicNativeInstrumentation>
<TestingPlatformCommandLineArguments>--coverage --coverage-output $(MSBuildProjectName).cobertura.xml --coverage-settings $(MSBuildThisFileDirectory)coverage.runsettings --results-directory $(MSBuildThisFileDirectory)coverage</TestingPlatformCommandLineArguments>
</PropertyGroup>

<!-- Unit test usings -->
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# .NET with `.esproj` Example

Example repository with C# and JS/TS with build orchestration via `dotnet` (`.csproj` and `.esproj`).

[![codecov](https://codecov.io/gh/connorjs/dotnet-with-esproj-example/graph/badge.svg?token=QFRYKH8OOY)](https://codecov.io/gh/connorjs/dotnet-with-esproj-example)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=connorjs_dotnet-with-esproj-example&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=connorjs_dotnet-with-esproj-example)
33 changes: 26 additions & 7 deletions ci-build.ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
try {
$env:CI = true
dotnet restore
dotnet build -c release --no-restore
dotnet test -c release --no-build
} finally {
$env:CI = $null
# Install reportgenerator if not present
if (-not (Get-Command reportgenerator -ErrorAction SilentlyContinue)) {
dotnet tool install --global dotnet-reportgenerator-globaltool
}

function Write-Color($color) {
$fc = $host.UI.RawUI.ForegroundColor; $host.UI.RawUI.ForegroundColor = $color; Write-Output $args; $host.UI.RawUI.ForegroundColor = $fc
}

# Clean coverage directory
Remove-Item -Recurse -Force coverage -ErrorAction SilentlyContinue

# Run the build
dotnet restore
dotnet build -c release --no-restore
dotnet test -c release --no-build
reportgenerator -reports:"coverage/*.cobertura.xml" -targetdir:coverage/report -reporttypes:"Cobertura;HtmlInline;JsonSummary;MarkdownSummaryGithub" -verbosity:Warning

# Output coverage information
$coverage = Get-Content -Raw coverage/report/Summary.json | ConvertFrom-Json
$coverage.summary | Format-Table @{ L = 'Line'; E = { "$($_.linecoverage.toString() )%" }; A = 'center' }, @{ L = 'Branch'; E = { "$( $_.branchcoverage )%" }; A = 'center' }, @{ L = 'Method'; E = { "$( $_.methodcoverage )%" }; A = 'center' }
if ($coverage.summary.linecoverage -lt 80 -or $coverage.summary.branchcoverage -lt 80 -or $coverage.summary.methodcoverage -lt 80) {
Write-Color red "Coverage does not meet threshold.`n`nCI build failed."; Exit 1
}

# Print success
Write-Color green "CI build succeeded."
17 changes: 17 additions & 0 deletions coverage.runsettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<Configuration>
<Format>cobertura</Format>
<CodeCoverage>
<Exclude>
<ModulePath>.*test.dll$</ModulePath>
</Exclude>
</CodeCoverage>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>
1 change: 1 addition & 0 deletions dotnet-with-esproj-example.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "root", "root", "{7592EEA2-3
.gitignore = .gitignore
.github\workflows\pipeline.yaml = .github\workflows\pipeline.yaml
ci-build.ps1 = ci-build.ps1
coverage.runsettings = coverage.runsettings
Directory.Build.props = Directory.Build.props
Directory.Packages.props = Directory.Packages.props
global.json = global.json
Expand Down
2 changes: 1 addition & 1 deletion libs/hello-cs-test/HelloUtilityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class HelloUtilityTest
[DataRow(null, "Hello, world!")]
[DataRow("", "Hello, world!")]
[DataRow(" ", "Hello, !")]
[DataRow("Nx", "Hello, Nx!")]
[DataRow("esproj", "Hello, esproj!")]
public void Hello(string? name, string expected)
{
HelloUtility.Hello(name).Should().Be(expected);
Expand Down
2 changes: 1 addition & 1 deletion libs/hello-cs-test/hello-cs-test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" />
<PackageReference Include="FluentAssertions" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 92c3753

Please sign in to comment.