From 65f31c06bb3a1ada8e705ca3106b141eb1c83bc2 Mon Sep 17 00:00:00 2001 From: Alex Jordan Date: Mon, 27 Jun 2016 15:41:40 -0700 Subject: [PATCH] Adding Invoke-Item tests --- test/powershell/Invoke-Item.Tests.ps1 | 92 +++++++++++++++++---------- 1 file changed, 60 insertions(+), 32 deletions(-) diff --git a/test/powershell/Invoke-Item.Tests.ps1 b/test/powershell/Invoke-Item.Tests.ps1 index 685af663eb..05e1013cd4 100644 --- a/test/powershell/Invoke-Item.Tests.ps1 +++ b/test/powershell/Invoke-Item.Tests.ps1 @@ -1,22 +1,7 @@ -function Clean-State -{ - if (Test-Path $FullyQualifiedLink) - { - Remove-Item $FullyQualifiedLink -Force - } +using namespace System.Diagnostics - if (Test-Path $FullyQualifiedFile) - { - Remove-Item $FullyQualifiedFile -Force - } +Describe "Invoke-Item" { - if (Test-Path $FullyQualifiedFolder) - { - Remove-Item $FullyQualifiedFolder -Force - } -} - -Describe "New-Item" { $tmpDirectory = $TestDrive $testfile = "testfile.txt" $testfolder = "newDirectory" @@ -24,23 +9,66 @@ Describe "New-Item" { $FullyQualifiedFile = Join-Path -Path $tmpDirectory -ChildPath $testfile $FullyQualifiedFolder = Join-Path -Path $tmpDirectory -ChildPath $testfolder $FullyQualifiedLink = Join-Path -Path $tmpDirectory -ChildPath $testlink - - BeforeEach { - Clean-State + + function NewProcessStartInfo([string]$CommandLine, [switch]$RedirectStdIn) + { + return [ProcessStartInfo]@{ + FileName = $powershell + Arguments = $CommandLine + RedirectStandardInput = $RedirectStdIn + RedirectStandardOutput = $true + RedirectStandardError = $true + UseShellExecute = $false + } } - It "Should call the function without error" { - { - New-Item -Name $testfile -Path $tmpDirectory -ItemType file } | Should Not Throw + function RunPowerShell([ProcessStartInfo]$debugfn) + { + $process = [Process]::Start($debugfn) + return $process + } + + function EnsureChildHasExited([Process]$process, [int]$WaitTimeInMS = 15000) + { + $process.WaitForExit($WaitTimeInMS) + + if (!$process.HasExited) + { + $process.HasExited | Should Be $true + $process.Kill() + } + } + + function Clean-State + { + if (Test-Path $FullyQualifiedLink) + { + Remove-Item $FullyQualifiedLink -Force + } + + if (Test-Path $FullyQualifiedFile) + { + Remove-Item $FullyQualifiedFile -Force + } + + if (Test-Path $FullyQualifiedFolder) + { + Remove-Item $FullyQualifiedFolder -Force + } + } + + BeforeAll { + $powershell = Join-Path -Path $PsHome -ChildPath "powershell" + } + + It "Should call the function without error" { + { New-Item -Name $testfile -Path $tmpDirectory -ItemType file } | Should Not Throw } It "Should invoke a text file without error" { - New-Item -Name $testfile -Path $tmpDirectory -ItemType file - Test-Path $FullyQualifiedFile | Should Be $true - $fileInfo = Get-ChildItem $FullyQualifiedFile - $fileInfo.Target | Should Be $null - $fileInfo.LinkType | Should Be $null - Invoke-Item $FullyQualifiedFile | Should Not throw - } - -} \ No newline at end of file + $debugfn = NewProcessStartInfo "-noprofile ""``Invoke-Item $FullyQualifiedFile`n" -RedirectStdIn + $process = RunPowerShell $debugfn + EnsureChildHasExited $process + $process.ExitCode | Should Be 0 + } +}