From 12be68c3333510253b1f1832d1a8fb99657e8c05 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 10 Oct 2018 13:50:16 -0700 Subject: [PATCH 1/4] allow root node of format.ps1xml to have attributes that are ignored added tests for format and types --- .../DisplayDatabase/typeDataXmlLoader.cs | 2 +- .../Update-FormatData.Tests.ps1 | 54 ++++++++++++++----- .../Update-TypeData.Tests.ps1 | 28 +++++++++- 3 files changed, 70 insertions(+), 14 deletions(-) diff --git a/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader.cs b/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader.cs index 736bfa1bf2e..7d10156929c 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 @@ private void LoadData(XmlDocument doc, TypeInfoDataBase db) 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/Update-FormatData.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Update-FormatData.Tests.ps1 index e50cd51acd0..abff31f3e3d 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 = @" 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 74ae2a513b3..ecb89ff62fe 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 From 405724fd24e1d9427bdcc47b64afd435f1336489 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 10 Oct 2018 18:14:52 -0700 Subject: [PATCH 2/4] give test.format.ps1xml files unique names to identify cause of failures --- .../Microsoft.PowerShell.Utility/Format-Table.Tests.ps1 | 8 ++++---- .../Update-FormatData.Tests.ps1 | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) 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 5cd6ef90393..60fcf791f6c 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 abff31f3e3d..cca75180a30 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Update-FormatData.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Update-FormatData.Tests.ps1 @@ -99,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" } } From c76cf4bedf240f6360e83421b9e59c6f757f7bab Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 10 Oct 2018 20:47:20 -0700 Subject: [PATCH 3/4] if there is a failure processing formats, revert to original formats for initialsessionstate --- .../commands/utility/Update-TypeData.cs | 8 ++++++++ 1 file changed, 8 insertions(+) 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 c283d2340d6..03168ab65e9 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 @@ protected override void ProcessRecord() } } + var originalFormats = Context.InitialSessionState.Formats; try { // Always rebuild the format information @@ -984,6 +985,13 @@ protected override void ProcessRecord() } catch (RuntimeException e) { + // revert Formats if there is a failure + Context.InitialSessionState.Formats.Clear(); + foreach (var format in originalFormats) + { + Context.InitialSessionState.Formats.Add(format); + } + this.WriteError(new ErrorRecord(e, "FormatXmlUpdateException", ErrorCategory.InvalidOperation, null)); } } From 02518240be871fe094f4a27b979fffa942f56b7f Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Thu, 11 Oct 2018 15:10:59 -0700 Subject: [PATCH 4/4] address Aditya's feedback --- .../commands/utility/Update-TypeData.cs | 6 +----- 1 file changed, 1 insertion(+), 5 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 03168ab65e9..f1b405ea848 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Update-TypeData.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Update-TypeData.cs @@ -987,11 +987,7 @@ protected override void ProcessRecord() { // revert Formats if there is a failure Context.InitialSessionState.Formats.Clear(); - foreach (var format in originalFormats) - { - Context.InitialSessionState.Formats.Add(format); - } - + Context.InitialSessionState.Formats.Add(originalFormats); this.WriteError(new ErrorRecord(e, "FormatXmlUpdateException", ErrorCategory.InvalidOperation, null)); } }