22# Licensed under the MIT License.
33
44Describe" ShouldProcess respects VerbosePreference" - Tags" CI" , " Feature" {
5-
5+
66 BeforeAll {
77function Test-ShouldProcess {
88 [CmdletBinding (SupportsShouldProcess )]
@@ -13,34 +13,60 @@ Describe "ShouldProcess respects VerbosePreference" -Tags "CI","Feature" {
1313
1414 Context" VerbosePreference variable" {
1515 It" Emits verbose output when VerbosePreference is Continue" {
16- $VerbosePreference = ' Continue'
17- $output = Test-ShouldProcess 4>&1
18- $VerbosePreference = ' SilentlyContinue'
19-
20- $output | Where-Object {$_ -eq " OK" }| Should-Not - BeNullOrEmpty
21- $verboseOutput = $output | Where-Object {$_ -is [System.Management.Automation.VerboseRecord ] }
22- $verboseOutput | Should-Not - BeNullOrEmpty
23- $verboseOutput.Message | Should-Match " TestAction.*TestTarget"
16+ $originalVerbosePreference = $VerbosePreference
17+ try {
18+ $VerbosePreference = ' Continue'
19+ $output = Test-ShouldProcess 4>&1
20+
21+ $output | Where-Object {$_ -eq " OK" }| Should-Not - BeNullOrEmpty
22+ $verboseOutput = $output | Where-Object {$_ -is [System.Management.Automation.VerboseRecord ] }
23+ $verboseOutput | Should-Not - BeNullOrEmpty
24+ $verboseOutput.Message | Should-Match " TestAction.*TestTarget"
25+ }
26+ finally {
27+ $VerbosePreference = $originalVerbosePreference
28+ }
2429 }
2530
2631 It" Does not emit verbose output when VerbosePreference is SilentlyContinue" {
27- $VerbosePreference = ' SilentlyContinue'
28- $output = Test-ShouldProcess 4>&1
29-
30- $output | Where-Object {$_ -eq " OK" }| Should-Not - BeNullOrEmpty
31- $verboseOutput = $output | Where-Object {$_ -is [System.Management.Automation.VerboseRecord ] }
32- $verboseOutput | Should- BeNullOrEmpty
32+ $originalVerbosePreference = $VerbosePreference
33+ try {
34+ $VerbosePreference = ' SilentlyContinue'
35+ $output = Test-ShouldProcess 4>&1
36+
37+ $output | Where-Object {$_ -eq " OK" }| Should-Not - BeNullOrEmpty
38+ $verboseOutput = $output | Where-Object {$_ -is [System.Management.Automation.VerboseRecord ] }
39+ $verboseOutput | Should- BeNullOrEmpty
40+ }
41+ finally {
42+ $VerbosePreference = $originalVerbosePreference
43+ }
3344 }
3445 }
3546
3647 Context" Verbose parameter" {
3748 It" Emits verbose output when -Verbose is specified" {
3849$output = Test-ShouldProcess - Verbose4>&1
39-
50+
4051$output | Where-Object {$_ -eq " OK" }| Should-Not - BeNullOrEmpty
4152$verboseOutput = $output | Where-Object {$_ -is [System.Management.Automation.VerboseRecord ] }
4253$verboseOutput | Should-Not - BeNullOrEmpty
4354$verboseOutput.Message | Should-Match " TestAction.*TestTarget"
4455 }
56+
57+ It" Does not emit verbose output when -Verbose:`$ false is specified even if VerbosePreference is Continue" {
58+ $originalVerbosePreference = $VerbosePreference
59+ try {
60+ $VerbosePreference = ' Continue'
61+ $output = Test-ShouldProcess - Verbose:$false 4>&1
62+
63+ $output | Where-Object {$_ -eq " OK" }| Should-Not - BeNullOrEmpty
64+ $verboseOutput = $output | Where-Object {$_ -is [System.Management.Automation.VerboseRecord ] }
65+ $verboseOutput | Should- BeNullOrEmpty
66+ }
67+ finally {
68+ $VerbosePreference = $originalVerbosePreference
69+ }
70+ }
4571 }
4672}