Approve console cmdlets tests#3101
Conversation
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.
| @@ -1,5 +1,14 @@ | |||
| Import-Module $PSScriptRoot\..\..\Common\Test.Helpers.psm1 | |||
| Import-Module $PSScriptRoot\..\..\Common\TestHostCS.psm1 | |||
| if ( ! (get-module -ea silentlycontinue Test.Helpers )) | |||
There was a problem hiding this comment.
Why? Import-Module does this already, right?
There was a problem hiding this comment.
"Don't trust code patterns... Don't trust code patterns... Don't trust code patterns..." :-)
I am sorry but I cleaned up all files with bad pattern.
|
|
||
| if ( ! ("TestHost.TestHost" -as "type" )) { | ||
| $t = add-Type -pass $definition -ref $references | ||
| $t = add-Type -pass $definition -ref $references -WarningAction SilentlyContinue |
There was a problem hiding this comment.
What warnings are being suppressed? I assume it's a C# warning - maybe disable with a pragma instead so we don't ignore future warnings that maybe shouldn't be ignored.
There was a problem hiding this comment.
I check CI logs - no warnings. I see the warnings only locally.
So revert the change.
| $ps.Runspace = $rs | ||
|
|
||
| $testHostCSData = @( | ||
| @{ Name = 'defaults';Command = "Write-Host a,b,c"; returnCount = 1; returnValue = @("White:Black:a b c:NewLine") } |
There was a problem hiding this comment.
I think this would be easier to read and review if each key/value pair was on a line by itself.
| public override void WriteLine(string value) | ||
| { | ||
| Streams.ConsoleOutput.Add(value); | ||
| Streams.ConsoleOutput.Add("::"+value+":NewLine"); |
There was a problem hiding this comment.
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.
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.
There was a problem hiding this comment.
Is the preceding :: necessary?
There was a problem hiding this comment.
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.
If color is specified, I would use color:output as the expected string to test the result, so why :: is needed?
There was a problem hiding this comment.
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.
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?
| } | ||
| It "Out-Host writes to host output" { | ||
| $stringToWrite = "thing to write" | ||
| $stringExpected = "::$($stringToWrite):NewLine" |
There was a problem hiding this comment.
Minor: the sub expression $() is not needed here.
There was a problem hiding this comment.
Without $() it is not worked 😕
There was a problem hiding this comment.
Ah, I see, the preceding colons.
There was a problem hiding this comment.
No, it's the colon at :NewLine. Maybe use another special character as the delimiter?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
|
|
||
| It "write-Host works with '<Name>' switch" -TestCases $testData -Pending:$IsOSX { | ||
| param($Command, $returnValue) | ||
| # I wonder that a console output is duplicated in Information Stream - Is it by design? |
There was a problem hiding this comment.
Yes, Write-Host creates information record. #2674 was a PR to speed up Write-Host by deferring initialization of its properties.
| # I wonder that a console output is duplicated in Information Stream - Is it by design? | ||
| Write-Host $th.ui.Streams.Information.Count | ||
| Write-Host $th.ui.Streams.Information[0] | ||
| Write-Host $th.ui.Streams.Information[1] |
There was a problem hiding this comment.
Maybe we can remove lines 83 to 85?
There was a problem hiding this comment.
Currently we haven't test for the "by design" feature so we should convert the lines to real tests.
| & $powershell -noprofile $Command | Should Be $returnValue | ||
| $result.Count | Should Be $returnCount | ||
| $result[0] | Should Be $returnValue[0] | ||
| $result[1] | Should Be $returnValue[1] |
There was a problem hiding this comment.
Maybe this should be changed to something similar to the following, in case a test data with more than 2 results gets added to $testHostCSData in future.
foreach ($i in 0..($returnCount - 1))
{
$result[$i] | Should Be $returnValue[$i]
}
There was a problem hiding this comment.
What scenarios do you have in mind? It seems these tests verify everything we need and look simple and clear.
There was a problem hiding this comment.
Thanks for making this change!
Yes, tests for Write-Host is thorough enough, but it's better to set a good pattern for other people to copy in their tests.
| @@ -1,18 +1,91 @@ | |||
| Describe "Write-Host DRT Unit Tests" -Tags "CI" { | |||
| Describe "Write-Host DRT Unit Tests" -Tags "Slow","Feature" { | |||
There was a problem hiding this comment.
"Write-Host DRT Unit Tests"
Can you change this description to be more specific? Other tests here seem also DRT unit tests.
There was a problem hiding this comment.
Fixed.
Maybe remove Feature tag?
There was a problem hiding this comment.
I found this:
Microsoft.PowerShell.Management\Start-Process.Tests.ps1:1:Describe "Start-Process" -Tags @("CI","SLOW") {
So let's keep both slow and feature here.
| public override void WriteLine(string value) | ||
| { | ||
| Streams.ConsoleOutput.Add(value); | ||
| Streams.ConsoleOutput.Add("::"+value+":NewLine"); |
There was a problem hiding this comment.
Is the preceding :: necessary?
| [array]$result = & $powershell -noprofile $Command | ||
| $result.Count | Should Be $returnCount | ||
| $result[0] | Should Be $returnValue[0] | ||
| $result[1] | Should Be $returnValue[1] |
There was a problem hiding this comment.
It's unlikely new test data will be aded to $testData, but I think it's better to use the same foreach ($i in 0..($returnCount - 1)) pattern (or something similar).
Suppress import-module warnings Rename Describes Add "-Object" test Add Stream.Information tests with TestHostCS
|
@PowerShellTeam We have a problem with CIs:
|
|
I re-ran the Linux Ci and this time there is no build break. It's possible that the myget feed we used for installing |
|
@daxian-dbw Thanks for code review! |
Main improvements refer to tests of the Write-Host cmdlet.
Original tests:
Done:
by 'Slow' tag. They is preserved because they actually check the output
on the work, not a test console.
can see colors and a new line in output.
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.