Skip to content

Drop Run.BeforeContainer option, keep the Pester.BeforeContainer.ps1 convention#2859

Draft
nohwnd wants to merge 1 commit into
mainfrom
nohwnd-remove-beforecontainer-option
Draft

Drop Run.BeforeContainer option, keep the Pester.BeforeContainer.ps1 convention#2859
nohwnd wants to merge 1 commit into
mainfrom
nohwnd-remove-beforecontainer-option

Conversation

@nohwnd

@nohwnd nohwnd commented Jul 10, 2026

Copy link
Copy Markdown
Member

Summary

The experimental BeforeContainer bootstrap shipped with two entry points:

  1. the Run.BeforeContainer scriptblock[] configuration option, and
  2. the repo-root Pester.BeforeContainer.ps1 convention file (resolved from Run.RepoRoot).

Having both created the inconsistent path story Frode raised in #2838: the file always
exposes a stable $PSScriptRoot/$PSCommandPath to anchor relative paths against, while
the scriptblock option only had the unstable $pwd and no file to anchor from.

This PR removes the configuration option and keeps only the zero-config convention file,
dot-sourced before each container in both sequential and parallel runs. One mechanism, one
consistent behaviour. (As Frode put it: "Do we need to support BeforeContainer as a
configuration option? While nice for debugging, it has some quirks like this."
)

Compelling example

Put shared setup once at the repo root in Pester.BeforeContainer.ps1 and every test file –
sequential or parallel – gets it for free. Because it is a real file, $PSScriptRoot is stable
regardless of the working directory Invoke-Pester was called from:

# ./Pester.BeforeContainer.ps1  (at Run.RepoRoot, e.g. the .git root)
Import-Module -Name (Join-Path $PSScriptRoot 'tests/Helpers.psm1') -Force

$PSDefaultParameterValues['Mock:ModuleName']          = 'MyModule'
$PSDefaultParameterValues['InModuleScope:ModuleName'] = 'MyModule'
$PSDefaultParameterValues['Should-Invoke:ModuleName'] = 'MyModule'
# ./tests/Calculator.Tests.ps1 – no per-file bootstrap needed
Describe 'Calculator' {
    It 'adds' { Get-Answer | Should -Be 42 }   # helper came from the shared bootstrap
}
$c = New-PesterConfiguration
$c.Run.Path     = './tests'
$c.Run.Parallel = $true            # each worker runspace re-runs the same bootstrap
Invoke-Pester -Configuration $c

This removes the ~duplicated BeforeAll/Import-Module boilerplate every file would otherwise
repeat, and – crucially – makes it work under Run.Parallel, where each worker starts from a
clean runspace (the real-world ask from #2772).

Where this leaves Frode's 6.1.0 issues

Issue Status after this PR
#2838 Inconsistent story for relative paths with BeforeContainer Fixed. Only the file convention remains, which always has a stable $PSScriptRoot. The $pwd-only quirk is gone with the option.
#2839 Expose current container to BeforeContainer Still open. Conditional per-file setup (if ($Container.Name -like '*.Integration.Tests.ps1')) is a separate feature and is not addressed here. Removing the option narrows it to "expose the container to the convention file".
#2825 Parallel debug output printed before the relevant test Still open / unrelated to BeforeContainer (parallel diagnostic-output ordering).

Changes

  • src/csharp/Pester/RunConfiguration.cs — remove the BeforeContainer option (field, ctor
    assignment, default initializer, property). Document the Pester.BeforeContainer.ps1 convention
    on the RepoRoot option so it stays discoverable. about_PesterConfiguration help regenerated.
  • src/functions/Pester.Parallel.ps1Resolve-PesterBeforeContainer now resolves only the
    convention file; drop the worker-config sanitation line for the removed option.
  • src/Main.ps1, src/Pester.Runtime.ps1 — comment updates.
  • Tests — replace the explicit-option cases in Pester.RSpec.Parallel.ts.ps1 with a
    multi-file parallel example that shares one bootstrap via the stable $PSScriptRoot; rework the
    Pester crashes due to native command output in BeforeAll #2655 stray-output test in Pester.RSpec.ts.ps1 to use a convention file instead of the option.

Verification

  • ./build.ps1 -Clean — succeeds; about-help regenerated without the option.
  • P-tests: Pester.RSpec.Parallel.ts.ps1 (20/0) and Pester.RSpec.ts.ps1 pass.
  • PesterConfiguration.Tests.ps1 (23/0) — format-data view stays in sync after removing the option.
  • Invoke-ScriptAnalyzer with the custom build rules — no new findings in the changed files.

Fix #2838

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

The experimental BeforeContainer bootstrap had two entry points: the
Run.BeforeContainer scriptblock[] configuration option and a repo-root
Pester.BeforeContainer.ps1 convention file. Having both created an
inconsistent story for anchoring relative paths - the file always has a
stable $PSScriptRoot/$PSCommandPath, while the scriptblock option only had
the unstable $pwd and no file to anchor from.

Remove the configuration option and keep only the zero-config convention
file resolved from Run.RepoRoot, dot-sourced before each container in both
sequential and parallel runs. This leaves a single, consistent mechanism.

- Remove the BeforeContainer option (field, ctor assignment, default and
  property) from RunConfiguration.cs; document the convention file on the
  RepoRoot option instead. about_PesterConfiguration help regenerated.
- Resolve-PesterBeforeContainer now resolves only the convention file.
- Update comments in Main.ps1, Pester.Runtime.ps1 and Invoke-TestInParallel.
- Tests: replace the explicit-option cases with a convention-file example
  that shares a single bootstrap across files in parallel via the stable
  $PSScriptRoot; rework the #2655 stray-output test to use the convention
  file instead of the option.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@nohwnd nohwnd added this to the 6.1.0 milestone Jul 10, 2026
@nohwnd

nohwnd commented Jul 10, 2026

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Contributor
Azure Pipelines successfully started running 1 pipeline(s).

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.

[v6] Inconsistent story for relative paths with BeforeContainer

1 participant