From ade6e1fc9181c00e3ceef41886939ba508d3369a Mon Sep 17 00:00:00 2001 From: iSazonov Date: Fri, 20 Apr 2018 20:46:41 +0500 Subject: [PATCH 1/4] Well format Export-FormatData XML output --- .../FormatAndOutput/common/FormatXMLWriter.cs | 7 +- .../Export-FormatData.Tests.ps1 | 102 +++++++++++++----- 2 files changed, 79 insertions(+), 30 deletions(-) diff --git a/src/System.Management.Automation/FormatAndOutput/common/FormatXMLWriter.cs b/src/System.Management.Automation/FormatAndOutput/common/FormatXMLWriter.cs index f027c7fa9e0..fa481d67322 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/FormatXMLWriter.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/FormatXMLWriter.cs @@ -41,7 +41,12 @@ internal static void WriteToPs1Xml(PSCmdlet cmdlet, List try { - using (XmlWriter xmlWriter = XmlWriter.Create(streamWriter)) + var settings = new XmlWriterSettings(); + settings.Indent = true; + settings.IndentChars = " "; + settings.NewLineOnAttributes = true; + + using (XmlWriter xmlWriter = XmlWriter.Create(streamWriter, settings)) { var writer = new FormatXmlWriter { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-FormatData.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-FormatData.Tests.ps1 index 2920a21f4c8..343af7aedcc 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-FormatData.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-FormatData.Tests.ps1 @@ -1,11 +1,18 @@ # 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 "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 @@ -14,33 +21,70 @@ Describe "Export-FormatData DRT Unit Tests" -Tags "CI" { $content.Contains($formatViewDefinition[$i].Name) | Should -BeTrue } } -} - -Describe "Export-FormatData" -Tags "CI" { - - $testOutput = Join-Path -Path $TestDrive -ChildPath "outputfile" - - AfterEach { - Remove-Item $testOutput -Force -ErrorAction SilentlyContinue - } - 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 - } + 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 "<" } - 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 - } + It "Should well format output xml" { + $xmlContent=@" + + + + ExportFormatDataName + + ExportFormatDataTypeName + + + + + + + + Guid + + + + + + + + +"@ + $expected = @" + + + + + ExportFormatDataName + + ExportFormatDataTypeName + + + + + + + + Guid + + + + + + + + +"@ + $testfilename = [guid]::NewGuid().ToString('N') + $testfile = Join-Path -Path $TestDrive -ChildPath "$testfilename.ps1xml" + Set-Content -Path $testfile -Value $xmlContent + Update-FormatData -Append $testfile + Get-FormatData -TypeName "ExportFormatDataTypeName" | Export-FormatData -Path $testOutput + $content = Get-Content $testOutput -Raw - 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 | Should -BeExactly $expected } } From 5198a3535a41eb20510da0ca73e3a500d834cef1 Mon Sep 17 00:00:00 2001 From: Ilya Date: Sat, 21 Apr 2018 22:58:52 +0500 Subject: [PATCH 2/4] Use runspace and move all Export-FormatData tests in one file --- .../Export-FormatData.Tests.ps1 | 82 +++++++++++++++++-- .../formatdata.tests.ps1 | 57 ------------- 2 files changed, 74 insertions(+), 65 deletions(-) delete mode 100644 test/powershell/Modules/Microsoft.PowerShell.Utility/formatdata.tests.ps1 diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-FormatData.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-FormatData.Tests.ps1 index 343af7aedcc..48d22f24e3f 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-FormatData.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-FormatData.Tests.ps1 @@ -10,6 +10,56 @@ Describe "Export-FormatData" -Tags "CI" { 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 @@ -28,7 +78,7 @@ Describe "Export-FormatData" -Tags "CI" { $piped[0] | Should -BeExactly "<" } - It "Should well format output xml" { + It "Should pretty print xml output" { $xmlContent=@" @@ -78,13 +128,29 @@ Describe "Export-FormatData" -Tags "CI" { "@ - $testfilename = [guid]::NewGuid().ToString('N') - $testfile = Join-Path -Path $TestDrive -ChildPath "$testfilename.ps1xml" - Set-Content -Path $testfile -Value $xmlContent - Update-FormatData -Append $testfile - Get-FormatData -TypeName "ExportFormatDataTypeName" | Export-FormatData -Path $testOutput - $content = Get-Content $testOutput -Raw + try + { + $testfilename = [guid]::NewGuid().ToString('N') + $testfile = Join-Path -Path $TestDrive -ChildPath "$testfilename.ps1xml" + Set-Content -Path $testfile -Value $xmlContent + + $sessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault() + $sessionState.Formats.Clear() + $sessionState.Types.Clear() - $content | Should -BeExactly $expected + $runspace = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace($sessionState) + $runspace.Open() + + $runspace.CreatePipeline("Update-FormatData -prependPath $testfile").Invoke() + $runspace.CreatePipeline("Get-FormatData -TypeName 'ExportFormatDataTypeName' | Export-FormatData -Path $testOutput").Invoke() + + $content = Get-Content $testOutput -Raw + + $content | Should -BeExactly $expected + } + finally + { + $runspace.Close() + } } } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/formatdata.tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/formatdata.tests.ps1 deleted file mode 100644 index ea3a89bd96b..00000000000 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/formatdata.tests.ps1 +++ /dev/null @@ -1,57 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -Describe "FormatData" -tags "Feature" { - - Context "Export" { - It "can export all types" { - try - { - $expectAllFormat = Get-FormatData -typename * - $expectAllFormat | 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() - - $expectAllFormat.Count | Should -Be $actualAllFormat.Count - Compare-Object $expectAllFormat $actualAllFormat | Should -Be $null - $runspace.Close() - } - finally - { - Remove-Item -Path $TESTDRIVE\allformat.ps1xml -Force -ErrorAction SilentlyContinue - } - } - - It "works with literal path" { - $filename = 'TestDrive:\[formats.ps1xml' - Get-FormatData -TypeName * | 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 - Get-FormatData -TypeName * | 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" - Get-FormatData -TypeName * | Export-FormatData -LiteralPath $filename - - { Get-FormatData -TypeName * | Export-FormatData -LiteralPath $filename -NoClobber } | Should -Throw -ErrorId 'NoClobber,Microsoft.PowerShell.Commands.ExportFormatDataCommand' - } - } -} From c9fca6b98a7082d19e0502d02a647edefb565410 Mon Sep 17 00:00:00 2001 From: Ilya Date: Sat, 21 Apr 2018 23:41:01 +0500 Subject: [PATCH 3/4] Use Should -FileContentMatchExactly --- .../Microsoft.PowerShell.Utility/Export-FormatData.Tests.ps1 | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-FormatData.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-FormatData.Tests.ps1 index 48d22f24e3f..acb6d3b4e2b 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-FormatData.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-FormatData.Tests.ps1 @@ -144,9 +144,7 @@ Describe "Export-FormatData" -Tags "CI" { $runspace.CreatePipeline("Update-FormatData -prependPath $testfile").Invoke() $runspace.CreatePipeline("Get-FormatData -TypeName 'ExportFormatDataTypeName' | Export-FormatData -Path $testOutput").Invoke() - $content = Get-Content $testOutput -Raw - - $content | Should -BeExactly $expected + $testOutput | Should -FileContentMatchExactly $expected } finally { From bbc826d20a65d0467f1d165cfd906829f8297c7b Mon Sep 17 00:00:00 2001 From: Ilya Date: Sun, 22 Apr 2018 00:20:03 +0500 Subject: [PATCH 4/4] Fix comparision with newlines --- .../Export-FormatData.Tests.ps1 | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-FormatData.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-FormatData.Tests.ps1 index acb6d3b4e2b..447a8ae4021 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-FormatData.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-FormatData.Tests.ps1 @@ -127,7 +127,7 @@ Describe "Export-FormatData" -Tags "CI" { -"@ +"@ -replace "`r`n?|`n", "" try { $testfilename = [guid]::NewGuid().ToString('N') @@ -144,7 +144,9 @@ Describe "Export-FormatData" -Tags "CI" { $runspace.CreatePipeline("Update-FormatData -prependPath $testfile").Invoke() $runspace.CreatePipeline("Get-FormatData -TypeName 'ExportFormatDataTypeName' | Export-FormatData -Path $testOutput").Invoke() - $testOutput | Should -FileContentMatchExactly $expected + $content = (Get-Content $testOutput -Raw) -replace "`r`n?|`n", "" + + $content | Should -BeExactly $expected } finally {