Skip to content

Commit 0df02b7

Browse files
authored
Merge pull request #41 from rulasg/Format-InvokeParameterString
feat: Add ConvertTo-InvokeParameterString and test
2 parents 0278129 + 5222c3f commit 0df02b7

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

InvokeHelperTest/public/Invoke-MyCommand.test.ps1

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,37 @@ function InvokeHelperTest_Invoke_MyCommand_WithParameters_WithMock {
7676
Assert-AreEqual -Expected 'fakeName' -Presented $result.login
7777
Assert-AreEqual -Expected 6666666 -Presented $result.id
7878

79+
}
80+
81+
function InvokeHelperTest_Invoke_MyCommand_WithDoubleQuotes {
82+
[CmdletBinding()]
83+
param()
84+
85+
$str = 'Hello "world"'
86+
$command = 'echo "{string}"'
87+
88+
## Wrong call with no parameter formatting
89+
90+
# Arrange
91+
$param = @{
92+
string= $str
93+
}
94+
# Act
95+
$result = Invoke-MyCommand -Command $command -Parameters $param
96+
97+
# Assert
98+
Assert-AreNotEqual -Expected $str -Presented $result
99+
100+
# Correct call with parameter formatting
101+
102+
# Arrange
103+
$param = @{
104+
string= $str | ConvertTo-InvokeParameterString
105+
}
106+
107+
# Act
108+
$result = Invoke-MyCommand -Command $command -Parameters $param
109+
110+
# Assert
111+
Assert-AreEqual -Expected $str -Presented $result
79112
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
# Invoke command use powershell calls to manage the dependencies
3+
# if a string contains double quotes, it will fail as it will confuse the string content with parameter values
4+
# 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
5+
6+
function ConvertTo-InvokeParameterString {
7+
param (
8+
[Parameter(ValueFromPipeline, Position = 0)][string]$InputString
9+
)
10+
11+
process{
12+
13+
if ([string]::IsNullOrEmpty($InputString)) {
14+
return $InputString
15+
}
16+
17+
$OutputString = $InputString -replace '"', '""'
18+
return $OutputString
19+
}
20+
} Export-ModuleMember -Function ConvertTo-InvokeParameterString

0 commit comments

Comments
 (0)