-
Notifications
You must be signed in to change notification settings - Fork 7.2k
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
Fix Issue in Select-Object where Hashtable members (e.g., Keys
) cannot be used with -Property or -ExpandProperty
#11097
Fix Issue in Select-Object where Hashtable members (e.g., Keys
) cannot be used with -Property or -ExpandProperty
#11097
Conversation
Note for reviewers -- the Select-Object test file hasn't been updated in a long time (still using tabs instead of spaces) so I included a format-only commit 6544c98 to sort that separately. You'll probably want to review that commit individually and then the following one for added tests. No existing tests were modified in this PR apart from the formatting fixes. I only did a quick tabs-to-spaces and then auto-format commit, so if there are specific style issues you also want to fix while I'm there let me know, we can fix them either here or in a follow up PR as necessary. 🙂 |
Also, I think the Codacy suggestion to make |
src/System.Management.Automation/FormatAndOutput/common/Utilities/Mshexpression.cs
Show resolved
Hide resolved
src/System.Management.Automation/FormatAndOutput/common/Utilities/Mshexpression.cs
Show resolved
Hide resolved
src/System.Management.Automation/FormatAndOutput/common/Utilities/Mshexpression.cs
Show resolved
Hide resolved
Hmm, interesting. Didn't expect to break that, but there you have it. |
@PoshChan please remind me in 17 hours |
Keys
) cannot be used with -Property or -ExpandPropertyKeys
) cannot be used with -Property or -ExpandProperty
@vexx32 The PR description is more complex than the fix :-) |
@vexx32, this is the reminder you requested 17 hours ago |
That was simpler than I expected! 🎉 |
@PoshChan please retry macos |
@vexx32, successfully started retry of |
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
src/System.Management.Automation/FormatAndOutput/common/Utilities/Mshexpression.cs
Outdated
Show resolved
Hide resolved
src/System.Management.Automation/FormatAndOutput/common/Utilities/Mshexpression.cs
Outdated
Show resolved
Hide resolved
src/System.Management.Automation/FormatAndOutput/common/Utilities/Mshexpression.cs
Outdated
Show resolved
Hide resolved
src/System.Management.Automation/FormatAndOutput/common/Utilities/Mshexpression.cs
Outdated
Show resolved
Hide resolved
src/System.Management.Automation/FormatAndOutput/common/Utilities/Mshexpression.cs
Show resolved
Hide resolved
src/System.Management.Automation/FormatAndOutput/common/Utilities/Mshexpression.cs
Outdated
Show resolved
Hide resolved
test/powershell/Modules/Microsoft.PowerShell.Utility/Select-Object.Tests.ps1
Show resolved
Hide resolved
- Fix an issue where Select-Object and similar could not resolve member names from hashtables due to them carelessly converting them. - Allows both hashtable keys/values and actual members to be referenced. Precedence is given to the hashtable keys first to avoid collisions.
- Add test for Count & Length members - Use base target object instead of wrapping hashtable
6de53fe
to
74e066c
Compare
@vexx32 Please look CI failures. |
- Still had tab stops in some places, replaced everything with spaces. - removed some accidentally duplicated code - 'Format Document' to ensure consistent indentation. Sorry about the diff, this file was a bit of a mess 🙏
8beeb21
to
cfea794
Compare
test/powershell/Modules/Microsoft.PowerShell.Utility/Select-Object.Tests.ps1
Outdated
Show resolved
Hide resolved
test/powershell/Modules/Microsoft.PowerShell.Utility/Select-Object.Tests.ps1
Outdated
Show resolved
Hide resolved
test/powershell/Modules/Microsoft.PowerShell.Utility/Select-Object.Tests.ps1
Outdated
Show resolved
Hide resolved
@vexx32 The test failure in mac CI is a known issue. Can you please check on the Windows failure? |
That's one I haven't seen before. Something to do with remoting? Not sure what to make of it 😅
|
@PoshChan please retry windows |
@daxian-dbw, did not find any matching pull request checks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests contain many style fixes - please move it in separate PR.
{ | ||
members = target.Members.Match( | ||
_stringValue, | ||
PSMemberTypes.Properties | PSMemberTypes.PropertySet | PSMemberTypes.Dynamic); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We evaluate this again. Maybe put this in a variable before line 172?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is evaluated against the wrapped target object in the prior case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean PSMemberTypes.Properties | PSMemberTypes.PropertySet | PSMemberTypes.Dynamic
src/System.Management.Automation/FormatAndOutput/common/Utilities/Mshexpression.cs
Outdated
Show resolved
Hide resolved
$result = $(Select-Object -InputObject $dirObject -Last $TestLength).Length | ||
$expected = $dirObject.Length | ||
{ $dirObject | Select-Object } | Should -Not -Throw |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should -Not -Throw
is not good test.
Why do we remove one test below? And why do we need $expected
above? Why do we change the tests at all?
@iSazonov It was called out at #11097 (comment) that there is a format-only commit included. BTW @iSazonov, it's easier to review after ignoring the white space characters: https://github.com/PowerShell/PowerShell/pull/11097/files?w=1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @iSazonov, something is wrong in the changes of these couple of tests.
$result = $(Select-Object -InputObject $dirObject -Last $TestLength).Length | ||
$expected = $dirObject.Length | ||
|
||
$result | Should -Be $expected |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$result | Should -Be $expected
shouldn't be removed here.
{ $dirObject | select } | Should -Not -Throw | ||
$result = $(Select-Object -InputObject $dirObject -Last $TestLength).Length | ||
$expected = $dirObject.Length | ||
{ $dirObject | Select-Object } | Should -Not -Throw |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to keep the It "Should be able to use the alias" {
here.
|
||
It "Select-Object with Property First Last Overlap should work"{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this test case removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Weird, not sure, will add it back.
@@ -362,13 +340,48 @@ Describe "Select-Object with Property = '*'" -Tags "CI" { | |||
$obj.psobject.TypeNames.Count | Should -Be 3 | |||
$obj.psobject.TypeNames[0] | Should -BeLike "Selected*" | |||
$obj.psobject.TypeNames[1] | Should -Not -BeLike "Selected*" | |||
$p = Get-Process -Id $pid | Select-Object -Property Process* -ExcludeProperty ProcessorAffinity -ExpandProperty Modules | |||
$p[0].psobject.Properties.Item("ProcessorAffinity") | Should -BeNullOrEmpty |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new code should be moved after $obj.psobject.TypeNames[2] | Should ..
line below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this got duplicated from above (L#328-329) somehow. Sorry about that, really not sure what on earth happened here. Will remove.
Some of the Select-Object tests got a bit mangled during format. This commit should restore them all back to proper order.
Apologies about the mangled tests, not sure how that happened. Should be all fixed up now. 💖 😊 |
🎉 Handy links: |
PR Summary
Prior to this PR, a hashtable piped into
Select-Object -[Expand]Property Keys
where the user expectedKeys
to retrieve the property value from the hashtable rather than the dictionary entry would return no value (in the case of -ExpandProperty, an error would be emitted, stating that the property could not be found).This PR fixes this issue, and examines hashtable members as well as their key/value entries. The logic is designed such that hashtable key/values are still prioritised over hashtable members, so if a user creates a hashtable such as
@{ Keys = "None" }
and then selects theKeys
property via Select-Object, it will have the same result as using$ht.Keys
already does: the returned value will always prioritise an actual key/value entry over the hashtable members.Note that this means creating a hashtable with a key named
Keys
will prevent access to theKeys
member on the hashtable in this manner. This new behaviour is consistent with existing behaviour with retrieval of hashtable keys, and also consistent with dot-property access behaviour with hashtables -- you can access$ht.Keys
up until you add a key with the nameKeys
, at which point you will always get that key value back.Tests have been added to cover these cases explicitly.
PR Context
Users should be able to utilise hashtables as actual hashtable objects as well as pseudo-PSObjects where appropriate. It is disingenuous for these commands to pretend hashtables don't have other members which may be desired in some circumstances.
This PR fixes #11094
PR Checklist
.h
,.cpp
,.cs
,.ps1
and.psm1
files have the correct copyright headerWIP:
or[ WIP ]
to the beginning of the title (theWIP
bot will keep its status check atPending
while the prefix is present) and remove the prefix when the PR is ready.