diff --git a/public/Invoke-DbaPfRelog.ps1 b/public/Invoke-DbaPfRelog.ps1 index 3e070066ae7..6f252b3f782 100644 --- a/public/Invoke-DbaPfRelog.ps1 +++ b/public/Invoke-DbaPfRelog.ps1 @@ -281,7 +281,11 @@ function Invoke-DbaPfRelog { $allpaths = $allpaths | Where-Object { $_ -match '.blg' } | Select-Object -Unique if (-not $allpaths) { - Stop-Function -Message "Could not find matching .blg files" -Target $file -Continue + # No -Continue here: the end block has no enclosing loop, so the continue escaped the + # command before the return below ever ran and ate an iteration of the caller's loop. + # The -Continue calls inside the script block below are different: they are caught by the + # per-file foreach that invokes the script block and skip to the next file as intended. + Stop-Function -Message "Could not find matching .blg files" -Target $file return } diff --git a/tests/Invoke-DbaPfRelog.Tests.ps1 b/tests/Invoke-DbaPfRelog.Tests.ps1 index 20a712928d1..a224de3dc07 100644 --- a/tests/Invoke-DbaPfRelog.Tests.ps1 +++ b/tests/Invoke-DbaPfRelog.Tests.ps1 @@ -33,8 +33,19 @@ Describe $CommandName -Tag UnitTests { } } } -<# - Integration test should appear below and are custom to the command you are writing. - Read https://github.com/dataplat/dbatools/blob/development/contributing.md#tests - for more guidence. -#> \ No newline at end of file +Describe $CommandName -Tag IntegrationTests { + Context "When no matching blg files exist" { + It "Warns without eating an iteration of the caller's loop" { + # The no-files guard used to run Stop-Function -Continue in the end block, where no loop + # encloses it - the continue escaped the command before its own return statement ran and + # consumed an iteration of this very loop, so the counter fell short (#10638). + $loopCount = 0 + foreach ($i in 1..3) { + $null = Invoke-DbaPfRelog -Path "$env:TEMP\dbatoolsci-does-not-exist.txt" -WarningAction SilentlyContinue + $loopCount++ + } + $loopCount | Should -Be 3 + $WarnVar | Should -BeLike "*Could not find matching*" + } + } +} \ No newline at end of file