Update-ServiceStatus, Set-DbaPrivilege - Keep test variables out of the module scope and filter on bound parameters - #10731
Merged
potatoqualitee merged 2 commits intoSep 22, 2026
Conversation
…he module scope and filter on bound parameters (do Update-ServiceStatus, Set-DbaPrivilege) Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…e a BeforeAll inside InModuleScope runs in the module scope (do Update-ServiceStatus, Set-DbaPrivilege) Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
andreasjordan
marked this pull request as ready for review
September 20, 2026 19:49
potatoqualitee
approved these changes
Sep 21, 2026
potatoqualitee
left a comment
Member
There was a problem hiding this comment.
Reviewed the complete test-only patch at this exact head, including module-scope isolation, bound-parameter assertions, surrounding implementations, discussions, and passing CI logs for both affected files. No material defects found.
potatoqualitee
deleted the
tests-update-servicestatus-module-scope-leak
branch
September 22, 2026 12:58
Member
|
👌🏼 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Set-DbaPrivilege.Tests.ps1failed its "does not pass the credential ... for the local computer" test in the reversed full run of 2026-09-19, and only there. Two test files together cause it:Update-ServiceStatus.Tests.ps1set up$script:credential(and a few other fixtures) in aBeforeAllinsideInModuleScope dbatools. Pester runs such aBeforeAllin the script scope of the dbatools module itself, so every variable it assigns lands in the module, with or without the$script:prefix (checked: dropping the prefix alone changed nothing). The module stays loaded for the whole process, so the variable outlives the file.Get-ContextToDefine); every other name resolves through the scope chain, up to the module's script scope.{ $null -eq $Credential }inSet-DbaPrivilege.Tests.ps1therefore read the leaked credential wheneverUpdate-ServiceStatus.Tests.ps1had run earlier in the same process, and matched 0 of 1 calls.Repro in a fresh process: Update-ServiceStatus then Set-DbaPrivilege = "Expected Test-PSRemoting in module dbatools to be called 1 times exactly, but was called 0 times"; Set-DbaPrivilege alone passes.
Both sides are fixed:
Update-ServiceStatus.Tests.ps1sets everything up inBeforeEach, which runs in a scope of its own that ends with the test, and uses no$script:. The two call recorders that the fakeNew-CimSession/Remove-CimSessionwrite to are entries of a hashtable owned by the test scope, because an assignment inside those functions would create a local variable. After the file has run, the module scope holds none of its variables (checked withGet-Variable -Scope Scriptinside the module).Set-DbaPrivilege.Tests.ps1asks the question it means:-not $PesterBoundParameters.ContainsKey("Credential"), which no variable from anywhere else can satisfy.Left alone:
Set-DbaPrivilege.Tests.ps1itself writes$script:mockServiceUser, andSync-DbaAvailabilityGroup.Tests.ps1andStart-DbaMigration.Tests.ps1use$script:call recorders insideInModuleScope; those names collide with nothing.Test plan
Update-ServiceStatus.Tests.ps1thenSet-DbaPrivilege.Tests.ps1(unit tests) = the failure above, and$script:credentialis set inside the module afterwards.credential/mockCimSession/service/recorded/passwordvariable in the module scope after either file.created by Claude and reviewed by Andreas Jordan
🤖 Generated with Claude Code