MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4. (Join-Path -Path $PSScriptRoot -ChildPath Test-Mocks.ps1)5Add-TestDynamicType6 7Describe "Select-Object" -Tags "CI" {8 BeforeEach {9 $dirObject = GetFileMock10 $TestLength = 311 }12 13 It "Handle piped input without error" {14 { $dirObject | Select-Object } | Should -Not -Throw15 }16 17 It "Should treat input as a single object with the inputObject parameter" {18 $result = $(Select-Object -InputObject $dirObject -Last $TestLength).Length19 $expected = $dirObject.Length20 $result | Should -Be $expected21 }22 23 It "Should be able to use the alias" {24 { $dirObject | select } | Should -Not -Throw25 }26 27 It "Should have same result when using alias" {28 $result = $dirObject | Select-Object29 $expected = $dirObject | Select-Object30 31 $result | Should -Be $expected32 }33 34 It "Should return correct object with First parameter" {35 $result = $dirObject | Select-Object -First $TestLength36 37 $result.Length | Should -Be $TestLength38 39 for ($i = 0; $i -lt $TestLength; $i++) {40 $result[$i].Name | Should -Be $dirObject[$i].Name41 }42 }43 44 It "Should return correct object with Last parameter" {45 $result = $dirObject | Select-Object -Last $TestLength46 47 $result.Length | Should -Be $TestLength48 49 for ($i = 0; $i -lt $TestLength; $i++) {50 $result[$i].Name | Should -Be $dirObject[$dirObject.Length - $TestLength + $i].Name51 }52 }53 54 It "Should work correctly with Unique parameter" {55 $result = ("a", "b", "c", "a", "a", "a" | Select-Object -Unique).Length56 $expected = 357 58 $result | Should -Be $expected59 }60 61 It "Should work work correctly with Unique and CaseInsensitive parameter" {62 $result = "abc", "Abc" | Select-Object -Unique -CaseInsensitive63 64 $result.Count | Should -Be 165 $result | Should -Be "abc"66 }67 68 It "Should return correct object with Skip parameter" {69 $result = $dirObject | Select-Object -Skip $TestLength70 71 $result.Length | Should -Be ($dirObject.Length - $TestLength)72 73 for ($i = 0; $i -lt $TestLength; $i++) {74 $result[$i].Name | Should -Be $dirObject[$TestLength + $i].Name75 }76 }77 78 It "Should return an object with selected columns" {79 $result = $dirObject | Select-Object -Property Name, Size80 81 $result.Length | Should -Be $dirObject.Length82 $result[0].Name | Should -Be $dirObject[0].Name83 $result[0].Size | Should -Be $dirObject[0].Size84 $result[0].Mode | Should -BeNullOrEmpty85 }86 87 It "Should send output to pipe properly" {88 { $dirObject | Select-Object -Unique | pipelineConsume } | Should -Not -Throw89 }90 91 It "Should select array indices with Index parameter" {92 $firstIndex = 293 $secondIndex = 494 $result = $dirObject | Select-Object -Index $firstIndex, $secondIndex95 96 $result[0].Name | Should -Be $dirObject[$firstIndex].Name97 $result[1].Name | Should -Be $dirObject[$secondIndex].Name98 }99 100 # Note that these two tests will modify original values of $dirObject101 102 It "Should not wait when used without -Wait option" {103 $orig1 = $dirObject[0].Size104 $orig2 = $dirObject[$TestLength].Size105 $result = $dirObject | addOneToSizeProperty | Select-Object -First $TestLength106 107 $result[0].Size | Should -Be ($orig1 + 1)108 $dirObject[0].Size | Should -Be ($orig1 + 1)109 $dirObject[$TestLength].Size | Should -Be $orig2110 }111 112 It "Should wait when used with -Wait option" {113 $orig1 = $dirObject[0].Size114 $orig2 = $dirObject[$TestLength].Size115 $result = $dirObject | addOneToSizeProperty | Select-Object -First $TestLength -Wait116 117 $result[0].Size | Should -Be ($orig1 + 1)118 $dirObject[0].Size | Should -Be ($orig1 + 1)119 $dirObject[$TestLength].Size | Should -Be ($orig2 + 1)120 }121 122 It "Should not leak 'StopUpstreamCommandsException' internal exception for stopping upstream" {123 1, 2 | Select-Object -First 1 -ErrorVariable err124 $err | Should -BeNullOrEmpty125 }126}127 128Describe "Select-Object DRT basic functionality" -Tags "CI" {129 BeforeAll {130 $employees = @(131 [pscustomobject]@{"FirstName" = "joseph"; "LastName" = "smith"; "YearsInMS" = 15 }132 [pscustomobject]@{"FirstName" = "paul"; "LastName" = "smith"; "YearsInMS" = 15 }133 [pscustomobject]@{"FirstName" = "mary"; "LastName" = "soe"; "YearsInMS" = 5 }134 [pscustomobject]@{"FirstName" = "edmund"; "LastName" = "bush"; "YearsInMS" = 9 }135 )136 }137 138 It "Select-Object with empty script block property should throw" {139 $e = { "bar" | Select-Object -Prop {} -ErrorAction Stop } |140 Should -Throw -ErrorId "EmptyScriptBlockAndNoName,Microsoft.PowerShell.Commands.SelectObjectCommand" -PassThru141 $e.CategoryInfo | Should -Match "PSArgumentException"142 }143 144 It "Select-Object with Property First Last Overlap should work" {145 $results = $employees | Select-Object -Property "YearsInMS", "L*" -First 2 -Last 3146 $results.Count | Should -Be 4147 }148 149 It "Select-Object with string property should work" {150 $result = "bar" | Select-Object -Prop foo | Measure-Object151 $result.Count | Should -Be 1152 }153 154 It "Select-Object with Property First Last should work" {155 $results = $employees | Select-Object -Property "YearsInMS", "L*" -First 2 -Last 1156 157 $results.Count | Should -Be 3158 159 $results[0].LastName | Should -Be $employees[0].LastName160 $results[1].LastName | Should -Be $employees[1].LastName161 $results[2].LastName | Should -Be $employees[3].LastName162 163 $results[0].YearsInMS | Should -Be $employees[0].YearsInMS164 $results[1].YearsInMS | Should -Be $employees[1].YearsInMS165 $results[2].YearsInMS | Should -Be $employees[3].YearsInMS166 }167 168 It "Select-Object with Property First should work" {169 $results = $employees | Select-Object -Property "YearsInMS", "L*" -First 2170 171 $results.Count | Should -Be 2172 173 $results[0].LastName | Should -Be $employees[0].LastName174 $results[1].LastName | Should -Be $employees[1].LastName175 176 $results[0].YearsInMS | Should -Be $employees[0].YearsInMS177 $results[1].YearsInMS | Should -Be $employees[1].YearsInMS178 }179 180 It "Select-Object with Property First Zero should work" {181 $results = $employees | Select-Object -Property "YearsInMS", "L*" -First 0182 183 $results.Count | Should -Be 0184 }185 186 It "Select-Object with Property Last Zero should work" {187 $results = $employees | Select-Object -Property "YearsInMS", "L*" -Last 0188 189 $results.Count | Should -Be 0190 }191 192 It "Select-Object with Unique should work" {193 $results = $employees | Select-Object -Property "YearsInMS", "L*" -Unique:$true194 195 $results.Count | Should -Be 3196 197 $results[0].LastName | Should -Be $employees[1].LastName198 $results[1].LastName | Should -Be $employees[2].LastName199 $results[2].LastName | Should -Be $employees[3].LastName200 201 $results[0].YearsInMS | Should -Be $employees[1].YearsInMS202 $results[1].YearsInMS | Should -Be $employees[2].YearsInMS203 $results[2].YearsInMS | Should -Be $employees[3].YearsInMS204 }205 206 It "Select-Object with Simple should work" {207 $employee1 = [pscustomobject]@{"FirstName" = "joesph"; "LastName" = "smith"; "YearsInMS" = 15 }208 $employee2 = [pscustomobject]@{"FirstName" = "paul"; "LastName" = "smith"; "YearsInMS" = 15 }209 $employee3 = [pscustomobject]@{"FirstName" = "mary"; "LastName" = "soe"; "YearsInMS" = 15 }210 $employees3 = @($employee1, $employee2, $employee3, $employee4)211 $results = $employees3 | Select-Object -Property "FirstName", "YearsInMS"212 213 $results.Count | Should -Be 3214 215 $results[0].FirstName | Should -Be $employees3[0].FirstName216 $results[1].FirstName | Should -Be $employees3[1].FirstName217 $results[2].FirstName | Should -Be $employees3[2].FirstName218 219 $results[0].YearsInMS | Should -Be $employees3[0].YearsInMS220 $results[1].YearsInMS | Should -Be $employees3[1].YearsInMS221 $results[2].YearsInMS | Should -Be $employees3[2].YearsInMS222 }223 224 It "Select-Object with no input should work" {225 $results = $null | Select-Object -Property "FirstName", "YearsInMS", "FirstNa*"226 $results.Count | Should -Be 0227 }228 229 It "Select-Object with Start-Time In Idle Process should work" {230 $results = Get-Process * | Select-Object ProcessName231 $results.Count | Should -Not -Be 0232 }233 234 It "Select-Object with Skip should work" {235 $results = "1", "2", "3" | Select-Object -Skip 1236 $results.Count | Should -Be 2237 $results[0] | Should -Be 2238 $results[1] | Should -Be 3239 }240 241 It "Select-Object with SkipLast should work" {242 $results = "1", "2", "3" | Select-Object -SkipLast 1243 $results.Count | Should -Be 2244 $results[0] | Should -BeExactly "1"245 $results[1] | Should -BeExactly "2"246 }247 248 It "Select-Object with Skip and SkipLast should work" {249 $results = "1", "2", "3" | Select-Object -Skip 1 -SkipLast 1250 $results.Count | Should -Be 1251 $results[0] | Should -BeExactly "2"252 }253 254 It "Select-Object with Skip and SkipLast should work with Skip overlapping SkipLast" {255 $results = "1", "2" | Select-Object -Skip 2 -SkipLast 1256 $results. Count | Should -Be 0257 }258 259 It "Select-Object with Skip and SkipLast should work with skiplast overlapping skip" {260 $results = "1", "2" | Select-Object -Skip 1 -SkipLast 2261 $results. Count | Should -Be 0262 }263 264 It "Select-Object with Index should work" {265 $results = "1", "2", "3" | Select-Object -Index 2266 $results.Count | Should -Be 1267 $results[0] | Should -BeExactly "3"268 }269 270 It "Select-Object with SkipIndex should work" {271 $results = "1", "2", "3" | Select-Object -SkipIndex 0, 2272 $results | Should -HaveCount 1273 $results[0] | Should -BeExactly "2"274 }275 276 It "Select-Object with SkipIndex should work with index out of range" {277 $results = 0..10 | Select-Object -SkipIndex 5, 6, 7, 8, 11278 $results | Should -HaveCount 7279 $results -join ',' | Should -BeExactly "0,1,2,3,4,9,10"280 }281 282 It "Select-Object should handle dynamic (DLR) properties" {283 $dynObj = [TestDynamic]::new()284 $results = $dynObj, $dynObj | Select-Object -ExpandProperty FooProp285 $results.Count | Should -Be 2286 $results[0] | Should -Be 123287 $results[1] | Should -Be 123288 }289 290 It "Select-Object should handle dynamic (DLR) properties without GetDynamicMemberNames hint" {291 $dynObj = [TestDynamic]::new()292 $results = $dynObj, $dynObj | Select-Object -ExpandProperty HiddenProp293 $results.Count | Should -Be 2294 $results[0] | Should -Be 789295 $results[1] | Should -Be 789296 }297 298 It "Select-Object should handle wildcarded dynamic (DLR) properties when hinted by GetDynamicMemberNames" {299 $dynObj = [TestDynamic]::new()300 $results = $dynObj, $dynObj | Select-Object -ExpandProperty FooP*301 $results.Count | Should -Be 2302 $results[0] | Should -Be 123303 $results[1] | Should -Be 123304 }305 306 It "Select-Object should work when multiple dynamic (DLR) properties match" {307 $dynObj = [TestDynamic]::new()308 $results = $dynObj, $dynObj | Select-Object *Prop309 $results.Count | Should -Be 2310 $results[0].FooProp | Should -Be 123311 $results[0].BarProp | Should -Be 456312 $results[1].FooProp | Should -Be 123313 $results[1].BarProp | Should -Be 456314 }315 316 It "Select-Object -ExpandProperty should yield errors if multiple dynamic (DLR) properties match" {317 $dynObj = [TestDynamic]::new()318 $e = { $results = $dynObj, $dynObj | Select-Object -ExpandProperty *Prop -ErrorAction Stop } |319 Should -Throw -PassThru -ErrorId "MutlipleExpandProperties,Microsoft.PowerShell.Commands.SelectObjectCommand"320 $e.CategoryInfo | Should -Match "PSArgumentException"321 }322}323 324Describe "Select-Object with Property = '*'" -Tags "CI" {325 326 # Issue #2420327 It "Select-Object with implicit Property = '*' don't return property named '*'" {328 $results = [pscustomobject]@{Thing = "thing1" } | Select-Object -ExcludeProperty thing329 $results.psobject.Properties.Item("*") | Should -BeNullOrEmpty330 }331 332 # Issue #2420333 It "Select-Object with explicit Property = '*' don't return property named '*'" {334 $results = [pscustomobject]@{Thing = "thing1" } | Select-Object -Property * -ExcludeProperty thing335 $results.psobject.Properties.Item("*") | Should -BeNullOrEmpty336 }337 338 # Issue #2351339 It "Select-Object with implicit Property = '*' exclude single property" {340 $results = [pscustomobject]@{Thing = "thing1" } | Select-Object -ExcludeProperty thing341 $results.psobject.Properties.Item("Thing") | Should -BeNullOrEmpty342 $results.psobject.Properties.Item("*") | Should -BeNullOrEmpty343 }344 345 # Issue #2351346 It "Select-Object with explicit Property = '*' exclude single property" {347 $results = [pscustomobject]@{Thing = "thing1" } | Select-Object -Property * -ExcludeProperty thing348 $results.psobject.Properties.Item("Thing") | Should -BeNullOrEmpty349 $results.psobject.Properties.Item("*") | Should -BeNullOrEmpty350 }351 352 # Issue #2351353 It "Select-Object with implicit Property = '*' exclude not single property" {354 $results = [pscustomobject]@{Thing = "thing1"; Param2 = "param2" } | Select-Object -ExcludeProperty Param2355 $results.Param2 | Should -BeNullOrEmpty356 $results.Thing | Should -BeExactly "thing1"357 }358 359 # Issue #2351360 It "Select-Object with explicit Property = '*' exclude not single property" {361 $results = [pscustomobject]@{Thing = "thing1"; Param2 = "param2" } | Select-Object -Property * -ExcludeProperty Param2362 $results.Param2 | Should -BeNullOrEmpty363 $results.Thing | Should -BeExactly "thing1"364 }365 366 It "Select-Object with ExpandProperty and Property don't skip processing ExcludeProperty" {367 $p = Get-Process -Id $PID | Select-Object -Property Process* -ExcludeProperty ProcessorAffinity -ExpandProperty Modules368 $p[0].psobject.Properties.Item("ProcessorAffinity") | Should -BeNullOrEmpty369 }370 371 It "Select-Object add 'Selected.*' type only once" {372 $obj = [PSCustomObject]@{ Name = 1 }373 374 $obj.psobject.TypeNames.Count | Should -Be 2375 $obj.psobject.TypeNames | Should -Not -BeLike "Selected*"376 377 $obj = $obj | Select-Object378 379 $obj.psobject.TypeNames.Count | Should -Be 3380 $obj.psobject.TypeNames[0] | Should -BeLike "Selected*"381 $obj.psobject.TypeNames[1] | Should -Not -BeLike "Selected*"382 $obj.psobject.TypeNames[2] | Should -Not -BeLike "Selected*"383 }384}385 386Describe 'Select-Object behaviour with hashtable entries and actual members' -Tags CI {387 388 It 'can retrieve a hashtable entry as a property' {389 $hashtable = @{ Entry = 100 }390 391 $result = $hashtable | Select-Object -Property Entry392 $result.Entry | Should -Be 100393 394 $hashtable |395 Select-Object -ExpandProperty Entry |396 Should -Be 100397 }398 399 It 'can retrieve true hashtable members' {400 $hashtable = @{ Value = 10 }401 $result = $hashtable | Select-Object -Property Keys402 $result.Keys | Should -Be 'Value'403 404 $hashtable |405 Select-Object -ExpandProperty Keys |406 Should -Be 'Value'407 }408 409 It 'should prioritise hashtable entries where available' {410 $hashtable = @{ Keys = 10 }411 $result = $hashtable | Select-Object -Property Keys412 $result.Keys | Should -Be 10413 414 $hashtable | Select-Object -ExpandProperty Keys | Should -Be 10415 }416 417 It 'should get the hashtable Count member' {418 $hashtable = @{ a = 10; b = 20; c = 30 }419 $result = $hashtable | Select-Object -Property Count420 $result.Count | Should -Be 3421 422 $hashtable | Select-Object -ExpandProperty Count | Should -Be 3423 }424}425 