Skip to content

Commit

Permalink
Implement CodeCoverage.ExcludeTests
Browse files Browse the repository at this point in the history
  • Loading branch information
fflaten committed Jul 1, 2024
1 parent 6cefccf commit 48643db
Show file tree
Hide file tree
Showing 2 changed files with 38 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 @@ -167,6 +167,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
# Jank 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
}
}
}

0 comments on commit 48643db

Please sign in to comment.