8000 Add a generic file watcher function in HelpersCommon.psm1 (#4775) · PowerShell/PowerShell@c06ffc4 · GitHub
[go: up one dir, main page]

Skip to content

Commit c06ffc4

Browse files
authored
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`.
1 parent 6a78e30 commit c06ffc4

File tree

7 files changed

+26
-41
lines changed

7 files changed

+26
-41
lines changed

test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,19 +196,13 @@ Categories=Application;
196196
elseif ($IsLinux) {
197197
# Validate on Linux by reassociating default app for text file
198198
& $TestFile
199-
$startTime = Get-Date
200199
# It may take time for handler to start
201-
while (((Get-Date) - $startTime).TotalSeconds -lt 10 -and (-not (Test-Path "$HOME/nativeCommandProcessor.Success"))) {
202-
Start-Sleep -Milliseconds 100
203-
}
200+
Wait-FileToBePresent -File "$HOME/nativeCommandProcessor.Success" -TimeoutInSeconds 10 -IntervalInMilliseconds 100
204201
Get-Content $HOME/nativeCommandProcessor.Success | Should Be $TestFile
205202
}
206203
else {
207204
& $TestFile
208-
$startTime = Get-Date
209-
while (((Get-Date) - $startTime).TotalSeconds -lt 10 -and (!(Test-Path $TestDrive\foo.txt))) {
210-
Start-Sleep -Milliseconds 100
211-
}
205+
Wait-FileToBePresent -File $TestDrive\foo.txt -TimeoutInSeconds 10 -IntervalInMilliseconds 100
212206
"$TestDrive\foo.txt" | Should Exist
213207
Get-Content $TestDrive\foo.txt | Should BeExactly $TestFile
214208
}

test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,7 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" {
233233
"Test timeout waiting for $cmdLine" | Set-Content -Path $errFile
234234

235235
runas.exe /trustlevel:0x20000 "$powershell -nop -c try { $cmdline -ErrorAction Stop } catch { `$_.FullyQualifiedErrorId | Out-File $errFile }; New-Item -Type File -Path $doneFile"
236-
237-
$startTime = Get-Date
238-
while (((Get-Date) - $startTime).TotalSeconds -lt 15 -and -not (Test-Path $doneFile))
239-
{
240-
Start-Sleep -Milliseconds 100
241-
}
236+
Wait-FileToBePresent -File $doneFile -TimeoutInSeconds 15 -IntervalInMilliseconds 100
242237

243238
$err = Get-Content $errFile
244239
$err | Should Be $expectedError

test/powershell/Modules/Microsoft.PowerShell.Management/Start-Process.Tests.ps1

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,9 @@ Describe "Start-Process tests requiring admin" -Tags "Feature","RequireAdminOnWi
133133
$fooFile = Join-Path $TestDrive "FooTest.foo"
134134
New-Item $fooFile -ItemType File -Force
135135
Start-Process $fooFile
136-
$startTime = Get-Date
137-
while (((Get-Date) - $startTime).TotalSeconds -lt 10 -and (!(Test-Path $testdrive\foo.txt)))
138-
{
139-
Start-Sleep -Milliseconds 100
140-
}
136+
137+
Wait-FileToBePresent -File "$testdrive\foo.txt" -TimeoutInSeconds 10 -IntervalInMilliseconds 100
138+
141139
"$testdrive\foo.txt" | Should Exist
142140
Get-Content $testdrive\foo.txt | Should BeExactly $fooFile
143141
}

test/powershell/Modules/Microsoft.PowerShell.Utility/Invoke-Item.Tests.ps1

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,8 @@ Categories=Application;
125125

126126
$before = $windows.Count
127127
Invoke-Item -Path $PSHOME
128-
$startTime = Get-Date
129128
# may take time for explorer to open window
130-
while (((Get-Date) - $startTime).TotalSeconds -lt 10 -and ($windows.Count -eq $before))
131-
{
132-
Start-Sleep -Milliseconds 100
133-
}
129+
Wait-UntilTrue -sb { $windows.Count -gt $before } -TimeoutInMilliseconds (10*1000) -IntervalInMilliseconds 100 > $null
134130
$after = $windows.Count
135131

