Skip to content

Instantly share code, notes, and snippets.

@RamblingCookieMonster
Last active August 29, 2015 14:16
Show Gist options
  • Save RamblingCookieMonster/05552112507db8a04246 to your computer and use it in GitHub Desktop.
Save RamblingCookieMonster/05552112507db8a04246 to your computer and use it in GitHub Desktop.
Variable Scope Tests
#Run this ps1, or just copy and paste the code... it will fail pester tests.
Remove-Variable -Name __DummyVariable -ErrorAction SilentlyContinue
"In SomeScript.ps1 via call operator"
& .\SomeScript.ps1
Remove-Variable -Name __DummyVariable -ErrorAction SilentlyContinue
"In SomeScript.ps1 via dot source operator"
. .\SomeScript.ps1
Import-Module .\Test-Variable.psm1 -force
$__DummyVariable = "hi!"
Describe "Test-Variable" {
Context 'Module' {
It 'should see _DummyVariable' {
( Test-Variable ) -contains "__DummyVariable" | Should be $True
}
}
}
Import-Module .\Test-Variable.ps1 -force
Describe "Test-Variable" {
Context 'Function' {
It 'should see _DummyVariable' {
( Test-Variable ) -contains "__DummyVariable" | Should be $True
}
}
}
#This is a script to abstract out pester tests
Invoke-Pester .\Pester.Tests.ps1
#A simple function, not in a module
function Test-Variable {
echo 0,1,2,local,script,global | %{Get-Variable -Scope $_ -ErrorAction SilentlyContinue} | Select -ExpandProperty Name | sort -Unique
}
#A simple module
function Test-Variable {
echo 0,1,2,local,script,global | %{Get-Variable -Scope $_ -ErrorAction SilentlyContinue} | Select -ExpandProperty Name | sort -Unique
}
Export-ModuleMember Test-Variable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment