From 5222c3fc7b6231c506796888f7120fd5da293d44 Mon Sep 17 00:00:00 2001 From: rulasg Date: Fri, 27 Jun 2025 17:30:03 +0200 Subject: [PATCH] feat: Add ConvertTo-InvokeParameterString and test --- .../public/Invoke-MyCommand.test.ps1 | 33 +++++++++++++++++++ public/Format-InvokeParameterString.ps1 | 20 +++++++++++ 2 files changed, 53 insertions(+) create mode 100644 public/Format-InvokeParameterString.ps1 diff --git a/InvokeHelperTest/public/Invoke-MyCommand.test.ps1 b/InvokeHelperTest/public/Invoke-MyCommand.test.ps1 index e81e1b8..30a877a 100644 --- a/InvokeHelperTest/public/Invoke-MyCommand.test.ps1 +++ b/InvokeHelperTest/public/Invoke-MyCommand.test.ps1 @@ -76,4 +76,37 @@ function InvokeHelperTest_Invoke_MyCommand_WithParameters_WithMock { Assert-AreEqual -Expected 'fakeName' -Presented $result.login Assert-AreEqual -Expected 6666666 -Presented $result.id +} + +function InvokeHelperTest_Invoke_MyCommand_WithDoubleQuotes { + [CmdletBinding()] + param() + + $str = 'Hello "world"' + $command = 'echo "{string}"' + + ## Wrong call with no parameter formatting + + # Arrange + $param = @{ + string= $str + } + # Act + $result = Invoke-MyCommand -Command $command -Parameters $param + + # Assert + Assert-AreNotEqual -Expected $str -Presented $result + + # Correct call with parameter formatting + + # Arrange + $param = @{ + string= $str | ConvertTo-InvokeParameterString + } + + # Act + $result = Invoke-MyCommand -Command $command -Parameters $param + + # Assert + Assert-AreEqual -Expected $str -Presented $result } \ No newline at end of file diff --git a/public/Format-InvokeParameterString.ps1 b/public/Format-InvokeParameterString.ps1 new file mode 100644 index 0000000..11b81d8 --- /dev/null +++ b/public/Format-InvokeParameterString.ps1 @@ -0,0 +1,20 @@ + +# Invoke command use powershell calls to manage the dependencies +# if a string contains double quotes, it will fail as it will confuse the string content with parameter values +# so we need to escape the double quotes in the string to ensure the quotes that are part of the string value and not parameter boundaries + +function ConvertTo-InvokeParameterString { + param ( + [Parameter(ValueFromPipeline, Position = 0)][string]$InputString + ) + + process{ + + if ([string]::IsNullOrEmpty($InputString)) { + return $InputString + } + + $OutputString = $InputString -replace '"', '""' + return $OutputString + } +} Export-ModuleMember -Function ConvertTo-InvokeParameterString