Skip to content

Commit 638b837

Browse files
committed
Add code coverage options for output file and format
1 parent 3785c3c commit 638b837

File tree

7 files changed

+64
-28
lines changed

7 files changed

+64
-28
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## [0.5.0] Unreleased
9+
10+
### Added
11+
12+
- New code coverage parameters for setting output path and format:
13+
- `$PSBPreference.Test.CodeCoverage.OutputFile` - Output file path for code coverage results
14+
- `$PSBPreference.Test.CodeCoverage.OutputFileFormat` - Code coverage output format
15+
816
## [0.5.0] (beta1) - 2020-11-15
917

1018
### Added

PowerShellBuild/Public/Test-PSBuildPester.ps1

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ function Test-PSBuildPester {
88
Directory Pester tests to execute.
99
.PARAMETER ModuleName
1010
Name of Module to test.
11+
.PARAMETER ModuleManifest
12+
Path to module manifest to import during test
1113
.PARAMETER OutputPath
1214
Output path to store Pester test results to.
1315
.PARAMETER OutputFormat
@@ -18,6 +20,10 @@ function Test-PSBuildPester {
1820
Threshold required to pass code coverage test (.90 = 90%).
1921
.PARAMETER CodeCoverageFiles
2022
Array of files to validate code coverage for.
23+
.PARAMETER CodeCoverageOutputFile
24+
Output path (relative to Pester tests directory) to store code coverage results to.
25+
.PARAMETER CodeCoverageOutputFileFormat
26+
Code coverage result output format. Currently, only 'JaCoCo' is supported by Pester.
2127
.PARAMETER ImportModule
2228
Import module from OutDir prior to running Pester tests.
2329
.EXAMPLE
@@ -32,6 +38,8 @@ function Test-PSBuildPester {
3238

3339
[string]$ModuleName,
3440

41+
[string]$ModuleManifest,
42+
3543
[string]$OutputPath,
3644

3745
[string]$OutputFormat = 'NUnit2.5',
@@ -42,6 +50,10 @@ function Test-PSBuildPester {
4250

4351
[string[]]$CodeCoverageFiles = @(),
4452

53+
[string]$CodeCoverageOutputFile = 'coverage.xml',
54+
55+
[string]$CodeCoverageOutputFileFormat = 'JaCoCo',
56+
4557
[switch]$ImportModule
4658
)
4759

@@ -51,10 +63,13 @@ function Test-PSBuildPester {
5163

5264
try {
5365
if ($ImportModule) {
54-
# Remove any previously imported project modules and import from the output dir
55-
$ModuleOutputManifest = [IO.Path]::Combine($env:BHBuildOutput, "$($ModuleName).psd1")
56-
Get-Module $ModuleName | Remove-Module -Force
57-
Import-Module $ModuleOutputManifest -Force
66+
if (-not (Test-Path $ModuleManifest)) {
67+
Write-Error "Unable to find module manifest [$ModuleManifest]. Can't import module"
68+
} else {
69+
# Remove any previously imported project modules and import from the output dir
70+
Get-Module $ModuleName | Remove-Module -Force -ErrorAction SilentlyContinue
71+
Import-Module $ModuleManifest -Force
72+
}
5873
}
5974

6075
Push-Location -LiteralPath $Path
@@ -72,7 +87,8 @@ function Test-PSBuildPester {
7287
if ($CodeCoverageFiles.Count -gt 0) {
7388
$configuration.CodeCoverage.Path = $CodeCoverageFiles
7489
}
75-
$configuration.CodeCoverage.OutputPath = 'coverage.xml'
90+
$configuration.CodeCoverage.OutputPath = $CodeCoverageOutputFile
91+
$configuration.CodeCoverage.OutputFormat = $CodeCoverageOutputFileFormat
7692
}
7793

7894
$testResult = Invoke-Pester -Configuration $configuration -Verbose:$VerbosePreference
@@ -83,9 +99,9 @@ function Test-PSBuildPester {
8399

84100
if ($CodeCoverage.IsPresent) {
85101
Write-Host "`nCode Coverage:`n" -ForegroundColor Yellow
86-
if (Test-Path coverage.xml) {
102+
if (Test-Path $CodeCoverageOutputFile) {
87103
$textInfo = (Get-Culture).TextInfo
88-
[xml]$testCoverage = Get-Content coverage.xml
104+
[xml]$testCoverage = Get-Content $CodeCoverageOutputFile
89105
$ccReport = $testCoverage.report.counter.ForEach({
90106
$total = [int]$_.missed + [int]$_.covered
91107
$perc = [Math]::Truncate([int]$_.covered / $total)
@@ -95,20 +111,19 @@ function Test-PSBuildPester {
95111
}
96112
})
97113

98-
$ccfail = $false
99114
$ccFailMsgs = @()
100115
$ccReport.ForEach({
101-
'Code coverage type [{0}] on specified files: {1:p}' -f $_.name, $_.percent
116+
'Type: [{0}]: {1:p}' -f $_.name, $_.percent
102117
if ($_.percent -lt $CodeCoverageThreshold) {
103-
$ccFail = $true
104118
$ccFailMsgs += ('Code coverage: [{0}] is [{1:p}], which is less than the threshold of [{2:p}]' -f $_.name, $_.percent, $CodeCoverageThreshold)
105119
}
106120
})
107-
if ($ccFail) {
108-
throw $ccFailMsgs
109-
}
121+
Write-Host "`n"
122+
$ccFailMsgs.Foreach({
123+
Write-Error $_
124+
})
110125
} else {
111-
Write-Error 'coverage.xml not found'
126+
Write-Error "Code coverage file [$CodeCoverageOutputFile] not found."
112127
}
113128
}
114129
} finally {

PowerShellBuild/build.properties.ps1

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ $moduleVersion = (Import-PowerShellDataFile -Path $env:BHPSModuleManifest).Modul
5252
RootDir = [IO.Path]::Combine($env:BHProjectPath, 'tests')
5353

5454
# Specifies an output file path to send to Invoke-Pester's -OutputFile parameter.
55-
# This is typically used to write out test results so that they can be sent to a CI
56-
# system like AppVeyor.
57-
OutputFile = $null
55+
# This is typically used to write out test results so that they can be sent to a CI system
56+
# This path is relative to the directory containing Pester tests
57+
OutputFile = [IO.Path]::Combine($env:BHProjectPath, 'testResults.xml')
5858

5959
# Specifies the test output format to use when the TestOutputFile property is given
6060
# a path. This parameter is passed through to Invoke-Pester's -OutputFormat parameter.
@@ -89,6 +89,12 @@ $moduleVersion = (Import-PowerShellDataFile -Path $env:BHPSModuleManifest).Modul
8989
# acts as a direct input to the Pester -CodeCoverage parameter, so will support constructions
9090
# like the ones found here: https://pester.dev/docs/usage/code-coverage.
9191
Files = @()
92+
93+
# Path to write code coverage report to
94+
OutputFile = [IO.Path]::Combine($env:BHProjectPath, 'codeCoverage.xml')
95+
96+
# The code coverage output format to use
97+
OutputFileFormat = 'JaCoCo'
9298
}
9399
}
94100
Help = @{

PowerShellBuild/psakeFile.ps1

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,17 @@ $pesterPreReqs = {
9494
}
9595
task Pester -depends Build -precondition $pesterPreReqs {
9696
$pesterParams = @{
97-
Path = $PSBPreference.Test.RootDir
98-
ModuleName = $PSBPreference.General.ModuleName
99-
OutputPath = $PSBPreference.Test.OutputFile
100-
OutputFormat = $PSBPreference.Test.OutputFormat
101-
CodeCoverage = $PSBPreference.Test.CodeCoverage.Enabled
102-
CodeCoverageThreshold = $PSBPreference.Test.CodeCoverage.Threshold
103-
CodeCoverageFiles = $PSBPreference.Test.CodeCoverage.Files
104-
ImportModule = $PSBPreference.Test.ImportModule
97+
Path = $PSBPreference.Test.RootDir
98+
ModuleName = $PSBPreference.General.ModuleName
99+
ModuleManifest = Join-Path $PSBPreference.Build.ModuleOutDir "$($PSBPreference.General.ModuleName).psd1"
100+
OutputPath = $PSBPreference.Test.OutputFile
101+
OutputFormat = $PSBPreference.Test.OutputFormat
102+
CodeCoverage = $PSBPreference.Test.CodeCoverage.Enabled
103+
CodeCoverageThreshold = $PSBPreference.Test.CodeCoverage.Threshold
104+
CodeCoverageFiles = $PSBPreference.Test.CodeCoverage.Files
105+
CodeCoverageOutputFile = $PSBPreference.Test.CodeCoverage.OutputFile
106+
CodeCoverageOutputFileFormat = $PSBPreference.Test.CodeCoverage.OutputFormat
107+
ImportModule = $PSBPreference.Test.ImportModule
105108
}
106109
Test-PSBuildPester @pesterParams
107110
} -description 'Execute Pester tests'

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ You can override these in either psake or Invoke-Build to match your environment
9494
| $PSBPreference.Test.CodeCoverage.Enabled | `$false` | Enable/disable Pester code coverage reporting
9595
| $PSBPreference.Test.CodeCoverage.Threshold | `.75` | Fail Pester code coverage test if below this threshold
9696
| $PSBPreference.Test.CodeCoverage.Files | `*.ps1, *.psm1` | Files to perform code coverage analysis on
97+
| $PSBPreference.Test.CodeCoverage.OutputFile | `coverage.xml` | Output file path (relative to Pester test directory) where Pester will save code coverage results to
98+
| $PSBPreference.Test.CodeCoverage.OutputFileFormat | `$null` | Test output format to use when saving Pester code coverage results
9799
| $PSBPreference.Test.ImportModule | `$false` | Import module from output directory prior to running Pester tests
98100
| $PSBPreference.Help.UpdatableHelpOutDir | `$OutDir/UpdatableHelp` | Output directory to store update module help (CAB)
99101
| $PSBPreference.Help.DefaultLocale | `(Get-UICulture).Name` | Default locale used for help generation

tests/TestModule/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
docs/
33
Output/
44
Tests/*.xml
5+
codeCoverage.xml
6+
testResults.xml

tests/TestModule/psakeFile.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ properties {
2121
$PSBPreference.Build.CompileScriptFooter = '# Function footer' + [Environment]::NewLine
2222

2323
# So Pester InModuleScope works
24-
$PSBPreference.Test.ImportModule = $true
25-
$PSBPreference.Test.OutputFile = 'fooResults.xml'
26-
$PSBPreference.Test.CodeCoverage.Enabled = $true
24+
$PSBPreference.Test.ImportModule = $true
25+
$PSBPreference.Test.CodeCoverage.Enabled = $true
2726
$PSBPreference.Test.CodeCoverage.Threshold = 0.0
27+
$PSBPreference.Test.CodeCoverage.OutputFile = 'cc.xml'
2828

2929
# Override the default output directory
3030
$PSBPreference.Build.OutDir = 'Output'

0 commit comments

Comments
 (0)