vexx32 / PSKoans

A simple, fun, and interactive way to learn the PowerShell language through Pester unit testing.
GNU General Public License v3.0
1.72k stars 176 forks source link

AboutRedirection misses some spaces in output? #458

Open DEberhardt opened 3 years ago

DEberhardt commented 3 years ago

Describe "Koan Bug, Issue, or Help Request"


    You have not yet reached enlightenment.

    The answers you seek...

Expected strings to be the same, but they were different.
Expected length: 82
Actual length:   78
Strings differ at index 0.
Expected: '    + Cate...'
But was:  '+ Category...'

Context "The Problematic Assertions"

        It 'can use the error stream (number 2)' {
            # Redirecting errors can generally only be done from any scope above where the error is generated.
            & { Write-Error 'The gasoline is in the water.' } 2> $FilePath
            $FileContent = Get-Content -Path $FilePath

            ' Write-Error ''The gasoline is in the water.''  : The gasoline is in the water.' | Should -Be $FileContent[0]
            "+ CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException" |
                Should -Be $FileContent[4]
            "+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException" |
                Should -Be $FileContent[5]
        }

Context "Your Attempts"

The last two lines were pre-filled, so I didn't even touch them. Testing the redirect output to file myself, I get the following output:

❯ Get-Content -Path C:\Temp\test.txt

 Write-Error 'The gasoline is in the water.'  : The gasoline is in the water.
At line:1 char:1
+ & { Write-Error 'The gasoline is in the water.' } 2> C:\Temp\Test.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

the following works:

        It 'can use the error stream (number 2)' {
            # Redirecting errors can generally only be done from any scope above where the error is generated.
            & { Write-Error 'The gasoline is in the water.' } 2> $FilePath
            $FileContent = Get-Content -Path $FilePath

            ' Write-Error ''The gasoline is in the water.''  : The gasoline is in the water.' | Should -Be $FileContent[0]
            '    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException' |
                Should -Be $FileContent[4]
            '    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException' |
                Should -Be $FileContent[5]
        }

Context "Additional Information"

I think it would be better/more consistent to do something like:

            '+ CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException' |
                Should -Be $FileContent[4].TrimStart()
            '+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException' |
                Should -Be $FileContent[5].TrimStart()