From 819f16b60723b41b1152e59e11d0e84cc7011ead Mon Sep 17 00:00:00 2001 From: Andreas Jordan Date: Sat, 29 Aug 2026 16:36:18 +0200 Subject: [PATCH] Invoke-DbaPfRelog - Stop eating the caller loop when no blg files match The no-files guard in the end block called Stop-Function -Continue with a return on the next line, but no loop encloses it: without -EnableException the continue escaped the command before its own return ever ran and consumed an iteration of whatever loop the caller was running in. Part of the #10638 inventory. Only this one of the file's ten flagged sites is fixed: the other nine sit inside the relog script block, whose -Continue calls are caught by the per-file foreach that invokes the script block and skip to the next file as intended - a comment at the fixed site records that distinction. The command had no integration tests at all; the new loop-counter test is the first. Verified via the lab harness: 2 tests, 0 failed; the unfixed command fails the new test with "Expected 3, but got 0". References #10638 (do Invoke-DbaPfRelog) Co-Authored-By: Claude Fable 5 --- public/Invoke-DbaPfRelog.ps1 | 6 +++++- tests/Invoke-DbaPfRelog.Tests.ps1 | 21 ++++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/public/Invoke-DbaPfRelog.ps1 b/public/Invoke-DbaPfRelog.ps1 index 3e070066ae7b..6f252b3f782d 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 20a712928d1d..a224de3dc077 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