5555 $violations.Count | Should Be 0
5656 }
5757
58- It " finds no violation if downstream function does not declare SupportsShouldProcess" {
58+ It " finds violation if downstream function does not declare SupportsShouldProcess" {
5959 $scriptDef = @'
6060function Foo
6161{
@@ -80,7 +80,7 @@ function Bar
8080Foo
8181'@
8282 $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDef - IncludeRule PSShouldProcess
83- $violations.Count | Should Be 0
83+ $violations.Count | Should Be 1
8484 }
8585
8686 It " finds no violation for 2 level downstream calls" {
@@ -113,11 +113,11 @@ function Bar
113113Foo
114114'@
115115 $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDef - IncludeRule PSShouldProcess
116- $violations.Count | Should Be 0
116+ $violations.Count | Should Be 1
117117 }
118118 }
119119
120- Context " When downstream function is defined locally in a function scope " {
120+ Context " When nested function definition calls ShouldProcess " {
121121 It " finds no violation" {
122122 $scriptDef = @'
123123function Foo
@@ -142,8 +142,8 @@ function Foo
142142 }
143143 }
144144
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" {
147147 $scriptDef = @'
148148function Remove-Foo {
149149[CmdletBinding(SupportsShouldProcess)]
@@ -158,7 +158,21 @@ function Remove-Foo {
158158 $violations.Count | Should Be 0
159159 }
160160
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" {
162176 $scriptDef = @'
163177function Install-Foo {
164178[CmdletBinding(SupportsShouldProcess)]
@@ -167,6 +181,19 @@ function Install-Foo {
167181 )
168182 Install-Module $ModuleName
169183}
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+ }
170197'@
171198 $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDef - IncludeRule PSShouldProcess
172199 $violations.Count | Should Be 0
@@ -175,7 +202,7 @@ function Install-Foo {
175202 It " finds no violation for a function with self reference" {
176203 $scriptDef = @'
177204function Install-ModuleWithDeps {
178- [CmdletBinding(SupportsShouldProcess)]
205+ [CmdletBinding(SupportsShouldProcess)]
179206 Param(
180207 [Parameter(ValueFromPipeline)]
181208 [string] $ModuleName
0 commit comments