diff --git a/test/powershell/Common/TestHostCS.psm1 b/test/powershell/Common/TestHostCS.psm1 index 2a56623fb7f..b08c57eceef 100755 --- a/test/powershell/Common/TestHostCS.psm1 +++ b/test/powershell/Common/TestHostCS.psm1 @@ -169,14 +169,25 @@ namespace TestHost return ss; } + // Cmdlets call 'Write' and 'WriteLine' methods implicitly. + // To see difference between 'Write' and 'WriteLine' with and w/o colors in the debug output + // we need use a meta information. + // So we make a output string as: + // : : <'user value'> : <'NewLine' or 'NoNewLine'> + // public override void Write(string value) { - Streams.ConsoleOutput.Add(value); + Streams.ConsoleOutput.Add("::"+value+":NoNewLine"); } public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value) { - Streams.ConsoleOutput.Add(value); + Streams.ConsoleOutput.Add(foregroundColor+":"+backgroundColor+":"+value+":NoNewLine"); + } + + public override void WriteLine(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value) + { + Streams.ConsoleOutput.Add(foregroundColor+":"+backgroundColor+":"+value+":NewLine"); } public override void WriteDebugLine(string message) @@ -191,7 +202,7 @@ namespace TestHost public override void WriteLine(string value) { - Streams.ConsoleOutput.Add(value); + Streams.ConsoleOutput.Add("::"+value+":NewLine"); } public override void WriteProgress(long sourceId, ProgressRecord record) @@ -208,6 +219,15 @@ namespace TestHost { Streams.Warning.Add(message); } + + public override void WriteInformation(InformationRecord record) + { + HostInformationMessage hostOutput = record.MessageData as HostInformationMessage; + if (hostOutput != null) { + string message = hostOutput.Message; + Streams.Information.Add(message); + } + } } public class TestHost : PSHost diff --git a/test/powershell/Host/HostUtilities.Tests.ps1 b/test/powershell/Host/HostUtilities.Tests.ps1 index 99247fad552..cdf516dc4eb 100644 --- a/test/powershell/Host/HostUtilities.Tests.ps1 +++ b/test/powershell/Host/HostUtilities.Tests.ps1 @@ -1,8 +1,5 @@ -if (-not (Get-Module TestRemoting -ErrorAction SilentlyContinue)) -{ - $remotingModule = Join-Path $PSScriptRoot "../Common/TestRemoting.psm1" - Import-Module $remotingModule -} +$remotingModule = Join-Path $PSScriptRoot "../Common/TestRemoting.psm1" +Import-Module $remotingModule Describe "InvokeOnRunspace method argument error handling" -tags "Feature" { diff --git a/test/powershell/Language/Scripting/ParameterBinding.Tests.ps1 b/test/powershell/Language/Scripting/ParameterBinding.Tests.ps1 index dbe3dd6d4d8..50c4a275890 100644 --- a/test/powershell/Language/Scripting/ParameterBinding.Tests.ps1 +++ b/test/powershell/Language/Scripting/ParameterBinding.Tests.ps1 @@ -1,5 +1,5 @@ -Import-Module $PSScriptRoot\..\..\Common\Test.Helpers.psm1 -Import-Module $PSScriptRoot\..\..\Common\TestHostCS.psm1 +Import-Module $PSScriptRoot\..\..\Common\Test.Helpers.psm1 -ErrorAction SilentlyContinue +Import-Module $PSScriptRoot\..\..\Common\TestHostCS.psm1 -ErrorAction SilentlyContinue Describe "Tests for parameter binding" -Tags "CI" { Context 'Test of Mandatory parameters' { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Core/Out-Host.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Core/Out-Host.Tests.ps1 index fd54bf8795a..15dcff926ba 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Core/Out-Host.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Core/Out-Host.Tests.ps1 @@ -1,12 +1,6 @@ - -if ( ! (get-module -ea silentlycontinue TestHostCS )) -{ - # this is sensitive to the location of this test and the common directory" - $pestertestroot = resolve-path "$psscriptroot/../.." - $common = join-path $pestertestroot Common - $hostmodule = join-path $common TestHostCS.psm1 - import-module $hostmodule -} +# this is sensitive to the location of this test and the common directory" +$hostmodule = Join-Path $PSScriptRoot "../../Common/TestHostCS.psm1" +import-module $hostmodule -ErrorAction SilentlyContinue Describe "Out-Host Tests" -tag CI { BeforeAll { @@ -27,8 +21,9 @@ Describe "Out-Host Tests" -tag CI { } It "Out-Host writes to host output" { $stringToWrite = "thing to write" + $stringExpected = "::$($stringToWrite):NewLine" $result = $ps.AddScript("Out-Host -inputobject '$stringToWrite'").Invoke() $th.UI.Streams.ConsoleOutput.Count | should be 1 - $th.UI.Streams.ConsoleOutput[0] | should be $stringToWrite + $th.UI.Streams.ConsoleOutput[0] | should be $stringExpected } } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Security/GetCredential.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Security/GetCredential.Tests.ps1 index df28efa7189..140be3a894c 100755 --- a/test/powershell/Modules/Microsoft.PowerShell.Security/GetCredential.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Security/GetCredential.Tests.ps1 @@ -1,8 +1,6 @@ -if ( ! (get-module -ea silentlycontinue TestHostCS )) -{ - $hostmodule = Join-Path $PSScriptRoot "../../Common/TestHostCS.psm1" - import-module $hostmodule -} +$hostmodule = Join-Path $PSScriptRoot "../../Common/TestHostCS.psm1" +import-module $hostmodule -ErrorAction SilentlyContinue + Describe "Get-Credential Test" -tag "CI" { BeforeAll { $th = New-TestHost diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Implicit.Remoting.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Implicit.Remoting.Tests.ps1 index 0d8dba96326..aa8b6ef4ec2 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Implicit.Remoting.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Implicit.Remoting.Tests.ps1 @@ -1,9 +1,5 @@ - -if (-not (Get-Module TestRemoting -ErrorAction SilentlyContinue)) -{ - $remotingModule = Join-Path $PSScriptRoot "../../Common/TestRemoting.psm1" - Import-Module $remotingModule -} +$remotingModule = Join-Path $PSScriptRoot "../../Common/TestRemoting.psm1" +Import-Module $remotingModule -ErrorAction SilentlyContinue Describe "Implicit remoting and CIM cmdlets with AllSigned and Restricted policy" -tags "Feature" { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 index 19e2b0ea566..e47517c5760 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 @@ -1,8 +1,6 @@ -if ( ! (get-module -ea silentlycontinue TestHostCS )) -{ - $hostmodule = Join-Path $PSScriptRoot "../../Common/TestHostCS.psm1" - import-module $hostmodule -} +$hostmodule = Join-Path $PSScriptRoot "../../Common/TestHostCS.psm1" +import-module $hostmodule -ErrorAction SilentlyContinue + Describe "Read-Host Test" -tag "CI" { BeforeAll { $th = New-TestHost diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Host.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Host.Tests.ps1 index b4890cf965c..1458d751b3e 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Host.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Host.Tests.ps1 @@ -1,18 +1,95 @@ -Describe "Write-Host DRT Unit Tests" -Tags "CI" { +Describe "Write-Host with default Console Host" -Tags "Slow","Feature" { BeforeAll { $powershell = Join-Path -Path $PsHome -ChildPath "powershell" + + $testData = @( + @{ Name = '-Separator'; Command = "Write-Host a,b,c -Separator '+'"; returnCount = 1; returnValue = @("a+b+c") } + @{ Name = '-NoNewline=true'; Command = "Write-Host a,b -NoNewline:`$true;Write-Host a,b"; returnCount = 1; returnValue = @("a ba b") } + @{ Name = '-NoNewline=false'; Command = "Write-Host a1,b1;Write-Host a2,b2"; returnCount = 2; returnValue = @("a1 b1","a2 b2") } + ) + } + + It "write-Host works with '' switch" -TestCases:$testData -Pending:$IsOSX { + param($Command, $returnCount, $returnValue) + + [array]$result = & $powershell -noprofile $Command + + $result.Count | Should Be $returnCount + foreach ($i in 0..($returnCount - 1)) + { + $result[$i] | Should Be $returnValue[$i] + } + } +} + +Describe "Write-Host with wrong colors" -Tags "CI" { + + BeforeAll { + $testWrongColor = @( + @{ ForegroundColor = -1; BackgroundColor = 'Red' } + @{ ForegroundColor = 16; BackgroundColor = 'Red' } + @{ ForegroundColor = 'Red'; BackgroundColor = -1 } + @{ ForegroundColor = 'Red'; BackgroundColor = 16 } + ) + } + + It 'Should throw if color is invalid: ForegroundColor = ; BackgroundColor = ' -TestCases:$testWrongColor { + param($ForegroundColor, $BackgroundColor) + try + { + Write-Host "No output from Write-Host" -ForegroundColor $ForegroundColor -BackgroundColor $BackgroundColor + throw "No Exception!" + } + catch { $_.FullyQualifiedErrorId | Should Be 'CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.WriteHostCommand' } } +} + +Describe "Write-Host with TestHostCS" -Tags "CI" { + + BeforeAll { + $hostmodule = Join-Path $PSScriptRoot "../../Common/TestHostCS.psm1" + import-module $hostmodule -ErrorAction SilentlyContinue + $th = New-TestHost + $rs = [runspacefactory]::Createrunspace($th) + $rs.open() + $ps = [powershell]::Create() + $ps.Runspace = $rs + + $testHostCSData = @( + @{ Name = 'defaults'; Command = "Write-Host a,b,c"; returnCount = 1; returnValue = @("White:Black:a b c:NewLine"); returnInfo = @("a b c") } + @{ Name = '-Object'; Command = "Write-Host -Object a,b,c"; returnCount = 1; returnValue = @("White:Black:a b c:NewLine"); returnInfo = @("a b c") } + @{ Name = '-Separator'; Command = "Write-Host a,b,c -Separator '+'"; returnCount = 1; returnValue = @("White:Black:a+b+c:NewLine"); returnInfo = @("a+b+c") } + @{ Name = '-Separator, colors and -NoNewLine'; Command = "Write-Host a,b,c -Separator ',' -ForegroundColor Yellow -BackgroundColor DarkBlue -NoNewline"; returnCount = 1; returnValue = @("Yellow:DarkBlue:a,b,c:NoNewLine"); returnInfo = @("a,b,c") } + @{ Name = '-NoNewline:$true and colors'; Command = "Write-Host a,b -NoNewline:`$true -ForegroundColor Red -BackgroundColor Green;Write-Host a,b"; returnCount = 2; returnValue = @("Red:Green:a b:NoNewLine", "White:Black:a b:NewLine"); returnInfo = @("a b", "a b") } + @{ Name = '-NoNewline:$false and colors'; Command = "Write-Host a,b -NoNewline:`$false -ForegroundColor Red -BackgroundColor Green;Write-Host a,b"; returnCount = 2; returnValue = @("Red:Green:a b:NewLine","White:Black:a b:NewLine"); returnInfo = @("a b", "a b") } + ) + + } + + AfterAll { + $rs.Close() + $rs.Dispose() + $ps.Dispose() + } + + AfterEach { + $ps.Commands.Clear() + $th.ui.Streams.Clear() + } + + It "Write-Host works with " -TestCases:$testHostCSData { + param($Command, $returnCount, $returnValue, $returnInfo) + $ps.AddScript($Command).Invoke() + $result = $th.ui.Streams.ConsoleOutput - $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" } - ) + $result.Count | Should Be $returnCount + (Compare-Object $result $returnValue -SyncWindow 0).length -eq 0 | Should Be $true - It "write-Host works with '' switch" -TestCases $testData -Pending:$IsOSX { - param($Command, $returnValue) + $result = $th.ui.Streams.Information - & $powershell -noprofile $Command | Should Be $returnValue + $result.Count | Should Be $returnCount + (Compare-Object $result $returnInfo -SyncWindow 0).length -eq 0 | Should Be $true } }