Allow root node of format.ps1xml to have attributes that are ignored (#7987)

This commit is contained in:
Steve Lee
2018-10-12 11:31:21 -07:00
committed by Aditya Patwardhan
parent b62e152f4d
commit 8bca5a6017
5 changed files with 80 additions and 20 deletions
@@ -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));
}
}
@@ -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))
@@ -228,7 +228,7 @@ Describe "Format-Table" -Tags "CI" {
</Configuration>
"@
$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
</Configuration>
"@
$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
</Configuration>
"@
$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
</Configuration>
"@
$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()
@@ -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 = @"
<Configuration xmlns:foo="bar">
<ViewDefinitions>
<View>
<Name>Test</Name>
<ViewSelectedBy>
<TypeName>Test</TypeName>
</ViewSelectedBy>
<ListControl>
<ListEntries>
<ListEntry>
<ListItems>
<ListItem>
<PropertyName>Test</PropertyName>
</ListItem>
</ListItems>
</ListEntry>
</ListEntries>
</ListControl>
</View>
</ViewDefinitions>
</Configuration>
"@
$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 = @"
<Configuration>
<ViewDefinitions>
@@ -69,8 +99,8 @@ Describe "Update-FormatData basic functionality" -Tags "CI" {
</ViewDefinitions>
</Configuration>
"@
$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"
}
}
@@ -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 = @"
<Types xmlns:foo="bar">
<Type>
<Name>Test</Name>
<Members>
<AliasProperty>
<Name>Yada</Name>
<ReferencedMemberName>Length</ReferencedMemberName>
</AliasProperty>
</Members>
</Type>
</Types>
"@
$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