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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Describe "Tests for (error, warning, etc) action preference" -Tags "CI" {
    $commonActionPreferenceParameterTestCases = foreach ($commonParameterName in [System.Management.Automation.Cmdlet]::CommonParameters | Select-String Action) {
        @{
            ActionPreferenceParameterName = $commonParameterName
        }
    }

    $actionPreferenceVariableTestCases = foreach ($variable in Get-Variable -Name *Preference -Scope Global | Where-Object Value -Is [System.Management.Automation.ActionPreference]) {
        @{
            ActionPreferenceVariableName = $variable.Name
            StreamName = $variable.Name -replace '(Action)?Preference$'
        }
    }

    $actionPreferenceVariableValueTestCases = @(
        @{
            Value = [System.Management.Automation.ActionPreference]::Suspend
            DisplayValue = '[System.Management.Automation.ActionPreference]::Suspend'
        }
        @{
            Value        = 'Suspend'
            DisplayValue = '''Suspend'''
        }
    )

    BeforeAll {
        $orgin = $GLOBAL:errorActionPreference

        function Join-TestCase {
            [OutputType([Hashtable[]])]
            [CmdletBinding()]
            param(
                [Hashtable[]]$Set1,
                [Hashtable[]]$Set2
            )
            foreach ($ht1 in $Set1) {
                foreach ($ht2 in $Set2) {
                    $ht1 + $ht2
                }
            }
        }

        function Test-ActionPreferenceVariableSuspendValue {
            [CmdletBinding()]
            param(
                $Value
            )
            if ($DebugPreference -eq $Value) {
                Write-Debug -Message 'A debug message'
            } elseif ($ErrorActionPreference -eq $Value) {
                Write-Error -Message 'An error message'
            } elseif ($InformationPreference -eq $Value) {
                Write-Information -MessageData 'Some information'
            } elseif ($ProgressPreference -eq $Value) {
                Write-Progress -Activity 'Some progress'
            } elseif ($VerbosePreference -eq $Value) {
                Write-Verbose -Message 'A verbose message'
            } elseif ($WarningPreference -eq $Value) {
                Write-Warning -Message 'A warning message'
            }
        }
    }

    AfterAll {
        if ($GLOBAL:errorActionPreference -ne $orgin) {
            $GLOBAL:errorActionPreference = $orgin
        }
    }

    Context 'Setting ErrorActionPreference to stop prevents user from getting the error exception' {
        $err = $null
        try {
            Get-ChildItem nosuchfile.nosuchextension -ErrorAction stop -ErrorVariable err
        } catch { }

        It '$err.Count' { $err.Count | Should -Be 1 }
        It '$err[0] should not be $null' { $err[0] | Should -Not -BeNullOrEmpty }
        It '$err[0].GetType().Name' { $err[0] | Should -BeOfType System.Management.Automation.ActionPreferenceStopException }
        It '$err[0].ErrorRecord' { $err[0].ErrorRecord | Should -Not -BeNullOrEmpty }
        It '$err[0].ErrorRecord.Exception.GetType().Name' { $err[0].ErrorRecord.Exception | Should -BeOfType System.Management.Automation.ItemNotFoundException }
    }

    It 'Action preference of Ignore can be set as a preference variable using a string value' {
        try {
            Remove-Variable -Name ErrorActionPreference -Scope Global -Force
            $GLOBAL:ErrorActionPreference = 'Ignore'
            $errorCount = $error.Count
            Get-Process -Name asdfasdfasdf
            $error.Count | Should -BeExactly $errorCount
        } finally {
            Remove-Variable -Name ErrorActionPreference -Scope Global
            # Re-create the action preference variable as a strongly typed variable like it was before
            [System.Management.Automation.ActionPreference]$GLOBAL:ErrorActionPreference = $orgin
        }
    }

    It 'Action preference of Ignore can be set as a preference variable using an enumerated value' {
        try {
            $GLOBAL:ErrorActionPreference = [System.Management.Automation.ActionPreference]::Ignore
            $errorCount = $error.Count
            Get-Process -Name asdfasdfasdf
            $error.Count | Should -BeExactly $errorCount
        } finally {
            $GLOBAL:ErrorActionPreference = $orgin
        }
    }

    It 'The $global:<ActionPreferenceVariableName> variable does not support Suspend' -TestCases $actionPreferenceVariableTestCases {
        param($ActionPreferenceVariableName)

        $e = {
            Set-Variable -Name $ActionPreferenceVariableName -Scope Global -Value ([System.Management.Automation.ActionPreference]::Suspend)
        } | Should -Throw -ErrorId RuntimeException -PassThru

        $e.CategoryInfo.Reason | Should -BeExactly 'ArgumentTransformationMetadataException'
    }

    It 'A local $<ActionPreferenceVariableName> variable does not support <DisplayValue>' -TestCases (Join-TestCase -Set1 $actionPreferenceVariableTestCases -Set2 $actionPreferenceVariableValueTestCases) {
        param(
            $ActionPreferenceVariableName,
            $StreamName,
            $Value,
            $DisplayValue
        )

        $e = {
            Set-Variable -Name $ActionPreferenceVariableName -Value $Value
            Test-ActionPreferenceVariableSuspendValue -Value $Value
        } | Should -Throw -ErrorId "System.NotSupportedException$(if ($StreamName -ne 'Error') {",Microsoft.PowerShell.Commands.Write${StreamName}Command"})" -PassThru

        $e.CategoryInfo.Reason | Should -BeExactly 'NotSupportedException'
    }

    It 'enum disambiguation works' {
        $errorCount = $error.Count
        Get-Process -Name asdfasdfsadfsadf -ErrorAction Ig

        $error.Count | Should -BeExactly $errorCount
    }

    #issue 2076
    It 'The -<ActionPreferenceParameterName> common parameter does not support Suspend on cmdlets' -TestCases $commonActionPreferenceParameterTestCases {
        param($ActionPreferenceParameterName)

        $commonParameters = @{
            "${ActionPreferenceParameterName}" = [System.Management.Automation.ActionPreference]::Suspend
        }

        { Write-Output -InputObject Test @commonParameters } | Should -Throw -ErrorId "ParameterBindingFailed,Microsoft.PowerShell.Commands.WriteOutputCommand"
    }

    It 'The -<ActionPreferenceParameterName> common parameter does not support Suspend on functions' -TestCases $commonActionPreferenceParameterTestCases {
        param($ActionPreferenceParameterName)

        function MyHelperFunction {
            [CmdletBinding()]
            param()
            "Hello"
        }

        $commonParameters = @{
            "${ActionPreferenceParameterName}" = [System.Management.Automation.ActionPreference]::Suspend
        }

        { MyHelperFunction -ErrorAction Suspend } | Should -Throw -ErrorId "ParameterBindingFailed,MyHelperFunction"
    }

    It '<switch> does not take precedence over $ErrorActionPreference' -TestCases @(
        @{switch = "Verbose" },
        @{switch = "Debug" }
    ) {
        param($switch)
        $ErrorActionPreference = "SilentlyContinue"
        $params = @{
            ItemType = "File";
            Path     = "$testdrive\test.txt";
            Confirm  = $false
        }
        New-Item @params > $null
        $params += @{$switch = $true }
        { New-Item @params } | Should -Not -Throw
        $ErrorActionPreference = "Stop"
        { New-Item @params } | Should -Throw -ErrorId "NewItemIOError,Microsoft.PowerShell.Commands.NewItemCommand"
        Remove-Item "$testdrive\test.txt" -Force
    }

    It "Parameter binding '-<name>' throws correctly (no NRE) if argument is <argValue>" -TestCases @(
        @{ name = "ErrorAction";       argValue = "null";           arguments = @{ ErrorAction = $null } }
        @{ name = "WarningAction";     argValue = "null";           arguments = @{ WarningAction = $null } }
        @{ name = "InformationAction"; argValue = "null";           arguments = @{ InformationAction = $null } }
        @{ name = "ErrorAction";       argValue = "AutomationNull"; arguments = @{ ErrorAction = [System.Management.Automation.Internal.AutomationNull]::Value } }
        @{ name = "WarningAction";     argValue = "AutomationNull"; arguments = @{ WarningAction = [System.Management.Automation.Internal.AutomationNull]::Value } }
        @{ name = "InformationAction"; argValue = "AutomationNull"; arguments = @{ InformationAction = [System.Management.Automation.Internal.AutomationNull]::Value } }
        @{ name = "ProgressAction";    argValue = "AutomationNull"; arguments = @{ ProgressAction = [System.Management.Automation.Internal.AutomationNull]::Value } }
    ) {
        param($arguments)

        $err = $null
        try {
            Test-Path .\noexistfile.ps1 @arguments
        } catch {
            $err = $_
        }

        $err.FullyQualifiedErrorId | Should -BeExactly "ParameterBindingFailed,Microsoft.PowerShell.Commands.TestPathCommand"
        $err.Exception.InnerException.InnerException | Should -BeOfType "System.Management.Automation.PSInvalidCastException"
    }
}

