Pester icon indicating copy to clipboard operation
Pester copied to clipboard

Throw on unbound user-provided scriptblocks

Open fflaten opened this issue 1 year ago • 2 comments

PR Summary

Throws when user provides scriptblocks that are not bound to a session state. They would be executed in Pester's module state which could cause unexpected behaviour.

Examples:

image

image

Fix #2411

Checklist:

  • [x] Add-AssertionOperator -Test (type: ScriptBlock)
  • [x] Add-ShouldOperator -Test (type: ScriptBlock)
  • [x] AfterAll -Scriptblock (type: ScriptBlock)
  • [x] AfterEach -Scriptblock (type: ScriptBlock)
  • [x] BeforeAll -Scriptblock (type: ScriptBlock)
  • [x] BeforeDiscovery -ScriptBlock (type: ScriptBlock)
  • [x] BeforeEach -Scriptblock (type: ScriptBlock)
  • [x] Context -Fixture (type: ScriptBlock)
  • [x] Describe -Fixture (type: ScriptBlock)
  • [x] InModuleScope -ScriptBlock (type: ScriptBlock)
    • Not applicable as it always overrides session state to specified module
  • [x] It -Test (type: ScriptBlock)
  • [x] Mock -MockWith (type: ScriptBlock)
    • Already handles unbound scriptblocks. Test added
  • [x] Mock -ParameterFilter (type: ScriptBlock)
    • Already handles unbound scriptblocks. Test added
  • [x] New-PesterContainer -ScriptBlock (type: ScriptBlock[])
  • [x] Should-All -FilterScript (type: ScriptBlock)
  • [x] Should-Any -FilterScript (type: ScriptBlock)
  • [x] Should-Throw -ScriptBlock (type: ScriptBlock)
  • [x] Run.Scriptblock
  • [x] Run.Container

PR Checklist

  • [x] PR has meaningful title
  • [x] Summary describes changes
  • [x] PR is ready to be merged
    • If not, use the arrow next to Create Pull Request to mark it as a draft. PR can be marked Ready for review when it's ready.
  • [x] Tests are added/update (if required)
  • [ ] Documentation is updated/added (if required)

fflaten avatar Jul 30 '24 18:07 fflaten

Good or checking too many places? Can we improve the error somehow? Included a truncated scriptblock string to help identity the source.

Update link to https://pester.dev/docs/migrations/v5-to-v6 (when available) with workaround?

fflaten avatar Jul 30 '24 18:07 fflaten

The PR looks great. There is some merge conflict

nohwnd avatar Oct 01 '24 20:10 nohwnd