Dotnet/template-files/code-coverage.ps1

[CmdletBinding()]
param (
    [switch]
    [bool]
    $OpenHtmlReport = $false
)

try {
    Push-Location
    Set-Location $PSScriptRoot

    dotnet test --collect:"XPlat Code Coverage" --settings "./coverlet.runsettings.xml" | Tee-Object -Variable testOutput
    $outXmlPath = ($testOutput -like '*coverage.cobertura.xml').Trim()

    # HTML report
    $htmlReportPath = Join-Path (Split-Path -Parent $outXmlPath) "html"
    dotnet reportgenerator -reports:"$outXmlPath" -reporttypes:Html -targetdir:"$htmlReportPath"
    if ($OpenHtmlReport) {
        Invoke-Item (Join-Path $htmlReportPath "index.html")
    }
}
finally {
    Pop-Location
}