136132
$before + 1 | Should Be $after
@@ -143,12 +139,8 @@ Categories=Application;
143139
{
144140
# validate on Unix by reassociating default app for directories
145141
Invoke-Item -Path $PSHOME
146-
$startTime = Get-Date
147142
# may take time for handler to start
148-
while (((Get-Date) - $startTime).TotalSeconds -lt 10 -and (-not (Test-Path "$HOME/InvokeItemTest.Success")))
149-
{
150-
Start-Sleep -Milliseconds 100
151-
}
143+
Wait-FileToBePresent -File "$HOME/InvokeItemTest.Success" -TimeoutInSeconds 10 -IntervalInMilliseconds 100
152144
Get-Content $HOME/InvokeItemTest.Success | Should Be $PSHOME
153145
}
154146
else

test/powershell/Modules/Microsoft.WSMan.Management/CredSSP.Tests.ps1

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,7 @@ Describe "CredSSP cmdlet tests" -Tags 'Feature','RequireAdminOnWindows' {
100100
param ($cmdline, $cmd)
101101

102102
runas.exe /trustlevel:0x20000 "$powershell -nop -c try { $cmdline } catch { `$_.FullyQualifiedErrorId | Out-File $errtxt }; New-Item -Type File -Path $donefile"
103-
$startTime = Get-Date
104-
while (((Get-Date) - $startTime).TotalSeconds -lt 5 -and -not (Test-Path "$donefile"))
105-
{
106-
Start-Sleep -Milliseconds 100
107-
}
103+
Wait-FileToBePresent -File $donefile -TimeoutInSeconds 5 -IntervalInMilliseconds 100
108104
$errtxt | Should Exist
109105
$err = Get-Content $errtxt
110106
$err | Should Be "System.InvalidOperationException,Microsoft.WSMan.Management.$cmd"

test/tools/Modules/HelpersCommon/HelpersCommon.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ Copyright = 'Copyright (C) Microsoft Corporation, All rights reserved.'
1616

1717
Description = 'Temporary module contains functions for using in tests'
1818

19-
FunctionsToExport = 'Wait-UntilTrue', 'Test-IsElevated', 'ShouldBeErrorId'
19+
FunctionsToExport = 'Wait-UntilTrue', 'Test-IsElevated', 'ShouldBeErrorId', 'Wait-FileToBePresent'
2020

2121
}

test/tools/Modules/HelpersCommon/HelpersCommon.psm1

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,28 @@ function Wait-UntilTrue
1111

1212
# Loop until the script block evaluates to true
1313
while (-not ($sb.Invoke())) {
14-
# Sleep for the specified interval
15-
start-sleep -mil $intervalInMilliseconds
16-
17-
# If the timeout period has passed, throw an exception
18-
if (([DateTime]::Now - $startTime).TotalMilliseconds -gt $timeoutInMilliseconds)
19-
{
14+
# If the timeout period has passed, return false
15+
if (([DateTime]::Now - $startTime).TotalMilliseconds -gt $timeoutInMilliseconds) {
2016
return $false
2117
}
18+
# Sleep for the specified interval
19+
Start-Sleep -Milliseconds $intervalInMilliseconds
2220
}
2321
return $true
2422
}
2523

24+
function Wait-FileToBePresent
25+
{
26+
[CmdletBinding()]
27+
param (
28+
[string]$File,
29+
[int]$TimeoutInSeconds = 10,
30+
[int]$IntervalInMilliseconds = 100
31+
)
32+
33+
Wait-UntilTrue -sb { Test-Path $File } -TimeoutInMilliseconds ($TimeoutInSeconds*1000) -IntervalInMilliseconds $IntervalInMilliseconds > $null
34+
}
35+
2636
function Test-IsElevated
2737
{
2838
$IsElevated = $False

0 commit comments

Comments
 (0)
0