Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,13 @@ private void appendOneNativeArgument(ExecutionContext context, object obj, char
if (NeedQuotes(arg))
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about:

_arguments.Append('"'); 
_arguments.Append(arg); 
for (int i = arg.Length-1; i >= 0 && arg[i] == '\\'; i--)
{
    _arguments.Append('\\');
}
_arguments.Append('"'); 

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's much better than my attempt. Will change.

_arguments.Append('"');
// need to escape all trailing backslashes so the native command receives it correctly
// according to http://www.daviddeley.com/autohotkey/parameters/parameters.htm#WINCRULESDOC
_arguments.Append(arg);
for (int i = arg.Length-1; i >= 0 && arg[i] == '\\'; i--)
{
_arguments.Append('\\');
}
_arguments.Append('"');
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Describe "Native Command Arguments" -tags "CI" {
It "Should handle quoted spaces correctly" {
$a = 'a"b c"d'
$lines = testexe -echoargs $a 'a"b c"d' a"b c"d
($lines | measure).Count | Should Be 3
$lines.Count | Should Be 3
$lines[0] | Should Be 'Arg 0 is <ab cd>'
$lines[1] | Should Be 'Arg 1 is <ab cd>'
$lines[2] | Should Be 'Arg 2 is <ab cd>'
Expand All @@ -27,8 +27,20 @@ Describe "Native Command Arguments" -tags "CI" {
# looking at how it got the arguments.
It "Should handle spaces between escaped quotes" {
$lines = testexe -echoargs 'a\"b c\"d' "a\`"b c\`"d"
($lines | measure).Count | Should Be 2
$lines.Count | Should Be 2
$lines[0] | Should Be 'Arg 0 is <a"b c"d>'
$lines[1] | Should Be 'Arg 1 is <a"b c"d>'
}

It "Should correctly quote paths with spaces: <arguments>" -TestCases @(
@{arguments = "'.\test 1\' `".\test 2\`"" ; expected = @(".\test 1\",".\test 2\")},
@{arguments = "'.\test 1\\\' `".\test 2\\`""; expected = @(".\test 1\\\",".\test 2\\")}
) {
param($arguments, $expected)
$lines = Invoke-Expression "testexe -echoargs $arguments"
$lines.Count | Should Be $expected.Count
for ($i = 0; $i -lt $lines.Count; $i++) {
$lines[$i] | Should Be "Arg $i is <$($expected[$i])>"
}
}
}