Skip to content

Commit

Permalink
update test to handle both new and old behaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesWTruher committed Feb 2, 2021
1 parent 49ac942 commit 7f6581d
Showing 1 changed file with 72 additions and 59 deletions.
@@ -1,71 +1,84 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
$PSNativeApplicationUsesArgumentList = 1
Describe "Native Command Arguments" -tags "CI" {
# When passing arguments to native commands, quoted segments that contain
# spaces need to be quoted with '"' characters when they are passed to the
# native command (or to bash or sh on Linux).
#
# This test checks that the proper quoting is occuring by passing arguments
# to the testexe native command and looking at how it got the arguments.
It "Should handle quoted spaces correctly" {
$a = 'a"b c"d'
$lines = testexe -echoargs $a 'a"b c"d' a"b c"d
$lines.Count | Should -Be 3
$lines[0] | Should -BeExactly 'Arg 0 is <a"b c"d>'
$lines[1] | Should -BeExactly 'Arg 1 is <a"b c"d>'
$lines[2] | Should -BeExactly 'Arg 2 is <ab cd>'
}
foreach ( $argumentListValue in 0,1 ) {
$PSNativeApplicationUsesArgumentList = $argumentListValue
Describe "Native Command Arguments" -tags "CI" {
# When passing arguments to native commands, quoted segments that contain
# spaces need to be quoted with '"' characters when they are passed to the
# native command (or to bash or sh on Linux).
#
# This test checks that the proper quoting is occuring by passing arguments
# to the testexe native command and looking at how it got the arguments.
It "Should handle quoted spaces correctly (ArgumentList=${PSNativeApplicationUsesArgumentList})" {
$a = 'a"b c"d'
$lines = testexe -echoargs $a 'a"b c"d' a"b c"d
$lines.Count | Should -Be 3
if ( $PSNativeApplicationUsesArgumentList -eq 1 ) {
$lines[0] | Should -BeExactly 'Arg 0 is <a"b c"d>'
$lines[1] | Should -BeExactly 'Arg 1 is <a"b c"d>'
}
else {
$lines[0] | Should -BeExactly 'Arg 0 is <ab cd>'
$lines[1] | Should -BeExactly 'Arg 1 is <ab cd>'
}
$lines[2] | Should -BeExactly 'Arg 2 is <ab cd>'
}

# In order to pass '"' characters so they are actually part of command line
# arguments for native commands, they need to be escaped with a '\' (this
# is in addition to the '`' escaping needed inside '"' quoted strings in
# PowerShell).
#
# This functionality was broken in PowerShell 5.0 and 5.1, so this test
# will fail on those versions unless the fix is backported to them.
#
# This test checks that the proper quoting and escaping is occurring by
# passing arguments with escaped quotes to the testexe native command and
# 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.Count | Should -Be 2
$lines[0] | Should -BeExactly 'Arg 0 is <a\"b c\"d>'
$lines[1] | Should -BeExactly 'Arg 1 is <a\"b c\"d>'
}
# In order to pass '"' characters so they are actually part of command line
# arguments for native commands, they need to be escaped with a '\' (this
# is in addition to the '`' escaping needed inside '"' quoted strings in
# PowerShell).
#
# This functionality was broken in PowerShell 5.0 and 5.1, so this test
# will fail on those versions unless the fix is backported to them.
#
# This test checks that the proper quoting and escaping is occurring by
# passing arguments with escaped quotes to the testexe native command and
# looking at how it got the arguments.
It "Should handle spaces between escaped quotes (ArgumentList=${PSNativeApplicationUsesArgumentList})" {
$lines = testexe -echoargs 'a\"b c\"d' "a\`"b c\`"d"
$lines.Count | Should -Be 2
if ( $PSNativeApplicationUsesArgumentList -eq 1 ) {
$lines[0] | Should -BeExactly 'Arg 0 is <a\"b c\"d>'
$lines[1] | Should -BeExactly 'Arg 1 is <a\"b c\"d>'
}
else {
$lines[0] | Should -BeExactly 'Arg 0 is <a"b c"d>'
$lines[1] | Should -BeExactly '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 -BeExactly "Arg $i is <$($expected[$i])>"
It "Should correctly quote paths with spaces (ArgumentList=${PSNativeApplicationUsesArgumentList}): <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 -BeExactly "Arg $i is <$($expected[$i])>"
}
}
}

It "Should handle PowerShell arrays with or without spaces correctly: <arguments>" -TestCases @(
@{arguments = "1,2"; expected = @("1,2")}
@{arguments = "1,2,3"; expected = @("1,2,3")}
@{arguments = "1, 2"; expected = "1,", "2"}
@{arguments = "1 ,2"; expected = "1", ",2"}
@{arguments = "1 , 2"; expected = "1", ",", "2"}
@{arguments = "1, 2,3"; expected = "1,", "2,3"}
@{arguments = "1 ,2,3"; expected = "1", ",2,3"}
@{arguments = "1 , 2,3"; expected = "1", ",", "2,3"}
) {
param($arguments, $expected)
$lines = @(Invoke-Expression "testexe -echoargs $arguments")
$lines.Count | Should -Be $expected.Count
for ($i = 0; $i -lt $expected.Count; $i++) {
$lines[$i] | Should -BeExactly "Arg $i is <$($expected[$i])>"
It "Should handle PowerShell arrays with or without spaces correctly (ArgumentList=${PSNativeApplicationUsesArgumentList}): <arguments>" -TestCases @(
@{arguments = "1,2"; expected = @("1,2")}
@{arguments = "1,2,3"; expected = @("1,2,3")}
@{arguments = "1, 2"; expected = "1,", "2"}
@{arguments = "1 ,2"; expected = "1", ",2"}
@{arguments = "1 , 2"; expected = "1", ",", "2"}
@{arguments = "1, 2,3"; expected = "1,", "2,3"}
@{arguments = "1 ,2,3"; expected = "1", ",2,3"}
@{arguments = "1 , 2,3"; expected = "1", ",", "2,3"}
) {
param($arguments, $expected)
$lines = @(Invoke-Expression "testexe -echoargs $arguments")
$lines.Count | Should -Be $expected.Count
for ($i = 0; $i -lt $expected.Count; $i++) {
$lines[$i] | Should -BeExactly "Arg $i is <$($expected[$i])>"
}
}
}
}

Describe 'PSPath to native commands' {
BeforeAll {
$featureEnabled = $EnabledExperimentalFeatures.Contains('PSNativePSPathResolution')
Expand Down

0 comments on commit 7f6581d

Please sign in to comment.