-
Notifications
You must be signed in to change notification settings - Fork 22
/
Get-DriveSize.Tests.ps1
110 lines (96 loc) · 4.04 KB
/
Get-DriveSize.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
$Here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace('.Tests.', '.')
. "$here\$sut"
"$here\$sut"
Import-Module PSScriptAnalyzer
$Rules = Get-ScriptAnalyzerRule
$Name = $sut.Split('.')[0]
Describe 'Script Analyzer Tests' {
Context 'Testing $sut for Standard Processing' {
foreach ($rule in $rules.Where{$_.RuleName -ne 'PSAvoidUsingWMICmdlet'}) {
$i = $rules.IndexOf($rule)
It "passes the PSScriptAnalyzer Rule number $i - $rule " {
(Invoke-ScriptAnalyzer -Path "$here\$sut" -IncludeRule $rule.RuleName ).Count | Should Be 0
}
}
}
}
Describe 'Tests For Help' {
# The module-qualified command fails on Microsoft.PowerShell.Archive cmdlets
$Help = Get-Help $Name -ErrorAction SilentlyContinue
# If help is not found, synopsis in auto-generated help is the syntax diagram
It "should not be auto-generated" {
$Help.Synopsis | Should Not Match '*[<CommonParameters>]*'
}
# Should be a description for every function
It "gets description for $Name" {
$Help.Description | Should Not BeNullOrEmpty
}
# Should be at least one example
It "gets example code from $Name" {
($Help.Examples.Example | Select-Object -First 1).Code | Should Not BeNullOrEmpty
}
# Should be at least one example description
It "gets example help from $Name" {
($Help.Examples.Example.Remarks | Select-Object -First 1).Text | Should Not BeNullOrEmpty
}
Context "Test parameter help for $Name" {
$Common = 'Debug', 'ErrorAction', 'ErrorVariable', 'InformationAction', 'InformationVariable', 'OutBuffer', 'OutVariable',
'PipelineVariable', 'Verbose', 'WarningAction', 'WarningVariable'
$command = Get-Command $name
$parameters = $command.ParameterSets.Parameters | Sort-Object -Property Name -Unique | Where-Object { $_.Name -notin $common }
$parameterNames = $parameters.Name
$HelpParameterNames = $Help.Parameters.Parameter.Name | Sort-Object -Unique
foreach ($parameter in $parameters)
{
$parameterName = $parameter.Name
$parameterHelp = $Help.parameters.parameter | Where-Object Name -EQ $parameterName
# Should be a description for every parameter
It "gets help for parameter: $parameterName : in $Name" {
$parameterHelp.Description.Text | Should Not BeNullOrEmpty
}
# Required value in Help should match IsMandatory property of parameter
It "help for $parameterName parameter in $Name has correct Mandatory value" {
$codeMandatory = $parameter.IsMandatory.toString()
$parameterHelp.Required | Should Be $codeMandatory
}
# Parameter type in Help should match code
It "help for $Name has correct parameter type for $parameterName" {
$codeType = $parameter.ParameterType.Name
# To avoid calling Trim method on a null object.
$helpType = if ($parameterHelp.parameterValue) { $parameterHelp.parameterValue.Trim() }
$helpType | Should be $codeType
}
}
foreach ($helpParm in $HelpParameterNames)
{
# Shouldn't find extra parameters in help.
It "finds help parameter in code: $helpParm" {
$helpParm -in $parameterNames | Should Be $true
}
}
}
}
Describe "$Name Tests"{
Context 'Function' {
BeforeAll {
$Name= Get-Command $Name
}
It 'Has Cmdlet Binding set to true' {
$Name.CmdletBinding |should Be True
}
It 'Has a Server Parameter'{
($Name.Parameters['Server']) | Should Be $true
}
It 'Server Parameter shoud be a string' {
$Name.Parameters['Server'].ParameterType | Should Be String
}
It 'Server Parameter should default to $Env:COMPUTERNAME' {
# $Name.Parameters['Server']
}
}
Context 'Output' {
It 'Should Output The Drive Letters' {
}
}
}