Last active
May 18, 2018 01:18
-
-
Save midacts/ef0eb1d30f9234c9dd2433a2129187a1 to your computer and use it in GitHub Desktop.
Set-CodeCoverageBadge theoretical testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Set-CodeCoverageBadge { | |
<# | |
.SYNOPSIS | |
Sets the colour and percentage values of the code coverage badge displayed in readme.md or other specified file. | |
.FUNCTIONALITY | |
CI/CD | |
.DESCRIPTION | |
Sets the colour and percentage values of the code coverage badge displayed in readme.md or other specified file. | |
Typically this would be used after using Invoke-Pester with the -CodeCoverage parameter and returning the coverage | |
result values via -PassThru to a variable (such as $PesterResults). | |
You also need to have initially added the badge to your readme.md or specified file by adding the following string: | |
![Test Coverage](https://img.shields.io/badge/coverage0%25-red.svg) | |
.PARAMETER Status | |
The value to assign to the badge | |
.PARAMETER TextFilePath | |
Path to the text file to update. By default this is $Env:BHProjectPath\Readme.md | |
.PARAMETER BadgeName | |
The name of the badge to update. Default: 'Test Coverage' | |
.EXAMPLE | |
Set-CodeCoverageBadge -Status ([math]::floor(100 - (($PesterResults.CodeCoverage.NumberOfCommandsMissed / $PesterResults.CodeCoverage.NumberOfCommandsAnalyzed) * 100))) | |
.LINK | |
http://wragg.io/add-a-code-coverage-badge-to-your-powershell-deployment-pipeline/ | |
.LINK | |
https://github.com/RamblingCookieMonster/BuildHelpers | |
.LINK | |
about_BuildHelpers | |
#> | |
[cmdletbinding(supportsshouldprocess)] | |
param( | |
$Status = 0, | |
[string] | |
$TextFilePath = "$Env:BHProjectPath\Readme.md", | |
[string] | |
$BadgeName = "Test Coverage" | |
) | |
Process | |
{ | |
$BadgeColor = switch ($Status) | |
{ | |
{$_ -in 90..100 -or $_ -eq "Pass"} { 'brightgreen' } | |
{$_ -in 75..89} { 'yellow' } | |
{$_ -in 60..74} { 'orange' } | |
default { 'red' } | |
} | |
# Add a percentage for numeric statuses | |
if ( $Status -is [int] ) | |
{ | |
$Percent = "%25" | |
} | |
if ($PSCmdlet.ShouldProcess($TextFilePath)) | |
{ | |
$BadgeRegex = "!\[$($BadgeName)\].+\)" | |
$ReadmeContent = (Get-Content $TextFilePath) | |
$ReadmeContent = $ReadmeContent -replace $BadgeRegex, "![$($BadgeName)](https://img.shields.io/badge/$($BadgeName.ToLower())-$($Status)$($Percent)-$BadgeColor.svg)" | |
$ReadmeContent | Set-Content -Path $TextFilePath | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment