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
# Adds BeforeAll at the top of Tests file to make it follow Pester v5 recommendation of putting | |
# all code into Pester controlled blocks. | |
# DO this: | |
# BeforeAll { | |
# . $PSScriptRoot/Code.ps1 | |
# } | |
# DO this: | |
# BeforeAll { |
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
# Quickly measuring if it is better to call function in foreach loop or gerate the array and pass it in the function once | |
# and then iterate over the array inside of the function | |
"`n" | |
# cross function boundary 10,000 times | |
$script:b = 0 | |
function a ($a) { $script:b += $a } | |
$sw = [Diagnostics.StopWatch]::StartNew() | |
foreach ($_ in 1..10000) { | |
a 1 | |
} |
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
# output | |
Pester v4.9.0 | |
Executing all tests in 'C:\Users\jajares\Dropbox\pester\parameters.tests.ps1' | |
Executing script C:\Users\jajares\Dropbox\pester\parameters.tests.ps1 | |
Describing a | |
[+] b 31ms | |
[+] c 15ms |
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
$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 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
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 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
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 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
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 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
# 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 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 = { |
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
# 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)) | |
} |