From 434cacfc51fb70c6da726196d790949d7bf50026 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Wed, 19 Jan 2022 19:58:07 +0000 Subject: [PATCH 1/2] Invoke-Formatter: Accept input from pipeline --- Engine/Commands/InvokeFormatterCommand.cs | 2 +- Tests/Engine/InvokeFormatter.tests.ps1 | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Engine/Commands/InvokeFormatterCommand.cs b/Engine/Commands/InvokeFormatterCommand.cs index c0d9fd168..e037605e2 100644 --- a/Engine/Commands/InvokeFormatterCommand.cs +++ b/Engine/Commands/InvokeFormatterCommand.cs @@ -25,7 +25,7 @@ public class InvokeFormatterCommand : PSCmdlet, IOutputWriter /// /// *NOTE*: Unlike ScriptBlock parameter, the ScriptDefinition parameter require a string value. /// - [ParameterAttribute(Mandatory = true, Position = 1)] + [ParameterAttribute(Mandatory = true, Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)] [ValidateNotNull] public string ScriptDefinition { get; set; } diff --git a/Tests/Engine/InvokeFormatter.tests.ps1 b/Tests/Engine/InvokeFormatter.tests.ps1 index 7fc0703fc..a3671b20e 100644 --- a/Tests/Engine/InvokeFormatter.tests.ps1 +++ b/Tests/Engine/InvokeFormatter.tests.ps1 @@ -14,6 +14,15 @@ Describe "Invoke-Formatter Cmdlet" { } } + Context 'Accept Value from Pipeline' { + It 'Value from Pipeline' { + 'foo' | Invoke-Formatter | Should -Be 'foo' + } + It 'Value from Pipeline by Property Name' { + [pscustomobject]@{ 'ScriptDefinition' = 'foo' } | Invoke-Formatter | Should -Be 'foo' + } + } + Context "When positional parameters are given" { It "Should use the positional parameters" { $def = @" From d647e4c8358faec72f1e36d77f1d2ec67b757ada Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Wed, 19 Jan 2022 20:07:57 +0000 Subject: [PATCH 2/2] other params as well --- Engine/Commands/InvokeFormatterCommand.cs | 6 +++--- Tests/Engine/InvokeFormatter.tests.ps1 | 6 +++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Engine/Commands/InvokeFormatterCommand.cs b/Engine/Commands/InvokeFormatterCommand.cs index e037605e2..d08d8c817 100644 --- a/Engine/Commands/InvokeFormatterCommand.cs +++ b/Engine/Commands/InvokeFormatterCommand.cs @@ -25,14 +25,14 @@ public class InvokeFormatterCommand : PSCmdlet, IOutputWriter /// /// *NOTE*: Unlike ScriptBlock parameter, the ScriptDefinition parameter require a string value. /// - [ParameterAttribute(Mandatory = true, Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)] + [ParameterAttribute(Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 1)] [ValidateNotNull] public string ScriptDefinition { get; set; } /// /// A settings hashtable or a path to a PowerShell data file (.psd1) file that contains the settings. /// - [Parameter(Mandatory = false, Position = 2)] + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 2)] [ValidateNotNull] public object Settings { get; set; } = defaultSettingsPreset; @@ -44,7 +44,7 @@ public class InvokeFormatterCommand : PSCmdlet, IOutputWriter /// end column number. These numbers must be greater than 0. /// /// - [Parameter(Mandatory = false, Position = 3)] + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 3)] [ValidateNotNull] [ValidateCount(4, 4)] public int[] Range { get; set; } diff --git a/Tests/Engine/InvokeFormatter.tests.ps1 b/Tests/Engine/InvokeFormatter.tests.ps1 index a3671b20e..dcc0a6174 100644 --- a/Tests/Engine/InvokeFormatter.tests.ps1 +++ b/Tests/Engine/InvokeFormatter.tests.ps1 @@ -18,9 +18,13 @@ Describe "Invoke-Formatter Cmdlet" { It 'Value from Pipeline' { 'foo' | Invoke-Formatter | Should -Be 'foo' } - It 'Value from Pipeline by Property Name' { + It 'Value from Pipeline by Property Name with just the mandatory ScriptDefinition parameter' { [pscustomobject]@{ 'ScriptDefinition' = 'foo' } | Invoke-Formatter | Should -Be 'foo' } + It 'Value from Pipeline by Property Name with all parameters' { + [pscustomobject]@{ ScriptDefinition = 'foo'; Settings = @(); Range = 1, 1, 1, 4 } | + Invoke-Formatter | Should -Be 'foo' + } } Context "When positional parameters are given" {