Skip to content

New-Fixture throws when $pwd is used as -parth argument #114

@ghost

Description

Hi!

I came across some minor "bug" when using $pwd or $(pwd) directly as -Path argument:
New-Fixture -Path $pwd
New-Fixture -Path $(pwd)

[W0216 12:44:14 C:\Temp]> New-Fixture -path $Pwd -name "Out-Log"
Method invocation failed because [System.Management.Automation.PathInfo] doesn't contain a method named 'TrimStart'.
At C:\Users\michael.kargl\Documents\WindowsPowerShell\Modules\Pester\Pester.psm1:197 char:9

  •     $path = $path.TrimStart(".")
    
  •     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (:) [], RuntimeException
    • FullyQualifiedErrorId : MethodNotFound

Method invocation failed because [System.Management.Automation.PathInfo] doesn't contain a method named 'TrimStart'.
At C:\Users\michael.kargl\Documents\WindowsPowerShell\Modules\Pester\Pester.psm1:198 char:9

  •     $path = $path.TrimStart("\")
    
  •     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (:) [], RuntimeException
    • FullyQualifiedErrorId : MethodNotFound

Method invocation failed because [System.Management.Automation.PathInfo] doesn't contain a method named 'TrimStart'.
At C:\Users\michael.kargl\Documents\WindowsPowerShell\Modules\Pester\Pester.psm1:199 char:9

  •     $path = $path.TrimStart("/")
    
  •     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (:) [], RuntimeException
    • FullyQualifiedErrorId : MethodNotFound

New-Fixture -Path "$pwd"
works just fine.

The reason is because of powershell's dynamic typing:
$PWD stores the value of $(pwd) which is a System.Management.Automation.PathInfo Object.

Possible fix:
Changing the New-Fixture Param section to:
param(
[string] $path,
[string] $name
)

This way only strings would be allowed as path and fix the minor bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions