forked from PowerShell/PSScriptAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAvoidDefaultValueForMandatoryParameter.tests.ps1
More file actions
181 lines (159 loc) · 7.31 KB
/
AvoidDefaultValueForMandatoryParameter.tests.ps1
File metadata and controls
181 lines (159 loc) · 7.31 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
BeforeAll {
$ruleName = 'PSAvoidDefaultValueForMandatoryParameter'
}
Describe "AvoidDefaultValueForMandatoryParameter" {
Context "Basic mandatory parameter violations" {
It "should flag mandatory parameter with default value (implicit)" {
$script = 'Function Test { Param([Parameter(Mandatory)]$Param = "default") }'
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
$violations.Count | Should -Be 1
}
It "should flag mandatory parameter with default value (explicit true)" {
$script = 'Function Test { Param([Parameter(Mandatory=$true)]$Param = "default") }'
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
$violations.Count | Should -Be 1
}
It "should flag mandatory parameter with default value (numeric true)" {
$script = 'Function Test { Param([Parameter(Mandatory=1)]$Param = "default") }'
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
$violations.Count | Should -Be 1
}
}
Context "Parameter sets (multiple Parameter attributes)" {
It "should NOT flag parameter mandatory in some but not all parameter sets" {
$script = @'
Function Test {
Param(
[Parameter(Mandatory, ParameterSetName='Set1')]
[Parameter(ParameterSetName='Set2')]
$Param = 'default'
)
}
'@
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
$violations.Count | Should -Be 0
}
It "should flag parameter mandatory in ALL parameter sets" {
$script = @'
Function Test {
Param(
[Parameter(Mandatory, ParameterSetName='Set1')]
[Parameter(Mandatory, ParameterSetName='Set2')]
$Param = 'default'
)
}
'@
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
$violations.Count | Should -Be 1
}
It "should handle mixed mandatory/non-mandatory in multiple parameter sets" {
$script = @'
Function Test {
Param(
[Parameter(Mandatory=$true, ParameterSetName='Set1')]
[Parameter(Mandatory=$false, ParameterSetName='Set2')]
[Parameter(ParameterSetName='Set3')]
$Param = 'default'
)
}
'@
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
$violations.Count | Should -Be 0
}
}
Context "Script-level param blocks" {
It "should flag mandatory parameters with defaults in script-level param blocks" {
$script = @'
Param(
[Parameter(Mandatory)]
$ScriptParam = 'default'
)
'@
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
$violations.Count | Should -Be 1
}
It "should NOT flag non-mandatory parameters in script-level param blocks" {
$script = @'
Param(
[Parameter()]
$ScriptParam = 'default'
)
'@
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
$violations.Count | Should -Be 0
}
}
Context "Non-Parameter attributes" {
It "should NOT flag non-Parameter attributes with Mandatory property" {
$script = 'Function Test { Param([MyCustomAttribute(Mandatory)]$Param = "default") }'
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
$violations.Count | Should -Be 0
}
It "should NOT flag parameters with only validation attributes" {
$script = 'Function Test { Param([ValidateNotNull()]$Param = "default") }'
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
$violations.Count | Should -Be 0
}
}
Context "Valid scenarios (no violations)" {
It "should NOT flag mandatory parameters without default values" {
$script = 'Function Test { Param([Parameter(Mandatory)]$Param) }'
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
$violations.Count | Should -Be 0
}
It "should NOT flag non-mandatory parameters with default values" {
$script = 'Function Test { Param([Parameter(Mandatory=$false)]$Param = "default") }'
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
$violations.Count | Should -Be 0
}
It "should NOT flag parameters without Parameter attributes" {
$script = 'Function Test { Param($Param = "default") }'
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
$violations.Count | Should -Be 0
}
It "should NOT flag mandatory=0 parameters" {
$script = 'Function Test { Param([Parameter(Mandatory=0)]$Param = "default") }'
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
$violations.Count | Should -Be 0
}
}
Context "Complex scenarios" {
It "should handle multiple parameters with mixed violations" {
$script = @'
Function Test {
Param(
[Parameter(Mandatory)]$BadParam = "default",
[Parameter()]$GoodParam = "default",
[Parameter(Mandatory)]$AnotherBadParam = "default"
)
}
'@
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
$violations.Count | Should -Be 2
}
It "should work with CmdletBinding" {
$script = 'Function Test { [CmdletBinding()]Param([Parameter(Mandatory)]$Param = "default") }'
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
$violations.Count | Should -Be 1
}
}
Context "Edge cases" {
It "should handle empty param blocks gracefully" {
$script = 'Function Test { Param() }'
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
$violations.Count | Should -Be 0
}
It "should handle null/empty default values" {
$script = 'Function Test { Param([Parameter(Mandatory)]$Param = $null) }'
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
$violations.Count | Should -Be 1
}
It "should handle parameters with multiple non-Parameter attributes" {
$script = 'Function Test { Param([ValidateNotNull()][Alias("P")]$Param = "default") }'
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
$violations.Count | Should -Be 0
}
}
}