|
| 1 | +# This script is for local use to analyze code coverage in more detail using HTML report. |
| 2 | + |
| 3 | +Param( |
| 4 | + [switch]$ProdPackagesOnly = $false |
| 5 | +) |
| 6 | + |
| 7 | +# Generate a timestamp for the current date and time |
| 8 | +$timestamp = Get-Date -Format "yyyyMMdd-HHmmss" |
| 9 | + |
| 10 | +# Define paths |
| 11 | +$scriptPath = Get-Item -Path $PSScriptRoot |
| 12 | +$coverageOutputPath = Join-Path $scriptPath "TestResults\Coverage\$timestamp" |
| 13 | +$reportOutputPath = Join-Path $scriptPath "TestResults\Reports\$timestamp" |
| 14 | + |
| 15 | +# Create output directories |
| 16 | +New-Item -ItemType Directory -Force -Path $coverageOutputPath |
| 17 | +New-Item -ItemType Directory -Force -Path $reportOutputPath |
| 18 | + |
| 19 | +# Find tests for projects ending with 'UnitTests.csproj' |
| 20 | +$testProjects = Get-ChildItem $scriptPath -Filter "*UnitTests.csproj" -Recurse |
| 21 | + |
| 22 | +foreach ($project in $testProjects) { |
| 23 | + $testProjectPath = $project.FullName |
| 24 | + Write-Host "Running tests for project: $($testProjectPath)" |
| 25 | + |
| 26 | + # Run tests |
| 27 | + dotnet test $testProjectPath ` |
| 28 | + --collect:"XPlat Code Coverage" ` |
| 29 | + --results-directory:$coverageOutputPath ` |
| 30 | + |
| 31 | +} |
| 32 | + |
| 33 | +# Install required tools |
| 34 | +& dotnet tool install -g coverlet.console |
| 35 | +& dotnet tool install -g dotnet-reportgenerator-globaltool |
| 36 | + |
| 37 | +# Generate HTML report |
| 38 | +if ($ProdPackagesOnly) { |
| 39 | + $assemblies = @( |
| 40 | + "+Microsoft.SemanticKernel.Abstractions", |
| 41 | + "+Microsoft.SemanticKernel.Core", |
| 42 | + "+Microsoft.SemanticKernel.PromptTemplates.Handlebars", |
| 43 | + "+Microsoft.SemanticKernel.Connectors.OpenAI", |
| 44 | + "+Microsoft.SemanticKernel.Yaml" |
| 45 | + ) |
| 46 | + |
| 47 | + $assemblyFilters = $assemblies -join ";" |
| 48 | + |
| 49 | + # Generate report for production assemblies only |
| 50 | + & reportgenerator -reports:"$coverageOutputPath/**/coverage.cobertura.xml" -targetdir:$reportOutputPath -reporttypes:Html -assemblyfilters:$assemblyFilters |
| 51 | +} |
| 52 | +else { |
| 53 | + & reportgenerator -reports:"$coverageOutputPath/**/coverage.cobertura.xml" -targetdir:$reportOutputPath -reporttypes:Html |
| 54 | +} |
| 55 | + |
| 56 | +Write-Host "Code coverage report generated at: $reportOutputPath" |
| 57 | + |
| 58 | +# Open report |
| 59 | +$reportIndexHtml = Join-Path $reportOutputPath "index.html" |
| 60 | +Invoke-Item -Path $reportIndexHtml |
0 commit comments