Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude duplicate files for Run.Path and CodeCoverage.Path #2535

Merged
merged 4 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions src/Pester.RSpec.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
[string] $Extension
)


$files =
foreach ($p in $Path) {
$files = foreach ($p in $Path) {
if ([String]::IsNullOrWhiteSpace($p)) {
continue
}
Expand Down Expand Up @@ -66,11 +64,13 @@
}
}

Filter-Excluded -Files $files -ExcludePath $ExcludePath | & $SafeCommands['Where-Object'] { $_ }
# Deduplicate files if overlapping -Path values
$uniquePaths = [System.Collections.Generic.HashSet[string]]::new(@($files).Count)
$uniqueFiles = foreach ($f in $files) { if ($uniquePaths.Add($f.FullName)) { $f } }
Filter-Excluded -Files $uniqueFiles -ExcludePath $ExcludePath | & $SafeCommands['Where-Object'] { $_ }

Check notice

Code scanning / PSScriptAnalyzer

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`. Note

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`.
}

function Filter-Excluded ($Files, $ExcludePath) {

if ($null -eq $ExcludePath -or @($ExcludePath).Length -eq 0) {
return @($Files)
}
Expand All @@ -81,12 +81,9 @@
$excluded = $false

foreach ($exclusion in (@($ExcludePath) -replace "/", "\")) {
if ($excluded) {
continue
}

if ($p -like $exclusion) {
$excluded = $true
continue
}
}

Expand Down Expand Up @@ -574,12 +571,10 @@
# the @() is significant here, it will make it iterate even if there are no data
# which allows files without data to run
foreach ($d in @($dt)) {
foreach ($p in $Path) {
# resolve the path we are given in the same way we would resolve -Path on Invoke-Pester
$files = @(Find-File -Path $p -ExcludePath $PesterPreference.Run.ExcludePath.Value -Extension $PesterPreference.Run.TestExtension.Value)
foreach ($file in $files) {
New-BlockContainerObject -File $file -Data $d
}
# resolve the path we are given in the same way we would resolve -Path on Invoke-Pester
$files = @(Find-File -Path $Path -ExcludePath $PesterPreference.Run.ExcludePath.Value -Extension $PesterPreference.Run.TestExtension.Value)
foreach ($file in $files) {
New-BlockContainerObject -File $file -Data $d
}
}
}
Expand Down
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)) {
fflaten marked this conversation as resolved.
Show resolved Hide resolved
if (('.ps1', '.psm1') -contains $file.Extension -and ($IncludeTests -or $file.Name -notlike $testsPattern)) {
$file.FullName
}
}

return $filePaths
})
fflaten marked this conversation as resolved.
Show resolved Hide resolved

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

function Get-CoverageBreakpoints {
Expand Down
5 changes: 5 additions & 0 deletions tst/Pester.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ InPesterModuleScope {
($paths -contains (Join-Path $testDrive "SomeOtherFile.Tests.ps1")) | Should -Be $true
}

It 'Deduplicates filepaths when the provided paths overlaps' {
$result = @(Find-File 'TestDrive:\*.ps1','TestDrive:\*.ps1' -Extension '.Tests.ps1')
$result.Count | Should -Be 2
}

# It 'Assigns empty array and hashtable to the Arguments and Parameters properties when none are specified by the caller' {
# $result = @(Find-File 'TestDrive:\SomeFile.ps1' -Extension ".Tests.ps1")

Expand Down