Skip to content

Instantly share code, notes, and snippets.

@alx9r
alx9r / verify.Tests.ps1
Created October 15, 2017 22:40
Verify function experiment for use in Pester's Assert-MockCalled
function Verify
{
param
(
[Parameter(ValueFromPipeline,
Position = 1,
Mandatory)]
$ExpressionResult
)
process
@alx9r
alx9r / IsUnrolledByPipeline.ps1
Last active November 20, 2017 02:27
function that tests whether PowerShell unrolls an object
function IsUnrolledByPipeline
{
param
(
[Parameter(Mandatory,
Position=1)]
[AllowNull()]
[object]
$InputObject
)
@alx9r
alx9r / chainedAssertionPattern.ps1
Created November 20, 2017 02:09
Demonstration of a pattern that displays comprehensive test results using chained assertions.
# This function is just to get the order that Pester displays exception failures to
# match the order the corresponding assertions are mentioned.
function Add-InnerMostException
{
param
(
[Parameter(ValueFromPipeline,
Mandatory)]
[AllowNull()]
[Exception]
@alx9r
alx9r / shadowing.ps1
Created February 11, 2018 02:18
Proof-of-concept of the the shadowing required to safely mock a PowerShell function in a module.
Get-Module Pester,ModuleUnderTest | Remove-Module
$PSModuleAutoLoadingPreference = 'None'
<#
This is a proof-of-concept of the shadowing required
to safely mock a function in a module.
This is achieved by first creating the mock function in a
new, temporary scope in the module. (The mock function the
temporary scope merely "shadows" rather than overwrites the
@alx9r
alx9r / honorCallerPrefs.ps1
Created February 28, 2018 17:35
Proof-of-concept of a utility module that simplifies honoring user preferences and common parameters throughout a module.
New-Module HonorCallerPrefs {
# Utility module for honoring user preference variables
# and common parameters.
<#
This ErrorActionPrefrence affects only execution in this module.
All errors arising in this utility module represent bugs and
should therefore throw exceptions and their cause fixed.
#>
@alx9r
alx9r / disposeLocalVariable.ps1
Last active March 6, 2018 23:15
Attempt at ensuring Dispose() is called on an advanced function's local variable on stop signal.
# Attempt at answering https://stackoverflow.com/questions/46714132
# "How can I ensure Dispose() is called on an advanced function's local variable on stop signal?"
# an IDisposable stub object for testing
class f : System.IDisposable {
Dispose() { Write-Host 'disposed' }
}
function g {
@alx9r
alx9r / invokeUsingObject.ps1
Last active September 19, 2022 21:12
Proof-of-concept of an error- and stop-safe cleanup pattern.
# Proof-of-concept of an error- and stop-safe cleanup pattern.
<#
Note that accepting pipeline input for these functions is not supported
because per https://gist.github.com/alx9r/f81cf64f50a016edda4e92968bdb9c3b
* there is no way to ensure cleanup when an upstream command breaks, throws,
continues, or throws a statement-terminating error, and
* there is no way to determine from a mid-pipeline command whether an exception
thrown downstream will cause the end of the invokation or will be swallowed by
an upstream command.
@alx9r
alx9r / errorTest.ps1
Last active March 16, 2018 00:53
test whether exceptions are thrown in different PowerShell error scenarios
Add-Type '
using System;
using System.Management.Automation;
[Cmdlet("Invoke", "ThrowTerminatingError")]
public class InvokeThrowTerminatingError : Cmdlet
{
[Parameter(ValueFromPipeline=true)]
public object InputObject { get; set; }
@alx9r
alx9r / searchPowerShell6419.ps1
Created March 20, 2018 15:23
search code snippet mentioned in PowerShell/PowerShell#6419
Get-Module -ListAvailable |
? {$_.Path -like '*System32*' } |
% { Get-Command -Module $_ } |
% {
$command = $_
[pscustomobject]@{
Command = $command
MOdule = $command.Module
Parameter = $command |
% {
@alx9r
alx9r / NestedMock.Tests.ps1
Created April 6, 2018 20:50
Example of mocking in nested modules for pester/Pester#1017
Import-Module NestedMock -Force
Describe 'nested mock' {
InModuleScope NestedMock1 {
Mock f2 -Verifiable { Write-Host 'mock called' }
f1
It 'invoked mock' {
Assert-MockCalled f2
}
}