Skip to content

Instantly share code, notes, and snippets.

@nohwnd
nohwnd / get-parmeter-value.tests.ps1
Created April 9, 2021 09:17
Get parameter value from Mock without using $script: scope
# Keeping a value from a mock call, or some other place
# while avoiding $script: to keep script scope clean
Describe "a" {
It 'Test' {
function Get-GitHubRelease ($Release) { }
# use a reference object instead of directly assiging to a
# variable to collect the values from a mock call
$container = @{}
@nohwnd
nohwnd / get-set-variable.ps1
Last active April 8, 2021 09:32
Get and set variable into the caller scope from a module scope
## Getting and setting variables from within a module into the calling scope
# make sure we re-import the module when experimenting with this
# to get our functions updated
Get-Module m | Remove-Module
New-Module m -ScriptBlock {
function Get-CallerVariable {
[CmdletBinding()]
@nohwnd
nohwnd / get-filter-closure.ps1
Last active April 7, 2021 15:22
Generating filter
function Get-Filter ($Predicate) {
# gets the property hashtable of an object
$properties = $Predicate.PSObject.Properties
# runs the code below in a scriptblock to ensure that we only capture
# the desired $Properties variable in case we would have more variables
# in this function. This is not strictly necessary, but closure only captures
# local variables so it is useful trick to limit the variables that we capture.
& {
$m = Get-Command Invoke-Pester -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Module
$psv = $PSVersionTable.PSVersion
$pre = $m.PrivateData -and $m.PrivateData.PSData -and $m.PrivateData.PSData.PreRelease
$pv = if ($pre) { "$($m.Version)-$($m.PrivateData.PSData.PreRelease)" } else { $m.Version }
"Pester version : " + $pv + " " + $m.Path
"PowerShell version : " + $psv
"OS version : " + [System.Environment]::OSVersion.VersionString
@nohwnd
nohwnd / Mocking static property getter
Created September 21, 2020 13:09
Mocking static property getter using Harmony
$harmony = "$PSScriptRoot/0Harmony.dll"
Import-Module $harmony
$Script:Patches = @()
function Set-StaticPropertyGetter {
param (
[Parameter(Mandatory)]
[Type] $Type,
@nohwnd
nohwnd / passing-values-from-discovery.tests.ps1
Last active May 27, 2020 07:52
Passing values from Discovery to Run when generating tests
Describe "a" {
$Sources = @(
[PSCustomObject]@{
Advanced = @{
Enabled = $true
}
}
[PSCustomObject]@{
Advanced = @{
Enabled = $true
@nohwnd
nohwnd / add-beforeall.ps1
Created April 4, 2020 16:05
Migrate from Pester v4 to v5
# 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 {
@nohwnd
nohwnd / Add-BeforeAll.ps1
Created March 29, 2020 18:01
Add-BeforeAll
# 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 {
@nohwnd
nohwnd / Cross function boundary
Created March 29, 2020 12:10
Cross function boundary perf
# 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
}
@nohwnd
nohwnd / output.txt
Created February 22, 2020 08:06
Pass various parameters to Pester
# 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