@@ -23,38 +23,101 @@ Describe "UseShouldProcessCorrectly" {
2323 }
2424 }
2525
26- Context " Where ShouldProcess is called in nested function" {
27- It " finds no violation" {
26+ Context " Where ShouldProcess is called by a downstream function" {
27+ It " finds no violation for 1 level downstream call " {
2828 $scriptDef = @'
29- function Outer
29+ function Foo
3030{
3131 [CmdletBinding(SupportsShouldProcess=$true)]
3232 param()
3333
34- Inner
34+ Bar
3535}
3636
37- function Inner
37+ function Bar
3838{
3939 [CmdletBinding(SupportsShouldProcess=$true)]
4040 param()
4141
42- if ($PSCmdlet.ShouldProcess("Inner "))
42+ if ($PSCmdlet.ShouldProcess(""))
4343 {
44- Write-Host "Process! "
44+ "Continue normally... "
4545 }
4646 else
4747 {
48- Write-Host "Skipped! "
48+ "what would happen... "
4949 }
5050}
5151
52- Outer -WhatIf
52+ Foo
5353'@
54- $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDef - IncludeRule PSShouldProcess
55- $violations.Count | Should Be 0
54+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDef - IncludeRule PSShouldProcess
55+ $violations.Count | Should Be 0
5656 }
5757
58+ It " finds no violation if downstream function does not declare SupportsShouldProcess" {
59+ $scriptDef = @'
60+ function Foo
61+ {
62+ [CmdletBinding(SupportsShouldProcess=$true)]
63+ param()
64+
65+ Bar
66+ }
67+
68+ function Bar
69+ {
70+ if ($PSCmdlet.ShouldProcess(""))
71+ {
72+ "Continue normally..."
73+ }
74+ else
75+ {
76+ "what would happen..."
77+ }
78+ }
79+
80+ Foo
81+ '@
82+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDef - IncludeRule PSShouldProcess
83+ $violations.Count | Should Be 0
84+ }
85+
86+ It " finds no violation for 2 level downstream calls" {
87+ $scriptDef = @'
88+ function Foo
89+ {
90+ [CmdletBinding(SupportsShouldProcess=$true)]
91+ param()
92+
93+ Baz
94+ }
95+
96+ function Baz
97+ {
98+ Bar
99+ }
100+
101+ function Bar
102+ {
103+ if ($PSCmdlet.ShouldProcess(""))
104+ {
105+ "Continue normally..."
106+ }
107+ else
108+ {
109+ "what would happen..."
110+ }
111+ }
112+
113+ Foo
114+ '@
115+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDef - IncludeRule PSShouldProcess
116+ $violations.Count | Should Be 0
117+ }
118+ }
119+
120+ Context " When downstream function is defined locally in a function scope" {
58121 It " finds no violation" {
59122 $scriptDef = @'
60123function Foo
@@ -63,19 +126,99 @@ function Foo
63126 param()
64127 begin
65128 {
66- function helper
129+ function Bar
67130 {
68131 if ($PSCmdlet.ShouldProcess('',''))
69132 {
70133
71134 }
72135 }
73- helper
136+ bar
74137 }
75138}
76139'@
77140 $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDef - IncludeRule PSShouldProcess
78141 $violations.Count | Should Be 0
79142 }
80143 }
144+
145+ Context " When a builtin command with SupportsShouldProcess is called" {
146+ It " finds no violation for a cmdlet" {
147+ $scriptDef = @'
148+ function Remove-Foo {
149+ [CmdletBinding(SupportsShouldProcess)]
150+ Param(
151+ [string] $Path
152+ )
153+ Write-Verbose "Removing $($path)"
154+ Remove-Item -Path $Path
155+ }
156+ '@
157+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDef - IncludeRule PSShouldProcess
158+ $violations.Count | Should Be 0
159+ }
160+
161+ It " finds no violation for a function" {
162+ $scriptDef = @'
163+ function Install-Foo {
164+ [CmdletBinding(SupportsShouldProcess)]
165+ Param(
166+ [string] $ModuleName
167+ )
168+ Install-Module $ModuleName
169+ }
170+ '@
171+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDef - IncludeRule PSShouldProcess
172+ $violations.Count | Should Be 0
173+ }
174+
175+ It " finds no violation for a function with self reference" {
176+ $scriptDef = @'
177+ function Install-ModuleWithDeps {
178+ [CmdletBinding(SupportsShouldProcess)]
179+ Param(
180+ [Parameter(ValueFromPipeline)]
181+ [string] $ModuleName
182+ )
183+ if ($PSCmdlet.ShouldProcess("Install module with dependencies"))
184+ {
185+ Get-Dependencies $ModuleName | Install-ModuleWithDeps
186+ Install-ModuleCustom $ModuleName
187+ }
188+ else
189+ {
190+ Get-Dependencies $ModuleName | Install-ModuleWithDeps
191+ Write-Host ("Would install module {0}" -f $ModuleName)
192+ }
193+ }
194+ '@
195+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDef - IncludeRule PSShouldProcess
196+ $violations.Count | Should Be 0
197+ }
198+
199+ It " finds no violation for a function with self reference and implicit call to ShouldProcess" {
200+ $scriptDef = @'
201+ function Install-ModuleWithDeps {
202+ [CmdletBinding(SupportsShouldProcess)]
203+ Param(
204+ [Parameter(ValueFromPipeline)]
205+ [string] $ModuleName
206+ )
207+ $deps = Get-Dependencies $ModuleName
208+ if ($deps -eq $null)
209+ {
210+ Install-Module $ModuleName
211+ }
212+ else
213+ {
214+ $deps | Install-ModuleWithDeps
215+ }
216+ Install-Module $ModuleName
217+ }
218+ '@
219+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDef - IncludeRule PSShouldProcess
220+ $violations.Count | Should Be 0
221+ }
222+
223+ }
81224}
0 commit comments