MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Format-Custom" -Tags "CI" {4 5 Context "Check Format-Custom aliases" {6 7 It "Should have the same output between the alias and the unaliased function" {8 $nonaliased = Get-FormatData | Format-Custom9 $aliased = Get-FormatData | fc10 $($nonaliased | Out-String).CompareTo($($aliased | Out-String)) | Should -Be 011 }12 }13 14 Context "Check specific flags on Format-Custom" {15 16 It "Should be able to specify the depth in output" {17 $getprocesspester = Get-FormatData | Format-Custom -Depth 118 ($getprocesspester).Count | Should -BeGreaterThan 019 }20 21 It "Should be able to use the Property flag to select properties" {22 Get-Command | Format-Custom -Property "Name" | Should -Not -Match "Source"23 }24 25 }26}27 28Describe "Format-Custom DRT basic functionality" -Tags "CI" {29 BeforeAll {30 if ($null -ne $PSStyle) {31 $outputRendering = $PSStyle.OutputRendering32 $PSStyle.OutputRendering = 'plaintext'33 }34 }35 36 AfterAll {37 if ($null -ne $PSStyle) {38 $PSStyle.OutputRendering = $outputRendering39 }40 }41 42 Add-Type -TypeDefinition @"43 public abstract class NamedItem44 {45 public string name;46 }47 48 public abstract class MyContainerBase : NamedItem49 {50 public System.Collections.Generic.List<NamedItem> children = new System.Collections.Generic.List<NamedItem> ();51 }52 53 public sealed class MyLeaf1 : NamedItem54 {55 public int val1 = 0;56 }57 58 public sealed class MyLeaf2 : NamedItem59 {60 public int val2 = 0;61 }62 63 public sealed class MyContainer1 : MyContainerBase64 {65 public string data1;66 }67 68 public sealed class MyContainer2 : MyContainerBase69 {70 public string data2;71 }72"@73 74 It "Format-Custom with subobject should work" {75 $expectResult1 = "this is the name"76 $expectResult2 = "this is the name of the sub object"77 $testObject = @{}78 $testObject.name = $expectResult179 $testObject.subObjectValue = @{}80 $testObject.subObjectValue.name = $expectResult281 $testObject.subObjectValue.array = (0..63)82 $testObject.subObjectValue.stringarray = @("one","two")83 $result = $testObject | Format-Custom | Out-String84 $result | Should -Match $expectResult185 $result | Should -Match $expectResult286 $result | Should -Match "one"87 $result | Should -Match "two"88 }89 90 It "Format-Custom with Tree Object should work" {91 $expectedResult=@"92class MyContainer193{94 data1 = data 195 children =96 [97 class MyLeaf198 {99 val1 = 10100 name = leaf 1101 }102 class MyLeaf2103 {104 val2 = 20105 name = leaf 2106 }107 ]108 109 name = container 1110}111 112class MyContainer2113{114 data2 = data 2115 children =116 [117 class MyLeaf1118 {119 val1 = 10120 name = leaf 1121 }122 class MyLeaf2123 {124 val2 = 20125 name = leaf 2126 }127 ]128 129 name = container 2130}131 132class MyContainer2133{134 data2 = data 2 deep135 children =136 [137 class MyContainer1138 {139 data1 = cx data140 children =141 [142 class MyLeaf1143 {144 val1 = 10145 name = leaf 1146 }147 class MyLeaf2148 {149 val2 = 20150 name = leaf 2151 }152 ]153 154 name = cx155 }156 ]157 158 name = container 2 deep159}160 161class MyContainer1162{163 data1 = cx data164 children =165 [166 class MyLeaf1167 {168 val1 = 10169 name = leaf 1170 }171 class MyLeaf2172 {173 val2 = 20174 name = leaf 2175 }176 ]177 178 name = cx179}180"@181 $leaf1 = New-Object MyLeaf1182 $leaf1.name = "leaf 1"183 $leaf1.val1 = 10184 185 $leaf2 = New-Object MyLeaf2186 $leaf2.name = "leaf 2"187 $leaf2.val2 = 20188 189 $c1 = New-Object MyContainer1190 $c1.name = "container 1"191 $c1.data1 = "data 1"192 $c1.children.Add($leaf1)193 $c1.children.Add($leaf2)194 195 $c2 = New-Object MyContainer2196 $c2.name = "container 2"197 $c2.data2 = "data 2"198 $c2.children.Add($leaf1)199 $c2.children.Add($leaf2)200 201 $cDeep = New-Object MyContainer2202 $cDeep.name = "container 2 deep"203 $cDeep.data2 = "data 2 deep"204 205 $cx= New-Object MyContainer1206 $cx.name = "cx"207 $cx.data1 = "cx data"208 $cx.children.Add($leaf1)209 $cx.children.Add($leaf2)210 $cDeep.children.Add($cx)211 212 $objectList = @($c1,$c2,$cDeep,$cx)213 $result = $objectList | Format-Custom | Out-String214 $result = $result -replace "[{} `n\r]",""215 $expectedResult = $expectedResult -replace "[{} `n\r]",""216 $result | Should -Be $expectedResult217 }218 219 It "Format-Custom with Empty Data Tree Object should work" {220 $expectedResult=@"221class MyContainer1222{223 data1 =224 children =225 name =226}227"@228 229 $c= New-Object MyContainer1230 $c.name = $null231 $c.data1 = $null232 $c.children = $null233 $objectList = @($c)234 235 $result = $objectList | Format-Custom | Out-String236 $result = $result -replace "[{} `n\r]",""237 $expectedResult = $expectedResult -replace "[{} `n\r]",""238 $result | Should -Be $expectedResult239 }240 241 It "Format-Custom with Back Pointers Tree Object should work" {242 $expectedResult=@"243class MyContainer1244{245 data1 =246 children =247 [248 class MyContainer1249 {250 data1 =251 children =252 [253 class MyContainer1254 {255 data1 =256 children =257 [258 MyContainer1259 ]260 261 name = ROOT262 }263 ]264 265 name = ROOT266 }267 ]268 269 name = ROOT270}271"@272 273 $root= New-Object MyContainer1274 $root.name = "ROOT"275 $root.children.Add($root)276 $objectList = @($root)277 278 $result = $objectList | Format-Custom | Out-String279 $result = $result -replace "[{} `n\r]",""280 $expectedResult = $expectedResult -replace "[{} `n\r]",""281 $result | Should -Be $expectedResult282 }283 284 It "Format-Custom with Leaf Only Data should work" {285 $expectedResult=@"286class MyLeaf1287{288 val1 = 10289 name = leaf 1290}291 292class MyLeaf2293{294 val2 = 20295 name = leaf 2296}297"@298 299 $leaf1 = New-Object MyLeaf1300 $leaf1.name = "leaf 1"301 $leaf1.val1 = 10302 303 $leaf2 = New-Object MyLeaf2304 $leaf2.name = "leaf 2"305 $leaf2.val2 = 20306 307 $objectList = @($leaf1,$leaf2)308 $result = $objectList | Format-Custom | Out-String309 $result = $result -replace "[{} `n\r]",""310 $expectedResult = $expectedResult -replace "[{} `n\r]",""311 $result | Should -Be $expectedResult312 }313 314 It "Format-Custom should not lost data" {315 # See https://github.com/PowerShell/PowerShell/pull/11342 for more information316 $data = (Get-Help Out-Null).Examples317 $formattedData = $data | Format-Custom | Out-String318 $formattedData | Should -BeLike "*$($data.Example.title)*"319 $formattedData | Should -BeLike "*$($data.Example.code)*"320 $formattedData | Should -BeLike "*$($data.Example.remarks.Text)*"321 }322 }323 324Describe "Format-Custom with expression based EntrySelectedBy in a CustomControl" -Tags "CI" {325 BeforeAll {326 if ($null -ne $PSStyle) {327 $outputRendering = $PSStyle.OutputRendering328 $PSStyle.OutputRendering = 'plaintext'329 }330 331 $formatFilePath = Join-Path $TestDrive 'UpdateFormatDataTests.format.ps1xml'332 $xmlContent = @'333<?xml version="1.0" encoding="UTF-8" ?>334<Configuration>335 <Controls>336 <Control>337 <Name>MyTestControl</Name>338 <CustomControl>339 <CustomEntries>340 <CustomEntry>341 <EntrySelectedBy>342 <SelectionCondition>343 <TypeName>MyTestObject</TypeName>344 <ScriptBlock>$_.Name -eq 'SelectScriptBlock'</ScriptBlock>345 </SelectionCondition>346 </EntrySelectedBy>347 <CustomItem>348 <Text>Entry selected by ScriptBlock</Text>349 </CustomItem>350 </CustomEntry>351 <CustomEntry>352 <EntrySelectedBy>353 <SelectionCondition>354 <TypeName>MyTestObject</TypeName>355 <PropertyName>SelectProperty</PropertyName>356 </SelectionCondition>357 </EntrySelectedBy>358 <CustomItem>359 <Text>Entry selected by property</Text>360 </CustomItem>361 </CustomEntry>362 <CustomEntry>363 <CustomItem>364 <Text>Default was picked</Text>365 </CustomItem>366 </CustomEntry>367 </CustomEntries>368 </CustomControl>369 </Control>370 </Controls>371 <ViewDefinitions>372 <View>373 <Name>DefaultView</Name>374 <ViewSelectedBy>375 <TypeName>MyTestObject</TypeName>376 </ViewSelectedBy>377 <GroupBy>378 <PropertyName>Name</PropertyName>379 <CustomControlName>MyTestControl</CustomControlName>380 </GroupBy>381 <TableControl>382 <TableHeaders>383 <TableColumnHeader />384 </TableHeaders>385 <TableRowEntries>386 <TableRowEntry>387 <TableColumnItems>388 <TableColumnItem>389 <PropertyName>Name</PropertyName>390 </TableColumnItem>391 </TableColumnItems>392 </TableRowEntry>393 </TableRowEntries>394 </TableControl>395 </View>396 </ViewDefinitions>397</Configuration>398'@399 400 Set-Content -Path $formatFilePath -Value $xmlContent401 $ps = [powershell]::Create()402 $iss = [initialsessionstate]::CreateDefault2()403 $iss.Formats.Add($formatFilePath)404 $rs = [runspacefactory]::CreateRunspace($iss)405 $rs.Open()406 $ps.Runspace = $rs407 }408 409 AfterAll {410 if ($null -ne $PSStyle) {411 $PSStyle.OutputRendering = $outputRendering412 }413 414 $rs.Close()415 $ps.Dispose()416 }417 418 It 'Property expression binding should be able to access the current object' {419 $script = {420 [PSCustomObject]@{421 PSTypeName = 'MyTestObject'422 SelectProperty = $true423 Name = 'testing'424 }425 }426 427 $null = $ps.AddScript($script).AddCommand('Out-String')428 $ps.Streams.Error.Clear()429 $expectedOutput = @'430 431Entry selected by property432 433Name434----435testing436 437 438'@ -replace '\r?\n', "^"439 440 $ps.Invoke() -replace '\r?\n', "^" | Should -BeExactly $expectedOutput441 $ps.Streams.Error | Should -BeNullOrEmpty442 }443 444 It 'ScriptBlock expression binding should be able to access the current object' {445 $script = {446 [PSCustomObject]@{447 PSTypeName = 'MyTestObject'448 SelectProperty = $false449 Name = 'SelectScriptBlock'450 }451 }452 453 $null = $ps.AddScript($script).AddCommand('Out-String')454 $ps.Streams.Error.Clear()455 $expectedOutput = @'456 457Entry selected by ScriptBlock458 459Name460----461SelectScriptBlock462 463 464'@ -replace '\r?\n', "^"465 466 $ps.Invoke() -replace '\r?\n', "^" | Should -BeExactly $expectedOutput467 $ps.Streams.Error | Should -BeNullOrEmpty468 }469 470 Context 'ExcludeProperty parameter' {471 It 'Should exclude specified properties' {472 $obj = [pscustomobject]@{ Name = 'Test'; Age = 30; City = 'Seattle' }473 $result = $obj | Format-Custom -ExcludeProperty Age | Out-String474 $result | Should -Match 'Name'475 $result | Should -Match 'City'476 $result | Should -Not -Match 'Age'477 }478 479 It 'Should work with wildcard patterns' {480 $obj = [pscustomobject]@{ Prop1 = 1; Prop2 = 2; Other = 3 }481 $result = $obj | Format-Custom -ExcludeProperty Prop* | Out-String482 $result | Should -Match 'Other'483 $result | Should -Not -Match 'Prop1'484 $result | Should -Not -Match 'Prop2'485 }486 487 It 'Should work without Property parameter (implies -Property *)' {488 $obj = [pscustomobject]@{ A = 1; B = 2; C = 3 }489 $result = $obj | Format-Custom -ExcludeProperty B | Out-String490 $result | Should -Match 'A\s*='491 $result | Should -Match 'C\s*='492 $result | Should -Not -Match 'B\s*='493 }494 495 It 'Should work with Property parameter' {496 $obj = [pscustomobject]@{ Name = 'Test'; Age = 30; City = 'Seattle'; Country = 'USA' }497 $result = $obj | Format-Custom -Property Name, Age, City, Country -ExcludeProperty Age | Out-String498 $result | Should -Match 'Name'499 $result | Should -Match 'City'500 $result | Should -Match 'Country'501 $result | Should -Not -Match 'Age'502 }503 504 It 'Should handle multiple excluded properties' {505 $obj = [pscustomobject]@{ A = 1; B = 2; C = 3; D = 4 }506 $result = $obj | Format-Custom -ExcludeProperty B, D | Out-String507 $result | Should -Match 'A\s*='508 $result | Should -Match 'C\s*='509 $result | Should -Not -Match 'B\s*='510 $result | Should -Not -Match 'D\s*='511 }512 }513}514 