Tests/Module.Tests.ps1

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
91
92
93
94
95
Set-StrictMode -Version Latest

$TestScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
$ModuleRoot = Resolve-Path "$TestScriptRoot\.."
$ModuleManifest = "$ModuleRoot\CimSweep.psd1"

Remove-Module [C]imSweep
$Module = Import-Module $ModuleManifest -Force -ErrorAction Stop -PassThru

Describe 'Module-wide tests' -Tags 'Module' {
    $FunctionsList = $Module.ExportedCommands.Keys
    
    foreach ($Function in $FunctionsList)
    {
        # Retrieve the Help of the function
        $Help = Get-Help -Name $Function -Full
        
        # Parse the function using AST
        $AST = [Management.Automation.Language.Parser]::ParseInput((Get-Content function:$Function), [ref]$null, [ref]$null)
        
        Context "Exported command parameters for $Function" {
            It 'should have have an OutputType attribute' {
                $OutputTypeAttribute = $AST.ParamBlock.Attributes | Where-Object { $_.TypeName.Name -eq 'OutputType' }
                $OutputTypeAttribute | Should Not BeNullOrEmpty
                $OutputTypeAttribute.TypeName.Name | Should BeExactly 'OutputType'
                $OutputTypeAttribute.PositionalArguments | Should Not BeNullOrEmpty
            }

            It 'should have a -CimSession parameter with proper capitalization and is of type Microsoft.Management.Infrastructure.CimSession[]' {
                $CimSessionParam = $AST.ParamBlock.Parameters | Where-Object { $_.Name.Extent.Text -eq '$CimSession' }
                $CimSessionParam | Should Not BeNullOrEmpty
                $CimSessionParam.Name.Extent.Text | Should BeExactly '$CimSession'
                $CimSessionParam.StaticType.FullName | Should BeExactly 'Microsoft.Management.Infrastructure.CimSession[]'
            }
        }

        Context "Required exported command naming scheme for $Function" {
            It 'should have a "CS" noun prefix' {
                $Module.ExportedCommands[$Function].Noun.Substring(0, 2) | Should BeExactly 'CS'
            }
        }

        # Comment-based help tests derived from:
        # http://www.lazywinadmin.com/2016/05/using-pester-to-test-your-comment-based.html
        Context "Comment-based help for: $Function"{
            It 'should contain a .SYNOPSIS block' {
                $Help.Synopsis | Should Not BeNullOrEmpty
            }

            It 'should have an author listed' {
                $Help.Synopsis.Contains('Author:') | Should Be $True
            }

            It 'should contain a BSD license in the synopsis' {
                $Help.Synopsis.Contains('License: BSD 3-Clause') | Should Be $True
            }

            It 'should contain a .DESCRIPTION block' {
                $Help.Description | Should Not BeNullOrEmpty
            }
            
            # Get the parameters declared in the Comment Based Help
            $HelpParameters = @($Help.Parameters.Parameter)
            
            # Get the parameters declared in the AST PARAM() Block
            $ASTParameters = @($AST.ParamBlock.Parameters.Name.Variablepath.Userpath)
            
            It 'should contain a matching number of .PARAMETER blocks for all defined parameters' {
                $NamedArgs = try { $AST.ParamBlock.Attributes.NamedArguments } catch { $null }

                if ($NamedArgs -and $NamedArgs.ArgumentName -contains 'SupportsShouldProcess') {
                    $Count = $ASTParameters.Count + 2 # Accounting for -WhatIf and -Confirm
                } else {
                    $Count = $ASTParameters.Count
                }

                $HelpParameters.Count | Should Be $Count
            }
            
            # Parameter Description
            $Help.Parameters.Parameter | ForEach-Object {
                if ($ASTParameters -contains $_.Name) {
                    It "should contain a .PARAMETER block for the following parameter: $($_.Name)"{
                        $_.Description | Should Not BeNullOrEmpty
                    }
                }
            }
            
            # Examples
            It 'should contain at least one .EXAMPLE block' {
                @($Help.Examples.Example.Code).Count | Should BeGreaterThan 0
            }
        }
    }
}