Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions .github/workflows/BuildAndTestOnPullRequests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ jobs:
- name: Build Stateless solution
run: dotnet build Stateless.sln --configuration Release --no-restore

- name: Test Stateless
run: dotnet test --no-restore --no-build --configuration Release
- name: Test Stateless with coverage and generate summary
id: test-summary
run: ./scripts/run-tests-with-coverage.ps1

- name: Output test summary
run: |
echo "${{ steps.test-summary.outputs.summary }}" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Coverage:** ${{ steps.test-summary.outputs.coverage_percentage }}%" >> $GITHUB_STEP_SUMMARY

- name: Pack alpha version
if: github.ref == 'refs/heads/dev' && github.event_name == 'push' && github.repository == 'dotnet-state-machine/stateless'
Expand Down
69 changes: 69 additions & 0 deletions scripts/run-tests-with-coverage.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env pwsh

# Run tests with coverage collection
dotnet test --no-restore --no-build --configuration Release --collect:"XPlat Code Coverage" --results-directory ./coverage --logger "console;verbosity=normal"

# Get test results summary from the output
$testOutput = dotnet test --no-restore --no-build --configuration Release --logger "console;verbosity=minimal" 2>&1
$testSummary = $testOutput | Select-String "Test Run" | Select-Object -Last 1

# If no test summary found, create a default one
if (-not $testSummary) {
$testSummary = "Test execution completed"
}

# Get build configuration from environment or default
$buildConfig = if ($env:BUILD_CONFIGURATION) { $env:BUILD_CONFIGURATION } else { "Release" }

# Get target frameworks from test project file
$testProjectPath = "./test/Stateless.Tests/Stateless.Tests.csproj"
$targetFrameworks = "Unknown"
if (Test-Path $testProjectPath) {
try {
[xml]$testProject = Get-Content $testProjectPath
$targetFrameworks = $testProject.Project.PropertyGroup.TargetFrameworks
if (-not $targetFrameworks) {
$targetFrameworks = $testProject.Project.PropertyGroup.TargetFramework
}
if (-not $targetFrameworks) {
$targetFrameworks = "Unknown"
}
} catch {
$targetFrameworks = "Error reading project file"
}
}

# Get coverage percentage
$coveragePercentage = "N/A"
$badgeColor = "lightgrey"

try {
$coverageFile = Get-ChildItem -Path "./coverage" -Recurse -Filter "coverage.cobertura.xml" | Select-Object -First 1
if ($coverageFile) {
[xml]$coverage = Get-Content $coverageFile.FullName
$lineRate = [double]$coverage.coverage.'line-rate'
$coveragePercentage = [math]::Round($lineRate * 100, 1)

$badgeColor = if ($coveragePercentage -ge 90) { "brightgreen" } elseif ($coveragePercentage -ge 80) { "green" } elseif ($coveragePercentage -ge 70) { "yellow" } elseif ($coveragePercentage -ge 60) { "orange" } else { "red" }
}
} catch {
$coveragePercentage = "Error calculating coverage"
$badgeColor = "lightgrey"
}

# Create summary
$summary = "## Test Results`n`n"
$summary += "**Tests:** $testSummary`n"
$summary += "**Coverage:** $coveragePercentage%`n"
$summary += "**Target Frameworks:** $targetFrameworks`n"
$summary += "**Configuration:** $buildConfig`n`n"

if ($coveragePercentage -ne "N/A" -and $coveragePercentage -ne "Error calculating coverage") {
$summary += "![Coverage](https://img.shields.io/badge/coverage-${coveragePercentage}%25-${badgeColor}?style=flat-square)"
}

echo "summary<<EOF" >> $env:GITHUB_OUTPUT
echo "$summary" >> $env:GITHUB_OUTPUT
echo "EOF" >> $env:GITHUB_OUTPUT
echo "coverage_percentage=$coveragePercentage" >> $env:GITHUB_OUTPUT
echo "badge_color=$badgeColor" >> $env:GITHUB_OUTPUT
4 changes: 4 additions & 0 deletions test/Stateless.Tests/Stateless.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.analyzers" Version="0.10.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
Loading