Skip to content

Instantly share code, notes, and snippets.

@wgross
Last active February 21, 2017 19:07
Show Gist options
  • Save wgross/e48ba849f99078d00da7b42af1a76f9e to your computer and use it in GitHub Desktop.
Save wgross/e48ba849f99078d00da7b42af1a76f9e to your computer and use it in GitHub Desktop.
Runs OpenCover and ReportGenerator on a dotnet core test project
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline)]
[ValidateScript({Test-Path -Path $_})]
$ProjectToMeasure = $PWD, # Allows to pipe in project directiories
[Parameter()]
[switch]$ShowResult
)
begin {
# Solution root is the directory containing a global.json
# If it isn't found break execution with an exception
function Get-AncestorWithGlobalJson {
$current = Get-Item -Path $ProjectToMeasure
while($current -and !(Test-Path -Path (Join-Path -Path $current -ChildPath global.json))) {
$current = Split-Path -Path $current -Parent
}
if(!($current)) {
throw "Couldn't find global.json ascending from directory $ProjectToMeasure"
}
return $current
}
$solutionRoot = Get-AncestorWithGlobalJson
# coverage information is stored in solutions root .coverage.
# the directory is created if necessary
if(Test-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath .coverage)) {
$script:coverageResultsDirectory = Get-Item -Path (Join-Path -Path $solutionRoot -ChildPath .coverage)
} else {
$script:coverageResultsDirectory = New-Item -Path (join-Path -Path $solutionRoot -ChildPath .coverage) -ItemType Directory
}
# I need Nuget.exe
$script:nuget = (Get-Command nuget.exe).Path
# Get latest Opencover as nuget package
& $script:nuget install OpenCover -Output $script:coverageResultsDirectory
# Get hold on the OpenCover executable
# This might fails if multiple versions of OpenCover are installed. I'll fix it if it happens
$script:openCoverConsole = Get-ChildItem -Path $script:coverageResultsDirectory -Filter OpenCover.Console.Exe -Recurse | Select-Object -ExpandProperty FullName
# Get latest ReportGenerator as a nuget package
& $script:nuget install ReportGenerator -Output $script:coverageResultsDirectory
# Get hold on ReportGenerator executable
$script:reportGenerator = Get-ChildItem -Path $script:coverageResultsDirectory -Filter ReportGenerator.Exe -Recurse | Select-Object -ExpandProperty FullName
# I need dotnet.exe to execute the tests
$script:dotnet = (Get-Command dotnet.exe).Path
}
process {
# I suppose the script is called from within a test project or in the test folder
$ProjectToMeasure | Get-ChildItem -Filter project.json -Recurse -File | ForEach-Object {
# run all of the found test projects and with dotnet test
$thisProjectDirectory = $_.Directory
$thisProjectNamespace = $_.Directory.BaseName
$thisProjectCoverageOutputFile = (Join-Path -Path $script:coverageResultsDirectory -ChildPath "$thisProjectNamespace.xml")
# call opencover.
# Let it run dotnet test and write the results to an XML file with the name of the project/assembly.
& $script:openCoverConsole -oldStyle -target:$script:dotnet -targetargs:test -targetdir:$($thisProjectDirectory.FullName) -register:user -output:$thisProjectCoverageOutputFile
}
}
end {
# If wanted, show the coverage result
if($ShowResult.IsPresent) {
# Generate a report for every coverage file found
& $script:reportGenerator -targetdir:$script:coverageResultsDirectory -reports:([string]::Join(";",(Get-ChildItem -Path $script:coverageResultsDirectory -Filter *.xml | Select-Object -ExpandProperty FullName)))
# Start default browser with index.html from coverage result directory
Invoke-Item -Path (Join-Path -Path $script:coverageResultsDirectory -ChildPath index.htm)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment