Skip to content

Commit eef7c45

Browse files
authored
Add the function cd~ (PowerShell#18308)
1 parent 7cae30d commit eef7c45

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/System.Management.Automation/engine/InitialSessionState.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4709,6 +4709,7 @@ internal static SessionStateAliasEntry[] BuiltInAliases
47094709
// Functions that don't require full language mode
47104710
SessionStateFunctionEntry.GetDelayParsedFunctionEntry("cd..", "Set-Location ..", isProductCode: true, languageMode: systemLanguageMode),
47114711
SessionStateFunctionEntry.GetDelayParsedFunctionEntry("cd\\", "Set-Location \\", isProductCode: true, languageMode: systemLanguageMode),
4712+
SessionStateFunctionEntry.GetDelayParsedFunctionEntry("cd~", "Set-Location ~", isProductCode: true, languageMode: systemLanguageMode),
47124713
// Win8: 320909. Retaining the original definition to ensure backward compatability.
47134714
SessionStateFunctionEntry.GetDelayParsedFunctionEntry("Pause",
47144715
string.Concat("$null = Read-Host '", CodeGeneration.EscapeSingleQuotedStringContent(RunspaceInit.PauseDefinitionString), "'"), isProductCode: true, languageMode: systemLanguageMode),

test/powershell/Modules/Microsoft.PowerShell.Management/Set-Location.Tests.ps1

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,36 @@ Describe "Set-Location" -Tags "CI" {
230230
$PWD.Path | Should -Be $location.Path
231231
}
232232
}
233+
234+
Context 'Parsing of Set-Location to cd' {
235+
It 'Should go to Filesystem home on cd~ run' {
236+
Set-Location 'TestDrive:\'
237+
cd~
238+
(Get-Location).Path | Should -BeExactly (Get-PSProvider FileSystem).Home
239+
}
240+
It 'Should go to the parent folder on cd.. run' {
241+
Set-Location 'TestDrive:\'
242+
$ParentDir = (Get-Location).path
243+
New-Item -Path 'TestDrive:\' -Name 'Directory1' -ItemType Directory
244+
Set-Location 'TestDrive:\Directory1'
245+
cd..
246+
(Get-Location).Path | Should -BeExactly $ParentDir
247+
}
248+
It 'Should go to root of current drive on cd\ run (Windows)' -Skip:(!$IsWindows){
249+
#root is / on linux and Mac, so it's not happy with this check.
250+
Set-Location 'TestDrive:\'
251+
$DriveRoot = (Get-Location).path
252+
New-Item -Path 'TestDrive:\Directory1' -Name 'Directory2' -ItemType Directory
253+
Set-Location 'TestDrive:\Directory1\Directory2'
254+
cd\
255+
(Get-Location).Path | Should -BeExactly $DriveRoot
256+
}
257+
It 'Should go to root of current drive on cd\ run (Linux/Mac)' -Skip:($IsWindows){
258+
Set-Location 'TestDrive:\'
259+
New-Item -Path 'TestDrive:\Directory1' -Name 'Directory2' -ItemType Directory
260+
Set-Location 'TestDrive:\Directory1\Directory2'
261+
cd\
262+
(Get-Location).Path | Should -BeExactly "/"
263+
}
264+
}
233265
}

0 commit comments

Comments
 (0)