Describe 'ActionPreference.Break tests' -Tag 'CI' {

    BeforeAll {
        Register-DebuggerHandler
    }

    AfterAll {
        Unregister-DebuggerHandler
    }

    Context '-ErrorAction Break should break on a non-terminating error' {
        BeforeAll {
            $testScript = {
                function Test-Break {
                    [CmdletBinding()]
                    param()
                    try {
                        # Generate a non-terminating error
                        Write-Error 'This is a non-terminating error.'
                        # Do something afterwards
                        'This should still run'
                    } catch {
                        'Do nothing'
                    } finally {
                        'This finally runs'
                    }
                }
                Test-Break -ErrorAction Break
            }

            $results = @(Test-Debugger -Scriptblock $testScript -CommandQueue 'v', 'v')
        }

        It 'Should show 3 debugger commands were invoked' {
            # There is always an implicit 'c' command that keeps the debugger automation moving
            $results.Count | Should -Be 3
        }

        It 'The breakpoint should be the statement that generated the non-terminating error' {
            $results[0] | ShouldHaveExtent -Line 7 -FromColumn 25 -ToColumn 71
        }

        It 'The second statement should be the statement after that which generated the non-terminating error' {
            $results[1] | ShouldHaveExtent -Line 9 -FromColumn 25 -ToColumn 48
        }

        It 'The third statement should be the statement in the finally block' {
            $results[2] | ShouldHaveExtent -Line 13 -FromColumn 25 -ToColumn 44
        }
    }

    Context '-ErrorAction Break should break on a terminating error' {
        BeforeAll {
            $testScript = {
                function Test-Break {
                    [CmdletBinding()]
                    param()
                    try {
                        # Generate a terminating error
                        Get-Process -TheAnswer 42
                        # Do something afterwards
                        'This should not run'
                    } catch {
                        'Do nothing'
                    } finally {
                        'This finally runs'
                    }
                }
                Test-Break -ErrorAction Break
            }

            $results = @(Test-Debugger -Scriptblock $testScript -CommandQueue 'v', 'v')
        }

        It 'Should show 3 debugger commands were invoked' {
            # There is always an implicit 'c' command that keeps the debugger automation moving
            $results.Count | Should -Be 3
        }

        It 'The breakpoint should be the statement that generated the terminating error' {
            $results[0] | ShouldHaveExtent -Line 7 -FromColumn 25 -ToColumn 50
        }

        It 'The second statement should be the statement in the catch block where the terminating error is caught' {
            $results[1] | ShouldHaveExtent -Line 11 -FromColumn 25 -ToColumn 37
        }

        It 'The third statement should be the statement in the finally block' {
            $results[2] | ShouldHaveExtent -Line 13 -FromColumn 25 -ToColumn 44
        }
    }

    Context '-ErrorAction Break should not break on a naked rethrow' {
        BeforeAll {
            $testScript = {
                function Test-Break {
                    [CmdletBinding()]
                    param()
                    try {
                        try {
                            # Generate a terminating error
                            Get-Process -TheAnswer 42
                        } catch {
                            throw
                        }
                    } catch {
                        # Swallow the exception here
                    }
                }
                Test-Break -ErrorAction Break
            }

            $results = @(Test-Debugger -Scriptblock $testScript)
        }

        It 'Should show 1 debugger command was invoked' {
            # ErrorAction break should only trigger on the initial terminating error
            $results.Count | Should -Be 1
        }

        It 'The breakpoint should be the statement that generated the terminating error' {
            $results[0] | ShouldHaveExtent -Line 8 -FromColumn 29 -ToColumn 54
        }
    }

    Context '-ErrorAction Break should break when throwing a specific error or object' {
        BeforeAll {
            $testScript = {
                function Test-Break {
                    [CmdletBinding()]
                    param()
                    try {
                        try {
                            # Generate a terminating error
                            Get-Process -TheAnswer 42
                        } catch {
                            throw $_
                        }
                    } catch {
                        # Swallow the exception here
                    }
                }
                Test-Break -ErrorAction Break
            }

            $results = @(Test-Debugger -Scriptblock $testScript)
        }

        It 'Should show 2 debugger commands were invoked' {
            # ErrorAction break should trigger on the initial terminating error and the throw
            # since it throws a "new" error (throwing anything is considered a new terminating
            # error)
            $results.Count | Should -Be 2
        }

        It 'The first breakpoint should be the statement that generated the terminating error' {
            $results[0] | ShouldHaveExtent -Line 8 -FromColumn 29 -ToColumn 54
        }

        It 'The second breakpoint should be the statement that threw $_' {
            $results[1] | ShouldHaveExtent -Line 10 -FromColumn 29 -ToColumn 37
        }
    }

    Context 'Other message types should break on their corresponding messages when requested' {
        BeforeAll {
            $testScript = {
                function Test-Break {
                    [CmdletBinding()]
                    param()
                    Write-Warning -Message 'This is a warning message'
                    Write-Verbose -Message 'This is a verbose message'
                    Write-Debug -Message 'This is a debug message'
                    Write-Information -MessageData 'This is an information message'
                    Write-Progress -Activity 'This shows progress'
                    Write-Progress -Activity 'This shows progress' -Completed
                }
                Test-Break -WarningAction Break -InformationAction Break *>$null
                $WarningPreference = $VerbosePreference = $DebugPreference = $InformationPreference = $ProgressPreference = [System.Management.Automation.ActionPreference]::Break
                Test-Break *>$null
            }
            $results = @(Test-Debugger -Scriptblock $testScript)
        }

        It 'Should show 8 debugger commands were invoked' {
            # When no debugger commands are provided, 'c' is invoked every time a breakpoint is hit
            $results.Count | Should -Be 8
        }

        It 'Write-Warning should trigger a breakpoint from -WarningAction Break' {
            $results[0] | ShouldHaveExtent -Line 5 -FromColumn 21 -ToColumn 71
        }

        It 'Write-Information should trigger a breakpoint from -InformationAction Break' {
            $results[1] | ShouldHaveExtent -Line 8 -FromColumn 21 -ToColumn 84
        }

        It 'Write-Warning should trigger a breakpoint from $WarningPreference = [System.Management.Automation.ActionPreference]::Break' {
            $results[2] | ShouldHaveExtent -Line 5 -FromColumn 21 -ToColumn 71
        }

        It 'Write-Verbose should trigger a breakpoint from $VerbosePreference = [System.Management.Automation.ActionPreference]::Break' {
            $results[3] | ShouldHaveExtent -Line 6 -FromColumn 21 -ToColumn 71
        }

        It 'Write-Debug should trigger a breakpoint from $DebugPreference = [System.Management.Automation.ActionPreference]::Break' {
            $results[4] | ShouldHaveExtent -Line 7 -FromColumn 21 -ToColumn 67
        }

        It 'Write-Information should trigger a breakpoint from $InformationPreference = [System.Management.Automation.ActionPreference]::Break' {
            $results[5] | ShouldHaveExtent -Line 8 -FromColumn 21 -ToColumn 84
        }

        It 'Write-Progress should trigger a breakpoint from $ProgressPreference = [System.Management.Automation.ActionPreference]::Break' {
            $results[6] | ShouldHaveExtent -Line 9 -FromColumn 21 -ToColumn 67
        }
    }

    Context 'ActionPreference.Break in jobs' {

        BeforeAll {
            $job = Start-Job {
                $ErrorActionPreference = [System.Management.Automation.ActionPreference]::Break
                Get-Process -TheAnswer 42
            }
        }

        AfterAll {
            Remove-Job -Job $job -Force
        }

        It 'ActionPreference.Break should break in a running job' {
            Wait-UntilTrue -sb { $job.State -eq 'AtBreakpoint' } -TimeoutInMilliseconds (60 * 1000) -IntervalInMilliseconds 100 | Should -BeTrue
        }
    }
}