Add a generic file watcher function in HelpersCommon.psm1 (#4775)

Add a generic file watcher function in HelpersCommon.psm1 and update some tests to use the `Wait-FileToBePresent`.
This commit is contained in:
Dongbo Wang
2017-09-12 13:05:47 -07:00
committed by GitHub
parent 6a78e303ff
commit c06ffc4a5c
7 changed files with 26 additions and 41 deletions
@@ -196,19 +196,13 @@ Categories=Application;
elseif ($IsLinux) {
# Validate on Linux by reassociating default app for text file
& $TestFile
$startTime = Get-Date
# It may take time for handler to start
while (((Get-Date) - $startTime).TotalSeconds -lt 10 -and (-not (Test-Path "$HOME/nativeCommandProcessor.Success"))) {
Start-Sleep -Milliseconds 100
}
Wait-FileToBePresent -File "$HOME/nativeCommandProcessor.Success" -TimeoutInSeconds 10 -IntervalInMilliseconds 100
Get-Content $HOME/nativeCommandProcessor.Success | Should Be $TestFile
}
else {
& $TestFile
$startTime = Get-Date
while (((Get-Date) - $startTime).TotalSeconds -lt 10 -and (!(Test-Path $TestDrive\foo.txt))) {
Start-Sleep -Milliseconds 100
}
Wait-FileToBePresent -File $TestDrive\foo.txt -TimeoutInSeconds 10 -IntervalInMilliseconds 100
"$TestDrive\foo.txt" | Should Exist
Get-Content $TestDrive\foo.txt | Should BeExactly $TestFile
}
@@ -233,12 +233,7 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" {
"Test timeout waiting for $cmdLine" | Set-Content -Path $errFile
runas.exe /trustlevel:0x20000 "$powershell -nop -c try { $cmdline -ErrorAction Stop } catch { `$_.FullyQualifiedErrorId | Out-File $errFile }; New-Item -Type File -Path $doneFile"
$startTime = Get-Date
while (((Get-Date) - $startTime).TotalSeconds -lt 15 -and -not (Test-Path $doneFile))
{
Start-Sleep -Milliseconds 100
}
Wait-FileToBePresent -File $doneFile -TimeoutInSeconds 15 -IntervalInMilliseconds 100
$err = Get-Content $errFile
$err | Should Be $expectedError
@@ -133,11 +133,9 @@ Describe "Start-Process tests requiring admin" -Tags "Feature","RequireAdminOnWi
$fooFile = Join-Path $TestDrive "FooTest.foo"
New-Item $fooFile -ItemType File -Force
Start-Process $fooFile
$startTime = Get-Date
while (((Get-Date) - $startTime).TotalSeconds -lt 10 -and (!(Test-Path $testdrive\foo.txt)))
{
Start-Sleep -Milliseconds 100
}
Wait-FileToBePresent -File "$testdrive\foo.txt" -TimeoutInSeconds 10 -IntervalInMilliseconds 100
"$testdrive\foo.txt" | Should Exist
Get-Content $testdrive\foo.txt | Should BeExactly $fooFile
}
@@ -125,12 +125,8 @@ Categories=Application;
$before = $windows.Count
Invoke-Item -Path $PSHOME
$startTime = Get-Date
# may take time for explorer to open window
while (((Get-Date) - $startTime).TotalSeconds -lt 10 -and ($windows.Count -eq $before))
{
Start-Sleep -Milliseconds 100
}
Wait-UntilTrue -sb { $windows.Count -gt $before } -TimeoutInMilliseconds (10*1000) -IntervalInMilliseconds 100 > $null
$after = $windows.Count
$before + 1 | Should Be $after
@@ -143,12 +139,8 @@ Categories=Application;
{
# validate on Unix by reassociating default app for directories
Invoke-Item -Path $PSHOME
$startTime = Get-Date
# may take time for handler to start
while (((Get-Date) - $startTime).TotalSeconds -lt 10 -and (-not (Test-Path "$HOME/InvokeItemTest.Success")))
{
Start-Sleep -Milliseconds 100
}
Wait-FileToBePresent -File "$HOME/InvokeItemTest.Success" -TimeoutInSeconds 10 -IntervalInMilliseconds 100
Get-Content $HOME/InvokeItemTest.Success | Should Be $PSHOME
}
else
@@ -100,11 +100,7 @@ Describe "CredSSP cmdlet tests" -Tags 'Feature','RequireAdminOnWindows' {
param ($cmdline, $cmd)
runas.exe /trustlevel:0x20000 "$powershell -nop -c try { $cmdline } catch { `$_.FullyQualifiedErrorId | Out-File $errtxt }; New-Item -Type File -Path $donefile"
$startTime = Get-Date
while (((Get-Date) - $startTime).TotalSeconds -lt 5 -and -not (Test-Path "$donefile"))
{
Start-Sleep -Milliseconds 100
}
Wait-FileToBePresent -File $donefile -TimeoutInSeconds 5 -IntervalInMilliseconds 100
$errtxt | Should Exist
$err = Get-Content $errtxt
$err | Should Be "System.InvalidOperationException,Microsoft.WSMan.Management.$cmd"
@@ -16,6 +16,6 @@ Copyright = 'Copyright (C) Microsoft Corporation, All rights reserved.'
Description = 'Temporary module contains functions for using in tests'
FunctionsToExport = 'Wait-UntilTrue', 'Test-IsElevated', 'ShouldBeErrorId'
FunctionsToExport = 'Wait-UntilTrue', 'Test-IsElevated', 'ShouldBeErrorId', 'Wait-FileToBePresent'
}
@@ -11,18 +11,28 @@ function Wait-UntilTrue
# Loop until the script block evaluates to true
while (-not ($sb.Invoke())) {
# Sleep for the specified interval
start-sleep -mil $intervalInMilliseconds
# If the timeout period has passed, throw an exception
if (([DateTime]::Now - $startTime).TotalMilliseconds -gt $timeoutInMilliseconds)
{
# If the timeout period has passed, return false
if (([DateTime]::Now - $startTime).TotalMilliseconds -gt $timeoutInMilliseconds) {
return $false
}
# Sleep for the specified interval
Start-Sleep -Milliseconds $intervalInMilliseconds
}
return $true
}
function Wait-FileToBePresent
{
[CmdletBinding()]
param (
[string]$File,
[int]$TimeoutInSeconds = 10,
[int]$IntervalInMilliseconds = 100
)
Wait-UntilTrue -sb { Test-Path $File } -TimeoutInMilliseconds ($TimeoutInSeconds*1000) -IntervalInMilliseconds $IntervalInMilliseconds > $null
}
function Test-IsElevated
{
$IsElevated = $False