Skip to content

Commit

Permalink
[rel/5.6.x] Implement CodeCoverage.ExcludeTests (#2524)
Browse files Browse the repository at this point in the history
* Implement CodeCoverage.ExcludeTests

* Update src/functions/Coverage.ps1

* Add missing test file

Fixed in #2525 for main (v6)

* Fix tests

---------

Co-authored-by: Frode Flaten <[email protected]>
Co-authored-by: Jakub Jareš <[email protected]>
  • Loading branch information
3 people authored Jul 10, 2024
1 parent bee7840 commit fba5dd7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/functions/Coverage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ function Get-CoverageInfoFromDictionary {
$includeTests = Get-DictionaryValueFromFirstKeyFound -Dictionary $Dictionary -Key 'IncludeTests'
$recursePaths = Get-DictionaryValueFromFirstKeyFound -Dictionary $Dictionary -Key 'RecursePaths'

# TODO: Implement or remove the IDictionary config logic from CodeCoverage
# Quick fix for https://github.com/pester/Pester/issues/2514 until CodeCoverage config logic is updated
if ($null -eq $includeTests) { $includeTests = $PesterPreference.CodeCoverage.ExcludeTests.Value -ne $true }

$startLine = Convert-UnknownValueToInt -Value $startLine -DefaultValue 0
$endLine = Convert-UnknownValueToInt -Value $endLine -DefaultValue 0
[bool] $includeTests = Convert-UnknownValueToInt -Value $includeTests -DefaultValue 0
Expand Down
34 changes: 34 additions & 0 deletions tst/Pester.RSpec.Coverage.ts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,38 @@ i -PassThru:$PassThru {
$r.Result | Verify-Equal 'Passed'
}
}

b 'Coverage path resolution' {
t 'Excludes test files when ExcludeTests is true' {
# https://github.com/pester/Pester/issues/2514
$c = New-PesterConfiguration
$c.Run.Path = "$PSScriptRoot/testProjects/CoverageTestFile.Tests.ps1"
$c.Run.PassThru = $true
$c.CodeCoverage.Enabled = $true
$c.CodeCoverage.ExcludeTests = $true # default
$c.CodeCoverage.Path = "$PSScriptRoot/CoverageTestFile.ps1", "$PSScriptRoot/testProjects"

$r = Invoke-Pester -Configuration $c

$r.Result | Verify-Equal 'Passed'
$r.CodeCoverage.FilesAnalyzedCount | Verify-Equal 1
@($r.CodeCoverage.FilesAnalyzed) -match '\.Tests.ps1$' | Verify-Null
}

t 'Includes test files when ExcludeTests is false' {
# https://github.com/pester/Pester/issues/2514
$c = New-PesterConfiguration
$c.Run.Path = "$PSScriptRoot/testProjects/CoverageTestFile.Tests.ps1"
$c.Run.PassThru = $true
$c.CodeCoverage.Enabled = $true
$c.CodeCoverage.ExcludeTests = $false
$c.CodeCoverage.Path = "$PSScriptRoot/CoverageTestFile.ps1", "$PSScriptRoot/testProjects"

$r = Invoke-Pester -Configuration $c

$r.Result | Verify-Equal 'Passed'
$r.CodeCoverage.FilesAnalyzedCount | Verify-Equal 4
@($r.CodeCoverage.FilesAnalyzed) -match '\.Tests.ps1$' | Verify-NotNull
}
}
}
7 changes: 7 additions & 0 deletions tst/testProjects/CoverageTestFile.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Set-StrictMode -Version Latest

Describe 'Testing CodeCoverage' {
It 'Single error' {
. "$PSScriptRoot/../CoverageTestFile.ps1"
}
}

0 comments on commit fba5dd7

Please sign in to comment.