MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Export-FormatData" -Tags "CI" {4 BeforeAll {5 $clientVersion = '5.0' # Preliminarily preserve the original test semantics in place before https://github.com/PowerShell/PowerShell/pull/112706 $fd = Get-FormatData -PowerShellVersion $clientVersion7 $testOutput = Join-Path -Path $TestDrive -ChildPath "outputfile"8 }9 10 AfterEach {11 Remove-Item $testOutput -Force -ErrorAction SilentlyContinue12 }13 14 It "Can export all types" {15 try16 {17 $fd | Export-FormatData -Path $TESTDRIVE\allformat.ps1xml -IncludeScriptBlock18 19 $sessionState = [initialsessionstate]::CreateDefault()20 $sessionState.Formats.Clear()21 $sessionState.Types.Clear()22 23 $runspace = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace($sessionState)24 $runspace.Open()25 26 $runspace.CreatePipeline("Update-FormatData -AppendPath $TESTDRIVE\allformat.ps1xml").Invoke()27 $actualAllFormat = $runspace.CreatePipeline("Get-FormatData -PowerShellVersion $clientVersion").Invoke()28 29 $fd.Count | Should -Be $actualAllFormat.Count30 Compare-Object $fd $actualAllFormat | Should -Be $null31 }32 finally33 {34 $runspace.Close()35 Remove-Item -Path $TESTDRIVE\allformat.ps1xml -Force -ErrorAction SilentlyContinue36 }37 }38 39 It "Works with literal path" {40 $filename = 'TestDrive:\[formats.ps1xml'41 $fd | Export-FormatData -LiteralPath $filename42 (Test-Path -LiteralPath $filename) | Should -BeTrue43 }44 45 It "Should overwrite the destination file" {46 $filename = 'TestDrive:\ExportFormatDataWithForce.ps1xml'47 $unexpected = "SHOULD BE OVERWRITTEN"48 $unexpected | Out-File -FilePath $filename -Force49 $file = Get-Item $filename50 $file.IsReadOnly = $true51 $fd | Export-FormatData -Path $filename -Force52 53 $actual = @(Get-Content $filename)[0]54 $actual | Should -Not -Be $unexpected55 }56 57 It "should not overwrite the destination file with NoClobber" {58 $filename = "TestDrive:\ExportFormatDataWithNoClobber.ps1xml"59 $fd | Export-FormatData -LiteralPath $filename60 61 { $fd | Export-FormatData -LiteralPath $filename -NoClobber } | Should -Throw -ErrorId 'NoClobber,Microsoft.PowerShell.Commands.ExportFormatDataCommand'62 }63 64 It "Test basic functionality" {65 Export-FormatData -InputObject $fd[0] -Path $testOutput66 $content = Get-Content $testOutput -Raw67 $formatViewDefinition = $fd[0].FormatViewDefinition68 $typeName = $fd[0].TypeName69 $content.Contains($typeName) | Should -BeTrue70 for ($i = 0; $i -lt $formatViewDefinition.Count;$i++)71 {72 $content.Contains($formatViewDefinition[$i].Name) | Should -BeTrue73 }74 }75 76 It "Should have a valid xml tag at the start of the file" {77 $fd | Export-FormatData -Path $testOutput78 $piped = Get-Content $testOutput -Raw79 $piped[0] | Should -BeExactly "<"80 }81 82 It "Should pretty print xml output" {83 $xmlContent=@"84 <Configuration>85 <ViewDefinitions>86 <View>87 <Name>ExportFormatDataName</Name>88 <ViewSelectedBy>89 <TypeName>ExportFormatDataTypeName</TypeName>90 </ViewSelectedBy>91 <TableControl>92 <TableHeaders />93 <TableRowEntries>94 <TableRowEntry>95 <TableColumnItems>96 <TableColumnItem>97 <PropertyName>Guid</PropertyName>98 </TableColumnItem>99 </TableColumnItems>100 </TableRowEntry>101 </TableRowEntries>102 </TableControl>103 </View>104 </ViewDefinitions>105 </Configuration>106"@107 $expected = @"108<?xml version="1.0" encoding="utf-8"?>109<Configuration>110 <ViewDefinitions>111 <View>112 <Name>ExportFormatDataName</Name>113 <ViewSelectedBy>114 <TypeName>ExportFormatDataTypeName</TypeName>115 </ViewSelectedBy>116 <TableControl>117 <TableHeaders />118 <TableRowEntries>119 <TableRowEntry>120 <TableColumnItems>121 <TableColumnItem>122 <PropertyName>Guid</PropertyName>123 </TableColumnItem>124 </TableColumnItems>125 </TableRowEntry>126 </TableRowEntries>127 </TableControl>128 </View>129 </ViewDefinitions>130</Configuration>131"@ -replace "`r`n?|`n", ""132 try133 {134 $testfilename = [guid]::NewGuid().ToString('N')135 $testfile = Join-Path -Path $TestDrive -ChildPath "$testfilename.ps1xml"136 Set-Content -Path $testfile -Value $xmlContent137 138 $sessionState = [initialsessionstate]::CreateDefault()139 $sessionState.Formats.Clear()140 $sessionState.Types.Clear()141 142 $runspace = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace($sessionState)143 $runspace.Open()144 145 $runspace.CreatePipeline("Update-FormatData -prependPath $testfile").Invoke()146 $runspace.CreatePipeline("Get-FormatData -TypeName 'ExportFormatDataTypeName' | Export-FormatData -Path $testOutput").Invoke()147 148 $content = (Get-Content $testOutput -Raw) -replace "`r`n?|`n", ""149 150 $content | Should -BeExactly $expected151 }152 finally153 {154 $runspace.Close()155 }156 }157 158 It 'Should be able to export multiple views' {159 $listControl = [System.Management.Automation.ListControl]::Create().StartEntry().AddItemProperty('test').AddItemProperty('test2').EndEntry().EndList()160 $tableControl = [System.Management.Automation.TableControl]::Create().StartRowDefinition().AddPropertyColumn('test').AddPropertyColumn('test2').EndRowDefinition().EndTable()161 162 $listView = [System.Management.Automation.FormatViewDefinition]::new('Default', $listControl)163 $tableView = [System.Management.Automation.FormatViewDefinition]::new('Default', $tableControl)164 165 $list = New-Object System.Collections.Generic.List[System.Management.Automation.FormatViewDefinition]166 $list.Add($listView)167 $list.Add($tableView)168 169 $typeDef = [System.Management.Automation.ExtendedTypeDefinition]::new('TestTypeName', $list)170 $filePath = Join-Path $TestDrive "test.format.ps1xml"171 $typeDef | Export-FormatData -Path $filePath172 [xml]$xml = Get-Content -Path $filePath173 @($xml.Configuration.ViewDefinitions.View).Count | Should -Be 2174 }175}176 