Skip to content

Commit 381763b

Browse files
author
Kapil Borle
committed
Update UseShouldProcessCorrectly rule tests
1 parent 14ec9c3 commit 381763b

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

Tests/Rules/UseShouldProcessCorrectly.tests.ps1

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Foo
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 = @'
6060
function Foo
6161
{
@@ -80,7 +80,7 @@ function Bar
8080
Foo
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
113113
Foo
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 = @'
123123
function 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 = @'
148148
function 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 = @'
163177
function 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 = @'
177204
function Install-ModuleWithDeps {
178-
[CmdletBinding(SupportsShouldProcess)]
205+
[CmdletBinding(SupportsShouldProcess)]
179206
Param(
180207
[Parameter(ValueFromPipeline)]
181208
[string] $ModuleName

0 commit comments

Comments
 (0)