diff --git a/test/powershell/Write-Debug.Tests.ps1 b/test/powershell/Write-Debug.Tests.ps1 index 2f60c5bba..194294a4e 100644 --- a/test/powershell/Write-Debug.Tests.ps1 +++ b/test/powershell/Write-Debug.Tests.ps1 @@ -1,23 +1,46 @@ -Describe "Write-Debug DRT Unit Tests" -Tags DRT{ +Describe "Write-Debug DRT Unit Tests" -Tags DRT{ function unittest-writedebugline { - Write-Host "" - Write-Host "foo" - Write-Host "hello" -ForegroundColor Black -BackgroundColor Cyan - Write-Debug "this is a test" + $DebugPreference = 'continue' + Write-Debug "this is a test" + } + + function unittest-writedebug + { + [cmdletbinding()] + Param ( + [String]$Value + ) + + $PSBoundParameters.GetEnumerator() | ForEach { + Write-Verbose $_ + } + Write-Verbose "DebugPreference: $DebugPreference" + + If ($PSBoundParameters['Debug']) { + $DebugPreference = 'Continue' + } + + Write-Debug $Value } - It "Write-Debug Test" { - $o = "this is a test" - { Write-Debug $o } | Should Not Throw + It 'Write-Debug Test1: $DebugPreference is set' { + $ps = [PowerShell]::Create() + $null = $ps.AddScript("`$o = 'this is a test';`$DebugPreference = 'continue';Write-Debug `$o").Invoke() + $ps.Streams.Debug | Should Be 'this is a test' } - It "Write-Debug Test2" { - { $debugpreference='continue'; Write-Debug 'hello' } | Should Not Throw - $debugpreference ='SilentlyContinue' + It "Write-Debug Test2: Calling a cmdleting binding and passing -Debug " { + $ps = [PowerShell]::Create() + $null = $ps.AddScript("Write-Debug foo -debug").Invoke() + $ps.Streams.Debug | Should Be 'foo' + } + + It "Write-Debug Test3: Calling a regular function" { + unittest-writedebugline 5>&1 | Should Be 'this is a test' } - It "Write-Debug Test3" { - { unittest-writedebugline } | Should Not Throw + It "Write-Debug Test4: Redirecting the debug stream" { + unittest-writedebug -Value "foo" -Debug 5>&1 | Should Be 'foo' } -} \ No newline at end of file +} diff --git a/test/powershell/Write-Error.Tests.ps1 b/test/powershell/Write-Error.Tests.ps1 index 57fcc7e8f..0e0e13e91 100644 --- a/test/powershell/Write-Error.Tests.ps1 +++ b/test/powershell/Write-Error.Tests.ps1 @@ -1,82 +1,82 @@ 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' + $e = Write-Error myerrortext 2>&1 + $e | Should Not BeNullOrEmpty + $e.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 + $e.Exception.GetType().Name | Should Be 'WriteErrorException' + $e.Exception.Message | Should Be 'myerrortext' + $e.Exception.Data.Count | Should Be 0 + $e.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' + $e.CategoryInfo | Should Not BeNullOrEmpty + $e.CategoryInfo.Category | Should Be 'NotSpecified' + $e.CategoryInfo.Activity | Should Be 'Write-Error' + $e.CategoryInfo.Reason | Should Be 'WriteErrorException' + $e.CategoryInfo.TargetName | Should BeNullOrEmpty + $e.CategoryInfo.TargetType | Should BeNullOrEmpty + $e.CategoryInfo.GetMessage() | Should Be 'NotSpecified: (:) [Write-Error], WriteErrorException' #ErrorDetails verification - $Error[0].ErrorDetails | Should BeNullOrEmpty + $e.ErrorDetails | Should BeNullOrEmpty #FullyQualifiedErrorId verification - $Error[0].FullyQualifiedErrorId | Should Be 'Microsoft.PowerShell.Commands.WriteErrorException' + $e.FullyQualifiedErrorId | Should Be 'Microsoft.PowerShell.Commands.WriteErrorException' #InvocationInfo verification - $Error[0].InvocationInfo | Should Not BeNullOrEmpty - $Error[0].InvocationInfo.MyCommand.Name | Should BeNullOrEmpty + $e.InvocationInfo | Should Not BeNullOrEmpty + $e.InvocationInfo.MyCommand.Name | Should BeNullOrEmpty } #Blocked by issue #846 It "Should be works with all parameters" -Pending { $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' + $e = Write-Error -Message myerrortext -Exception $exception -ErrorId myerrorid -Category syntaxerror -TargetObject TargetObject -CategoryActivity myactivity -CategoryReason myreason -CategoryTargetName mytargetname -CategoryTargetType mytargettype -RecommendedAction myrecommendedaction 2>&1 + $e | Should Not BeNullOrEmpty + $e.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 + $e.Exception | Should Not BeNullOrEmpty + $e.Exception.GetType().Name | Should Be 'ArgumentNullException' + $e.Exception.ParamName | Should Be 'paramname' + $e.Exception.Data.Count | Should Be 0 + $e.Exception.InnerException | Should BeNullOrEmpty #TargetObject verification - $Error[0].TargetObject | Should Be 'TargetObject' + $e.TargetObject | Should Be 'TargetObject' #FullyQualifiedErrorId verification - $Error[0].FullyQualifiedErrorId | Should Be 'myerrorid' + $e.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' + $e.CategoryInfo | Should Not BeNullOrEmpty + $e.CategoryInfo.Category | Should Be 'SyntaxError' + $e.CategoryInfo.Activity | Should Be 'myactivity' + $e.CategoryInfo.Reason | Should Be 'myreason' + $e.CategoryInfo.TargetName | Should Be 'mytargetname' + $e.CategoryInfo.TargetType | Should Be 'mytargettype' + $e.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' + $e.ErrorDetails | Should Not BeNullOrEmpty + $e.ErrorDetails.Message | Should Be 'myerrortext' + $e.ErrorDetails.RecommendedAction | Should Be 'myrecommendedaction' #InvocationInfo verification - $Error[0].InvocationInfo | Should Not BeNullOrEmpty - $Error[0].InvocationInfo.MyCommand.Name | Should BeNullOrEmpty + $e.InvocationInfo | Should Not BeNullOrEmpty + $e.InvocationInfo.MyCommand.Name | Should BeNullOrEmpty } #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' + $e = write-error -Activity fooAct -Reason fooReason -TargetName fooTargetName -TargetType fooTargetType -Message fooMessage 2>&1 + $e.CategoryInfo.Activity | Should Be 'fooAct' + $e.CategoryInfo.Reason | Should Be 'fooReason' + $e.CategoryInfo.TargetName | Should Be 'fooTargetName' + $e.CategoryInfo.TargetType | Should Be 'fooTargetType' + $e.CategoryInfo.GetMessage() | Should Be 'NotSpecified: (fooTargetName:fooTargetType) [fooAct], fooReason' } }