Merge pull request #919 from PowerShell/WriteTests

Add Unit Test For Write Command
This commit is contained in:
Jason Shirk 2016-06-03 10:46:26 -07:00
commit a3748647d5
3 changed files with 96 additions and 42 deletions

View File

@ -1,72 +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
}
# Skip with issue #846
It "Should be works with all parameters" -Pending {
#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 {
$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'
}
}

View File

@ -0,0 +1,22 @@
Describe "Write-Host DRT Unit Tests" -Tags DRT{
$testData = @(
@{ Name = 'NoNewline';Command = "Write-Host a,b -Separator ',' -ForegroundColor Yellow -BackgroundColor DarkBlue -NoNewline"; returnValue = "a,b" }
@{ Name = 'Separator';Command = "Write-Host a,b,c -Separator '+'"; returnValue = "a+b+c" }
)
It "write-Host works with '<Name>' switch" -TestCases $testData -Pending:$IsOSX {
param($Command, $returnValue)
If($IsLinux)
{
$content = powershell -noprofile -command $Command
}
If ($IsWindows)
{
$content = powershell.exe -noprofile -command $Command
}
$content | Should Be $returnValue
}
}

View File

@ -1,3 +1,25 @@
Describe "Write-Output DRT Unit Tests" -Tags DRT{
It "Simple Write Object Test" {
$objectWritten = 1, 2.2, @("John", "Smith", 10), "abc"
$results = Write-Output $objectWritten
$results.Length | Should Be $objectWritten.Length
$results[0] | Should Be $objectWritten[0]
$results[1] | Should Be $objectWritten[1]
$results[2] | Should Be $objectWritten[2]
$results[2] -is [System.Array] | Should Be $true
$results[3] | Should Be $objectWritten[3]
$results[3] -is [System.String] | Should Be $true
}
It "Works with NoEnumerate switch" {
$objectWritten = 1, 2.2, @("John", "Smith", 10), "abc"
Write-Output $objectWritten -NoEnumerate 6>&1 | Should be '1 2.2 System.Object[] abc'
}
}
Describe "Write-Output" {
$testString = $testString
Context "Input Tests" {