forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActionPreference.Tests.ps1
More file actions
129 lines (111 loc) · 4.93 KB
/
Copy pathActionPreference.Tests.ps1
File metadata and controls
129 lines (111 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
Describe "Tests for (error, warning, etc) action preference" -Tags "CI" {
BeforeAll {
$orgin = $GLOBAL:errorActionPreference
}
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 -ea stop -ev err
}
catch {}
It '$err.Count' { $err.Count | Should Be 1 }
It '$err[0] should not be $null' { $err[0] | Should Not Be $null }
It '$err[0].GetType().Name' { $err[0].GetType().Name | Should Be ActionPreferenceStopException }
It '$err[0].ErrorRecord' { $err[0].ErrorRecord | Should not BeNullOrEmpty }
It '$err[0].ErrorRecord.Exception.GetType().Name' { $err[0].ErrorRecord.Exception.GetType().Name | Should Be ItemNotFoundException }
}
It 'ActionPreference Ignore Works' {
$errorCount = $error.Count
Get-Process -Name asdfasdfsadfsadf -ErrorAction Ignore
$error.Count | Should Be $errorCount
}
It 'action preference of Ignore cannot be set as a preference variable' {
try {
$GLOBAL:errorActionPreference = "Ignore"
Get-Process -Name asdfasdfasdf
Throw "Exception expected, execution should not have reached here"
} catch {
$_.CategoryInfo.Reason | Should Be NotSupportedException
} finally {
$GLOBAL:errorActionPreference = $orgin
}
}
It 'action preference of Suspend cannot be set as a preference variable' {
try {
$GLOBAL:errorActionPreference = "Suspend"
Get-Process -Name asdfasdfasdf
Throw "Exception expected, execution should not have reached here"
} catch {
$_.CategoryInfo.Reason | Should Be ArgumentTransformationMetadataException
}
finally {
$GLOBAL:errorActionPreference = $orgin
}
}
It 'enum disambiguation works' {
$errorCount = $error.Count
Get-Process -Name asdfasdfsadfsadf -ErrorAction Ig
$error.Count | Should Be $errorCount
}
It 'ErrorAction = Suspend works on Workflow' -Skip:$IsCoreCLR {
. .\TestsOnWinFullOnly.ps1
Run-TestOnWinFull "ActionPreference:ErrorAction=SuspendOnWorkflow"
}
It 'ErrorAction = Suspend does not work on functions' {
function MyHelperFunction {
[CmdletBinding()]
param()
"Hello"
}
try
{
MyHelperFunction -ErrorAction Suspend
Throw "Exception expected, execution should not have reached here"
} catch {
$_.FullyQualifiedErrorId | Should Be "ParameterBindingFailed,MyHelperFunction"
}
}
It 'ErrorAction = Suspend does not work on cmdlets' {
try
{
Get-Process -ErrorAction Suspend
Throw "Exception expected, execution should not have reached here"
}
catch {
$_.FullyQualifiedErrorId | Should Be "ParameterBindingFailed,Microsoft.PowerShell.Commands.GetProcessCommand"
}
}
It 'WarningAction = Suspend does not work' {
try
{
Get-Process -WarningAction Suspend
Throw "Exception expected, execution should not have reached here"
}
catch {
$_.FullyQualifiedErrorId | Should Be "ParameterBindingFailed,Microsoft.PowerShell.Commands.GetProcessCommand"
}
}
#issue 2076
It 'ErrorAction and WarningAction are the only action preferences do not support suspend' -Pending{
$params = [System.Management.Automation.Internal.CommonParameters].GetProperties().Name | Select-String Action
$suspendErrors = $null
$num=0
$params | % {
$input=@{'InputObject' = 'Test';$_='Suspend'}
try {
Write-Output @input
} catch {
$_.FullyQualifiedErrorId | Should Be "ParameterBindingFailed,Microsoft.PowerShell.Commands.WriteOutputCommand"
$num++
}
}
$num | Should Be 2
}
}