Skip to content

Instantly share code, notes, and snippets.

@maxfunke
Created January 11, 2019 20:47
Show Gist options
  • Save maxfunke/b1ac6c19e1281fb2f87502656a5abee6 to your computer and use it in GitHub Desktop.
Save maxfunke/b1ac6c19e1281fb2f87502656a5abee6 to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Finds all test .dlls and adds them to a code coverage run.
Generates a code coverage report.
.EXAMPLE
"ExecuteCodeCoverage.ps1 -testDllDir . -reportOutDir .\report -reportOutFormat html -testRunnerDir ."
All arguments are optional. Should work like magic.
#>
param (
[Parameter(Mandatory=$false)][string] $testDllDir,
[Parameter(Mandatory=$false)][string] $reportOutDir,
[Parameter(Mandatory=$false)][string] $reportOutFormat,
[Parameter(Mandatory=$false)][string] $testRunnerDir
)
$tempDir = (Resolve-Path -EA Stop '.').Path + "\tmp_cover"
if(!(test-path $tempDir))
{
New-Item -ItemType Directory -Force -Path $tempDir
}
if(!($testDllDir))
{
# go up one directory
$testDllDir = (Resolve-Path -EA Stop '..').Path
}
if(!($reportOutFormat))
{
# html by default
$reportOutFormat = "html"
}
if(!($reportOutDir))
{
$reportOutDir = (Resolve-Path -EA Stop '.').Path
}
if(!($testRunnerDir))
{
$testRunnerDir = (Resolve-Path -EA Stop (Join-Path(Get-Item env:VS*COMNTOOLS)[-1].Value '..\IDE\MSTest.exe')).Path
}
"==== called with args[] ====" | Add-Content -Encoding utf8 "ExecuteCoveragePS1.log"
foreach ($key in $MyInvocation.BoundParameters.keys)
{
$value = (get-variable $key).Value
"$key -> $value" | Add-Content -Encoding utf8 "ExecuteCoveragePS1.log"
}
$timestamp = (Get-Date).ToString("yyyy-MM-dd_HHmmss")
$reportOutDir += "\report_$timestamp"
$reportOutPath = "$reportOutDir\report.$reportOutFormat"
if( -Not (Test-Path -Path $reportOutDir))
{
New-Item -ItemType directory -Path $reportOutDir
}
$dlls = (Get-ChildItem -Path "$testDllDir" -Recurse -Filter "*Test?.dll" | Select-Object -ExpandProperty FullName) | Where-Object { $_ -notmatch "TestResults\\" }
[string[]] $testContainers = @()
$dlls | Foreach-Object { $testContainers += "/testcontainer:`"" + $_ + "`"" }
"==== testDllDir[] ====" | Add-Content -Encoding utf8 "ExecuteCoveragePS1.log"
foreach ($dll in $dlls)
{
"$dll" | Add-Content -Encoding utf8 "ExecuteCoveragePS1.log"
}
$testContainersString = $testContainers -join " "
$placeholder_Containers = "[TestContainers]"
$placeholder_TestRunner = "[TestRunner]"
$placeholder_ReportDir = "[TestReport]"
$placeholder_ReportFormat = "[ReportFormat]"
$placeholder_tmpDir = "[TmpDir]"
$placeholder_WorkingDir = "[WorkingDir]"
$templateDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$configTemplate = Get-Content -Path "$templateDir\dotCover_config_template.xml" | Out-String
$configTemplate = $configTemplate.Replace($placeholder_Containers, $testContainersString)
$configTemplate = $configTemplate.Replace($placeholder_TestRunner, $testRunnerDir)
$configTemplate = $configTemplate.Replace($placeholder_ReportDir, $reportOutDir)
$configTemplate = $configTemplate.Replace($placeholder_ReportFormat, $reportOutFormat)
$configTemplate = $configTemplate.Replace($placeholder_tmpDir, $tempDir)
$configTemplate = $configTemplate.Replace($placeholder_WorkingDir, $testDllDir)
$configTemplate | Out-File -Encoding "UTF8" "$templateDir\dotCover_config_generated.xml"
dotcover analyse "$templateDir\dotCover_config_generated.xml" /output=$reportOutPath | Add-Content -Encoding utf8 "ExecuteCoveragePS1.log"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment