8000 Pretty print Export-FormatData XML output (#6691) · PowerShell/PowerShell@313a859 · GitHub
[go: up one dir, main page]

Skip to content

Commit 313a859

Browse files
authored
Pretty print Export-FormatData XML output (#6691)
Pretty print Export-FormatData XML output by default. Refactor Export-FormatData tests, remove test duplications.
1 parent 942c68e commit 313a859

File tree

3 files changed

+143
-85
lines changed

3 files changed

+143
-85
lines changed

src/System.Management.Automation/FormatAndOutput/common/FormatXMLWriter.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ internal static void WriteToPs1Xml(PSCmdlet cmdlet, List<ExtendedTypeDefinition>
4141

4242
try
4343
{
44-
using (XmlWriter xmlWriter = XmlWriter.Create(streamWriter))
44+
var settings = new XmlWriterSettings();
45+
settings.Indent = true;
46+
settings.IndentChars = " ";
47+
settings.NewLineOnAttributes = true;
48+
49+
using (XmlWriter xmlWriter = XmlWriter.Create(streamWriter, settings))
4550
{
4651
var writer = new FormatXmlWriter
4752
{
Lines changed: 137 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,68 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
3-
Describe "Export-FormatData DRT Unit Tests" -Tags "CI" {
4-
It "Test basic functionality" {
3+
Describe "Export-FormatData" -Tags "CI" {
4+
BeforeAll {
55
$fd = Get-FormatData
6-
$tempFile = Join-Path $TestDrive -ChildPath "exportFormatTest.txt"
7-
$results = Export-FormatData -InputObject $fd[0] -Path $tempFile
8-
$content = Get-Content $tempFile
6+
$testOutput = Join-Path -Path $TestDrive -ChildPath "outputfile"
7+
}
8+
9+
AfterEach {
10+
Remove-Item $testOutput -Force -ErrorAction SilentlyContinue
11+
}
12+
13+
It "Can export all types" {
14+
try
15+
{
16+
$fd | Export-FormatData -path $TESTDRIVE\allformat.ps1xml -IncludeScriptBlock
17+
18+
$sessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
19+
$sessionState.Formats.Clear()
20+
$sessionState.Types.Clear()
21+
22+
$runspace = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace($sessionState)
23+
$runspace.Open()
24+
25+
$runspace.CreatePipeline("Update-FormatData -AppendPath $TESTDRIVE\allformat.ps1xml").Invoke()
26+
$actualAllFormat = $runspace.CreatePipeline("Get-FormatData -TypeName *").Invoke()
27+
28+
$fd.Count | Should -Be $actualAllFormat.Count
29+
Compare-Object $fd $actualAllFormat | Should -Be $null
30+
}
31+
finally
32+
{
33+
$runspace.Close()
34+
Remove-Item -Path $TESTDRIVE\allformat.ps1xml -Force -ErrorAction SilentlyContinue
35+
}
36+
}
37+
38+
It "Works with literal path" {
39+
$filename = 'TestDrive:\[formats.ps1xml'
40+
$fd | Export-FormatData -LiteralPath $filename
41+
(Test-Path -LiteralPath $filename) | Should -BeTrue
42+
}
43+
44+
It "Should overwrite the destination file" {
45+
$filename = 'TestDrive:\ExportFormatDataWithForce.ps1xml'
46+
$unexpected = "SHOULD BE OVERWRITTEN"
47+
$unexpected | Out-File -FilePath $filename -Force
48+
$file = Get-Item $filename
49+
$file.IsReadOnly = $true
50+
$fd | Export-FormatData -Path $filename -Force
51+
52+
$actual = @(Get-Content $filename)[0]
53+
$actual | Should -Not -Be $unexpected
54+
}
55+
56+
It "should not overwrite the destination file with NoClobber" {
57+
$filename = "TestDrive:\ExportFormatDataWithNoClobber.ps1xml"
58+
$fd | Export-FormatData -LiteralPath $filename
59+
60+
{ $fd | Export-FormatData -LiteralPath $filename -NoClobber } | Should -Throw -ErrorId 'NoClobber,Microsoft.PowerShell.Commands.ExportFormatDataCommand'
61+
}
62+
63+
It "Test basic functionality" {
64+
Export-FormatData -InputObject $fd[0] -Path $testOutput
65+
$content = Get-Content $testOutput -Raw
966
$formatViewDefinition = $fd[0].FormatViewDefinition
1067
$typeName = $fd[0].TypeName
1168
$content.Contains($typeName) | Should -BeTrue
@@ -14,33 +71,86 @@ Describe "Export-FormatData DRT Unit Tests" -Tags "CI" {
1471
$content.Contains($formatViewDefinition[$i].Name) | Should -BeTrue
1572
}
1673
}
17-
}
1874

19-
Describe "Export-FormatData" -Tags "CI" {
75+
It "Should have a valid xml tag at the start of the file" {
76+
$fd | Export-FormatData -Path $testOutput
77+
$piped = Get-Content $testOutput -Raw
78+
$piped[0] | Should -BeExactly "<"
79+
}
2080

21-
$testOutput = Join-Path -Path $TestDrive -ChildPath "outputfile"
81+
It "Should pretty print xml output" {
82+
$xmlContent=@"
83+
<Configuration>
84+
<ViewDefinitions>
85+
<View>
86+
<Name>ExportFormatDataName</Name>
87+
<ViewSelectedBy>
88+
<TypeName>ExportFormatDataTypeName</TypeName>
89+
</ViewSelectedBy>
90+
<TableControl>
91+
<TableHeaders />
92+
<TableRowEntries>
93+
<TableRowEntry>
94+
<TableColumnItems>
95+
<TableColumnItem>
96+
<PropertyName>Guid</PropertyName>
97+
</TableColumnItem>
98+
</TableColumnItems>
99+
</TableRowEntry>
100+
</TableRowEntries>
101+
</TableControl>
102+
</View>
103+
</ViewDefinitions>
104+
</Configuration>
< F438 /code>105+
"@
106+
$expected = @"
107+
<?xml version="1.0" encoding="utf-8"?>
108+
<Configuration>
109+
<ViewDefinitions>
110+
<View>
111+
<Name>ExportFormatDataName</Name>
112+
<ViewSelectedBy>
113+
<TypeName>ExportFormatDataTypeName</TypeName>
114+
</ViewSelectedBy>
115+
<TableControl>
116+
<TableHeaders />
117+
<TableRowEntries>
118+
<TableRowEntry>
119+
<TableColumnItems>
120+
<TableColumnItem>
121+
<PropertyName>Guid</PropertyName>
122+
</TableColumnItem>
123+
</TableColumnItems>
124+
</TableRowEntry>
125+
</TableRowEntries>
126+
</TableControl>
127+
</View>
128+
</ViewDefinitions>
129+
</Configuration>
130+
"@ -replace "`r`n?|`n", ""
131+
try
132+
{
133+
$testfilename = [guid]::NewGuid().ToString('N')
134+
$testfile = Join-Path -Path $TestDrive -ChildPath "$testfilename.ps1xml"
135+
Set-Content -Path $testfile -Value $xmlContent
22136

23-
AfterEach {
24-
Remove-Item $testOutput -Force -ErrorAction SilentlyContinue
25-
}
137+
$sessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
138+
$sessionState.Formats.Clear()
139+
$sessionState.Types.Clear()
26140

27-
Context "Check Export-FormatData can be called validly." {
28-
It "Should be able to be called without error" {
29-
{ Get-FormatData | Export-FormatData -Path $testOutput } | Should -Not -Throw
30-
}
31-
}
141+
$runspace = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace($sessionState)
142+
$runspace.Open()
32143

33-
Context "Check that the output is in the correct format" {
34-
It "Should not return an empty xml file" {
35-
Get-FormatData | Export-FormatData -Path $testOutput
36-
$piped = Get-Content $testOutput
37-
$piped | Should -Not -BeNullOrEmpty
38-
}
144+
$runspace.CreatePipeline("Update-FormatData -prependPath $testfile").Invoke()
145+
$runspace.CreatePipeline("Get-FormatData -TypeName 'ExportFormatDataTypeName' | Export-FormatData -Path $testOutput").Invoke()
39146

40-
It "Should have a valid xml tag at the start of the file" {
41-
Get-FormatData | Export-FormatData -Path $testOutput
42-
$piped = Get-Content $testOutput
43-
$piped[0] | Should -BeExactly "<"
44-
}
147+
$content = (Get-Content $testOutput -Raw) -replace "`r`n?|`n", ""
148+
149+
$content | Should -BeExactly $expected
150+
}
151+
finally
152+
{
153+
$runspace.Close()
154+
}
45155
}
46156
}

test/powershell/Modules/Microsoft.PowerShell.Utility/formatdata.tests.ps1

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0