Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upGitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
Should -Be gives confusing error message comparing arrays that are not enumerated. #1154
Comments
|
Agreed, this needs to get better. At the very least the error should show that there is a wrapping array as: Expected @(1, 2, 3), but got @(@(1, 2, 3)).Or better: Expected a collection @(1, 2, 3) with length 3, but got a collection @(@(1, 2, 3)) with length 1.Problem is finding the right balance where this kind output stays readable even for bigger objects. Probably the best approach here is to list the info that stays short on the top and progress to expanding the objects on the bottom. Expected a collection with length 3, but got a collection with length 1.
Expected:
Type: [Object[]]
Length: 3
Content:
@(1, 2, 3)
Actual:
Type: [Object[]]
Length: 1
Content:
@(
@(1, 2, 3)
)I will start with the simplest fix, and solve it better when Assert merges into Pester, because the formatting there is solved better. |
|
This problem should lie in the Format.psm1 module where the formatting is defined. |
|
Demo:
Something like this might work to fix both this issue and the string-specific error message that the lines were there to fix. Looks kinda ugly though...
During testing I also noticed that this test is passing, bug in
|
|
The problem here is that when you pass an array through the pipeline it enumerates it, but when you wrap the array into an extra array (by using If we have an array after the pipeline we know that it must have been wrapped (otherwise the pipeline would enumerate it) and so we want to add extra array to the incoming value when it is array and print it via the format collection.
The pipeline unrolls the array created by 1 | foreach { 1 -eq $_ } # -> true 1 = 1
,1 | foreach { 1 -eq $_ } # -> true 1 = 1
,,1 | foreach { 1 -eq $_ } # -> false @(1) != 1 |
1. General summary of the issue
In writing tests to ensure the accurate fixing of the issue resolved by this PR, I discovered that
Shouldcan and will differentiate between a complete array passed over the pipeline and an enumerated array, and it seems to expect the latter.Simple repro:
2. Describe Your Environment
Operating System, Pester version, and PowerShell version:
3. Expected Behavior
Shouldshould more clearly indicate what is happening, with either symbolic representation of the different element that more closely reflects what's happened on the command line, or by explicitly calling out the fact that the two arrays are different in some other way.Bonus: perhaps an optional switch or operator might be a good idea that doesn't check for this, however this is getting picked up.
4.Current Behavior
Clearly, it is able to distinguish between what is a multidimensional array that has been enumerated, and a flat array. However, its error message is the polar opposite of clear.😄
Simple example:
6. Context
PowerShell/PowerShell#8407
It was noted that
Get-Variablepasses collections whole over the pipeline, thus similar to the original example, this too throws a strangely confusing error. This particular case is fixed in the linked PR, however, making the command properly enumerate collections when outputting.