-
-
Notifications
You must be signed in to change notification settings - Fork 474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix using [ScriptBlock]::Create with InModuleScope. #1146
Conversation
@sethvs nice catch. Could you add tests for it as well please? 🙂 |
Sorry for delay :) |
Functions/InModuleScope.Tests.ps1
Outdated
@@ -40,6 +40,15 @@ Describe "Executing test code inside a module" { | |||
} | |||
} | |||
|
|||
It "Can use ScriptBlock inside the module scope" { | |||
$ScriptBlockOne = { Write-Output "I am a ScriptBlockOne" } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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:
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.