Enable-CommandCoverage.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
function Enable-CommandCoverage
{
    <#
    .Synopsis
        Enables command coverage for a module
    .Description
        Enables command coverage for a PowerShell module.
 
        Command coverage tracks which functions are called, and which parameters are used.
    .Example
        Test-Module -Name ScriptCop -GetCommandCoverage # this will Enable-CommandCoverage
    .Example
        Enable-CommandCoverage -Module ScriptCop
        Test-Command -ScriptBlock {
            function foo() {
            }
        }
        Get-CommandCoverage -Module ScriptCop
        Disable-CommandCoverage -Module ScriptCop
    .Link
        Disable-CommandCoverage
    .Link
        Test-Module
    #>

    [OutputType([Nullable])]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidGlobalVars", "", Justification="This needs to be global")]
    param(
    # The name of the module that will be instrumented for command coverage
    [Parameter(Mandatory=$true,Position=0,ValueFromPipelineByPropertyName=$true)]
    [Alias('Name')]
    [string]
    $Module
    )

    process {
        $Global:CommandCoverage = @{}
        #region Initialize Debugger Action
        $commandDebuggerAction = {
            $callstack = Get-PSCallStack
            $calledCommand = $callstack[1].InvocationInfo.InvocationName
            $commandParameters= $callstack[1].InvocationInfo.BoundParameters
            if (-not $global:CommandCoverage[$calledCommand]) {
                $global:CommandCoverage[$calledCommand] = New-Object Collections.ArrayList
            }
            $null = $global:CommandCoverage[$calledCommand].AddRange(@($commandParameters.Keys))
        }
        #endregion Initialize Debugger Action

        #region Create Command Breakpoints
        $moduleCommands = Get-Command -Module $module -commandType Function

        $null = @($moduleCommands | Where-Object { $_.CommandType -eq 'Function' } |
            ForEach-Object {
                Set-PSBreakpoint -Command $_ -Action $commandDebuggerAction
            })
        #endregion Create Command Breakpoints
    }


}