Fix $PSNativeCommandArgPassing = Windows to handle empty args correctly (#16639)

This commit is contained in:
Steve Lee
2022-01-11 23:21:32 -08:00
committed by GitHub
parent e2f39fa0eb
commit 0f21156dee
2 changed files with 25 additions and 3 deletions
@@ -314,7 +314,7 @@ namespace System.Management.Automation
}
else
{
if (argArrayAst != null && ArgumentPassingStyle == NativeArgumentPassingStyle.Standard)
if (argArrayAst != null && ArgumentPassingStyle != NativeArgumentPassingStyle.Legacy)
{
// We have a literal array, so take the extent, break it on spaces and add them to the argument list.
foreach (string element in argArrayAst.Extent.Text.Split(' ', StringSplitOptions.RemoveEmptyEntries))
@@ -331,7 +331,7 @@ namespace System.Management.Automation
}
}
}
else if (ArgumentPassingStyle == NativeArgumentPassingStyle.Standard && currentObj != null)
else if (ArgumentPassingStyle != NativeArgumentPassingStyle.Legacy && currentObj != null)
{
// add empty strings to arglist, but not nulls
AddToArgumentList(parameter, arg);
@@ -171,7 +171,7 @@ Describe "Will error correctly if an attempt to set variable to improper value"
}
}
foreach ( $argumentListValue in "Standard","Legacy" ) {
foreach ( $argumentListValue in "Standard","Legacy","Windows" ) {
$PSNativeCommandArgumentPassing = $argumentListValue
Describe "Native Command Arguments (${PSNativeCommandArgumentPassing})" -tags "CI" {
# When passing arguments to native commands, quoted segments that contain
@@ -263,6 +263,28 @@ foreach ( $argumentListValue in "Standard","Legacy" ) {
$lines[$i] | Should -BeExactly "Arg $i is <$($expected[$i])>"
}
}
It "Should handle empty args correctly (ArgumentList=${PSNativeCommandArgumentPassing})" {
if ($PSNativeCommandArgumentPassing -eq 'Legacy') {
$expectedLines = 2
}
else {
$expectedLines = 3
}
$lines = testexe -echoargs 1 '' 2
$lines.Count | Should -Be $expectedLines
$lines[0] | Should -BeExactly 'Arg 0 is <1>'
if ($expectedLines -eq 2) {
$lines[1] | Should -BeExactly 'Arg 1 is <2>'
}
else {
$lines[1] | Should -BeExactly 'Arg 1 is <>'
$lines[2] | Should -BeExactly 'Arg 2 is <2>'
}
}
}
}
Describe 'PSPath to native commands' -tags "CI" {