Skip to content

Commit 2f80ab1

Browse files
SeeminglyScienceiSazonov
authored andcommitted
Fix Property and ScriptBlock expressions in EntrySelectedBy tags within custom controls (PowerShell#7913)
Send the current object when evaluating `EntrySelectedBy` in a custom control definition
1 parent 31e90ac commit 2f80ab1

File tree

2 files changed

+143
-1
lines changed

2 files changed

+143
-1
lines changed

src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator_Complex.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private ComplexControlEntryDefinition GetActiveComplexControlEntryDefinition(Com
149149
TypeMatch match = new TypeMatch(_expressionFactory, _db, typeNames);
150150
foreach (ComplexControlEntryDefinition x in complexBody.optionalEntryList)
151151
{
152-
if (match.PerfectMatch(new TypeMatchItem(x, x.appliesTo)))
152+
if (match.PerfectMatch(new TypeMatchItem(x, x.appliesTo, so)))
153153
{
154154
return x;
155155
}

test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Custom.Tests.ps1

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,145 @@ class MyLeaf2
298298
$result | Should -Be $expectedResult
299299
}
300300
}
301+
302+
Describe "Format-Custom with expression based EntrySelectedBy in a CustomControl" -Tags "CI" {
303+
BeforeAll {
304+
$formatFilePath = Join-Path $TestDrive 'UpdateFormatDataTests.format.ps1xml'
305+
$xmlContent = @'
306+
<?xml version="1.0" encoding="UTF-8" ?>
307+
<Configuration>
308+
<Controls>
309+
<Control>
310+
<Name>MyTestControl</Name>
311+
<CustomControl>
312+
<CustomEntries>
313+
<CustomEntry>
314+
<EntrySelectedBy>
315+
<SelectionCondition>
316+
<TypeName>MyTestObject</TypeName>
317+
<ScriptBlock>$_.Name -eq 'SelectScriptBlock'</ScriptBlock>
318+
</SelectionCondition>
319+
</EntrySelectedBy>
320+
<CustomItem>
321+
<Text>Entry selected by ScriptBlock</Text>
322+
</CustomItem>
323+
</CustomEntry>
324+
<CustomEntry>
325+
<EntrySelectedBy>
326+
<SelectionCondition>
327+
<TypeName>MyTestObject</TypeName>
328+
<PropertyName>SelectProperty</PropertyName>
329+
</SelectionCondition>
330+
</EntrySelectedBy>
331+
<CustomItem>
332+
<Text>Entry selected by property</Text>
333+
</CustomItem>
334+
</CustomEntry>
335+
<CustomEntry>
336+
<CustomItem>
337+
<Text>Default was picked</Text>
338+
</CustomItem>
339+
</CustomEntry>
340+
</CustomEntries>
341+
</CustomControl>
342+
</Control>
343+
</Controls>
344+
<ViewDefinitions>
345+
<View>
346+
<Name>DefaultView</Name>
347+
<ViewSelectedBy>
348+
<TypeName>MyTestObject</TypeName>
349+
</ViewSelectedBy>
350+
<GroupBy>
351+
<PropertyName>Name</PropertyName>
352+
<CustomControlName>MyTestControl</CustomControlName>
353+
</GroupBy>
354+
<TableControl>
355+
<TableHeaders>
356+
<TableColumnHeader />
357+
</TableHeaders>
358+
<TableRowEntries>
359+
<TableRowEntry>
360+
<TableColumnItems>
361+
<TableColumnItem>
362+
<PropertyName>Name</PropertyName>
363+
</TableColumnItem>
364+
</TableColumnItems>
365+
</TableRowEntry>
366+
</TableRowEntries>
367+
</TableControl>
368+
</View>
369+
</ViewDefinitions>
370+
</Configuration>
371+
'@
372+
373+
Set-Content -Path $formatFilePath -Value $xmlContent
374+
$ps = [powershell]::Create()
375+
$iss = [initialsessionstate]::CreateDefault2()
376+
$iss.Formats.Add($formatFilePath)
377+
$rs = [runspacefactory]::CreateRunspace($iss)
378+
$rs.Open()
379+
$ps.Runspace = $rs
380+
}
381+
382+
AfterAll {
383+
$rs.Close()
384+
$ps.Dispose()
385+
}
386+
387+
It 'Property expression binding should be able to access the current object' {
388+
$script = {
389+
[PSCustomObject]@{
390+
PSTypeName = 'MyTestObject'
391+
SelectProperty = $true
392+
Name = 'testing'
393+
}
394+
}
395+
396+
$null = $ps.AddScript($script).AddCommand('Out-String')
397+
$ps.Streams.Error.Clear()
398+
$expectedOutput = @'
399+
400+
401+
Entry selected by property
402+
403+
Name
404+
----
405+
testing
406+
407+
408+
409+
'@ -replace '\r?\n', [Environment]::NewLine
410+
411+
$ps.Invoke() | Should -BeExactly $expectedOutput
412+
$ps.Streams.Error | Should -BeNullOrEmpty
413+
}
414+
415+
It 'ScriptBlock expression binding should be able to access the current object' {
416+
$script = {
417+
[PSCustomObject]@{
418+
PSTypeName = 'MyTestObject'
419+
SelectProperty = $false
420+
Name = 'SelectScriptBlock'
421+
}
422+
}
423+
424+
$null = $ps.AddScript($script).AddCommand('Out-String')
425+
$ps.Streams.Error.Clear()
426+
$expectedOutput = @'
427+
428+
429+
Entry selected by ScriptBlock
430+
431+
Name
432+
----
433+
SelectScriptBlock
434+
435+
436+
437+
'@ -replace '\r?\n', [Environment]::NewLine
438+
439+
$ps.Invoke() | Should -BeExactly $expectedOutput
440+
$ps.Streams.Error | Should -BeNullOrEmpty
441+
}
442+
}

0 commit comments

Comments
 (0)