2016-04-14 09:17:02 +02:00
|
|
|
Describe "Write-Error DRT Unit Tests" -Tags DRT{
|
|
|
|
It "Should be works with command: write-error myerrortext" {
|
|
|
|
Write-Error myerrortext -ErrorAction SilentlyContinue
|
|
|
|
$Error[0] | Should Not BeNullOrEmpty
|
|
|
|
$Error[0].GetType().Name | Should Be 'ErrorRecord'
|
|
|
|
|
|
|
|
#Exception verification
|
|
|
|
$Error[0].Exception.GetType().Name | Should Be 'WriteErrorException'
|
|
|
|
$Error[0].Exception.Message | Should Be 'myerrortext'
|
|
|
|
$Error[0].Exception.Data.Count | Should Be 0
|
|
|
|
$Error[0].Exception.InnerException | Should BeNullOrEmpty
|
|
|
|
|
|
|
|
#ErrorCategoryInfo verification
|
|
|
|
$Error[0].CategoryInfo | Should Not BeNullOrEmpty
|
|
|
|
$Error[0].CategoryInfo.Category | Should Be 'NotSpecified'
|
|
|
|
$Error[0].CategoryInfo.Activity | Should Be 'Write-Error'
|
|
|
|
$Error[0].CategoryInfo.Reason | Should Be 'WriteErrorException'
|
|
|
|
$Error[0].CategoryInfo.TargetName | Should BeNullOrEmpty
|
|
|
|
$Error[0].CategoryInfo.TargetType | Should BeNullOrEmpty
|
|
|
|
$Error[0].CategoryInfo.GetMessage() | Should Be 'NotSpecified: (:) [Write-Error], WriteErrorException'
|
|
|
|
|
|
|
|
#ErrorDetails verification
|
|
|
|
$Error[0].ErrorDetails | Should BeNullOrEmpty
|
|
|
|
|
|
|
|
#FullyQualifiedErrorId verification
|
|
|
|
$Error[0].FullyQualifiedErrorId | Should Be 'Microsoft.PowerShell.Commands.WriteErrorException'
|
|
|
|
|
|
|
|
#InvocationInfo verification
|
|
|
|
$Error[0].InvocationInfo | Should Not BeNullOrEmpty
|
|
|
|
$Error[0].InvocationInfo.MyCommand.Name | Should BeNullOrEmpty
|
|
|
|
}
|
|
|
|
|
2016-04-27 10:08:53 +02:00
|
|
|
#Blocked by issue #846
|
|
|
|
It "Should be works with all parameters" -Pending {
|
2016-04-14 09:17:02 +02:00
|
|
|
$exception = New-Object -TypeName System.ArgumentNullException -ArgumentList paramname
|
|
|
|
Write-Error -Message myerrortext -Exception $exception -ErrorId myerrorid -Category syntaxerror -TargetObject TargetObject -CategoryActivity myactivity -CategoryReason myreason -CategoryTargetName mytargetname -CategoryTargetType mytargettype -RecommendedAction myrecommendedaction -ErrorAction SilentlyContinue
|
|
|
|
$Error[0] | Should Not BeNullOrEmpty
|
|
|
|
$Error[0].GetType().Name | Should Be 'ErrorRecord'
|
|
|
|
|
|
|
|
#Exception verification
|
|
|
|
$Error[0].Exception | Should Not BeNullOrEmpty
|
|
|
|
$Error[0].Exception.GetType().Name | Should Be 'ArgumentNullException'
|
|
|
|
$Error[0].Exception.ParamName | Should Be 'paramname'
|
|
|
|
$Error[0].Exception.Data.Count | Should Be 0
|
|
|
|
$Error[0].Exception.InnerException | Should BeNullOrEmpty
|
|
|
|
|
|
|
|
#TargetObject verification
|
|
|
|
$Error[0].TargetObject | Should Be 'TargetObject'
|
|
|
|
|
|
|
|
#FullyQualifiedErrorId verification
|
|
|
|
$Error[0].FullyQualifiedErrorId | Should Be 'myerrorid'
|
|
|
|
|
|
|
|
#ErrorCategoryInfo verification
|
|
|
|
$Error[0].CategoryInfo | Should Not BeNullOrEmpty
|
|
|
|
$Error[0].CategoryInfo.Category | Should Be 'SyntaxError'
|
|
|
|
$Error[0].CategoryInfo.Activity | Should Be 'myactivity'
|
|
|
|
$Error[0].CategoryInfo.Reason | Should Be 'myreason'
|
|
|
|
$Error[0].CategoryInfo.TargetName | Should Be 'mytargetname'
|
|
|
|
$Error[0].CategoryInfo.TargetType | Should Be 'mytargettype'
|
|
|
|
$Error[0].CategoryInfo.GetMessage() | Should Be 'SyntaxError: (mytargetname:mytargettype) [myactivity], myreason'
|
|
|
|
|
|
|
|
#ErrorDetails verification
|
|
|
|
$Error[0].ErrorDetails | Should Not BeNullOrEmpty
|
|
|
|
$Error[0].ErrorDetails.Message | Should Be 'myerrortext'
|
|
|
|
$Error[0].ErrorDetails.RecommendedAction | Should Be 'myrecommendedaction'
|
|
|
|
|
|
|
|
#InvocationInfo verification
|
|
|
|
$Error[0].InvocationInfo | Should Not BeNullOrEmpty
|
|
|
|
$Error[0].InvocationInfo.MyCommand.Name | Should BeNullOrEmpty
|
|
|
|
}
|
2016-04-27 10:08:53 +02:00
|
|
|
|
|
|
|
#Blocked by issue #846
|
|
|
|
It "Should be works with all parameters" -Pending {
|
|
|
|
write-error -Activity fooAct -Reason fooReason -TargetName fooTargetName -TargetType fooTargetType -Message fooMessage
|
|
|
|
$Error[0].CategoryInfo.Activity | Should Be 'fooAct'
|
|
|
|
$Error[0].CategoryInfo.Reason | Should Be 'fooReason'
|
|
|
|
$Error[0].CategoryInfo.TargetName | Should Be 'fooTargetName'
|
|
|
|
$Error[0].CategoryInfo.TargetType | Should Be 'fooTargetType'
|
|
|
|
$Error[0].CategoryInfo.GetMessage() | Should Be 'NotSpecified: (fooTargetName:fooTargetType) [fooAct], fooReason'
|
|
|
|
}
|
2016-04-14 09:17:02 +02:00
|
|
|
}
|
|
|
|
|
2016-03-04 23:48:30 +01:00
|
|
|
Describe "Write-Error" {
|
|
|
|
It "Should be able to throw" {
|
2016-03-04 23:52:27 +01:00
|
|
|
Write-Error "test throw" -ErrorAction SilentlyContinue | Should Throw
|
2016-03-04 23:48:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should throw a non-terminating error" {
|
2016-03-04 23:52:27 +01:00
|
|
|
Write-Error "test throw" -ErrorAction SilentlyContinue
|
2016-03-04 23:48:30 +01:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
1 + 1 | Should Be 2
|
2016-03-04 23:48:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should trip an exception using the exception switch" {
|
2016-03-04 23:52:27 +01:00
|
|
|
$var = 0
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Write-Error -Exception -Message "test throw"
|
|
|
|
}
|
|
|
|
catch [System.Exception]
|
|
|
|
{
|
|
|
|
|
|
|
|
$var++
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
$var | Should Be 1
|
|
|
|
}
|
2016-03-04 23:48:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should output the error message to the `$error automatic variable" {
|
2016-03-04 23:52:27 +01:00
|
|
|
$theError = "Error: Too many input values."
|
|
|
|
write-error -message $theError -category InvalidArgument -ErrorAction SilentlyContinue
|
2016-03-04 23:48:30 +01:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$error[0]| Should Be $theError
|
2016-03-04 23:48:30 +01:00
|
|
|
}
|
2016-05-12 23:12:56 +02:00
|
|
|
}
|