From 8bca5a6017bc5e502a2a7cd0df806724e1ddca97 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 12 Oct 2018 11:31:21 -0700 Subject: [PATCH] Allow root node of format.ps1xml to have attributes that are ignored (#7987) --- .../commands/utility/Update-TypeData.cs | 4 ++ .../DisplayDatabase/typeDataXmlLoader.cs | 2 +- .../Format-Table.Tests.ps1 | 8 +-- .../Update-FormatData.Tests.ps1 | 58 ++++++++++++++----- .../Update-TypeData.Tests.ps1 | 28 ++++++++- 5 files changed, 80 insertions(+), 20 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Update-TypeData.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Update-TypeData.cs index c283d2340d..f1b405ea84 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Update-TypeData.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Update-TypeData.cs @@ -939,6 +939,7 @@ namespace Microsoft.PowerShell.Commands } } + var originalFormats = Context.InitialSessionState.Formats; try { // Always rebuild the format information @@ -984,6 +985,9 @@ namespace Microsoft.PowerShell.Commands } catch (RuntimeException e) { + // revert Formats if there is a failure + Context.InitialSessionState.Formats.Clear(); + Context.InitialSessionState.Formats.Add(originalFormats); this.WriteError(new ErrorRecord(e, "FormatXmlUpdateException", ErrorCategory.InvalidOperation, null)); } } diff --git a/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader.cs b/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader.cs index 736bfa1bf2..7d10156929 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader.cs @@ -344,7 +344,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format bool viewDefinitionsFound = false; bool controlDefinitionsFound = false; - if (MatchNodeName(documentElement, XmlTags.ConfigurationNode)) + if (MatchNodeNameWithAttributes(documentElement, XmlTags.ConfigurationNode)) { // load the various sections using (this.StackFrame(documentElement)) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Table.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Table.Tests.ps1 index 5cd6ef9039..60fcf791f6 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Table.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Table.Tests.ps1 @@ -228,7 +228,7 @@ Describe "Format-Table" -Tags "CI" { "@ - $ps1xmlPath = Join-Path -Path $TestDrive -ChildPath "test.format.ps1xml" + $ps1xmlPath = Join-Path -Path $TestDrive -ChildPath "alignment.format.ps1xml" Set-Content -Path $ps1xmlPath -Value $ps1xml # run in own runspace so not affect global sessionstate $ps = [powershell]::Create() @@ -315,7 +315,7 @@ Left Center Right "@ - $ps1xmlPath = Join-Path -Path $TestDrive -ChildPath "test.format.ps1xml" + $ps1xmlPath = Join-Path -Path $TestDrive -ChildPath "truncation.format.ps1xml" Set-Content -Path $ps1xmlPath -Value $ps1xml # run in own runspace so not affect global sessionstate $ps = [powershell]::Create() @@ -456,7 +456,7 @@ er "@ $ps1xml = $ps1xml.Replace("{0}", $widths[0]).Replace("{1}", $widths[1]).Replace("{2}", $widths[2]) - $ps1xmlPath = Join-Path -Path $TestDrive -ChildPath "test.format.ps1xml" + $ps1xmlPath = Join-Path -Path $TestDrive -ChildPath "span.format.ps1xml" Set-Content -Path $ps1xmlPath -Value $ps1xml # run in own runspace so not affect global sessionstate $ps = [powershell]::Create() @@ -662,7 +662,7 @@ er "@ $ps1xml = $ps1xml.Replace("{0}", $widths[0]).Replace("{1}", $widths[1]).Replace("{2}", $widths[2]) - $ps1xmlPath = Join-Path -Path $TestDrive -ChildPath "test.format.ps1xml" + $ps1xmlPath = Join-Path -Path $TestDrive -ChildPath "render.format.ps1xml" Set-Content -Path $ps1xmlPath -Value $ps1xml # run in own runspace so not affect global sessionstate $ps = [powershell]::Create() diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Update-FormatData.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Update-FormatData.Tests.ps1 index e50cd51acd..cca75180a3 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Update-FormatData.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Update-FormatData.Tests.ps1 @@ -2,18 +2,10 @@ # Licensed under the MIT License. Describe "Update-FormatData" -Tags "CI" { - BeforeAll { - $path = Join-Path -Path $TestDrive -ChildPath "outputfile.ps1xml" - $ps = [powershell]::Create() - $iss = [system.management.automation.runspaces.initialsessionstate]::CreateDefault2() - $rs = [system.management.automation.runspaces.runspacefactory]::CreateRunspace($iss) - $rs.Open() - $ps.Runspace = $rs - } - AfterAll { - $rs.Close() - $ps.Dispose() + BeforeEach { + $ps = [PowerShell]::Create() } + Context "Validate Update-FormatData update correctly" { It "Should not throw upon reloading previous formatting file" { @@ -21,11 +13,49 @@ Describe "Update-FormatData" -Tags "CI" { } It "Should validly load formatting data" { + $path = Join-Path -Path $TestDrive -ChildPath "outputfile.ps1xml" Get-FormatData -typename System.Diagnostics.Process | Export-FormatData -Path $path $null = $ps.AddScript("Update-FormatData -prependPath $path") $ps.Invoke() $ps.HadErrors | Should -BeFalse } + + It "Update with atributes on Configuration node should be ignored" { + $xmlContent = @" + + + + Test + + Test + + + + + + + Test + + + + + + + + +"@ + $path = "$testdrive\rootattribute.format.ps1xml" + Set-Content -Path $path -Value $xmlContent + $null = $ps.AddScript("Update-FormatData -prependPath $path") + $ps.Invoke() + $ps.HadErrors | Should -BeFalse + $ps.Commands.Clear() + $null = $ps.AddScript("Get-FormatData test") + $formatData = $ps.Invoke() + $formatData | Should -HaveCount 1 + $formatData.TypeNames | Should -BeExactly "Test" + $formatData.FormatViewDefinition.Name | Should -BeExactly "Test" + } } } @@ -59,7 +89,7 @@ Describe "Update-FormatData basic functionality" -Tags "CI" { { Update-FormatData -Prepend $testfile -WhatIf } | Should -Not -Throw } - It "Update with invalid format xml should fail" -Pending { + It "Update with invalid format xml should fail" { $xmlContent = @" @@ -69,8 +99,8 @@ Describe "Update-FormatData basic functionality" -Tags "CI" { "@ - $xmlContent | Out-File -FilePath "$testdrive\test.format.ps1xml" -Encoding ascii - { Update-FormatData -Path "$testdrive\test.format.ps1xml" -ErrorAction Stop } | Should -Throw -ErrorId "FormatXmlUpdateException,Microsoft.PowerShell.Commands.UpdateFormatDataCommand" + $xmlContent | Out-File -FilePath "$testdrive\invalid.format.ps1xml" -Encoding ascii + { Update-FormatData -Path "$testdrive\invalid.format.ps1xml" -ErrorAction Stop } | Should -Throw -ErrorId "FormatXmlUpdateException,Microsoft.PowerShell.Commands.UpdateFormatDataCommand" } } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Update-TypeData.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Update-TypeData.Tests.ps1 index 74ae2a513b..ecb89ff62f 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Update-TypeData.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Update-TypeData.Tests.ps1 @@ -48,7 +48,33 @@ Describe "Update-TypeData basic functionality" -Tags "CI" { $ps.Dispose() } - It "Update-TypeData with Invalid TypesXml should throw Exception" { + It "Update-TypeData with attributes on root node should succeed" { + $xmlContent = @" + + + Test + + + Yada + Length + + + + +"@ + $path = "$testdrive\test.types.ps1xml" + Set-Content -Value $xmlContent -Path $path + $null = $ps.AddScript("Update-TypeData -AppendPath $path") + $ps.Invoke() + $ps.HadErrors | Should -BeFalse + $ps.Commands.Clear() + $null = $ps.AddScript("Get-TypeData test") + $typeData = $ps.Invoke() + $typeData | Should -HaveCount 1 + $typeData.TypeName | Should -BeExactly "Test" + } + + It "Update-TypeData with Invalid TypesXml should throw Exception" { $null = $ps.AddScript("Update-TypeData -PrependPath $testfile") $ps.Invoke() $ps.HadErrors | Should -BeTrue