PSPesterTestHelpers.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
function Assert-PTHMandatoryParameter {
    <#
    .SYNOPSIS
    Asserts that a function has only the mandatory parameters we pass.
    .DESCRIPTION
    Asserts that a function has only the mandatory parameters we pass. Returns true if it does and false otherwise.
    .EXAMPLE
    Assert-MandatoryParameter -FunctionName 'Get-MyFunction' -ParameterName 'Name'
 
    Asserts that the function 'Get-MyFunction' has only one mandatory parameter called 'Name'.
    .NOTES
    Author : Paul Broadwith https://github.com/pauby
    History: 2019-01-09 - pauby - Initial release
    .LINK
    https://github.com/pauby/pspestertesthelpers/blob/master/docs/Get-PTHMandatoryParameter.md
#>

    [OutputType([System.Boolean])]
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory)]
        [string]
        $FunctionName,

        [Parameter(Mandatory)]
        [string[]]
        $ParameterName
    )

    # this will find a functions mandatory parameters that are not in the $ParameterName array.
    # if anything is found then it means the mandatory parameters we're testing for are different.
    $unknownParameters = Get-PHFunctionParameter -Name $FunctionName | Where-Object {
        $_.Value.Attributes.Mandatory -eq $true -and $ParameterName -notcontains $_.Key
    }

    # if $unknownParameters is 0 then that is $false so we must negate it to mean $true as not finding any parameters different is a pass
    -not [bool]$unknownParameters
}
function Get-PTHFunctionParameter {
    <#
    .SYNOPSIS
        Gets the parameters and their properties for a specified function.
    .DESCRIPTION
        Gets the parameters and their properties for a specified function. The
        function must exist within the 'Function:' provider so will not work on
        cmdlets.
    .EXAMPLE
        Get-PTHFunctionParameter -Name 'Get-FunctionParameters'
 
        Returns the properties of each function parameter for the 'Import-Module'
        function excluding the common parameters for Advanced Functions.
    .EXAMPLE
        Get-PTHFunctionParameter -Name 'Get-FunctionParameters' -Exclude ''
 
        Returns the properties of each function parameter for the 'Import-Module'
        function excluding no parameter names.
    .NOTES
        Author : Paul Broadwith (https://github.com/pauby)
        History : 2018-03-17 - pauby - Initial release
    .LINK
        https://github.com/pauby/pspestertesthelpers/blob/master/docs/Get-PTHFunctionParameter.md
    #>

    [CmdletBinding()]
    Param (
        # Name of the function. The function must exist within the 'Function:'
        # provider or an exception will be thrown.
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]
        $Name,

        # Array of parameter names to exclude. By default the Advanced Functions
        # common parameters are excluded. Pass an empty array to have all
        # parameters returned.
        [AllowEmptyCollection()]
        [string[]]
        $Exclude = @('Verbose', 'Debug', 'ErrorAction', 'WarningAction', 'InformationAction', 'ErrorVariable', 'WarningVariable', 'InformationVariable', 'OutVariable', 'OutBuffer', 'PipelineVariable')
    )

    if (-not $PSBoundParameters.ContainsKey('Verbose')) {
        $VerbosePreference = $PSCmdlet.GetVariableValue('VerbosePreference')
    }

    try {
        (Get-Item -Path "Function:\$Name").Parameters.GetEnumerator() | Where-Object { $Exclude -notcontains $_.key}
    }
    catch {
        throw "Cannot find function '$Name' loaded in the current session."
    }
}