@@ -23,38 +23,101 @@ Describe "UseShouldProcessCorrectly" {
23
23
}
24
24
}
25
25
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 " {
28
28
$scriptDef = @'
29
- function Outer
29
+ function Foo
30
30
{
31
31
[CmdletBinding(SupportsShouldProcess=$true)]
32
32
param()
33
33
34
- Inner
34
+ Bar
35
35
}
36
36
37
- function Inner
37
+ function Bar
38
38
{
39
39
[CmdletBinding(SupportsShouldProcess=$true)]
40
40
param()
41
41
42
- if ($PSCmdlet.ShouldProcess("Inner "))
42
+ if ($PSCmdlet.ShouldProcess(""))
43
43
{
44
- Write-Host "Process! "
44
+ "Continue normally... "
45
45
}
46
46
else
47
47
{
48
- Write-Host "Skipped! "
48
+ "what would happen... "
49
49
}
50
50
}
51
51
52
- Outer -WhatIf
52
+ Foo
53
53
'@
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
56
56
}
57
57
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" {
58
121
It " finds no violation" {
59
122
$scriptDef = @'
60
123
function Foo
@@ -63,19 +126,99 @@ function Foo
63
126
param()
64
127
begin
65
128
{
66
- function helper
129
+ function Bar
67
130
{
68
131
if ($PSCmdlet.ShouldProcess('',''))
69
132
{
70
133
71
134
}
72
135
}
73
- helper
136
+ bar
74
137
}
75
138
}
76
139
'@
77
140
$violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDef - IncludeRule PSShouldProcess
78
141
$violations.Count | Should Be 0
79
142
}
80
143
}
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
+ }
81
224
}
0 commit comments