Add LocationChangedAction handler and tests to support the Windows Compat module (#7552)

Add LocationChangedAction handler and tests to support the Windows Compat module.
This commit is contained in:
Bruce Payette
2018-08-22 12:08:03 -07:00
committed by Travis Plunk
parent 97d2e10c22
commit 4943bbfd9e
3 changed files with 93 additions and 0 deletions
@@ -144,4 +144,40 @@ Describe "Set-Location" -Tags "CI" {
{ Set-Location - } | Should -Throw -ErrorId 'System.InvalidOperationException,Microsoft.PowerShell.Commands.SetLocationCommand'
}
}
Context 'Test the LocationChangedAction event handler' {
AfterEach {
$ExecutionContext.InvokeCommand.LocationChangedAction = $null
}
It 'The LocationChangedAction should fire when changing location' {
$initialPath = $pwd
$oldPath = $null
$newPath = $null
$eventSessionState = $null
$eventRunspace = $null
$ExecutionContext.InvokeCommand.LocationChangedAction = {
(Get-Variable eventRunspace).Value = $this
(Get-Variable eventSessionState).Value = $_.SessionState
(Get-Variable oldPath).Value = $_.oldPath
(Get-Variable newPath).Value = $_.newPath
}
Set-Location ..
$newPath.Path | Should -Be $pwd.Path
$oldPath.Path | Should -Be $initialPath.Path
$eventSessionState | Should -Be $ExecutionContext.SessionState
$eventRunspace | Should -Be ([runspace]::DefaultRunspace)
}
It 'Errors in the LocationChangedAction should be catchable but not fail the cd' {
$location = $PWD
Set-Location ..
$ExecutionContext.InvokeCommand.LocationChangedAction = { throw "Boom" }
# Verify that the exception occurred
{ Set-Location $location } | Should Throw "Boom"
# But the location should still have changed
$PWD.Path | Should -Be $location.Path
}
}
}