Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes308downloads
Update-FormatData.Tests.ps1153 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Update-FormatData" -Tags "CI" {4 5    BeforeEach {6        $ps = [PowerShell]::Create()7    }8 9    Context "Validate Update-FormatData update correctly" {10 11        It "Should not throw upon reloading previous formatting file" {12            { Update-FormatData } | Should -Not -Throw13        }14 15        It "Should validly load formatting data" {16            $path = Join-Path -Path $TestDrive -ChildPath "outputfile.ps1xml"17            Get-FormatData -TypeName System.Diagnostics.Process | Export-FormatData -Path $path18            $null = $ps.AddScript("Update-FormatData -prependPath $path")19            $ps.Invoke()20            $ps.HadErrors | Should -BeFalse21        }22 23        It "Update with atributes on Configuration node should be ignored" {24            $xmlContent = @"25    <Configuration xmlns:foo="bar">26        <ViewDefinitions>27            <View>28                <Name>Test</Name>29                <ViewSelectedBy>30                    <TypeName>Test</TypeName>31                </ViewSelectedBy>32                <ListControl>33                    <ListEntries>34                        <ListEntry>35                            <ListItems>36                                <ListItem>37                                    <PropertyName>Test</PropertyName>38                                </ListItem>39                            </ListItems>40                        </ListEntry>41                    </ListEntries>42                </ListControl>43            </View>44        </ViewDefinitions>45    </Configuration>46"@47            $path = "$testdrive\rootattribute.format.ps1xml"48            Set-Content -Path $path -Value $xmlContent49            $null = $ps.AddScript("Update-FormatData -prependPath $path")50            $ps.Invoke()51            $ps.HadErrors | Should -BeFalse52            $ps.Commands.Clear()53            $null = $ps.AddScript("Get-FormatData test")54            $formatData = $ps.Invoke()55            $formatData | Should -HaveCount 156            $formatData.TypeNames | Should -BeExactly "Test"57            $formatData.FormatViewDefinition.Name | Should -BeExactly "Test"58        }59    }60}61 62Describe "Update-FormatData basic functionality" -Tags "CI" {63    BeforeAll {64        $testfilename = "testfile.ps1xml"65        $testfile = Join-Path -Path $TestDrive -ChildPath $testfilename66 67        $xmlContent=@"68                <Types>69                    <Type>70                        <Name>AnyName</Name>71                        <Members>72                            <PropertySet>73                                <Name>PropertySetName</Name>74                                <ReferencedProperties>75                                    <Name>FirstName</Name>76                                    <Name>LastName</Name>77                                </ReferencedProperties>78                            </PropertySet>79                        </Members>80                    </Type>81                </Types>82"@83        $xmlContent > $testfile84    }85 86    It "Update-FormatData with WhatIf should work"{87 88        { Update-FormatData -Append $testfile -WhatIf } | Should -Not -Throw89        { Update-FormatData -Prepend $testfile -WhatIf } | Should -Not -Throw90    }91 92    It "Update with invalid format xml should fail" {93        $xmlContent = @"94<Configuration>95    <ViewDefinitions>96        <View>97            <Name>T2</Name>98        </View>99    </ViewDefinitions>100</Configuration>101"@102        $xmlContent | Out-File -FilePath "$testdrive\invalid.format.ps1xml" -Encoding ascii103        { Update-FormatData -Path "$testdrive\invalid.format.ps1xml" -ErrorAction Stop } | Should -Throw -ErrorId "FormatXmlUpdateException,Microsoft.PowerShell.Commands.UpdateFormatDataCommand"104    }105}106 107Describe "Update-FormatData with resources in CustomControls" -Tags "CI" {108 109    BeforeAll {110        $templatePath = Join-Path $PSScriptRoot (Join-Path 'assets' 'UpdateFormatDataTests.format.ps1xml')111        $formatFilePath = Join-Path $TestDrive 'UpdateFormatDataTests.format.ps1xml'112        $ps = [powershell]::Create()113        $iss = [initialsessionstate]::CreateDefault2()114        $rs = [system.management.automation.runspaces.runspacefactory]::CreateRunspace($iss)115        $rs.Open()116        $ps.Runspace = $rs117    }118    AfterAll {119        $rs.Close()120        $ps.Dispose()121    }122    Context "Validate Update-FormatData" {123        It "Resources in WindowsPS syntax should be loaded successfully" {124            $format = Get-Content -Path $templatePath -Raw125            $format.Replace("%BaseName%","FileSystemProviderStrings") | Set-Content -Path $formatFilePath -Force126            $null = $ps.AddScript("Update-FormatData -PrependPath $formatFilePath")127            $ps.Streams.Error.Clear()128            $ps.Invoke()129            $ps.Streams.Error | Should -BeNullOrEmpty130 131        }132        It "Resources in CorePS syntax should be loaded successfully" {133            $format = Get-Content -Path $templatePath -Raw134            $format.Replace("%BaseName%","System.Management.Automation.resources.FileSystemProviderStrings") | Set-Content -Path $formatFilePath -Force135            $null = $ps.AddScript("Update-FormatData -PrependPath $formatFilePath")136            $ps.Streams.Error.Clear()137            $ps.Invoke()138            $ps.Streams.Error | Should -BeNullOrEmpty139        }140        It "Verify assembly path in error message when resource is Not found" {141            $format = Get-Content -Path $templatePath -Raw142            $format.Replace("%BaseName%","NonExistingResource") | Set-Content -Path $formatFilePath -Force143            $null = $ps.AddScript("Update-FormatData -PrependPath $formatFilePath")144            $ps.Streams.Error.Clear()145            $ps.Invoke()146            $sma = [appdomain]::CurrentDomain.GetAssemblies() | Where-Object { if ($_.Location) {$_.Location.EndsWith("System.Management.Automation.dll")}}147            $smaLocation = $sma.Location148            $ps.Streams.Error | ForEach-Object { $_.Exception.Message.Contains($smaLocation) | Should -BeTrue }149            $ps.Streams.Error | ForEach-Object { $_.FullyQualifiedErrorId | Should -Match 'FormatXmlUpdateException' }150        }151    }152}153