Skip to content

Invoke-ManualPester - Run the tests from outside the module - #10830

Merged
potatoqualitee merged 4 commits into
developmentfrom
invoke-manualpester-outside-module
Nov 11, 2026
Merged

potatoqualitee merged 4 commits into
developmentfrom
invoke-manualpester-outside-module

Conversation

@andreasjordan

Copy link
Copy Markdown
Collaborator

Fixes #10827

Problem

Run the way the contributor guide documents it (Import-Module .\dbatools.psm1, then Invoke-ManualPester -Path Get-DbaBuild), Get-DbaBuild.Tests.ps1 ran 23 tests with 8 failures and printed errors for every test file. Started from outside the module, the same file passes.

Mechanism

Invoke-ManualPester lived in private/testing, which dbatools.psm1 dot-sources into the module. Called as a module function:

  1. Invoke-Pester runs the test files in the session state of its caller, so the tests ran inside the dbatools module. There Get-Module -Name dbatools also returns the binary module that dbatools.psm1:115 loads from dbatools.dll. Mock Update-DbaBuildReference -ModuleName dbatools was never hit, and $ModuleBase in Get-DbaBuild.Tests.ps1 became two paths joined by a space.
  2. Its own Import-Module dbatools.psd1 -Force removed the module it was running in, and with it the functions defined in its begin block. Write-DetailedMessage and Test-PesterTestHeader failed, so a test file without the Pester header was run instead of reported.
  3. After the run no dbatools module was left in the session.

CI was not affected: appveyor.pester.ps1 runs Pester from a script, and the Windows SSPI step in ci-azure.yml already dot-sourced the file from outside the module.

