fix(path): only replace actual home folder

resolves #2832
This commit is contained in:
Jan De Dobbeleer 2022-09-26 09:45:57 +02:00 committed by Jan De Dobbeleer
parent 56ae9e6fda
commit 7c7ce069e0
2 changed files with 50 additions and 36 deletions

View file

@ -355,11 +355,13 @@ func (pt *Path) replaceMappedLocations(pwd string) string {
pwd = strings.Replace(pwd, "Microsoft.PowerShell.Core\\FileSystem::", "", 1)
}
enableMappedLocations := pt.props.GetBool(MappedLocationsEnabled, true)
// built-in mapped locations, can be disabled
mappedLocations := map[string]string{}
if pt.props.GetBool(MappedLocationsEnabled, true) {
if enableMappedLocations {
mappedLocations["hkcu:"] = pt.props.GetString(WindowsRegistryIcon, "\uF013")
mappedLocations["hklm:"] = pt.props.GetString(WindowsRegistryIcon, "\uF013")
mappedLocations[pt.normalize(pt.env.Home())] = pt.props.GetString(HomeIcon, "~")
}
// merge custom locations with mapped locations
@ -387,6 +389,12 @@ func (pt *Path) replaceMappedLocations(pwd string) string {
return value + pwd[len(key):]
}
}
// treat this as a built-in mapped location
if enableMappedLocations && pt.inHomeDir(pwd) {
return pt.props.GetString(HomeIcon, "~") + pwd[len(pt.env.Home()):]
}
return pwd
}
@ -405,7 +413,14 @@ func (pt *Path) replaceFolderSeparators(pwd string) string {
}
func (pt *Path) inHomeDir(pwd string) bool {
return strings.HasPrefix(pwd, pt.env.Home())
home := pt.env.Home()
if home == pwd {
return true
}
if !strings.HasSuffix(home, pt.env.PathSeparator()) {
home += pt.env.PathSeparator()
}
return strings.HasPrefix(pwd, home)
}
func (pt *Path) rootLocation() string {

View file

@ -46,30 +46,39 @@ const (
levelDir = "/level"
)
func TestIsInHomeDirTrue(t *testing.T) {
home := homeBill
env := new(mock.MockedEnvironment)
env.On("Home").Return(home)
path := &Path{
env: env,
func TestIsInHomeDir(t *testing.T) {
cases := []struct {
Case string
Expected bool
Dir string
}{
{
Case: "in home dir",
Expected: true,
Dir: homeBill,
},
{
Case: "in home dir subdirectory",
Expected: true,
Dir: homeBill + "/go/src/github.com/JanDeDobbeleer/oh-my-posh",
},
{
Case: "in similar home dir but not really",
Expected: false,
Dir: "/home/bill-test",
},
}
got := path.inHomeDir(home)
assert.True(t, got)
}
func TestIsInHomeDirLevelTrue(t *testing.T) {
home := homeBill
pwd := home
for i := 0; i < 99; i++ {
pwd += levelDir
for _, tc := range cases {
home := homeBill
env := new(mock.MockedEnvironment)
env.On("Home").Return(home)
env.On("PathSeparator").Return("/")
path := &Path{
env: env,
}
got := path.inHomeDir(tc.Dir)
assert.Equal(t, tc.Expected, got, tc.Case)
}
env := new(mock.MockedEnvironment)
env.On("Home").Return(home)
path := &Path{
env: env,
}
got := path.inHomeDir(pwd)
assert.True(t, got)
}
func TestRootLocationHome(t *testing.T) {
@ -149,17 +158,6 @@ func TestParent(t *testing.T) {
}
}
func TestIsInHomeDirFalse(t *testing.T) {
home := homeBill
env := new(mock.MockedEnvironment)
env.On("Home").Return(home)
path := &Path{
env: env,
}
got := path.inHomeDir("/usr/error")
assert.False(t, got)
}
func TestPathDepthMultipleLevelsDeep(t *testing.T) {
pwd := "/usr"
for i := 0; i < 99; i++ {
@ -865,6 +863,7 @@ func TestGetPwd(t *testing.T) {
Pswd string
Expected string
}{
{MappedLocationsEnabled: true, Pwd: "/usr/home-test", Expected: "/usr/home-test"},
{MappedLocationsEnabled: true, Pwd: "", Expected: ""},
{MappedLocationsEnabled: true, Pwd: "/usr", Expected: "/usr"},
{MappedLocationsEnabled: true, Pwd: "/usr/home", Expected: "~"},