Skip to content

Commit

Permalink
Remove drives on invocation and handle io error
Browse files Browse the repository at this point in the history
  • Loading branch information
nohwnd committed Feb 1, 2020
1 parent d1cdfc5 commit 9560000
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
2 changes: 2 additions & 0 deletions Functions/Describe.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ https://pester.dev/docs/usage/testdrive
if ($null -eq (& $SafeCommands['Get-Variable'] -Name Pester -ValueOnly -ErrorAction $script:IgnoreErrorPreference)) {
# User has executed a test script directly instead of calling Invoke-Pester
Remove-MockFunctionsAndAliases
Remove-TestRegistry
Remove-TestDrive
$sessionState = Set-SessionStateHint -PassThru -Hint "Caller - Captured in Describe" -SessionState $PSCmdlet.SessionState
$Pester = New-PesterState -Path (& $SafeCommands['Resolve-Path'] .) -TestNameFilter $null -TagFilter @() -SessionState $sessionState
$script:mockTable = @{ }
Expand Down
20 changes: 11 additions & 9 deletions Functions/TestDrive.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ function Clear-TestDrive ([String[]]$Exclude) {

#Get-ChildItem -Exclude did not seem to work with full paths
& $SafeCommands['Get-ChildItem'] -Recurse -Path $Path |
& $SafeCommands['Sort-Object'] -Descending -Property "FullName" |
& $SafeCommands['Where-Object'] { $Exclude -NotContains $_.FullName } |
& $SafeCommands['Remove-Item'] -Force -Recurse
& $SafeCommands['Sort-Object'] -Descending -Property "FullName" |
& $SafeCommands['Where-Object'] { $Exclude -NotContains $_.FullName } |
& $SafeCommands['Remove-Item'] -Force -Recurse

}
}
Expand Down Expand Up @@ -118,8 +118,8 @@ function Remove-TestDriveSymbolicLinks ([String] $Path) {
# powershell 2-compatible
$reparsePoint = [System.IO.FileAttributes]::ReparsePoint
& $SafeCommands["Get-ChildItem"] -Recurse -Path $Path |
where-object { ($_.Attributes -band $reparsePoint) -eq $reparsePoint } |
foreach-object { $_.Delete() }
where-object { ($_.Attributes -band $reparsePoint) -eq $reparsePoint } |
foreach-object { $_.Delete() }
}

function Remove-TestDrive {
Expand All @@ -139,10 +139,12 @@ function Remove-TestDrive {
$Drive | & $SafeCommands['Remove-PSDrive'] -Force #This should fail explicitly as it impacts future pester runs
}

Remove-TestDriveSymbolicLinks -Path $Path
if ($null -ne $Path) {
Remove-TestDriveSymbolicLinks -Path $Path

if (& $SafeCommands['Test-Path'] -Path $Path) {
& $SafeCommands['Remove-Item'] -Path $Path -Force -Recurse
if (& $SafeCommands['Test-Path'] -Path $Path) {
& $SafeCommands['Remove-Item'] -Path $Path -Force -Recurse
}
}

if (& $SafeCommands['Get-Variable'] -Name $DriveName -Scope Global -ErrorAction $script:IgnoreErrorPreference) {
Expand All @@ -166,7 +168,7 @@ function Setup {
Assert-DescribeInProgress -CommandName Setup

$TestDriveName = & $SafeCommands['Get-PSDrive'] TestDrive |
& $SafeCommands['Select-Object'] -ExpandProperty Root
& $SafeCommands['Select-Object'] -ExpandProperty Root

if ($Dir) {
$item = & $SafeCommands['New-Item'] -Name $Path -Path "${TestDriveName}\" -Type Container -Force
Expand Down
20 changes: 17 additions & 3 deletions Functions/TestRegistry.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function New-TestRegistry {
function New-TestRegistry {
param(
[Switch]
$PassThru,
Expand Down Expand Up @@ -66,6 +66,13 @@ function Clear-TestRegistry {
$Exclude
)

# if the setup fails before we mark test registry added
# we would be trying to teardown something that does not
# exist and fail in Get-TestRegistryPath
if (-not (& $SafeCommands['Test-Path'] "TestRegistry")) {
return
}

$path = Get-TestRegistryPath

if ($null -ne $path -and (& $SafeCommands['Test-Path'] -Path $Path)) {
Expand All @@ -79,7 +86,6 @@ function Clear-TestRegistry {

function Get-TestRegistryChildItem {
$path = Get-TestRegistryPath

& $SafeCommands['Get-ChildItem'] -Recurse -Path $path
}

Expand All @@ -89,7 +95,15 @@ function New-RandomTempRegistry {
$Path = & $SafeCommands['Join-Path'] -Path $tempPath -ChildPath ([Guid]::NewGuid())
} until (-not (& $SafeCommands['Test-Path'] -Path $Path ))

& $SafeCommands['New-Item'] -Path $Path
try {
& $SafeCommands['New-Item'] -Path $Path
}
catch [System.IO.IOException] {
# when running in parallel this occasionally triggers
# IOException: No more data is available
# let's just retry the operation
& $SafeCommands['New-Item'] -Path $Path
}
}

function Remove-TestRegistry {
Expand Down

0 comments on commit 9560000

Please sign in to comment.