Visitar URL original
Preserve verbatim stop-parsing argument passing by huong8373tt-beep · Pull Request #28162 · PowerShell/PowerShell · GitHub
Skip to content
Open
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 @@ -67,8 +67,17 @@ internal override object GetDefaultParameterValue(string name)
return null;
}

private bool HasVerbatimArgumentMarker { get; set; }

internal void BindParameters(Collection<CommandParameterInternal> parameters)
{
// The stop-parsing marker requires legacy string construction on Windows.
// Detect it before binding so arguments that precede the marker use the
// same construction rules as arguments that follow it.
HasVerbatimArgumentMarker = Platform.IsWindows && parameters.Any(
parameter => parameter.ArgumentSpecified
&& string.Equals("--%", parameter.ArgumentValue as string, StringComparison.OrdinalIgnoreCase));

bool sawVerbatimArgumentMarker = false;
bool first = true;
foreach (CommandParameterInternal parameter in parameters)
Expand Down Expand Up @@ -193,6 +202,11 @@ internal NativeArgumentPassingStyle ArgumentPassingStyle
{
get
{
if (HasVerbatimArgumentMarker)
{
return NativeArgumentPassingStyle.Legacy;
}

try
{
var preference = LanguagePrimitives.ConvertTo<NativeArgumentPassingStyle>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,44 @@ Describe "find.exe uses legacy behavior on Windows" -Tag 'CI' {
}
}

Describe "Stop-parsing uses legacy native argument passing" -Tag 'CI' {
BeforeAll {
$currentSetting = $PSNativeCommandArgumentPassing
}

AfterAll {
$PSNativeCommandArgumentPassing = $currentSetting
}

It "Preserves escaped quotes after '--%' with <argumentListValue> argument passing" -Skip:(-not $IsWindows) -TestCases @(
@{ argumentListValue = 'Standard' }
@{ argumentListValue = 'Windows' }
) {
param($argumentListValue)

$PSNativeCommandArgumentPassing = $argumentListValue
$lines = @(Invoke-Expression 'testexe -echoargs --% /p:Category=\"CI,Nightly\"')

$lines | Should -BeExactly 'Arg 0 is </p:Category="CI,Nightly">'
}

It "Uses legacy construction for arguments before '--%' with <argumentListValue> argument passing" -Skip:(-not $IsWindows) -TestCases @(
@{ argumentListValue = 'Standard' }
@{ argumentListValue = 'Windows' }
) {
param($argumentListValue)

$PSNativeCommandArgumentPassing = $argumentListValue
$lines = @(Invoke-Expression 'testexe -echoargs 1, 2 --% tail')

$lines | Should -BeExactly @(
'Arg 0 is <1,>'
'Arg 1 is <2>'
'Arg 2 is <tail>'
)
}
}

foreach ( $argumentListValue in "Standard","Legacy","Windows" ) {
$PSNativeCommandArgumentPassing = $argumentListValue
Describe "Native Command Arguments (${PSNativeCommandArgumentPassing})" -tags "CI" {
Expand Down