This file contains 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
$callStack = Get-PSCallStack | |
$caller = $callStack[1].ScriptName | |
$callerDirectory = Split-Path $caller | |
[string] $PSGitRoot = $null | |
if ((Get-Command 'git' -ErrorAction Ignore)) | |
{ | |
$workTree = @(git rev-parse --git-path $callerDirectory --is-inside-work-tree) | |
if (0 -lt $workTree.Count -and 'true' -eq $workTree[-1]) { | |
$PSGitRoot = Resolve-Path (@(git rev-parse --git-path $callerDirectory --show-toplevel)[-1]) |
This file contains 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
Describe "In Pester v5 assertions don't fail by default" { | |
It "is awesome for ensuring a lot of stuff about single object" { | |
$user = @{ | |
Name = 'Jakub' | |
Age = 31 | |
Languages = [PSCustomObject]@{ | |
Speaking = 'Czech', 'English' | |
Programming = @( | |
'C#' | |
'PowerShell' |
This file contains 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
Get-Module m | Remove-Module | |
New-Module -Name m -ScriptBlock { | |
function Do-Expensive { } | |
} | Import-Module | |
function f () { | |
$variable = 2 | |
# if ($variable -eq 1) { | |
Do-Expensive | |
# } |
This file contains 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
Get-Module Pester, m | Remove-Module | |
Import-Module Pester -MaximumVersion 4.9.9 | |
New-Module -Name m -ScriptBlock { | |
function a { "hello" } | |
function b { a } | |
Export-ModuleMember -Function b | |
} | Import-Module | |
This file contains 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
# this snippet describes how to automatically clean up | |
# changes in environment varables | |
# it can clean up re-writing, adding and removing | |
# scroll down to see how to use it in real test | |
Get-Module Pester | Remove-Module | |
Import-Module Pester -MaximumVersion 4.9.9 | |
function Get-Environment { | |
$vars = Get-Item Env:\ |
This file contains 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 = { |
This file contains 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
# Sets variable $a = 1000 in the local scope from which it was called from, like $matches in regex | |
Get-Module m | Remove-Module | |
New-Module m { | |
function f { | |
[CmdletBinding()] | |
param() | |
$sb = [ScriptBlock]::Create('param($Value); $a = $Value') | |
$ExecutionContext.InvokeCommand.InvokeScript($PSCmdlet.SessionState, $sb, @(1000)) | |
} |
This file contains 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
# This example shows three versions of behavior, in the first the data binding only | |
# picks up the property that is defined on the type, in the second it picks up ever | |
# powershell adapted property Name (alias property), and in the third it picks up | |
# all properties even though they are all powershell properties and not on the PSObjectType | |
# is there some special handling for PSObject regarding WPF in Windows PowerShell? | |
Add-Type -AssemblyName PresentationFramework | |
[string]$xaml = @" |
This file contains 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
# GOOD: | |
# shares data among tests without polluting | |
# the script scope | |
Describe "I share data" { | |
$container = @{ Value = 1 } | |
It "I give data" { | |
$container.Value = 5 | |
} |
This file contains 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
function Import-Script { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory)] | |
[String] $Path, | |
[Hashtable] $Parameters = @{}, | |
[Object[]] $Arguments = @(), | |
[String] $EntryPoint = 'Main' | |
) |