8000 Pretty print Export-FormatData XML output by iSazonov · Pull Request #6691 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Pretty print Export-FormatData XML output #6691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ internal static void WriteToPs1Xml(PSCmdlet cmdlet, List<ExtendedTypeDefinition>

try
{
using (XmlWriter xmlWriter = XmlWriter.Create(streamWriter))
var settings = new XmlWriterSettings();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use a static XmlWriterSettings?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thoughts is that the cmdlet is used so rarely that we can allow the allocation and don't use a static.

settings.Indent = true;
settings.IndentChars = " ";
settings.NewLineOnAttributes = true;

using (XmlWriter xmlWriter = XmlWriter.Create(streamWriter, settings))
{
var writer = new FormatXmlWriter
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,68 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
Describe "Export-FormatData DRT Unit Tests" -Tags "CI" {
It "Test basic functionality" {
Describe "Export-FormatData" -Tags "CI" {
BeforeAll {
$fd = Get-FormatData
$tempFile = Join-Path $TestDrive -ChildPath "exportFormatTest.txt"
$results = Export-FormatData -InputObject $fd[0] -Path $tempFile
$content = Get-Content $tempFile
$testOutput = Join-Path -Path $TestDrive -ChildPath "outputfile"
}

AfterEach {
Remove-Item $testOutput -Force -ErrorAction SilentlyContinue
}

It "Can export all types" {
try
{
$fd | Export-FormatData -path $TESTDRIVE\allformat.ps1xml -IncludeScriptBlock

$sessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
$sessionState.Formats.Clear()
$sessionState.Types.Clear()

$runspace = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace($sessionState)
$runspace.Open()

$runspace.CreatePipeline("Update-FormatData -AppendPath $TESTDRIVE\allformat.ps1xml").Invoke()
$actualAllFormat = $runspace.CreatePipeline("Get-FormatData -TypeName *").Invoke()

$fd.Count | Should -Be $actualAllFormat.Count
Compare-Object $fd $actualAllFormat | Should -Be $null
}
finally
{
$runspace.Close()
Remove-Item -Path $TESTDRIVE\allformat.ps1xml -Force -ErrorAction SilentlyContinue
}
}

It "Works with literal path" {
$filename = 'TestDrive:\[formats.ps1xml'
$fd | Export-FormatData -LiteralPath $filename
(Test-Path -LiteralPath $filename) | Should -BeTrue
}

It "Should overwrite the destination file" {
$filename = 'TestDrive:\ExportFormatDataWithForce.ps1xml'
$unexpected = "SHOULD BE OVERWRITTEN"
$unexpected | Out-File -FilePath $filename -Force
$file = Get-Item $filename
$file.IsReadOnly = $true
$fd | Export-FormatData -Path $filename -Force

$actual = @(Get-Content $filename)[0]
$actual | Should -Not -Be $unexpected
}

It "should not overwrite the destination file with NoClobber" {
$filename = "TestDrive:\ExportFormatDataWithNoClobber.ps1xml"
$fd | Export-FormatData -LiteralPath $filename

{ $fd | Export-FormatData -LiteralPath $filename -NoClobber } | Should -Throw -ErrorId 'NoClobber,Microsoft.PowerShell.Commands.ExportFormatDataCommand'
}

It "Test basic functionality" {
Export-FormatData -InputObject $fd[0] -Path $testOutput
$content = Get-Content $testOutput -Raw
$formatViewDefinition = $fd[0].FormatViewDefinition
$typeName = $fd[0].TypeName
$content.Contains($typeName) | Should -BeTrue
Expand All @@ -14,33 +71,86 @@ Describe "Export-FormatData DRT Unit Tests" -Tags "CI" {
$content.Contains($formatViewDefinition[$i].Name) | Should -BeTrue
}
}
}

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

$testOutput = Join-Path -Path $TestDrive -ChildPath "outputfile"
It "Should pretty print xml output" {
$xmlContent=@"
<Configuration>
<ViewDefinitions>
<View>
<Name>ExportFormatDataName</Name>
<ViewSelectedBy>
<TypeName>ExportFormatDataTypeName</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders />
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Guid</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
"@
$expected = @"
<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<ViewDefinitions>
<View>
<Name>ExportFormatDataName</Name>
<ViewSelectedBy>
<TypeName>ExportFormatDataTypeName</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders />
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Guid</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
"@ -replace "`r`n?|`n", ""
try
{
$testfilename = [guid]::NewGuid().ToString('N')
$testfile = Join-Path -Path $TestDrive -ChildPath "$testfilename.ps1xml"
Set-Content -Path $testfile -Value $xmlContent

AfterEach {
Remove-Item $testOutput -Force -ErrorAction SilentlyContinue
}
$sessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
$sessionState.Formats.Clear()
$sessionState.Types.Clear()

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

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

It "Should have a valid xml tag at the start of the file" {
Get-FormatData | Export-FormatData -Path $testOutput
$piped = Get-Content $testOutput
$piped[0] | Should -BeExactly "<"
}
$content = (Get-Content $testOutput -Raw) -replace "`r`n?|`n", ""
Copy link
Contributor
@mklement0 mklement0 Apr 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking this on. Quibble: "`r?`n" will do (or '\r?\n')


$content | Should -BeExactly $expected
}
finally
{
$runspace.Close()
}
}
}

This file was deleted.

0