fix(path): map Parent correctly

This commit is contained in:
Jan De Dobbeleer 2022-08-03 08:39:49 +02:00 committed by Jan De Dobbeleer
parent 18e23f93a8
commit 47265b1bc1
3 changed files with 49 additions and 2 deletions

View file

@ -109,7 +109,18 @@ func (pt *Path) Enabled() bool {
}
func (pt *Path) Parent() string {
return filepath.Dir(pt.pwd)
if pt.pwd == pt.env.Home() {
return ""
}
parent := filepath.Dir(pt.pwd)
if pt.pwd == parent {
return ""
}
separator := pt.env.PathSeparator()
if parent == pt.rootLocation() || parent == separator {
separator = ""
}
return pt.replaceMappedLocations(parent) + separator
}
func (pt *Path) formatWindowsDrive(pwd string) string {

View file

@ -5,6 +5,7 @@ import (
"oh-my-posh/mock"
"oh-my-posh/properties"
"oh-my-posh/template"
"runtime"
"strings"
"testing"
@ -112,6 +113,41 @@ func TestRootLocationHome(t *testing.T) {
}
}
func TestParent(t *testing.T) {
// there's no Windows support/validation for this just yet
// mainly due to root being a special case
if runtime.GOOS == environment.WINDOWS {
return
}
cases := []struct {
Case string
Expected string
HomePath string
Pwd string
PathSeparator string
}{
{Case: "Home folder", Expected: "", HomePath: "/home/bill", Pwd: "/home/bill", PathSeparator: "/"},
{Case: "Inside home folder", Expected: "~/", HomePath: "/home/bill", Pwd: "/home/bill/test", PathSeparator: "/"},
{Case: "Root", Expected: "", HomePath: "/home/bill", Pwd: "/", PathSeparator: "/"},
{Case: "Root + 1", Expected: "/", HomePath: "/home/bill", Pwd: "/usr", PathSeparator: "/"},
}
for _, tc := range cases {
env := new(mock.MockedEnvironment)
env.On("Home").Return(tc.HomePath)
env.On("Pwd").Return(tc.Pwd)
env.On("Flags").Return(&environment.Flags{})
env.On("PathSeparator").Return(tc.PathSeparator)
env.On("GOOS").Return(environment.DARWIN)
path := &Path{
env: env,
props: properties.Map{},
}
path.pwd = tc.Pwd
got := path.Parent()
assert.EqualValues(t, tc.Expected, got, tc.Case)
}
}
func TestIsInHomeDirFalse(t *testing.T) {
home := homeBill
env := new(mock.MockedEnvironment)

View file

@ -147,7 +147,7 @@ folders at the same level, so if `C:\projectA\dev` and `C:\projectB\dev` exist,
### Properties
- `.Path`: `string` - the current directory (based on the `style` property)
- `.Parent`: `string` - the current directory's parent folder
- `.Parent`: `string` - the current directory's parent folder (designed for use with style `folder`)
- `.Location`: `string` - the current directory (raw value)
- `.StackCount`: `int` - the stack count
- `.Writable`: `boolean` - is the current directory writable by the user or not