-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGet-CommandCoverage.ps1
108 lines (96 loc) · 9.36 KB
/
Get-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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
function Get-CommandCoverage
{
<#
.Synopsis
Gets Command Coverage
.Description
Gets code coverage of the commands within a module.
.Example
Get-CommandCoverage
.Link
Test-Module
.Link
Enable-CommandCoverage
.Link
Disable-CommandCoverage
#>
[OutputType([PSObject])]
[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 {
#region Caculate Command Coverage
# First, get all functions from the module.
$moduleCommands = Get-Command -Module $module -commandType Function
# Then, find the commands that were hit.
$commandsCovered = @($moduleCommands | Where-Object { $global:CommandCoverage.Contains("$_") } ) # Get the commands
# Then, find the commands that were not hit.
$missingCommands = @($moduleCommands | Where-Object { -not $global:CommandCoverage.Contains("$_") } )
$percentageCommandsCovered = # calculate the % command coverage.
$commandsCovered.Count / $moduleCommands.Count
#endregion Caculate Command Coverage
#region Caculate Parameter Coverage
$totalParameterCount = # Next, total up the number of parameters
$moduleCommands |
ForEach-Object { ([Management.Automation.CommandMetaData]$_).Parameters.Count} |
Measure-Object -Sum |
Select-Object -ExpandProperty Sum
$coveredParameterTotal =
$commandsCovered |
ForEach-Object { ([Management.Automation.CommandMetaData]$_).Parameters.Count} |
Measure-Object -Sum |
Select-Object -ExpandProperty Sum
$totalMissedParameters = 0
$totalCoveredParameters = 0
$specificCommandCoverage = $commandsCovered | # Next, walk thru each command
ForEach-Object {
$params = ([Management.Automation.CommandMetaData]$_).Parameters
$coverageData = @($global:CommandCoverage["$_"]) # find coverage data related to that command
# and see what parameters were missed.
$missedParameters = @($params.Keys | Where-Object { -not ($coverageData -eq $_) })
$totalMissedParameters += $missedParameters.Count
$coveredParams = @($coverageData | Group-Object -NoElement |
Select-Object Name, @{
Expression={$_.Count}
Name='TimesHit'
}) # Then summarize how often each parameter was used.
$totalCoveredParameters += $coveredParams.Count
# Pack all of this information into a property bag for the command.
$o = New-Object PSOBject -Property @{
Command = $_
CoveredParameters = $coveredParams
MissedParameters = $missedParameters
PercentageParameterCoverage =
try {
$coveredParams.Count * 100 / ($coveredParams.Count + $missedParameters.Count )
} catch { 1 }
}
# and decorate it as a 'ScriptCop.Command.Coverage'
$o.pstypenames.clear()
$o.pstypenames.add('ScriptCop.Command.Coverage')
$o
}
#endregion Caculate Parameter Coverage
$commandCoverageOutput = New-Object PSObject -Property @{
CommandsCovered = $commandsCovered
CoveredParameterTotal = $coveredParameterTotal
CoverageData = $specificCommandCoverage
PercentageCommandCoverage = $percentageCommandsCovered * 100
NumberOfCommandsCovered = $commandsCovered.Count
TotalNumberOfCommands = $moduleCommands.Count
NumberOfParametersCovered = $totalCoveredParameters
OverallParameterCoverage = $totalCoveredParameters * 100 / $totalParameterCount
ParameterCoverageInCoveredCommands = $totalCoveredParameters * 100 / $coveredParameterTotal
MissingCommands = $missingCommands
TotalNumberOfParameters = $totalParameterCount
}
$commandCoverageOutput.pstypenames.clear()
$commandCoverageOutput.pstypenames.add('ScriptCop.Command.Coverage.Report')
$commandCoverageOutput
}
}