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

AboutCmdletVerbs.Koans.ps1 - Forward slash in $Filename results in wrong string comparison at the end #453

Open DEberhardt opened 3 years ago

DEberhardt commented 3 years ago

Describe "Koan Bug, Issue, or Help Request"

minor typo...

AboutCmdletVerbs.Koans.ps1 Line 25: $FilePath = "$env:TMP/YOUR_PATH.txt"

This works to find the path all the way until the last assertion

Context "The Problematic Assertions"

        $Message = "Cannot find path '$FilePath' because it does not exist."
        { Remove-Item -Path $FilePath -ErrorAction Stop } | Should -Throw -ExpectedMessage $Message

Context "Your Attempts"

    The answers you seek...

Expected an exception, with message 'Cannot find path
'C:\Users\David\AppData\Local\Temp**/**YOUR_PATH.txt' because it does not exist.' to be
thrown, but the message was 'Cannot find path
'C:\Users\David\AppData\Local\Temp**\**YOUR_PATH.txt' because it does not exist.'. from
C:\Users\David\PSKoans\Introduction\AboutCmdletVerbs.Koans.ps1:286 char:15
    +             { Remove-Item -Path $FilePath -ErrorAction Stop } | Shoul ...
    +               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Context "Additional Information"

My local Environment:

PowerShell Module Paths
C:\Users\David\Documents\WindowsPowerShell\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules

PowerShell Version Table

Name                           Value
----                           -----
PSVersion                      5.1.19041.1023
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.1023
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

PowerShell Host

Name             : ConsoleHost
Version          : 5.1.19041.1023
InstanceId       : 3d2266c5-f8cb-40e4-8275-45dd17603f04
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterfa
                   ce
CurrentCulture   : en-GB
CurrentUICulture : en-GB
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

Installed NET Frameworks

PSChildName                      Version        Release
-----------                      -------        -------
v2.0.50727                       2.0.50727.4927
v3.0                             3.0.30729.4926
Windows Communication Foundation 3.0.4506.4926
Windows Presentation Foundation  3.0.6920.4902
v3.5                             3.5.30729.4926
Client                           4.8.04084      528372
Full                             4.8.04084      528372
Client                           4.0.0.0

Relevant Modules (installed)

❯ Get-Module Pester

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     4.10.1     Pester                              {Add-AssertionOperator, ...

❯ Get-Module Pester -ListAvailable

    Directory: C:\Users\David\Documents\WindowsPowerShell\Modules

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     5.2.2      Pester                              {Invoke-Pester, Describe...
Script     5.2.1      Pester                              {Invoke-Pester, Describe...
Script     5.1.1      Pester                              {Invoke-Pester, Describe...
Script     4.10.1     Pester                              {Describe, Context, It, ...

    Directory: C:\Program Files\WindowsPowerShell\Modules

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     3.4.0      Pester                              {Describe, Context, It, ...

Changing Line 25 to $FilePath = "$env:TMP\YOUR_PATH.txt" works for me. Is this intentional to highlight? I did not think so as it isn't described...

vexx32 commented 3 years ago

Hmm. That'll be tricky, it'll break for folks on non-Windows OSes if we change that to be \ for everyone. I suppose we should change this to use [IO.Path]::DirectorySeparatorChar to find the right char to use in this instance.

paulhey commented 1 year ago

An easy solution would be to change:

$FilePath = "$env:TMP/YOUR_PATH.txt"

to:

$FilePath = (Join-Path -Path $env:TMP -ChildPath 'YOUR_PATH.txt')

This seems to work correctly on PS7 and PS5.1 on Windows. On PS7 on Linux, I had to also define $env:TMP before running Show-Karma by using:

[Environment]::SetEnvironmentVariable('TMP','/tmp')

as it doesn't seem to be defined by default in my installation and I was getting errors like Access to the path '/YOUR_PATH.txt' is denied..