@@ -6,37 +6,176 @@ BeforeAll {
66}
77
88Describe " AvoidDefaultValueForMandatoryParameter" {
9- Context " When there are violations" {
10- It " has 1 provide default value for mandatory parameter violation with CmdletBinding" {
11- $violations = Invoke-ScriptAnalyzer - ScriptDefinition ' Function foo{ [CmdletBinding()]Param([Parameter(Mandatory)]$Param1='' defaultValue'' ) }' |
12- Where-Object { $_.RuleName -eq $ruleName }
9+
10+ Context " Basic mandatory parameter violations" {
11+ It " should flag mandatory parameter with default value (implicit)" {
12+ $script = ' Function Test { Param([Parameter(Mandatory)]$Param = "default") }'
13+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
14+ $violations.Count | Should - Be 1
15+ }
16+
17+ It " should flag mandatory parameter with default value (explicit true)" {
18+ $script = ' Function Test { Param([Parameter(Mandatory=$true)]$Param = "default") }'
19+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
20+ $violations.Count | Should - Be 1
21+ }
22+
23+ It " should flag mandatory parameter with default value (numeric true)" {
24+ $script = ' Function Test { Param([Parameter(Mandatory=1)]$Param = "default") }'
25+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
26+ $violations.Count | Should - Be 1
27+ }
28+ }
29+
30+ Context " Parameter sets (multiple Parameter attributes)" {
31+ It " should NOT flag parameter mandatory in some but not all parameter sets" {
32+ $script = @'
33+ Function Test {
34+ Param(
35+ [Parameter(Mandatory, ParameterSetName='Set1')]
36+ [Parameter(ParameterSetName='Set2')]
37+ $Param = 'default'
38+ )
39+ }
40+ '@
41+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
42+ $violations.Count | Should - Be 0
43+ }
44+
45+ It " should flag parameter mandatory in ALL parameter sets" {
46+ $script = @'
47+ Function Test {
48+ Param(
49+ [Parameter(Mandatory, ParameterSetName='Set1')]
50+ [Parameter(Mandatory, ParameterSetName='Set2')]
51+ $Param = 'default'
52+ )
53+ }
54+ '@
55+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
1356 $violations.Count | Should - Be 1
1457 }
1558
16- It " has 1 provide default value for mandatory=$true parameter violation without CmdletBinding" {
17- $violations = Invoke-ScriptAnalyzer - ScriptDefinition ' Function foo{ Param([Parameter(Mandatory=$true)]$Param1='' defaultValue'' ) }' |
18- Where-Object { $_.RuleName -eq $ruleName }
59+ It " should handle mixed mandatory/non-mandatory in multiple parameter sets" {
60+ $script = @'
61+ Function Test {
62+ Param(
63+ [Parameter(Mandatory=$true, ParameterSetName='Set1')]
64+ [Parameter(Mandatory=$false, ParameterSetName='Set2')]
65+ [Parameter(ParameterSetName='Set3')]
66+ $Param = 'default'
67+ )
68+ }
69+ '@
70+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
71+ $violations.Count | Should - Be 0
72+ }
73+ }
74+
75+ Context " Script-level param blocks" {
76+ It " should flag mandatory parameters with defaults in script-level param blocks" {
77+ $script = @'
78+ Param(
79+ [Parameter(Mandatory)]
80+ $ScriptParam = 'default'
81+ )
82+ '@
83+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
1984 $violations.Count | Should - Be 1
2085 }
2186
22- It " returns violations when the parameter is specified as mandatory=1 and has a default value" {
23- $violations = Invoke-ScriptAnalyzer - ScriptDefinition ' Function foo{ Param([Parameter(Mandatory=1)]$Param1='' defaultValue'' ) }' |
24- Where-Object { $_.RuleName -eq $ruleName }
87+ It " should NOT flag non-mandatory parameters in script-level param blocks" {
88+ $script = @'
89+ Param(
90+ [Parameter()]
91+ $ScriptParam = 'default'
92+ )
93+ '@
94+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
95+ $violations.Count | Should - Be 0
96+ }
97+ }
98+
99+ Context " Non-Parameter attributes" {
100+ It " should NOT flag non-Parameter attributes with Mandatory property" {
101+ $script = ' Function Test { Param([MyCustomAttribute(Mandatory)]$Param = "default") }'
102+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
103+ $violations.Count | Should - Be 0
104+ }
105+
106+ It " should NOT flag parameters with only validation attributes" {
107+ $script = ' Function Test { Param([ValidateNotNull()]$Param = "default") }'
108+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
109+ $violations.Count | Should - Be 0
110+ }
111+ }
112+
113+ Context " Valid scenarios (no violations)" {
114+ It " should NOT flag mandatory parameters without default values" {
115+ $script = ' Function Test { Param([Parameter(Mandatory)]$Param) }'
116+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
117+ $violations.Count | Should - Be 0
118+ }
119+
120+ It " should NOT flag non-mandatory parameters with default values" {
121+ $script = ' Function Test { Param([Parameter(Mandatory=$false)]$Param = "default") }'
122+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
123+ $violations.Count | Should - Be 0
124+ }
125+
126+ It " should NOT flag parameters without Parameter attributes" {
127+ $script = ' Function Test { Param($Param = "default") }'
128+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
129+ $violations.Count | Should - Be 0
130+ }
131+
132+ It " should NOT flag mandatory=0 parameters" {
133+ $script = ' Function Test { Param([Parameter(Mandatory=0)]$Param = "default") }'
134+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
135+ $violations.Count | Should - Be 0
136+ }
137+ }
138+
139+ Context " Complex scenarios" {
140+ It " should handle multiple parameters with mixed violations" {
141+ $script = @'
142+ Function Test {
143+ Param(
144+ [Parameter(Mandatory)]$BadParam = "default",
145+ [Parameter()]$GoodParam = "default",
146+ [Parameter(Mandatory)]$AnotherBadParam = "default"
147+ )
148+ }
149+ '@
150+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
151+ $violations.Count | Should - Be 2
152+ }
153+
154+ It " should work with CmdletBinding" {
155+ $script = ' Function Test { [CmdletBinding()]Param([Parameter(Mandatory)]$Param = "default") }'
156+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
25157 $violations.Count | Should - Be 1
26158 }
27159 }
28160
29- Context " When there are no violations " {
30- It " has 1 provide default value for mandatory parameter violation " {
31- $violations = Invoke-ScriptAnalyzer - ScriptDefinition ' Function foo { Param([Parameter(Mandatory=$false)]$Param1= '' val1 '' , [Parameter(Mandatory)]$Param2= '' val2 '' , $Param3= '' val3 '' ) } ' |
32- Where-Object { $_.RuleName -eq $ruleName }
161+ Context " Edge cases " {
162+ It " should handle empty param blocks gracefully " {
163+ $script = ' Function Test { Param() } '
164+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
33165 $violations.Count | Should - Be 0
34166 }
35167
36- It " returns no violations when the parameter is specified as mandatory=0 and has a default value" {
37- $violations = Invoke-ScriptAnalyzer - ScriptDefinition ' Function foo{ Param([Parameter(Mandatory=0)]$Param1='' val1'' ) }' |
38- Where-Object { $_.RuleName -eq $ruleName }
168+ It " should handle null/empty default values" {
169+ $script = ' Function Test { Param([Parameter(Mandatory)]$Param = $null) }'
170+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
171+ $violations.Count | Should - Be 1
172+ }
173+
174+ It " should handle parameters with multiple non-Parameter attributes" {
175+ $script = ' Function Test { Param([ValidateNotNull()][Alias("P")]$Param = "default") }'
176+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $script | Where-Object { $_.RuleName -eq $ruleName }
39177 $violations.Count | Should - Be 0
40178 }
41179 }
180+
42181}
0 commit comments