From 97c00e6343f4b4d704a165ed48210f8d774643a7 Mon Sep 17 00:00:00 2001 From: TingLiu6 Date: Wed, 27 Apr 2016 01:08:53 -0700 Subject: [PATCH 1/6] Add Unit Test For Write Command --- test/powershell/Write-Debug.Tests.ps1 | 23 +++++++++++++++++++++++ test/powershell/Write-Error.Tests.ps1 | 14 ++++++++++++-- test/powershell/Write-Output.Tests.ps1 | 17 +++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 test/powershell/Write-Debug.Tests.ps1 diff --git a/test/powershell/Write-Debug.Tests.ps1 b/test/powershell/Write-Debug.Tests.ps1 new file mode 100644 index 000000000..2f60c5bba --- /dev/null +++ b/test/powershell/Write-Debug.Tests.ps1 @@ -0,0 +1,23 @@ +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" + } + + It "Write-Debug Test" { + $o = "this is a test" + { Write-Debug $o } | Should Not Throw + } + + It "Write-Debug Test2" { + { $debugpreference='continue'; Write-Debug 'hello' } | Should Not Throw + $debugpreference ='SilentlyContinue' + } + + It "Write-Debug Test3" { + { unittest-writedebugline } | Should Not Throw + } +} \ No newline at end of file diff --git a/test/powershell/Write-Error.Tests.ps1 b/test/powershell/Write-Error.Tests.ps1 index c951b7706..57fcc7e8f 100644 --- a/test/powershell/Write-Error.Tests.ps1 +++ b/test/powershell/Write-Error.Tests.ps1 @@ -30,8 +30,8 @@ Describe "Write-Error DRT Unit Tests" -Tags DRT{ $Error[0].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 @@ -68,6 +68,16 @@ Describe "Write-Error DRT Unit Tests" -Tags DRT{ $Error[0].InvocationInfo | Should Not BeNullOrEmpty $Error[0].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' + } } Describe "Write-Error" { diff --git a/test/powershell/Write-Output.Tests.ps1 b/test/powershell/Write-Output.Tests.ps1 index 89c2210bb..fc29185a9 100644 --- a/test/powershell/Write-Output.Tests.ps1 +++ b/test/powershell/Write-Output.Tests.ps1 @@ -1,3 +1,20 @@ +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 + } +} + Describe "Write-Output" { $testString = $testString Context "Input Tests" { From b96d207ea88e6d704b26fb324c142647230a3793 Mon Sep 17 00:00:00 2001 From: TingLiu6 Date: Fri, 29 Apr 2016 01:07:57 -0700 Subject: [PATCH 2/6] Fix the CR issues for Write command test --- test/powershell/Write-Debug.Tests.ps1 | 51 +++++++++++---- test/powershell/Write-Error.Tests.ps1 | 92 +++++++++++++-------------- 2 files changed, 83 insertions(+), 60 deletions(-) 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' } } From d7db0a6f7089a5c96feeadfaf0026c28274d005d Mon Sep 17 00:00:00 2001 From: TingLiu6 Date: Tue, 10 May 2016 00:33:33 -0700 Subject: [PATCH 3/6] Add Unit Test for Write-Host --- test/powershell/Write-Debug.Tests.ps1 | 2 +- test/powershell/Write-Host.Tests.ps1 | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 test/powershell/Write-Host.Tests.ps1 diff --git a/test/powershell/Write-Debug.Tests.ps1 b/test/powershell/Write-Debug.Tests.ps1 index 194294a4e..0878f9e1b 100644 --- a/test/powershell/Write-Debug.Tests.ps1 +++ b/test/powershell/Write-Debug.Tests.ps1 @@ -13,7 +13,7 @@ ) $PSBoundParameters.GetEnumerator() | ForEach { - Write-Verbose $_ + Write-Verbose $_ } Write-Verbose "DebugPreference: $DebugPreference" diff --git a/test/powershell/Write-Host.Tests.ps1 b/test/powershell/Write-Host.Tests.ps1 new file mode 100644 index 000000000..0c0cfbfa0 --- /dev/null +++ b/test/powershell/Write-Host.Tests.ps1 @@ -0,0 +1,26 @@ +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 '' switch" -TestCases $testData { + param($Command, $returnValue) + + $tempFile = [io.path]::getTempFIleName() + $script = Join-Path $TestDrive -ChildPath writeHost.ps1 + + $Command > $script + + if($IsLinux) + { + powershell $script > $tempFile + } + else + { + powershell.exe $script > $tempFile + } + $content = Get-Content $tempFile + $content | Should Be $returnValue + } +} \ No newline at end of file From df6e4c4a92d88bdda557ec83b5d32cdadcac71a2 Mon Sep 17 00:00:00 2001 From: TingLiu6 Date: Tue, 24 May 2016 20:19:48 -0700 Subject: [PATCH 4/6] Remove Write-Debug Test --- test/powershell/Write-Debug.Tests.ps1 | 46 --------------------------- 1 file changed, 46 deletions(-) delete mode 100644 test/powershell/Write-Debug.Tests.ps1 diff --git a/test/powershell/Write-Debug.Tests.ps1 b/test/powershell/Write-Debug.Tests.ps1 deleted file mode 100644 index 0878f9e1b..000000000 --- a/test/powershell/Write-Debug.Tests.ps1 +++ /dev/null @@ -1,46 +0,0 @@ -Describe "Write-Debug DRT Unit Tests" -Tags DRT{ - function unittest-writedebugline - { - $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 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: 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 Test4: Redirecting the debug stream" { - unittest-writedebug -Value "foo" -Debug 5>&1 | Should Be 'foo' - } -} From 0fa73fb37560d0a419b0028e29ae1de3723d555e Mon Sep 17 00:00:00 2001 From: TingLiu6 Date: Tue, 24 May 2016 23:01:22 -0700 Subject: [PATCH 5/6] Fixed issue on OSX --- test/powershell/Write-Host.Tests.ps1 | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/powershell/Write-Host.Tests.ps1 b/test/powershell/Write-Host.Tests.ps1 index 0c0cfbfa0..5df8ac767 100644 --- a/test/powershell/Write-Host.Tests.ps1 +++ b/test/powershell/Write-Host.Tests.ps1 @@ -4,7 +4,7 @@ @{ Name = 'Separator';Command = "Write-Host a,b,c -Separator '+'"; returnValue = "a+b+c" } ) - It "write-Host works with '' switch" -TestCases $testData { + It "write-Host works with '' switch" -TestCases $testData -Pending:$IsOSX { param($Command, $returnValue) $tempFile = [io.path]::getTempFIleName() @@ -12,11 +12,12 @@ $Command > $script - if($IsLinux) + If($IsLinux) { powershell $script > $tempFile } - else + + If ($IsWindows) { powershell.exe $script > $tempFile } From 9ae922551c777b5c81c447bf39b284032b52f036 Mon Sep 17 00:00:00 2001 From: TingLiu6 Date: Thu, 2 Jun 2016 23:31:12 -0700 Subject: [PATCH 6/6] Fixed CR Issues of Write-Host and Write-Output --- test/powershell/Write-Host.Tests.ps1 | 11 +++-------- test/powershell/Write-Output.Tests.ps1 | 5 +++++ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/test/powershell/Write-Host.Tests.ps1 b/test/powershell/Write-Host.Tests.ps1 index 5df8ac767..2395d91bf 100644 --- a/test/powershell/Write-Host.Tests.ps1 +++ b/test/powershell/Write-Host.Tests.ps1 @@ -7,21 +7,16 @@ It "write-Host works with '' switch" -TestCases $testData -Pending:$IsOSX { param($Command, $returnValue) - $tempFile = [io.path]::getTempFIleName() - $script = Join-Path $TestDrive -ChildPath writeHost.ps1 - - $Command > $script - If($IsLinux) { - powershell $script > $tempFile + $content = powershell -noprofile -command $Command } If ($IsWindows) { - powershell.exe $script > $tempFile + $content = powershell.exe -noprofile -command $Command } - $content = Get-Content $tempFile + $content | Should Be $returnValue } } \ No newline at end of file diff --git a/test/powershell/Write-Output.Tests.ps1 b/test/powershell/Write-Output.Tests.ps1 index fc29185a9..ffc2df858 100644 --- a/test/powershell/Write-Output.Tests.ps1 +++ b/test/powershell/Write-Output.Tests.ps1 @@ -13,6 +13,11 @@ Describe "Write-Output DRT Unit Tests" -Tags DRT{ $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" {