mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
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:
committed by
Travis Plunk
parent
97d2e10c22
commit
4943bbfd9e
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user