Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions test/powershell/Common/TestHostCS.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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:
// <Foregraund color name> : <Background color name> : <'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)
Expand All @@ -191,7 +202,7 @@ namespace TestHost

public override void WriteLine(string value)
{
Streams.ConsoleOutput.Add(value);
Streams.ConsoleOutput.Add("::"+value+":NewLine");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use `n, or I guess since this is actually C#, \n? Then you don't need :NoNewline.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idea is that we shouldn't mix "user" chars and "code" chars. If we would use "n" and value is "testn" then we will not be able to determine whether this character from the original string or generated by code (WriteLine).
We could use "test:n" or "test:\n" but if we look at the text of tests, it is easier to read NewLineandNoNewLine, especially since this is key information. We only need to avoid using ':' in value`. I believe that this is not a problem for test environment.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the preceding :: necessary?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means that colors are not specified. We should use one template for all outputs. In the future someone can use "-Split" to analyze results regardless of which Write/WriteLine method is used.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If color is specified, I would use color:output as the expected string to test the result, so why :: is needed?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here colors is not specified and it is Empty. I believe both output methods (with colors and w/o colors) should return strings in the same format. I would make "Empty:Empty:teststring:NewLine" but it is seems unneeded complicity.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point now. It sounds over complex and useful at the same time 😄
I'm OK to have this change in TestHostCS.psm1, but could you please put more comments there for the Write/WriteLine APIs so that people can a bit easier to understand the behavior?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}

public override void WriteProgress(long sourceId, ProgressRecord record)
Expand All @@ -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
Expand Down
7 changes: 2 additions & 5 deletions test/powershell/Host/HostUtilities.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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" {

Expand Down
Original file line number Diff line number Diff line change
@@ -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' {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -27,8 +21,9 @@ Describe "Out-Host Tests" -tag CI {
}
It "Out-Host writes to host output" {
$stringToWrite = "thing to write"
$stringExpected = "::$($stringToWrite):NewLine"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: the sub expression $() is not needed here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without $() it is not worked 😕

Copy link
Copy Markdown
Member

@daxian-dbw daxian-dbw Feb 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see, the preceding colons.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's the colon at :NewLine. Maybe use another special character as the delimiter?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is debug output - we can use any perfectly visible symbol.
The only restriction is that not to use it in the source debug string ().
This character ':' is rarely used, so I suppose it is appropriate.
Or I missed something?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just less intuitive to construct the expected string if you are using a variable. But currently there is only one instance where $() is required. Let's keep it for now, and we can always change to something else if there are any complaints.

$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
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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" {

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 '<Name>' 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 = <ForegroundColor>; BackgroundColor = <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 <Name>" -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 '<Name>' 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
}
}