Skip to content
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

Prevent Select-Object with ExpandProperty from updating source PSObjects #21328

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -550,7 +550,7 @@ private void ProcessParameter(MshParameter p, PSObject inputObject, List<PSNoteP
// directly with it. We want the NoteProperty to be associated only with this
// particular PSObject, so that when the user uses the base object else where,
// its members remain the same as before the Select-Object command run.
PSObject expandedObject = PSObject.AsPSObject(r.Result, true);
PSObject expandedObject = PSObject.AsPSObject(r.Result, true).Copy();
AddNoteProperties(expandedObject, inputObject, matchedProperties);

FilteredWriteObject(expandedObject, matchedProperties);
Expand All @@ -570,7 +570,7 @@ private void ProcessParameter(MshParameter p, PSObject inputObject, List<PSNoteP
// directly with it. We want the NoteProperty to be associated only with this
// particular PSObject, so that when the user uses the base object else where,
// its members remain the same as before the Select-Object command run.
PSObject expandedObject = PSObject.AsPSObject(expandedValue, true);
PSObject expandedObject = PSObject.AsPSObject(expandedValue, true).Copy();
AddNoteProperties(expandedObject, inputObject, matchedProperties);

FilteredWriteObject(expandedObject, matchedProperties);
Expand Down
Expand Up @@ -250,12 +250,12 @@ Describe "Select-Object DRT basic functionality" -Tags "CI" {
$results.Count | Should -Be 1
$results[0] | Should -BeExactly "2"
}

It "Select-Object with Skip and SkipLast should work with Skip overlapping SkipLast" {
$results = "1", "2" | Select-Object -Skip 2 -SkipLast 1
$results. Count | Should -Be 0
}

It "Select-Object with Skip and SkipLast should work with skiplast overlapping skip" {
$results = "1", "2" | Select-Object -Skip 1 -SkipLast 2
$results. Count | Should -Be 0
Expand Down Expand Up @@ -383,7 +383,58 @@ Describe "Select-Object with Property = '*'" -Tags "CI" {
}
}

Describe 'Select-Object behaviour with hashtable entries and actual members' -Tags CI {
Describe "Select-Object with ExpandProperty and Property" -Tags "CI" {

Context "Preserves ETS instance members" {
BeforeAll {
# Use a .NET reference type, because the copy semantics of value types could hide some behaviors.
$obj = [regex] 'foo'
# Add a property via the resurrection tables.
$obj | Add-Member resurrectTableProp 1
# Now embed the object inside another object and expand it via -ExpandProperty, while also decorating it via another property using -Property.
$results = [PSCustomObject] @{ psobjectWrapperProp = 2; prop = $obj } |
Select-Object -Property psobjectWrapperProp -ExpandProperty prop
}

#* Ideally the ETS members of a property that has been expanded would be surfaced, but that is not currently the case.
# Issue #7937
# It "Resurection Table member is present" {
# $results.resurrectTableProp | Should -BeExactly 1
# }
It "PSObject-attached member is present" {
$results.psobjectWrapperProp | Should -BeExactly 2
}
It "Resurrection-table member has not become a PSObject-attached member" {
$results.PSObject.BaseObject.resurrectTableProp | Should -BeExactly 1
}
It "PSObject-attached member has not become a resurection-table member" {
$results.PSObject.BaseObject.psobjectWrapperProp | Should -BeNullOrEmpty
}
# Issue #21308
It "Original object remains unchanged" {
$obj.psobjectWrapperProp | Should -BeNullOrEmpty
}
}

# Issue #21308
Context "Does not modify source object" {
BeforeAll {
$obj = [PSCustomObject]@{name1="admin1";children=[PSCustomObject]@{name2="admin2"}}
$obj | Select-Object -Property name1, @{N="country";E={$_.name1}} -ExpandProperty children | Out-Null
}
# Issue #21308
It "Doing a simple select" {
$obj.children.name1 | Should -BeNullOrEmpty
}
# Issue #21308
It "Doing a expression select" {
$obj.children.country | Should -BeNullOrEmpty
}
}

}

Describe 'Select-Object behaviour with hashtable entries and actual members' -Tags "CI" {

It 'can retrieve a hashtable entry as a property' {
$hashtable = @{ Entry = 100 }
Expand Down