Add Unit Test for Out-File,Out-String,Write-Error

This commit is contained in:
TingLiu6 2016-04-14 00:17:02 -07:00
parent 5300811ff9
commit 92ac20f83b
3 changed files with 103 additions and 0 deletions

View file

@ -1,3 +1,22 @@
Describe "Out-File DRT Unit Tests" -Tags DRT{
It "Should be able to write the contens into a file with -pspath" {
$tempFile = "ExposeBug928965"
{ 1 | Out-File -PSPath $tempFile } | Should Not Throw
$fileContents = Get-Content $tempFile
$fileContents | Should be 1
Remove-Item $tempFile -Force
}
It "Should be able to write the contens into a file with -pspath" {
$tempFile = "outfileAppendTest.txt"
{ 'This is first line.' | out-file $tempFile } | Should Not Throw
{ 'This is second line.' | out-file -append $tempFile } | Should Not Throw
$tempFile |Should Contain "first"
$tempFile |Should Contain "second"
Remove-Item $tempFile -Force
}
}
Describe "Out-File" {
$expectedContent = "some test text"
$inObject = New-Object psobject -Property @{text=$expectedContent}

View file

@ -1,3 +1,15 @@
Describe "Out-String DRT Unit Tests" -Tags DRT{
It "check display of properties with names containing wildcard characters" {
$results = new-object psobject | add-member -passthru noteproperty 'name with square brackets: [0]' 'myvalue' | out-string
$results.Length | Should BeGreaterThan 1
$results.GetType() | Should Be string
$results.Contains("myvalue") | Should Be $true
$results.Contains("name with square brackets: [0]") | Should Be $true
}
}
Describe "Out-String" {
$nl = [Environment]::NewLine

View file

@ -1,3 +1,75 @@
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
}
#Skip with issue #846
It "Should be works with all parameters" -Skip:$true {
$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
}
}
Describe "Write-Error" {
It "Should be able to throw" {
Write-Error "test throw" -ErrorAction SilentlyContinue | Should Throw