55
55
$violations.Count | Should Be 0
56
56
}
57
57
58
- It " finds no violation if downstream function does not declare SupportsShouldProcess" {
58
+ It " finds violation if downstream function does not declare SupportsShouldProcess" {
59
59
$scriptDef = @'
60
60
function Foo
61
61
{
@@ -80,7 +80,7 @@ function Bar
80
80
Foo
81
81
'@
82
82
$violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDef - IncludeRule PSShouldProcess
83
- $violations.Count | Should Be 0
83
+ $violations.Count | Should Be 1
84
84
}
85
85
86
86
It " finds no violation for 2 level downstream calls" {
@@ -113,11 +113,11 @@ function Bar
113
113
Foo
114
114
'@
115
115
$violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDef - IncludeRule PSShouldProcess
116
- $violations.Count | Should Be 0
116
+ $violations.Count | Should Be 1
117
117
}
118
118
}
119
119
120
- Context " When downstream function is defined locally in a function scope " {
120
+ Context " When nested function definition calls ShouldProcess " {
121
121
It " finds no violation" {
122
122
$scriptDef = @'
123
123
function Foo
@@ -142,8 +142,8 @@ function Foo
142
142
}
143
143
}
144
144
145
- Context " When a builtin command with SupportsShouldProcess is called" {
146
- It " finds no violation for a cmdlet" {
145
+ Context " When a builtin command that supports ShouldProcess is called" {
146
+ It " finds no violation when caller declares SupportsShouldProcess and callee is a cmdlet" {
147
147
$scriptDef = @'
148
148
function Remove-Foo {
149
149
[CmdletBinding(SupportsShouldProcess)]
@@ -158,7 +158,21 @@ function Remove-Foo {
158
158
$violations.Count | Should Be 0
159
159
}
160
160
161
- It " finds no violation for a function" {
161
+ It " finds no violation when caller does not declare SupportsShouldProcess and callee is a cmdlet" {
162
+ $scriptDef = @'
163
+ function Remove-Foo {
164
+ Param(
165
+ [string] $Path
166
+ )
167
+ Write-Verbose "Removing $($path)"
168
+ Remove-Item -Path $Path
169
+ }
170
+ '@
171
+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDef - IncludeRule PSShouldProcess
172
+ $violations.Count | Should Be 0
173
+ }
174
+
175
+ It " finds no violation when caller declares SupportsShouldProcess and callee is a function" {
162
176
$scriptDef = @'
163
177
function Install-Foo {
164
178
[CmdletBinding(SupportsShouldProcess)]
@@ -167,6 +181,19 @@ function Install-Foo {
167
181
)
168
182
Install-Module $ModuleName
169
183
}
184
+ '@
185
+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDef - IncludeRule PSShouldProcess
186
+ $violations.Count | Should Be 0
187
+ }
188
+
189
+ It " finds no violation when caller does not declare SupportsShouldProcess and callee is a function" {
190
+ $scriptDef = @'
191
+ function Install-Foo {
192
+ Param(
193
+ [string] $ModuleName
194
+ )
195
+ Install-Module $ModuleName
196
+ }
170
197
'@
171
198
$violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDef - IncludeRule PSShouldProcess
172
199
$violations.Count | Should Be 0
@@ -175,7 +202,7 @@ function Install-Foo {
175
202
It " finds no violation for a function with self reference" {
176
203
$scriptDef = @'
177
204
function Install-ModuleWithDeps {
178
- [CmdletBinding(SupportsShouldProcess)]
205
+ [CmdletBinding(SupportsShouldProcess)]
179
206
Param(
180
207
[Parameter(ValueFromPipeline)]
181
208
[string] $ModuleName
0 commit comments