mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Consolidated the two test exes into one (#3982)
Combined the previous echoargs and createchildprocess test exes into a single TestExe that can be extended for other tests.
This commit is contained in:
+34
-41
@@ -1,41 +1,34 @@
|
||||
Describe "Native Command Arguments" -tags "CI" {
|
||||
# Find where test/powershell is so we can find the echoargs command relative to it
|
||||
$powershellTestDir = $PSScriptRoot
|
||||
while ($powershellTestDir -notmatch 'test[\\/]powershell$') {
|
||||
$powershellTestDir = Split-Path $powershellTestDir
|
||||
}
|
||||
$echoArgs = Join-Path (Split-Path $powershellTestDir) tools/EchoArgs/bin/echoargs
|
||||
|
||||
# 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 echoargs native command and looking at how it got the arguments.
|
||||
It "Should handle quoted spaces correctly" {
|
||||
$a = 'a"b c"d'
|
||||
$lines = & $echoArgs $a 'a"b c"d' a"b c"d
|
||||
($lines | measure).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>'
|
||||
}
|
||||
|
||||
# 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 echoargs native command and
|
||||
# looking at how it got the arguments.
|
||||
It "Should handle spaces between escaped quotes" {
|
||||
$lines = & $echoArgs 'a\"b c\"d' "a\`"b c\`"d"
|
||||
($lines | measure).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>'
|
||||
}
|
||||
}
|
||||
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 | measure).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>'
|
||||
}
|
||||
|
||||
# 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 | measure).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>'
|
||||
}
|
||||
}
|
||||
|
||||
+6
-16
@@ -27,35 +27,25 @@ Describe 'native commands lifecycle' -tags 'Feature' {
|
||||
|
||||
Describe "Native Command Processor" -tags "Feature" {
|
||||
|
||||
BeforeAll {
|
||||
# Find where test/powershell is so we can find the createchildprocess command relative to it
|
||||
$powershellTestDir = $PSScriptRoot
|
||||
while ($powershellTestDir -notmatch 'test[\\/]powershell$') {
|
||||
$powershellTestDir = Split-Path $powershellTestDir
|
||||
}
|
||||
$createchildprocess = Join-Path (Split-Path $powershellTestDir) tools/CreateChildProcess/bin/createchildprocess
|
||||
}
|
||||
|
||||
# If powershell receives a StopProcessing, it should kill the native process and all child processes
|
||||
|
||||
# this test should pass and no longer Pending when #2561 is fixed
|
||||
It "Should kill native process tree" -Pending {
|
||||
|
||||
# make sure no test processes are running
|
||||
# on Linux, the Process class truncates the name so filter using Where-Object
|
||||
Get-Process | Where-Object {$_.Name -like 'createchildproc*'} | Stop-Process
|
||||
Get-Process testexe -ErrorAction SilentlyContinue | Stop-Process
|
||||
|
||||
[int] $numToCreate = 2
|
||||
|
||||
$ps = [PowerShell]::Create().AddCommand($createchildprocess)
|
||||
$ps.AddParameter($numToCreate)
|
||||
$ps = [PowerShell]::Create().AddCommand("testexe")
|
||||
$ps.AddArgument("-createchildprocess")
|
||||
$ps.AddArgument($numToCreate)
|
||||
$async = $ps.BeginInvoke()
|
||||
$ps.InvocationStateInfo.State | Should Be "Running"
|
||||
|
||||
[bool] $childrenCreated = $false
|
||||
while (-not $childrenCreated)
|
||||
{
|
||||
$childprocesses = Get-Process | Where-Object {$_.Name -like 'createchildproc*'}
|
||||
$childprocesses = Get-Process testexe -ErrorAction SilentlyContinue
|
||||
if ($childprocesses.count -eq $numToCreate+1)
|
||||
{
|
||||
$childrenCreated = $true
|
||||
@@ -72,7 +62,7 @@ Describe "Native Command Processor" -tags "Feature" {
|
||||
break
|
||||
}
|
||||
}
|
||||
$childprocesses = Get-Process | Where-Object {$_.Name -like 'createchildproc*'}
|
||||
$childprocesses = Get-Process testexe
|
||||
$count = $childprocesses.count
|
||||
$childprocesses | Stop-Process
|
||||
$count | Should Be 0
|
||||
|
||||
@@ -1,70 +1,63 @@
|
||||
Describe "Native streams behavior with PowerShell" -Tags 'CI' {
|
||||
$powershell = Join-Path -Path $PsHome -ChildPath "powershell"
|
||||
|
||||
Context "Error stream" {
|
||||
# we are using powershell itself as an example of a native program.
|
||||
# we can create a behavior we want on the fly and test complex scenarios.
|
||||
|
||||
$error.Clear()
|
||||
|
||||
$command = [string]::Join('', @(
|
||||
'[Console]::Error.Write(\"foo`n`nbar`n`nbaz\"); ',
|
||||
'[Console]::Error.Write(\"middle\"); ',
|
||||
'[Console]::Error.Write(\"foo`n`nbar`n`nbaz\")'
|
||||
))
|
||||
|
||||
$out = & $powershell -noprofile -command $command 2>&1
|
||||
|
||||
# this check should be the first one, because $error is a global shared variable
|
||||
It 'should not add records to $error variable' {
|
||||
# we are keeping existing Windows PS v5.1 behavior for $error variable
|
||||
$error.Count | Should Be 9
|
||||
}
|
||||
|
||||
It 'uses ErrorRecord object to return stderr output' {
|
||||
($out | measure).Count | Should BeGreaterThan 1
|
||||
|
||||
$out[0] | Should BeOfType 'System.Management.Automation.ErrorRecord'
|
||||
$out[0].FullyQualifiedErrorId | Should Be 'NativeCommandError'
|
||||
|
||||
$out | Select-Object -Skip 1 | % {
|
||||
$_ | Should BeOfType 'System.Management.Automation.ErrorRecord'
|
||||
$_.FullyQualifiedErrorId | Should Be 'NativeCommandErrorMessage'
|
||||
}
|
||||
}
|
||||
|
||||
It 'uses correct exception messages for error stream' {
|
||||
($out | measure).Count | Should Be 9
|
||||
$out[0].Exception.Message | Should Be 'foo'
|
||||
$out[1].Exception.Message | Should Be ''
|
||||
$out[2].Exception.Message | Should Be 'bar'
|
||||
$out[3].Exception.Message | Should Be ''
|
||||
$out[4].Exception.Message | Should Be 'bazmiddlefoo'
|
||||
$out[5].Exception.Message | Should Be ''
|
||||
$out[6].Exception.Message | Should Be 'bar'
|
||||
$out[7].Exception.Message | Should Be ''
|
||||
$out[8].Exception.Message | Should Be 'baz'
|
||||
}
|
||||
|
||||
It 'preserves error stream as is with Out-String' {
|
||||
($out | Out-String).Replace("`r", '') | Should Be "foo`n`nbar`n`nbazmiddlefoo`n`nbar`n`nbaz`n"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Describe 'piping powershell objects to finished native executable' -Tags 'CI' {
|
||||
# Find where test/powershell is so we can find the echoargs command relative to it
|
||||
$powershellTestDir = $PSScriptRoot
|
||||
while ($powershellTestDir -notmatch 'test[\\/]powershell$') {
|
||||
$powershellTestDir = Split-Path $powershellTestDir
|
||||
}
|
||||
$echoArgs = Join-Path (Split-Path $powershellTestDir) tools/EchoArgs/bin/echoargs
|
||||
|
||||
It 'doesn''t throw any exceptions, when we are piping to the closed executable' {
|
||||
1..3 | % {
|
||||
Start-Sleep -Milliseconds 100
|
||||
# yeild some multi-line formatted object
|
||||
@{'a' = 'b'}
|
||||
} | & $echoArgs | Should Be $null
|
||||
}
|
||||
}
|
||||
Describe "Native streams behavior with PowerShell" -Tags 'CI' {
|
||||
$powershell = Join-Path -Path $PsHome -ChildPath "powershell"
|
||||
|
||||
Context "Error stream" {
|
||||
# we are using powershell itself as an example of a native program.
|
||||
# we can create a behavior we want on the fly and test complex scenarios.
|
||||
|
||||
$error.Clear()
|
||||
|
||||
$command = [string]::Join('', @(
|
||||
'[Console]::Error.Write(\"foo`n`nbar`n`nbaz\"); ',
|
||||
'[Console]::Error.Write(\"middle\"); ',
|
||||
'[Console]::Error.Write(\"foo`n`nbar`n`nbaz\")'
|
||||
))
|
||||
|
||||
$out = & $powershell -noprofile -command $command 2>&1
|
||||
|
||||
# this check should be the first one, because $error is a global shared variable
|
||||
It 'should not add records to $error variable' {
|
||||
# we are keeping existing Windows PS v5.1 behavior for $error variable
|
||||
$error.Count | Should Be 9
|
||||
}
|
||||
|
||||
It 'uses ErrorRecord object to return stderr output' {
|
||||
($out | measure).Count | Should BeGreaterThan 1
|
||||
|
||||
$out[0] | Should BeOfType 'System.Management.Automation.ErrorRecord'
|
||||
$out[0].FullyQualifiedErrorId | Should Be 'NativeCommandError'
|
||||
|
||||
$out | Select-Object -Skip 1 | % {
|
||||
$_ | Should BeOfType 'System.Management.Automation.ErrorRecord'
|
||||
$_.FullyQualifiedErrorId | Should Be 'NativeCommandErrorMessage'
|
||||
}
|
||||
}
|
||||
|
||||
It 'uses correct exception messages for error stream' {
|
||||
($out | measure).Count | Should Be 9
|
||||
$out[0].Exception.Message | Should Be 'foo'
|
||||
$out[1].Exception.Message | Should Be ''
|
||||
$out[2].Exception.Message | Should Be 'bar'
|
||||
$out[3].Exception.Message | Should Be ''
|
||||
$out[4].Exception.Message | Should Be 'bazmiddlefoo'
|
||||
$out[5].Exception.Message | Should Be ''
|
||||
$out[6].Exception.Message | Should Be 'bar'
|
||||
$out[7].Exception.Message | Should Be ''
|
||||
$out[8].Exception.Message | Should Be 'baz'
|
||||
}
|
||||
|
||||
It 'preserves error stream as is with Out-String' {
|
||||
($out | Out-String).Replace("`r", '') | Should Be "foo`n`nbar`n`nbazmiddlefoo`n`nbar`n`nbaz`n"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Describe 'piping powershell objects to finished native executable' -Tags 'CI' {
|
||||
It 'doesn''t throw any exceptions, when we are piping to the closed executable' {
|
||||
1..3 | % {
|
||||
Start-Sleep -Milliseconds 100
|
||||
# yeild some multi-line formatted object
|
||||
@{'a' = 'b'}
|
||||
} | testexe -echoargs | Should Be $null
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user