Skip to content

Commit

Permalink
Refactor Get-CodeCoverageFilePaths and remove duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
fflaten committed Jul 8, 2024
1 parent 745f04c commit d3129cc
Showing 1 changed file with 9 additions and 28 deletions.
37 changes: 9 additions & 28 deletions src/functions/Coverage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -211,54 +211,35 @@ function Resolve-CoverageInfo {

$filePaths = Get-CodeCoverageFilePaths -Paths $resolvedPaths -IncludeTests $includeTests -RecursePaths $recursePaths

$params = @{
$commonParams = @{
StartLine = $UnresolvedCoverageInfo.StartLine
EndLine = $UnresolvedCoverageInfo.EndLine
Class = $UnresolvedCoverageInfo.Class
Function = $UnresolvedCoverageInfo.Function
}

foreach ($filePath in $filePaths) {
$params['Path'] = $filePath
New-CoverageInfo @params
New-CoverageInfo @commonParams -Path $filePath
}
}

function Get-CodeCoverageFilePaths {
param (
[object]$Paths,
[string[]]$Paths,
[bool]$IncludeTests,
[bool]$RecursePaths
)

$testsPattern = "*$($PesterPreference.Run.TestExtension.Value)"

$filePaths = foreach ($path in $Paths) {
$item = & $SafeCommands['Get-Item'] -LiteralPath $path
if ($item -is [System.IO.FileInfo] -and ('.ps1', '.psm1') -contains $item.Extension -and ($IncludeTests -or $item.Name -notlike $testsPattern)) {
$item.FullName
}
elseif ($item -is [System.IO.DirectoryInfo]) {
$children = foreach ($i in & $SafeCommands['Get-ChildItem'] -LiteralPath $item) {
# if we recurse paths return both directories and files so they can be resolved in the
# recursive call to Get-CodeCoverageFilePaths, otherwise return just files
if ($RecursePaths) {
$i.PSPath
}
elseif (-not $i.PSIsContainer) {
$i.PSPath
}
}
Get-CodeCoverageFilePaths -Paths $children -IncludeTests $IncludeTests -RecursePaths $RecursePaths
}
elseif (-not $item.PsIsContainer) {
# todo: enable this warning for non wildcarded paths? otherwise it prints a ton of warnings for documentation and so on when using "folder/*" wildcard
# & $SafeCommands['Write-Warning'] "CodeCoverage path '$path' resolved to a non-PowerShell file '$($item.FullName)'; this path will not be part of the coverage report."
[string[]] $filteredFiles = @(foreach ($file in (& $SafeCommands['Get-ChildItem'] -LiteralPath $Paths -File -Recurse:$RecursePaths)) {
if (('.ps1', '.psm1') -contains $file.Extension -and ($IncludeTests -or $file.Name -notlike $testsPattern)) {
$file.FullName
}
}

return $filePaths
})

$uniqueFiles = [System.Collections.Generic.HashSet[string]]::new($filteredFiles)
return $uniqueFiles
}

function Get-CoverageBreakpoints {
Expand Down

0 comments on commit d3129cc

Please sign in to comment.