Skip to content

User defined alias v breaks the Assert-Equivalent due to command resolution order in PowerShell #55

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

Open
kborowinski opened this issue Mar 31, 2025 · 0 comments · May be fixed by #56
Open

Comments

@kborowinski
Copy link

kborowinski commented Mar 31, 2025

Steps to reproduce:

    Set-Alias -Name v -Value 'vncviewer.exe'; 1 | Assert-Equivalent -Expected 2

Output:

v: C:\Program Files\WindowsPowerShell\Modules\Assert\0.9.7\src\Equivalence\Assert-Equivalent.ps1:598
Line |
 598 |      v "`$Expected has type $(Get-Type $Expected), `$Actual has type $ …
     |      ~
     | The term 'vncviewer.exe' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify
     | that the path is correct and try again.
v: C:\Program Files\WindowsPowerShell\Modules\Assert\0.9.7\src\Equivalence\Assert-Equivalent.ps1:604
Line |
 604 |          v "`$Expected is a value (value type, string, single value ar …
     |          ~
     | The term 'vncviewer.exe' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify
     | that the path is correct and try again.
v: C:\Program Files\WindowsPowerShell\Modules\Assert\0.9.7\src\Equivalence\Assert-Equivalent.ps1:207
Line |
 207 |          v "Equivalency comparator is used, values will be compared fo …
     |          ~
     | The term 'vncviewer.exe' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify
     | that the path is correct and try again.
v: C:\Program Files\WindowsPowerShell\Modules\Assert\0.9.7\src\Equivalence\Assert-Equivalent.ps1:256
Line |
 256 |      v "Comparing values as $(Format-Nicely (Get-Type $Expected)) beca …
     |      ~
     | The term 'vncviewer.exe' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify
     | that the path is correct and try again.
v: C:\Program Files\WindowsPowerShell\Modules\Assert\0.9.7\src\Equivalence\Assert-Equivalent.ps1:262
Line |
 262 |          v -Difference "`$Actual is not equivalent to $(Format-Nicely
     |          ~
     | The term 'vncviewer.exe' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify
     | that the path is correct and try again.
OperationStopped: C:\Program Files\WindowsPowerShell\Modules\Assert\0.9.7\src\Equivalence\Assert-Equivalent.ps1:676
Line |
 676 |          throw [Assertions.AssertionException]$message
     |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Expected and actual are not equivalent! Expected: 2  Actual: 1  Summary: Expected '2' to be equivalent to the actual value, but got '1'.

The issue is caused by the command resolution order in PowerShell that gives aliases higher precedence than functions.
Assert-Equivalent defines v function (line 519) that is overshadowed by the v alias:

function v {
    [CmdletBinding()]
    param(
        [String] $String,
        [Switch] $Difference,
        [Switch] $Equivalence,
        [Switch] $Skip
    )

    # we are using implict variable $Path
    # from the parent scope, this is ugly
    # and bad practice, but saves us ton of
    # coding and boilerplate code

    $p = ""
    $p += if ($null -ne $Path) {
        "($Path)"
    }

    $p += if ($Difference) {
        " DIFFERENCE"
    }

    $p += if ($Equivalence) {
        " EQUIVALENCE"
    }

    $p += if ($Skip) {
        " SKIP"
    }

    $p += if (""-ne $p) {
        " - "
    }

    Write-Verbose ("$p$String".Trim() + " ")
}

@nohwnd: Since the assert functions have been moved to Pester v6, would you accept PR to Assert module fixing the issue? I have noticed that in Pester v6 the v function has been renamed to Write-EquivalenceResult. I could backport it to the Assert module if you are still accepting the PRs.

For others, the workaround is not to use v alias 🤷‍♂️

Edit:
I have done the PR anyways... could not help myself 😜

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant