Fix Property and ScriptBlock expressions in EntrySelectedBy tags within custom controls (#7913)

Send the current object when evaluating `EntrySelectedBy` in a custom control definition
This commit is contained in:
Patrick Meinecke
2018-10-11 21:09:39 +05:00
committed by Ilya
parent e660e9623a
commit 680ad9c835
2 changed files with 143 additions and 1 deletions
@@ -149,7 +149,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
TypeMatch match = new TypeMatch(_expressionFactory, _db, typeNames);
foreach (ComplexControlEntryDefinition x in complexBody.optionalEntryList)
{
if (match.PerfectMatch(new TypeMatchItem(x, x.appliesTo)))
if (match.PerfectMatch(new TypeMatchItem(x, x.appliesTo, so)))
{
return x;
}
@@ -298,3 +298,145 @@ class MyLeaf2
$result | Should -Be $expectedResult
}
}
Describe "Format-Custom with expression based EntrySelectedBy in a CustomControl" -Tags "CI" {
BeforeAll {
$formatFilePath = Join-Path $TestDrive 'UpdateFormatDataTests.format.ps1xml'
$xmlContent = @'
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
<Controls>
<Control>
<Name>MyTestControl</Name>
<CustomControl>
<CustomEntries>
<CustomEntry>
<EntrySelectedBy>
<SelectionCondition>
<TypeName>MyTestObject</TypeName>
<ScriptBlock>$_.Name -eq 'SelectScriptBlock'</ScriptBlock>
</SelectionCondition>
</EntrySelectedBy>
<CustomItem>
<Text>Entry selected by ScriptBlock</Text>
</CustomItem>
</CustomEntry>
<CustomEntry>
<EntrySelectedBy>
<SelectionCondition>
<TypeName>MyTestObject</TypeName>
<PropertyName>SelectProperty</PropertyName>
</SelectionCondition>
</EntrySelectedBy>
<CustomItem>
<Text>Entry selected by property</Text>
</CustomItem>
</CustomEntry>
<CustomEntry>
<CustomItem>
<Text>Default was picked</Text>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</Control>
</Controls>
<ViewDefinitions>
<View>
<Name>DefaultView</Name>
<ViewSelectedBy>
<TypeName>MyTestObject</TypeName>
</ViewSelectedBy>
<GroupBy>
<PropertyName>Name</PropertyName>
<CustomControlName>MyTestControl</CustomControlName>
</GroupBy>
<TableControl>
<TableHeaders>
<TableColumnHeader />
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Name</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
'@
Set-Content -Path $formatFilePath -Value $xmlContent
$ps = [powershell]::Create()
$iss = [initialsessionstate]::CreateDefault2()
$iss.Formats.Add($formatFilePath)
$rs = [runspacefactory]::CreateRunspace($iss)
$rs.Open()
$ps.Runspace = $rs
}
AfterAll {
$rs.Close()
$ps.Dispose()
}
It 'Property expression binding should be able to access the current object' {
$script = {
[PSCustomObject]@{
PSTypeName = 'MyTestObject'
SelectProperty = $true
Name = 'testing'
}
}
$null = $ps.AddScript($script).AddCommand('Out-String')
$ps.Streams.Error.Clear()
$expectedOutput = @'
Entry selected by property
Name
----
testing
'@ -replace '\r?\n', [Environment]::NewLine
$ps.Invoke() | Should -BeExactly $expectedOutput
$ps.Streams.Error | Should -BeNullOrEmpty
}
It 'ScriptBlock expression binding should be able to access the current object' {
$script = {
[PSCustomObject]@{
PSTypeName = 'MyTestObject'
SelectProperty = $false
Name = 'SelectScriptBlock'
}
}
$null = $ps.AddScript($script).AddCommand('Out-String')
$ps.Streams.Error.Clear()
$expectedOutput = @'
Entry selected by ScriptBlock
Name
----
SelectScriptBlock
'@ -replace '\r?\n', [Environment]::NewLine
$ps.Invoke() | Should -BeExactly $expectedOutput
$ps.Streams.Error | Should -BeNullOrEmpty
}
}