Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
typedata.tests.ps1132 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "TestData cmdlets" -Tags "CI" {4    Context "Get-TypeData" {5        It "System.DateTime" {6            (Get-TypeData System.DateTime).TypeName | Should -Be System.DateTime7            # Supports pipelining?8            ("System.DateTime" | Get-TypeData).TypeName | Should -Be System.DateTime9        }10 11        It "Type accelerators" {12            (Get-TypeData DateTime).TypeName | Should -Be System.DateTime13            (Get-TypeData psCredential).TypeName | Should -Be System.Management.Automation.PSCredential14        }15 16        It "Accept multiple types" {17            $types = Get-TypeData System.DateTime, System.Management.Automation* | Sort-Object -Property TypeName18            $types[0].TypeName | Should -Be System.DateTime19            for($i = 1; $i -lt $types.Count; $i++)20            {21                $types[$i].TypeName.StartsWith("System.Management.Automation") | Should -BeTrue22            }23        }24 25        It "System.Object" {26            Get-TypeData System.Object | Should -Be $null27        }28    }29 30    # The rest of these tests do their work in another runspace to avoid messing up the current runspace31    Context "Update-TypeData" {32        BeforeAll {33            $script:ps = [PowerShell]::Create()34        }35 36        BeforeEach {37            $ps.Commands.Clear()38            $ps.Streams.Error.Clear()39        }40 41        AfterAll {42            $ps.Dispose()43            $script:ps = $null44        }45 46        It "TypeAdapter parameter" {47            $type = $ps.AddScript(@"48Update-TypeData -TypeName Void -TypeAdapter Microsoft.PowerShell.Cim.CimInstanceAdapter49Get-TypeData System.Void50"@).Invoke()51            $type[0].TypeName | Should -Be System.Void52            $type[0].TypeAdapter.FullName | Should -Be Microsoft.PowerShell.Cim.CimInstanceAdapter53        }54    }55 56    Context "Remove-TypeData" {57        BeforeAll {58            $script:ps = [PowerShell]::Create()59            Setup -F dummy1.types.ps1xml -Content "<Types><Type><Name>yyyDummy</Name><Members><ScriptProperty><Name>yyyDummy</Name><GetScriptBlock>'yyyDummy'</GetScriptBlock></ScriptProperty></Members></Type></Types>"60            Setup -F dummy2.types.ps1xml -Content "<Types><Type><Name>zzzDummy</Name><Members><ScriptProperty><Name>zzzDummy</Name><GetScriptBlock>'zzzDummy'</GetScriptBlock></ScriptProperty></Members></Type></Types>"61        }62 63        BeforeEach {64            $ps.Commands.Clear()65            $ps.Streams.Error.Clear()66        }67 68        AfterAll {69            $ps.Dispose()70            $script:ps = $null71        }72 73        It "Remove type that doesn't exist" {74            $typeName = "TypeThatDoesNotExistsAnywhere" + (Get-Random)75            $ps.AddScript("Remove-TypeData -TypeName $typeName").Invoke()76            $ps.Streams.Error[0].FullyQualifiedErrorId | Should -Be "TypesDynamicRemoveException,Microsoft.PowerShell.Commands.RemoveTypeDataCommand"77        }78 79        ##{ All of the following It blocks are intended to run in sequence, so don't reorder them.80 81        It "Add type file," {82            $ps.AddScript("Update-TypeData -AppendPath $TestDrive\dummy1.types.ps1xml").Invoke()83            $ps.Streams.Error.Count | Should -Be 084        }85 86        It "Get type data from file just added," {87            $type = $ps.AddScript("Get-TypeData -TypeName yyyDummy").Invoke()88            $ps.Streams.Error.Count | Should -Be 089            $type[0].TypeName | Should -Be yyyDummy90        }91 92        It "Remove type file just added," {93            $ps.AddScript("Remove-TypeData -Path $TestDrive\dummy1.types.ps1xml").Invoke()94            $ps.Streams.Error.Count | Should -Be 095        }96 97        It "Make sure type was removed" {98            $type = $ps.AddScript("Get-TypeData -TypeName yyyDummy").Invoke()99            $type.Count | Should -Be 0100        }101 102        It "Add another type file," {103            $ps.AddScript("Update-TypeData -AppendPath $TestDrive\dummy2.types.ps1xml").Invoke()104            $ps.Streams.Error.Count | Should -Be 0105        }106 107        It "Now add some type data to one of those newly added types" {108            $ps.AddScript("Update-TypeData -TypeName zzzDummy -MemberType NoteProperty -MemberName DynamicDummyProperty -Value 10").Invoke()109            $ps.Streams.Error.Count | Should -Be 0110        }111 112        It "Remove the newly type file," {113            $ps.AddScript("Remove-TypeData -Path $TestDrive\dummy2.types.ps1xml").Invoke()114            $ps.Streams.Error.Count | Should -Be 0115        }116 117        It "Remove the newly type file a second time, should error," {118            $ps.AddScript("Remove-TypeData -Path $TestDrive\dummy2.types.ps1xml").Invoke()119            $ps.Streams.Error.Count | Should -Be 1120            $ps.Streams.Error[0].FullyQualifiedErrorId | Should -Be "TypeFileNotExistsInCurrentSession,Microsoft.PowerShell.Commands.RemoveTypeDataCommand"121        }122 123        It "Dynamic property added should still be there" {124            $res = $ps.AddScript("Get-TypeData -TypeName zzzDummy").Invoke()125            $res.Count | Should -Be 1126            $res[0].Members.Keys[0] | Should -Be DynamicDummyProperty127        }128 129        ##} All of the preceding It blocks are intended to run in sequence, so don't reorder them.130    }131}132