Last active
June 27, 2019 15:00
-
-
Save nohwnd/8e8dd1365e3c39979cb844ae725e21d3 to your computer and use it in GitHub Desktop.
Dot sourcing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Import-Module Pester -MaximumVersion 4.9.9 | |
# use the fact that closures are dynamic modules with | |
# their own session state, and that scriptblocks are bound | |
# to the session state that created them, that makes | |
# the call to `dot` transparent, because the scope | |
# that the function creates is in a different sessionstate | |
# and so the `$ScriptBlock` still dot-sources directly into | |
# the calling session state, just like with the naked `.` | |
$function:dot = { | |
param($ScriptBlock) | |
. $ScriptBlock | |
}.GetNewClosure() | |
Describe "a" { | |
It "dot sources" { | |
$a = 1 | |
# ensure that we are running the same scope | |
# by writing into the `$a` variable | |
dot { $a = 20 } | |
$a | Should -be 20 | |
} | |
It "can be mocked" { | |
Mock dot { Write-Host "I am mock" -ForegroundColor Cyan } | |
$a = 1 | |
dot { $a = 20 } | |
$a | Should -Be 1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment