-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Approve console cmdlets tests #3101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 { | ||
|
|
@@ -27,8 +21,9 @@ Describe "Out-Host Tests" -tag CI { | |
| } | ||
| It "Out-Host writes to host output" { | ||
| $stringToWrite = "thing to write" | ||
| $stringExpected = "::$($stringToWrite):NewLine" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: the sub expression
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without $() it is not worked 😕
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see, the preceding colons.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it's the colon at
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is debug output - we can use any perfectly visible symbol.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| $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,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 | ||
| } | ||
| } |
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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" andvalueis "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 readNewLineandNoNewLine, especially since this is key information. We only need to avoid using ':' invalue`. I believe that this is not a problem for test environment.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the preceding
::necessary?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:outputas the expected string to test the result, so why::is needed?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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/WriteLineAPIs so that people can a bit easier to understand the behavior?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.