Skip to content

Commit fd70adb

Browse files
iSazonovdaxian-dbw
authored andcommitted
Improve console cmdlets tests (PowerShell#3101)
* Improve console cmdlets tests Main improvements refer to tests of the Write-Host cmdlet. Original tests: 1. Slow because run external processes 2. Don't test colors and -NoNewLine in fact. 1. The original tests is preserved (deleted one as redundant) but marked by 'Slow' tag. They is preserved because they actually check the output on the work, not a test console. 2. Add negative color tests. (Code cover grow!) 3. Add tests based on TestHostCS. This test host has been refined so we can see colors and a new line in output. 4. Add minor fixes for test modules loads. Also I add support for Information stream. I originally planned to use it but not actually used. However, I have left this as a useful addition for future tests. I wonder that a Write-Host console output is duplicated in Information Stream - Is it by design? I left a debug print on this matter in the test code. * Fix after code review * Corrections after code review Suppress import-module warnings Rename Describes Add "-Object" test Add Stream.Information tests with TestHostCS * Add checks for Streams.Information and add comments
1 parent ca12001 commit fd70adb

8 files changed

Lines changed: 125 additions & 44 deletions

File tree

test/powershell/Common/TestHostCS.psm1

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,25 @@ namespace TestHost
169169
return ss;
170170
}
171171
172+
// Cmdlets call 'Write' and 'WriteLine' methods implicitly.
173+
// To see difference between 'Write' and 'WriteLine' with and w/o colors in the debug output
174+
// we need use a meta information.
175+
// So we make a output string as:
176+
// <Foregraund color name> : <Background color name> : <'user value'> : <'NewLine' or 'NoNewLine'>
177+
//
172178
public override void Write(string value)
173179
{
174-
Streams.ConsoleOutput.Add(value);
180+
Streams.ConsoleOutput.Add("::"+value+":NoNewLine");
175181
}
176182
177183
public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
178184
{
179-
Streams.ConsoleOutput.Add(value);
185+
Streams.ConsoleOutput.Add(foregroundColor+":"+backgroundColor+":"+value+":NoNewLine");
186+
}
187+
188+
public override void WriteLine(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
189+
{
190+
Streams.ConsoleOutput.Add(foregroundColor+":"+backgroundColor+":"+value+":NewLine");
180191
}
181192
182193
public override void WriteDebugLine(string message)
@@ -191,7 +202,7 @@ namespace TestHost
191202
192203
public override void WriteLine(string value)
193204
{
194-
Streams.ConsoleOutput.Add(value);
205+
Streams.ConsoleOutput.Add("::"+value+":NewLine");
195206
}
196207
197208
public override void WriteProgress(long sourceId, ProgressRecord record)
@@ -208,6 +219,15 @@ namespace TestHost
208219
{
209220
Streams.Warning.Add(message);
210221
}
222+
223+
public override void WriteInformation(InformationRecord record)
224+
{
225+
HostInformationMessage hostOutput = record.MessageData as HostInformationMessage;
226+
if (hostOutput != null) {
227+
string message = hostOutput.Message;
228+
Streams.Information.Add(message);
229+
}
230+
}
211231
}
212232
213233
public class TestHost : PSHost

test/powershell/Host/HostUtilities.Tests.ps1

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
if (-not (Get-Module TestRemoting -ErrorAction SilentlyContinue))
2-
{
3-
$remotingModule = Join-Path $PSScriptRoot "../Common/TestRemoting.psm1"
4-
Import-Module $remotingModule
5-
}
1+
$remotingModule = Join-Path $PSScriptRoot "../Common/TestRemoting.psm1"
2+
Import-Module $remotingModule
63

