Fix Start-Process Pester tests

These tests attempted to stop the test ping process by name, which was
fragile. Now they stop by PID, which is far less fragile.
This commit is contained in:
Andrew Schwartzmeyer 2015-12-21 19:14:48 -08:00
parent bc9baddfc1
commit 1de894813c

View file

@ -18,14 +18,6 @@ Describe "Start-Process" {
$pingParamStop = "localhost -c 2"
}
AfterEach {
Stop-Process -Name ping -ErrorAction SilentlyContinue
}
It "Should start a process without error" {
{ Start-Process ping } | Should Not Throw
}
It "Should process arguments without error" {
{ Start-Process ping -ArgumentList $pingParamNoStop} | Should Not Throw
@ -34,6 +26,8 @@ Describe "Start-Process" {
$process.Length | Should Be 1
$process.Id | Should BeGreaterThan 1
$process.ProcessName | Should Be "ping"
Stop-Process -Id $process.Id
}
It "Should create process object when used with PassThru argument" {
@ -42,6 +36,8 @@ Describe "Start-Process" {
$process.Length | Should Be 1
$process.Id | Should BeGreaterThan 1
$process.ProcessName | Should Be "ping"
Stop-Process -Id $process.Id
}
It "Should work correctly when used with full path name" {
@ -50,22 +46,22 @@ Describe "Start-Process" {
$process.Length | Should Be 1
$process.Id | Should BeGreaterThan 1
$process.ProcessName | Should Be "ping"
Stop-Process -Id $process.Id
}
It "Should invoke correct path when used with FilePath argument" {
$process = Start-Process -FilePath $pingCommand -ArgumentList $pingParamNoStop -PassThru
$process.Length | Should Be 1
$process.Id | Should BeGreaterThan 1
$process.ProcessName | Should Be "ping"
Stop-Process -Id $process.Id
}
It "Should wait for command completion if used with Wait argument" {
Start-Process ping -ArgumentList $pingParamStop -Wait
$process = Get-Process -Name ping -ErrorAction SilentlyContinue
$process.Length | Should Be 0
$process = Start-Process ping -ArgumentList $pingParamStop -Wait -PassThru
( Get-Process -Id $process.Id -ErrorAction SilentlyContinue ) | Should BeNullOrEmpty
}
It "Should work correctly with WorkingDirectory argument" {
@ -74,30 +70,30 @@ Describe "Start-Process" {
$process.Length | Should Be 1
$process.Id | Should BeGreaterThan 1
$process.ProcessName | Should Be "ping"
Stop-Process -Id $process.Id
}
It "Should should handle stderr redirection without error" {
$process = Start-Process ping -ArgumentList $pingParamNoStop -PassThru -RedirectStandardError $tempFile
$process = Start-Process ping -ArgumentList $pingParamNoStop -PassThru -RedirectStandardError $tempFile
$process.Length | Should Be 1
$process.Id | Should BeGreaterThan 1
$process.ProcessName | Should Be "ping"
Stop-Process -Id $process.Id
}
It "Should should handle stdout redirection without error" {
$process = Start-Process ping -ArgumentList $pingParamStop -Wait -RedirectStandardOutput $tempFile
$process = Start-Process ping -ArgumentList $pingParamStop -Wait -RedirectStandardOutput $tempFile
$dirEntry = dir $tempFile
$dirEntry.Length | Should BeGreaterThan 0
$dirEntry.Length | Should BeGreaterThan 0
}
It "Should should handle stdin redirection without error" {
$process = Start-Process sort -Wait -RedirectStandardOutput $tempFile -RedirectStandardInput $assetsFile
$process = Start-Process sort -Wait -RedirectStandardOutput $tempFile -RedirectStandardInput $assetsFile
$dirEntry = dir $tempFile
$dirEntry.Length | Should BeGreaterThan 0
$dirEntry.Length | Should BeGreaterThan 0
}
}