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
function Verify | |
{ | |
param | |
( | |
[Parameter(ValueFromPipeline, | |
Position = 1, | |
Mandatory)] | |
$ExpressionResult | |
) | |
process |
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
function IsUnrolledByPipeline | |
{ | |
param | |
( | |
[Parameter(Mandatory, | |
Position=1)] | |
[AllowNull()] | |
[object] | |
$InputObject | |
) |
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 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] |
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,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 |
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
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. | |
#> |
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
# 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 { |
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
# 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. |
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
Add-Type ' | |
using System; | |
using System.Management.Automation; | |
[Cmdlet("Invoke", "ThrowTerminatingError")] | |
public class InvokeThrowTerminatingError : Cmdlet | |
{ | |
[Parameter(ValueFromPipeline=true)] | |
public object InputObject { get; set; } |
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 -ListAvailable | | |
? {$_.Path -like '*System32*' } | | |
% { Get-Command -Module $_ } | | |
% { | |
$command = $_ | |
[pscustomobject]@{ | |
Command = $command | |
MOdule = $command.Module | |
Parameter = $command | | |
% { |
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 NestedMock -Force | |
Describe 'nested mock' { | |
InModuleScope NestedMock1 { | |
Mock f2 -Verifiable { Write-Host 'mock called' } | |
f1 | |
It 'invoked mock' { | |
Assert-MockCalled f2 | |
} | |
} |