74
Describe "InvokeOnRunspace method argument error handling" -tags "Feature" {
85

test/powershell/Language/Scripting/ParameterBinding.Tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Import-Module $PSScriptRoot\..\..\Common\Test.Helpers.psm1
2-
Import-Module $PSScriptRoot\..\..\Common\TestHostCS.psm1
1+
Import-Module $PSScriptRoot\..\..\Common\Test.Helpers.psm1 -ErrorAction SilentlyContinue
2+
Import-Module $PSScriptRoot\..\..\Common\TestHostCS.psm1 -ErrorAction SilentlyContinue
33

44
Describe "Tests for parameter binding" -Tags "CI" {
55
Context 'Test of Mandatory parameters' {

test/powershell/Modules/Microsoft.PowerShell.Core/Out-Host.Tests.ps1

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
2-
if ( ! (get-module -ea silentlycontinue TestHostCS ))
3-
{
4-
# this is sensitive to the location of this test and the common directory"
5-
$pestertestroot = resolve-path "$psscriptroot/../.."
6-
$common = join-path $pestertestroot Common
7-
$hostmodule = join-path $common TestHostCS.psm1
8-
import-module $hostmodule
9-
}
1+
# this is sensitive to the location of this test and the common directory"
2+
$hostmodule = Join-Path $PSScriptRoot "../../Common/TestHostCS.psm1"
3+
import-module $hostmodule -ErrorAction SilentlyContinue
104

115
Describe "Out-Host Tests" -tag CI {
126
BeforeAll {
@@ -27,8 +21,9 @@ Describe "Out-Host Tests" -tag CI {
2721
}
2822
It "Out-Host writes to host output" {
2923
$stringToWrite = "thing to write"
24+
$stringExpected = "::$($stringToWrite):NewLine"
3025
$result = $ps.AddScript("Out-Host -inputobject '$stringToWrite'").Invoke()
3126
$th.UI.Streams.ConsoleOutput.Count | should be 1
32-
$th.UI.Streams.ConsoleOutput[0] | should be $stringToWrite
27+
$th.UI.Streams.ConsoleOutput[0] | should be $stringExpected
3328
}
3429
}

test/powershell/Modules/Microsoft.PowerShell.Security/GetCredential.Tests.ps1

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
if ( ! (get-module -ea silentlycontinue TestHostCS ))
2-
{
3-
$hostmodule = Join-Path $PSScriptRoot "../../Common/TestHostCS.psm1"
4-
import-module $hostmodule
5-
}
1+
$hostmodule = Join-Path $PSScriptRoot "../../Common/TestHostCS.psm1"
2+
import-module $hostmodule -ErrorAction SilentlyContinue
3+
64
Describe "Get-Credential Test" -tag "CI" {
75
BeforeAll {
86
$th = New-TestHost

test/powershell/Modules/Microsoft.PowerShell.Utility/Implicit.Remoting.Tests.ps1

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
2-
if (-not (Get-Module TestRemoting -ErrorAction SilentlyContinue))
3-
{
4-
$remotingModule = Join-Path $PSScriptRoot "../../Common/TestRemoting.psm1"
5-
Import-Module $remotingModule
6-
}
1+
$remotingModule = Join-Path $PSScriptRoot "../../Common/TestRemoting.psm1"
2+
Import-Module $remotingModule -ErrorAction SilentlyContinue
73

84
Describe "Implicit remoting and CIM cmdlets with AllSigned and Restricted policy" -tags "Feature" {
95

test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
if ( ! (get-module -ea silentlycontinue TestHostCS ))
2-
{
3-
$hostmodule = Join-Path $PSScriptRoot "../../Common/TestHostCS.psm1"
4-
import-module $hostmodule
5-
}
1+
$hostmodule = Join-Path $PSScriptRoot "../../Common/TestHostCS.psm1"
2+
import-module $hostmodule -ErrorAction SilentlyContinue
3+
64
Describe "Read-Host Test" -tag "CI" {
75
BeforeAll {
86
$th = New-TestHost
Lines changed: 85 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,95 @@
1-
Describe "Write-Host DRT Unit Tests" -Tags "CI" {
1+
Describe "Write-Host with default Console Host" -Tags "Slow","Feature" {
22

33
BeforeAll {
44
$powershell = Join-Path -Path $PsHome -ChildPath "powershell"
5+
6+
$testData = @(
7+
@{ Name = '-Separator'; Command = "Write-Host a,b,c -Separator '+'"; returnCount = 1; returnValue = @("a+b+c") }
8+
@{ Name = '-NoNewline=true'; Command = "Write-Host a,b -NoNewline:`$true;Write-Host a,b"; returnCount = 1; returnValue = @("a ba b") }
9+
@{ Name = '-NoNewline=false'; Command = "Write-Host a1,b1;Write-Host a2,b2"; returnCount = 2; returnValue = @("a1 b1","a2 b2") }
10+
)
11+
}
12+
13+
It "write-Host works with '<Name>' switch" -TestCases:$testData -Pending:$IsOSX {
14+
param($Command, $returnCount, $returnValue)
15+
16+
[array]$result = & $powershell -noprofile $Command
17+
18+
$result.Count | Should Be $returnCount
19+
foreach ($i in 0..($returnCount - 1))
20+
{
21+
$result[$i] | Should Be $returnValue[$i]
22+
}
23+
}
24+
}
25+
26+
Describe "Write-Host with wrong colors" -Tags "CI" {
27+
28+
BeforeAll {
29+
$testWrongColor = @(
30+
@{ ForegroundColor = -1; BackgroundColor = 'Red' }
31+
@{ ForegroundColor = 16; BackgroundColor = 'Red' }
32+
@{ ForegroundColor = 'Red'; BackgroundColor = -1 }
33+
@{ ForegroundColor = 'Red'; BackgroundColor = 16 }
34+
)
35+
}
36+
37+
It 'Should throw if color is invalid: ForegroundColor = <ForegroundColor>; BackgroundColor = <BackgroundColor>' -TestCases:$testWrongColor {
38+
param($ForegroundColor, $BackgroundColor)
39+
try
40+
{
41+
Write-Host "No output from Write-Host" -ForegroundColor $ForegroundColor -BackgroundColor $BackgroundColor
42+
throw "No Exception!"
43+
}
44+
catch { $_.FullyQualifiedErrorId | Should Be 'CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.WriteHostCommand' }
545
}
46+
}
47+
48+
Describe "Write-Host with TestHostCS" -Tags "CI" {
49+
50+
BeforeAll {
51+
$hostmodule = Join-Path $PSScriptRoot "../../Common/TestHostCS.psm1"
52+
import-module $hostmodule -ErrorAction SilentlyContinue
53+
$th = New-TestHost
54+
$rs = [runspacefactory]::Createrunspace($th)
55+
$rs.open()
56+
$ps = [powershell]::Create()
57+
$ps.Runspace = $rs
58+
59+
$testHostCSData = @(
60+
@{ Name = 'defaults'; Command = "Write-Host a,b,c"; returnCount = 1; returnValue = @("White:Black:a b c:NewLine"); returnInfo = @("a b c") }
61+
@{ Name = '-Object'; Command = "Write-Host -Object a,b,c"; returnCount = 1; returnValue = @("White:Black:a b c:NewLine"); returnInfo = @("a b c") }
62+
@{ Name = '-Separator'; Command = "Write-Host a,b,c -Separator '+'"; returnCount = 1; returnValue = @("White:Black:a+b+c:NewLine"); returnInfo = @("a+b+c") }
63+
@{ 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") }
64+
@{ 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") }
65+
@{ 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") }
66+
)
67+
68+
}
69+
70+
AfterAll {
71+
$rs.Close()
72+
$rs.Dispose()
73+
$ps.Dispose()
74+
}
75+
76+
AfterEach {
77+
$ps.Commands.Clear()
78+
$th.ui.Streams.Clear()
79+
}
80+
81+
It "Write-Host works with <Name>" -TestCases:$testHostCSData {
82+
param($Command, $returnCount, $returnValue, $returnInfo)
83+
$ps.AddScript($Command).Invoke()
684

85+
$result = $th.ui.Streams.ConsoleOutput
786

8-
$testData = @(
9-
@{ Name = 'NoNewline';Command = "Write-Host a,b -Separator ',' -ForegroundColor Yellow -BackgroundColor DarkBlue -NoNewline"; returnValue = "a,b" }
10-
@{ Name = 'Separator';Command = "Write-Host a,b,c -Separator '+'"; returnValue = "a+b+c" }
11-
)
87+
$result.Count | Should Be $returnCount
88+
(Compare-Object $result $returnValue -SyncWindow 0).length -eq 0 | Should Be $true
1289

13-
It "write-Host works with '<Name>' switch" -TestCases $testData -Pending:$IsOSX {
14-
param($Command, $returnValue)
90+
$result = $th.ui.Streams.Information
1591

16-
& $powershell -noprofile $Command | Should Be $returnValue
92+
$result.Count | Should Be $returnCount
93+
(Compare-Object $result $returnInfo -SyncWindow 0).length -eq 0 | Should Be $true
1794
}
1895
}

0 commit comments

Comments
 (0)