From 0dc1fbb544a025aaf93d160308294b1f9d8b1045 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Thu, 7 Sep 2017 08:51:05 -0700 Subject: [PATCH 1/4] [Feature] Add a generic file watcher function in HelpersCommon.psm1 --- .../NativeCommandProcessor.Tests.ps1 | 10 ++----- .../FileSystem.Tests.ps1 | 6 +--- .../Start-Process.Tests.ps1 | 8 ++--- .../Invoke-Item.Tests.ps1 | 12 ++------ .../CredSSP.Tests.ps1 | 6 +--- .../Modules/HelpersCommon/HelpersCommon.psm1 | 29 ++++++++++++++++--- 6 files changed, 34 insertions(+), 37 deletions(-) diff --git a/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 b/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 index f09c6727430..a2e12416e13 100644 --- a/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 +++ b/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 @@ -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 > $null 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 > $null "$TestDrive\foo.txt" | Should Exist Get-Content $TestDrive\foo.txt | Should BeExactly $TestFile } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index 210e6ea30bf..c85ad8d16bd 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -229,11 +229,7 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" { param ($cmdline, $expectedError) 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 10 -and -not (Test-Path $doneFile)) - { - Start-Sleep -Milliseconds 100 - } + Wait-FileToBePresent -File $doneFile -TimeoutInSeconds 10 -IntervalInMilliseconds 100 > $null $errFile | Should Exist $err = Get-Content $errFile diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Start-Process.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Start-Process.Tests.ps1 index 90783dbe5b6..ff0fff15e82 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Start-Process.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Start-Process.Tests.ps1 @@ -123,11 +123,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 > $null + "$testdrive\foo.txt" | Should Exist Get-Content $testdrive\foo.txt | Should BeExactly $fooFile } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Invoke-Item.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Invoke-Item.Tests.ps1 index d5d1e936ce6..67377c3d4d2 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Invoke-Item.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Invoke-Item.Tests.ps1 @@ -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 > $null Get-Content $HOME/InvokeItemTest.Success | Should Be $PSHOME } else diff --git a/test/powershell/Modules/Microsoft.WSMan.Management/CredSSP.Tests.ps1 b/test/powershell/Modules/Microsoft.WSMan.Management/CredSSP.Tests.ps1 index 09afa49b281..b4f6ac4e3d0 100644 --- a/test/powershell/Modules/Microsoft.WSMan.Management/CredSSP.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.WSMan.Management/CredSSP.Tests.ps1 @@ -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 > $null $errtxt | Should Exist $err = Get-Content $errtxt $err | Should Be "System.InvalidOperationException,Microsoft.WSMan.Management.$cmd" diff --git a/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 b/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 index e3376c1231f..9d8c4e1b57b 100644 --- a/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 +++ b/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 @@ -11,14 +11,35 @@ function Wait-UntilTrue # Loop until the script block evaluates to true while (-not ($sb.Invoke())) { + # If the timeout period has passed, return false + if (([DateTime]::Now - $startTime).TotalMilliseconds -gt $timeoutInMilliseconds) { + return $false + } # Sleep for the specified interval - start-sleep -mil $intervalInMilliseconds + Start-Sleep -Milliseconds $intervalInMilliseconds + } + return $true +} - # If the timeout period has passed, throw an exception - if (([DateTime]::Now - $startTime).TotalMilliseconds -gt $timeoutInMilliseconds) - { +function Wait-FileToBePresent +{ + [CmdletBinding()] + param ( + [string]$File, + [int]$TimeoutInSeconds = 10, + [int]$IntervalInMilliseconds = 100 + ) + # Get the current time + $startTime = [DateTime]::Now + + # Loop until the file is present + while (-not (Test-Path $File)) { + # If the timeout period has passed, return false + if (([DateTime]::Now - $startTime).TotalSeconds -gt $TimeoutInSeconds) { return $false } + # Sleep for the specified interval + Start-Sleep -Milliseconds $intervalInMilliseconds } return $true } From cde1fbb6dcd15cf667cc0ae66a29b9584ec3a15e Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Thu, 7 Sep 2017 09:19:36 -0700 Subject: [PATCH 2/4] [Feature] Expose 'Wait-FileToBePresent' from HelpersCommon module --- test/tools/Modules/HelpersCommon/HelpersCommon.psd1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/tools/Modules/HelpersCommon/HelpersCommon.psd1 b/test/tools/Modules/HelpersCommon/HelpersCommon.psd1 index 5367155d098..fbc067114fc 100644 --- a/test/tools/Modules/HelpersCommon/HelpersCommon.psd1 +++ b/test/tools/Modules/HelpersCommon/HelpersCommon.psd1 @@ -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' } From 7a9e3c6414b02a064ad1184ced3c30f600d2d813 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Thu, 7 Sep 2017 12:41:19 -0700 Subject: [PATCH 3/4] [Feature] Fix a test failure --- .../Modules/Microsoft.PowerShell.Utility/Invoke-Item.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Invoke-Item.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Invoke-Item.Tests.ps1 index 67377c3d4d2..642581dce2f 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Invoke-Item.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Invoke-Item.Tests.ps1 @@ -126,7 +126,7 @@ Categories=Application; $before = $windows.Count Invoke-Item -Path $PSHOME # may take time for explorer to open window - Wait-UntilTrue -sb { $windows.Count -gt $before } -TimeoutInMilliseconds 10*1000 -IntervalInMilliseconds 100 > $null + Wait-UntilTrue -sb { $windows.Count -gt $before } -TimeoutInMilliseconds (10*1000) -IntervalInMilliseconds 100 > $null $after = $windows.Count $before + 1 | Should Be $after From d9a41029f40832bebc0e4f8374709ad27b4c0d89 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 11 Sep 2017 10:51:19 -0700 Subject: [PATCH 4/4] [Feature] Address comments --- .../NativeCommandProcessor.Tests.ps1 | 4 ++-- .../FileSystem.Tests.ps1 | 2 +- .../Start-Process.Tests.ps1 | 2 +- .../Invoke-Item.Tests.ps1 | 2 +- .../Microsoft.WSMan.Management/CredSSP.Tests.ps1 | 2 +- test/tools/Modules/HelpersCommon/HelpersCommon.psm1 | 13 +------------ 6 files changed, 7 insertions(+), 18 deletions(-) diff --git a/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 b/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 index a2e12416e13..3acc721f592 100644 --- a/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 +++ b/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 @@ -197,12 +197,12 @@ Categories=Application; # Validate on Linux by reassociating default app for text file & $TestFile # It may take time for handler to start - Wait-FileToBePresent -File "$HOME/nativeCommandProcessor.Success" -TimeoutInSeconds 10 -IntervalInMilliseconds 100 > $null + Wait-FileToBePresent -File "$HOME/nativeCommandProcessor.Success" -TimeoutInSeconds 10 -IntervalInMilliseconds 100 Get-Content $HOME/nativeCommandProcessor.Success | Should Be $TestFile } else { & $TestFile - Wait-FileToBePresent -File $TestDrive\foo.txt -TimeoutInSeconds 10 -IntervalInMilliseconds 100 > $null + Wait-FileToBePresent -File $TestDrive\foo.txt -TimeoutInSeconds 10 -IntervalInMilliseconds 100 "$TestDrive\foo.txt" | Should Exist Get-Content $TestDrive\foo.txt | Should BeExactly $TestFile } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index c85ad8d16bd..db2bf61abd5 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -229,7 +229,7 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" { param ($cmdline, $expectedError) runas.exe /trustlevel:0x20000 "$powershell -nop -c try { $cmdline -ErrorAction Stop } catch { `$_.FullyQualifiedErrorId | Out-File $errFile }; New-Item -Type File -Path $doneFile" - Wait-FileToBePresent -File $doneFile -TimeoutInSeconds 10 -IntervalInMilliseconds 100 > $null + Wait-FileToBePresent -File $doneFile -TimeoutInSeconds 10 -IntervalInMilliseconds 100 $errFile | Should Exist $err = Get-Content $errFile diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Start-Process.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Start-Process.Tests.ps1 index ff0fff15e82..5e1d943c884 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Start-Process.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Start-Process.Tests.ps1 @@ -124,7 +124,7 @@ Describe "Start-Process tests requiring admin" -Tags "Feature","RequireAdminOnWi New-Item $fooFile -ItemType File -Force Start-Process $fooFile - Wait-FileToBePresent -File "$testdrive\foo.txt" -TimeoutInSeconds 10 -IntervalInMilliseconds 100 > $null + Wait-FileToBePresent -File "$testdrive\foo.txt" -TimeoutInSeconds 10 -IntervalInMilliseconds 100 "$testdrive\foo.txt" | Should Exist Get-Content $testdrive\foo.txt | Should BeExactly $fooFile diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Invoke-Item.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Invoke-Item.Tests.ps1 index 642581dce2f..28d719d25d9 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Invoke-Item.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Invoke-Item.Tests.ps1 @@ -140,7 +140,7 @@ Categories=Application; # validate on Unix by reassociating default app for directories Invoke-Item -Path $PSHOME # may take time for handler to start - Wait-FileToBePresent -File "$HOME/InvokeItemTest.Success" -TimeoutInSeconds 10 -IntervalInMilliseconds 100 > $null + Wait-FileToBePresent -File "$HOME/InvokeItemTest.Success" -TimeoutInSeconds 10 -IntervalInMilliseconds 100 Get-Content $HOME/InvokeItemTest.Success | Should Be $PSHOME } else diff --git a/test/powershell/Modules/Microsoft.WSMan.Management/CredSSP.Tests.ps1 b/test/powershell/Modules/Microsoft.WSMan.Management/CredSSP.Tests.ps1 index b4f6ac4e3d0..91f39511fc0 100644 --- a/test/powershell/Modules/Microsoft.WSMan.Management/CredSSP.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.WSMan.Management/CredSSP.Tests.ps1 @@ -100,7 +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" - Wait-FileToBePresent -File $donefile -TimeoutInSeconds 5 -IntervalInMilliseconds 100 > $null + Wait-FileToBePresent -File $donefile -TimeoutInSeconds 5 -IntervalInMilliseconds 100 $errtxt | Should Exist $err = Get-Content $errtxt $err | Should Be "System.InvalidOperationException,Microsoft.WSMan.Management.$cmd" diff --git a/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 b/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 index 9d8c4e1b57b..74e441b002c 100644 --- a/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 +++ b/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 @@ -29,19 +29,8 @@ function Wait-FileToBePresent [int]$TimeoutInSeconds = 10, [int]$IntervalInMilliseconds = 100 ) - # Get the current time - $startTime = [DateTime]::Now - # Loop until the file is present - while (-not (Test-Path $File)) { - # If the timeout period has passed, return false - if (([DateTime]::Now - $startTime).TotalSeconds -gt $TimeoutInSeconds) { - return $false - } - # Sleep for the specified interval - Start-Sleep -Milliseconds $intervalInMilliseconds - } - return $true + Wait-UntilTrue -sb { Test-Path $File } -TimeoutInMilliseconds ($TimeoutInSeconds*1000) -IntervalInMilliseconds $IntervalInMilliseconds > $null } function Test-IsElevated