Fix using [ScriptBlock]::Create with InModuleScope.#1146
Fix using [ScriptBlock]::Create with InModuleScope.#1146nohwnd merged 3 commits intopester:masterfrom
Conversation
|
@sethvs nice catch. Could you add tests for it as well please? 🙂 |
|
Sorry for delay :) |
| } | ||
|
|
||
| It "Can use ScriptBlock inside the module scope" { | ||
| $ScriptBlockOne = { Write-Output "I am a ScriptBlockOne" } |
There was a problem hiding this comment.
I don't see any need to mix the two tests cases together. Could you split them to Can execute bound ScriptBlock inside the module scope ({} makes scriptblock bound to the current session) and Can execute unbound ScriptBlock in module scope please? :)
|
Is it OK with Should swallow test output without -PassThru test in PowerShell v2? |
|
@sethvs sorry I don't get what you are asking. Could you rephrase please? |
|
Publish Status to GitHub (Pester) check is failed on test with name Should swallow test output without -PassThru when it was run on PowerShell v2. |
|
@sethvs Now I get it. There is some ongoing problem with the build server, that makes some of the tests fail, I am |
|
@sethvs Merged, thanks ! 👍 |
1. General summary of the pull request
We use ScriptBlock with InModuleScope.
Now we can use it like this:
the result is:
Describing {}But if we need to construct ScriptBlock dynamicly by using [ScriptBlock]::Create method
the result will be:
This is because, when InModuleScope function gets $originalScriptBlockScope of the ScriptBlock
Pester/Functions/InModuleScope.ps1
Line 74 in 5a53c46
the result will be $null.
It seems, that ScriptBlock created with Create method doesn't have SessionStateInternal property.
For example this code returns null:
So, when InModuleScope function tries to return the $originalScriptBlockScope to the ScriptBlock
Pester/Functions/InModuleScope.ps1
Line 90 in 5a53c46
the Set-ScriptBlockScope throws an error, because -SessionStateInternal parameter cannot be null.
This PS allows -SessionStateInternal parameter to accept null, so that ScriptBlock will have its original SessionStateInternal back, even if it is $null.