What changed

  • private/testing/Invoke-ManualPester.ps1 moves to tests/Invoke-ManualPester.ps1 and becomes a script that is started by its path and needs no Import-Module first. The first commit is the pure rename. The second removes the function wrapper, which re-indents the body (use "Hide whitespace"). The code is otherwise unchanged. The help says why it is a script, and its examples use the new call.
  • ci-azure.yml calls the script instead of dot-sourcing the function.
  • CONTRIBUTING-TESTING.md, the pull request template and tests/CLAUDE.md show .\tests\Invoke-ManualPester.ps1 -Path <command>.
  • Get-DbaBuild.Tests.ps1 (lines 31 and 193) finds the script module with ModuleType -eq "Script" instead of ModuleBase -NotMatch net (Get/Test-DbaBuild, handle version "aliases" #8959), as Invoke-DbaDbDecryptObject.Tests.ps1 does since Invoke-DbaDbDecryptObject - Add DataPages to decrypt without a dedicated admin connection #10581. The old filter no longer excludes the binary module, whose base is now core\lib or desktop\lib. It also drops the script module when the working copy sits below a path that contains "net", for example C:\Users\Kenneth\... or D:\dotnet\....

Deliberately not changed

  • Invoke-ManualPester is no longer a command after importing dbatools, and no stub is left behind.
  • The other test files that read Get-Module -Name dbatools without a filter (Get-DbaWaitingTask, Import-DbaParquet, Find-DbaCommand, Set-DbatoolsConfig, and the many Select-Object -First 1 sites) only see two modules inside the module's scope, which the runner no longer enters.
  • The comment and the workaround in Invoke-DbaDbDecryptObject.Tests.ps1 that name Invoke-ManualPester as the source of the two modules. The comment stays as written, and the workaround is harmless.

Tests

Lab, PowerShell 7.6.3 and Windows PowerShell 5.1, dbatools.library 2026.9.14. development is c8f2da6.

Run development This branch
Get-DbaBuild through the runner, fresh session 23 tests, 8 failed 92 tests, 89 passed, 0 failed, 3 integration tests not run
Same, after Import-Module .\dbatools.psm1 23 tests, 8 failed 92 / 89 / 0 / 3
Same call as the ci-azure.yml step - 92 / 89 / 0 / 3
Test file without the Pester header run anyway warned and skipped
dbatools modules in the session after the run none the script module
Working copy below ...\dotnet\ (junction, 7.6 only), old vs new Get-DbaBuild.Tests.ps1 23 tests, 7 failed 92 / 89 / 0 / 3

.\tests\Invoke-ManualPester.ps1 -Path Get-DbaBuild -ScriptAnalyzer -Compliance on 7.6: Compliance 61 passed, 0 failed, and ScriptAnalyzer reports no findings.

There is no new Pester test, because the change is in the test tooling and the runs above are its proof. The Windows SSPI step in ci-azure.yml runs only when its partition has tests selected, so it may be skipped on this pull request. The lab row "Same call as the ci-azure.yml step" covers the call itself.

🤖 Generated with Claude Code

andreasjordan and others added 3 commits November 11, 2026 19:19
Pure rename. The next commit turns the function into a script, which
re-indents every line of its body, so the move is kept apart to let
the conversion show up as its own diff.

(do Get-DbaBuild)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Invoke-ManualPester was a private function of dbatools, so the way the
contributor guide documents it - Import-Module .\dbatools.psm1, then
Invoke-ManualPester - ran it inside the module. Pester runs the test files
in the scope of whoever calls Invoke-Pester, so the tests ran inside the
module too, where Get-Module -Name dbatools also returns the binary module
that dbatools.psm1 loads from dbatools.dll. Mock -ModuleName dbatools and
every read of the module base broke. Its Import-Module -Force also removed
the module it was running in, which took its begin-block helpers with it
(the Pester header check silently did not run), and left no dbatools module
in the session.

The runner is now a script, tests\Invoke-ManualPester.ps1, started by its
path like tests\appveyor.pester.ps1. Its code is unchanged apart from the
function wrapper. ci-azure.yml already dot-sourced it from outside the
module and now calls the script; the contributor guide, the pull request
template and tests\CLAUDE.md show the new call.

Get-DbaBuild.Tests.ps1 goes from 23 tests with 8 failures to 92 tests,
89 passed, 3 integration tests not run, on PowerShell 7.6 and 5.1.

Part of #10827

(do Get-DbaBuild)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The tests read the module base from Get-Module -Name dbatools filtered by
ModuleBase -NotMatch net (#8959). That no longer excludes the binary module
of the same name, whose base is now core\lib or desktop\lib, and it drops
the script module as well when the working copy sits below a path that
contains "net": from a working copy below ...\dotnet\, the file ran 23
tests with 7 failures, with the fixed runner too. Filter on
ModuleType -eq "Script" instead, as Invoke-DbaDbDecryptObject.Tests.ps1
does since #10581. From both paths the file now runs 92 tests, 89 passed.

Fixes #10827

(do Get-DbaBuild)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@andreasjordan
andreasjordan marked this pull request as ready for review November 11, 2026 18:53

@potatoqualitee potatoqualitee left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed head 4610625d311eaef28d72216224ce3fc18ca62e86.

Reviewed the script relocation, module-scope isolation, CI call site, and affected test/module detection. Running Pester from a standalone script addresses module-scope contamination without changing production commands. No material defect identified.

@potatoqualitee potatoqualitee left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fresh review of head 4610625d311eaef28d72216224ce3fc18ca62e86.

Reviewed all seven changed files, the module loader, complete runner flow, updated CI call site and result checks, module selection in the tests, discussion and CI. Independently normalized the old function body against the new script: the executable body is unchanged after removing the wrapper. No material regression found.

Executed the actual script in a disposable copy on PowerShell 7.6.7 with Pester 6.0.0: 89 passed, 0 failed, 3 integration cases not run. The first run hit the unchanged Get-DbaBuild cold-cache path, which bypasses its mocked update call when creating the writable index; the unchanged rerun passed after initialization. The script module remains available afterward. Current import/platform/security checks pass; main ci-azure is still pending.

@potatoqualitee

Copy link
Copy Markdown
Member

thank you kindly

@potatoqualitee
potatoqualitee merged commit 8ee546c into development Nov 11, 2026
18 checks passed
@potatoqualitee
potatoqualitee deleted the invoke-manualpester-outside-module branch November 11, 2026 20:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invoke-ManualPester - Running the tests inside the module breaks its helpers and Get-DbaBuild.Tests.ps1

2 participants