Add coverage enforcement for test projects (#6950)

* Add coverage enforcement for test projects

* Expand GitHub Actions triggers to include additional branch patterns

* Update `coverlet.msbuild` configuration and centralize dependency version management

- Removed inline version specification for `coverlet.msbuild` in `test/Directory.Build.props`.
- Centralized `coverlet.msbuild` version definition in `Directory.Packages.props` for consistency and maintainability.

* Remove `Elsa.Common.Core` unit test project and related test files

* Add Directory.Build.props for test project organization and update property configurations

- Introduced `Directory.Build.props` files for `test/unit` and `test/integration` to define project-specific properties.
- Updated `test/Directory.Build.props` to include new coverage formats and an exclusion for `Elsa.Testing.Shared`.
- Adjusted solution file to link new `Directory.Build.props` files.
- Configured threshold properties for `unit`, `integration`, and `component` test directories.

* Disable coverage collection for performance tests in project file

* Expand GitHub Actions workflow triggers and add PR-specific condition for test job
This commit is contained in:
Sipke Schoorstra 2025-10-07 19:59:53 +02:00 committed by GitHub
parent fcdb6f5499
commit fe4d74189a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 125 additions and 61 deletions

View file

@ -1,4 +1,5 @@
name: Packages
on:
workflow_dispatch:
push:
@ -12,18 +13,87 @@ on:
- 'rc/*'
- 'develop/*'
- 'codex/*'
pull_request:
branches:
- 'main'
- 'bug/*'
- 'perf/*'
- 'patch/*'
- 'feat/*'
- 'enh/*'
- 'rc/*'
- 'develop/*'
- 'codex/*'
release:
types: [ prereleased, published ]
types: [prereleased, published]
env:
base_version: '3.6.0'
feedz_feed_source: 'https://f.feedz.io/elsa-workflows/elsa-3/nuget/index.json'
nuget_feed_source: 'https://api.nuget.org/v3/index.json'
jobs:
build:
name: Build packages
test:
name: Test with coverage
runs-on: ubuntu-latest
timeout-minutes: 30
if: ${{ github.event_name == 'pull_request' }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up .NET SDKs
uses: actions/setup-dotnet@v4
with:
dotnet-version: |
8.0.x
9.0.x
- name: Cache NuGet packages
uses: actions/cache@v4
with:
path: ~/.nuget/packages
key: ${{ runner.os }}-nuget-${{ hashFiles('**/Directory.Packages.props', '**/*.csproj') }}
restore-keys: |
${{ runner.os }}-nuget-
- name: Restore solution
run: dotnet restore Elsa.sln
- name: Build solution
run: dotnet build Elsa.sln --no-restore --configuration Release
- name: Prepare coverage directory
run: |
rm -rf artifacts/coverage
mkdir -p artifacts/coverage
- name: Run tests with coverage
run: |
set -euo pipefail
mapfile -t projects < <(find test -type f -name '*.csproj' | sort)
if [ ${#projects[@]} -eq 0 ]; then
echo "No test projects were found in the test directory." >&2
exit 1
fi
for project in "${projects[@]}"; do
echo "Running tests with coverage for ${project}"
dotnet test "$project" --configuration Release --no-build --logger "GitHubActions;report-warnings=false" /p:CollectCoverage=true
done
- name: Upload coverage reports
if: always()
uses: actions/upload-artifact@v4
with:
name: coverage-reports
path: artifacts/coverage
build:
name: Build packages
needs: test
runs-on: ubuntu-latest
timeout-minutes: 30
if: ${{ github.event_name != 'pull_request' }}
steps:
- name: Extract branch name
run: |
@ -42,17 +112,20 @@ jobs:
echo "Package prefix: ${PACKAGE_PREFIX}"
echo "BRANCH_NAME=${BRANCH_NAME}" >> $GITHUB_ENV
echo "PACKAGE_PREFIX=${PACKAGE_PREFIX}" >> $GITHUB_ENV
- name: Checkout
uses: actions/checkout@v4
- name: Verify commit exists in branch
run: |
if [[ "${{ github.ref }}" == refs/tags/* && "${{ github.event_name }}" == "release" && ("${{ github.event.action }}" == "published" || "${{ github.event.action }}" == "prereleased")]]; then
if [[ "${{ github.ref }}" == refs/tags/* && "${{ github.event_name }}" == "release" && ("${{ github.event.action }}" == "published" || "${{ github.event.action }}" == "prereleased") ]]; then
git fetch --no-tags --prune --depth=1 origin +refs/heads/*:refs/remotes/origin/*
git branch --remote --contains | grep origin/develop/3.6.0
else
git fetch --no-tags --prune --depth=1 origin +refs/heads/*:refs/remotes/origin/*
git branch --remote --contains | grep origin/${BRANCH_NAME}
fi
- name: Set VERSION variable
run: |
if [[ "${{ github.ref }}" == refs/tags/* && "${{ github.event_name }}" == "release" && ("${{ github.event.action }}" == "published" || "${{ github.event.action }}" == "prereleased") ]]; then
@ -62,28 +135,14 @@ jobs:
else
echo "VERSION=${{env.base_version}}-preview.${{github.run_number}}" >> $GITHUB_ENV
fi
# - name: Set up JDK 17
# uses: actions/setup-java@v2
# with:
# java-version: '17'
# distribution: 'adopt'
- uses: actions/setup-dotnet@v4
with:
dotnet-version: 9.x
# - name: Install SonarScanner for .NET
# run: dotnet tool install --global dotnet-sonarscanner
# - name: Install Coverlet for code coverage
# run: dotnet tool install --global coverlet.console
# - name: Begin SonarCloud analysis
# env:
# SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
# run: dotnet sonarscanner begin /k:"elsa-workflows_elsa-core" /o:"elsa-workflows" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.exclusions=**/obj/**,**/*.dll,build/**,samples/**,src/apps/** /d:"sonar.verbose=true" /d:sonar.cs.opencover.reportsPaths=**/testresults/**/coverage.opencover.xml
- name: Compile+Test+Pack
run: ./build.sh Compile+Test+Pack --version ${VERSION} --analyseCode true
# - name: End SonarCloud analysis
# env:
# SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
# run: dotnet sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}"
- name: Upload artifact
uses: actions/upload-artifact@v4
with:

View file

@ -29,6 +29,7 @@
<PackageVersion Include="Confluent.Kafka" Version="2.10.0"/>
<PackageVersion Include="Confluent.SchemaRegistry.Serdes.Avro" Version="2.10.0"/>
<PackageVersion Include="coverlet.collector" Version="6.0.4" PrivateAssets="All"/>
<PackageVersion Include="coverlet.msbuild" Version="6.0.0" PrivateAssets="All"/>
<PackageVersion Include="Cronos" Version="0.11.0"/>
<PackageVersion Include="Dapper" Version="2.1.66"/>
<PackageVersion Include="Datadog.Trace.Bundle" Version="3.16.0"/>

View file

@ -62,8 +62,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docker", "docker", "{986E54
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "unit", "unit", "{18453B51-25EB-4317-A4B3-B10518252E92}"
ProjectSection(SolutionItems) = preProject
test\unit\Directory.Build.props = test\unit\Directory.Build.props
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "integration", "integration", "{1B8D5897-902E-4632-8698-E89CAF3DDF54}"
ProjectSection(SolutionItems) = preProject
test\integration\Directory.Build.props = test\integration\Directory.Build.props
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "component", "component", "{08B41FFA-CEE3-46A7-B5C0-3EB65D37A16C}"
ProjectSection(SolutionItems) = preProject

View file

@ -9,6 +9,15 @@
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<CollectCoverage Condition="'$(CollectCoverage)' == ''">true</CollectCoverage>
<CoverletOutputFormat Condition="'$(CoverletOutputFormat)' == ''">cobertura,lcov,opencover</CoverletOutputFormat>
<CoverletOutput Condition="'$(CoverletOutput)' == ''">$(MSBuildThisFileDirectory)../artifacts/coverage/$(MSBuildProjectName)/coverage</CoverletOutput>
<Threshold Condition="'$(Threshold)' == ''">10</Threshold>
<ThresholdType Condition="'$(ThresholdType)' == ''">line</ThresholdType>
<ThresholdStat Condition="'$(ThresholdStat)' == ''">total</ThresholdStat>
<Exclude>
[*Elsa.Testing.Shared*]*
</Exclude>
</PropertyGroup>
<ItemGroup>
@ -17,7 +26,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk"/>
<PackageReference Include="Moq"/>
<PackageReference Include="NSubstitute"/>
<PackageReference Include="coverlet.collector" PrivateAssets="all"/>
<PackageReference Include="coverlet.msbuild"/>
<PackageReference Include="xunit"/>
<PackageReference Include="xunit.runner.visualstudio" PrivateAssets="all"/>
</ItemGroup>

View file

@ -2,6 +2,10 @@
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
<PropertyGroup>
<Threshold>40</Threshold>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Testcontainers.PostgreSql"/>
<PackageReference Include="Testcontainers.RabbitMq"/>

View file

@ -0,0 +1,9 @@
<Project>
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
<PropertyGroup>
<Threshold>10</Threshold>
</PropertyGroup>
</Project>

View file

@ -7,6 +7,7 @@
<IsPackable>false</IsPackable>
<OutputType>Exe</OutputType>
<CollectCoverage>false</CollectCoverage>
</PropertyGroup>
<ItemGroup>

View file

@ -0,0 +1,9 @@
<Project>
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
<PropertyGroup>
<Threshold>0</Threshold>
</PropertyGroup>
</Project>

View file

@ -1,5 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\..\src\modules\Elsa.Common\Elsa.Common.csproj" />
</ItemGroup>
</Project>

View file

@ -1,29 +0,0 @@
using Elsa.Extensions;
using Xunit;
namespace Elsa.Common.Core;
public class EnumerableExtensionsTests
{
[Fact]
public void ToBatches_ReturnsItemsInListOfPages()
{
var items = new List<int>
{
1,
2,
3,
4,
5
};
var batchSize = 2;
var batches = items.ToBatches(batchSize).ToList();
Assert.Equal(3, batches.Count);
Assert.Equal(2, batches.ElementAt(0).Count());
Assert.Equal(2, batches.ElementAt(1).Count());
Assert.Equal(1, batches.ElementAt(2).Count());
Assert.Equal(3, batches.ElementAt(1).ElementAt(0));
Assert.Equal(4, batches.ElementAt(1).ElementAt(1));
}
}