fix(path): correct mapped_locations logic

resolves #2839
This commit is contained in:
Jan De Dobbeleer 2022-09-27 11:31:35 +02:00 committed by Jan De Dobbeleer
parent 17181f3bef
commit 9653506e34
2 changed files with 21 additions and 71 deletions

View file

@ -347,6 +347,9 @@ func (pt *Path) normalize(inputPath string) string {
if goos == environment.WINDOWS || goos == environment.DARWIN {
normalized = strings.ToLower(normalized)
}
if !strings.HasSuffix(normalized, "/") {
normalized += "/"
}
return normalized
}
@ -355,13 +358,11 @@ 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 enableMappedLocations {
mappedLocations["hkcu:"] = pt.props.GetString(WindowsRegistryIcon, "\uF013")
mappedLocations["hklm:"] = pt.props.GetString(WindowsRegistryIcon, "\uF013")
if pt.props.GetBool(MappedLocationsEnabled, true) {
mappedLocations[pt.normalize("hkcu:")] = pt.props.GetString(WindowsRegistryIcon, "\uF013")
mappedLocations[pt.normalize("hklm:")] = pt.props.GetString(WindowsRegistryIcon, "\uF013")
mappedLocations[pt.normalize(pt.env.Home())] = pt.props.GetString(HomeIcon, "~")
}
// merge custom locations with mapped locations
@ -385,16 +386,12 @@ func (pt *Path) replaceMappedLocations(pwd string) string {
normalizedPwd := pt.normalize(pwd)
for _, key := range keys {
if strings.HasPrefix(normalizedPwd, key) {
value := mappedLocations[key]
return value + pwd[len(key):]
replacement := mappedLocations[key]
// -1 as we want to ignore the trailing slash
// set by the normalize function
return replacement + pwd[len(key)-1:]
}
}
// 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
}
@ -412,17 +409,6 @@ func (pt *Path) replaceFolderSeparators(pwd string) string {
return pwd
}
func (pt *Path) inHomeDir(pwd string) bool {
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 {
pwd := pt.getPwd()
pwd = strings.TrimPrefix(pwd, pt.env.PathSeparator())

View file

@ -40,47 +40,11 @@ func renderTemplate(env *mock.MockedEnvironment, segmentTemplate string, context
}
const (
homeBill = "/home/bill"
homeJan = "/usr/home/jan"
homeBillWindows = "C:\\Users\\Bill"
levelDir = "/level"
)
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",
},
}
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)
}
}
func TestRootLocationHome(t *testing.T) {
cases := []struct {
Expected string
@ -92,10 +56,10 @@ func TestRootLocationHome(t *testing.T) {
HomeIcon string
RegistryIcon string
}{
{Expected: "REG", RegistryIcon: "REG", HomePath: "C:\\Users\\Bill", Pwd: "HKCU:\\Program Files\\Go", GOOS: environment.WINDOWS, PathSeparator: "\\"},
{Expected: "~", HomeIcon: "~", HomePath: "/home/bill/", Pwd: "/home/bill/", PathSeparator: "/"},
{Expected: "usr", HomePath: "/home/bill/", Pwd: "/usr/error/what", PathSeparator: "/"},
{Expected: "C:", HomePath: "C:\\Users\\Bill", Pwd: "C:\\Program Files\\Go", GOOS: environment.WINDOWS, PathSeparator: "\\"},
{Expected: "REG", RegistryIcon: "REG", HomePath: "C:\\Users\\Bill", Pwd: "HKCU:\\Program Files\\Go", GOOS: environment.WINDOWS, PathSeparator: "\\"},
{Expected: "~", HomeIcon: "~", HomePath: "C:\\Users\\Bill", Pwd: "Microsoft.PowerShell.Core\\FileSystem::C:\\Users\\Bill", GOOS: environment.WINDOWS, PathSeparator: "\\"},
{Expected: "C:", HomePath: "C:\\Users\\Jack", Pwd: "Microsoft.PowerShell.Core\\FileSystem::C:\\Users\\Bill", GOOS: environment.WINDOWS, PathSeparator: "\\"},
{Expected: "", HomePath: "C:\\Users\\Jack", Pwd: "", GOOS: environment.WINDOWS, PathSeparator: "\\"},
@ -511,14 +475,14 @@ func TestNormalizePath(t *testing.T) {
GOOS string
Expected string
}{
{Input: "C:\\Users\\Bob\\Foo", GOOS: environment.LINUX, Expected: "C:/Users/Bob/Foo"},
{Input: "C:\\Users\\Bob\\Foo", GOOS: environment.WINDOWS, Expected: "c:/users/bob/foo"},
{Input: "~\\Bob\\Foo", GOOS: environment.LINUX, Expected: "/usr/home/Bob/Foo"},
{Input: "~\\Bob\\Foo", GOOS: environment.WINDOWS, Expected: "/usr/home/bob/foo"},
{Input: "/foo/~/bar", GOOS: environment.LINUX, Expected: "/foo/~/bar"},
{Input: "/foo/~/bar", GOOS: environment.WINDOWS, Expected: "/foo/~/bar"},
{Input: "~/baz", GOOS: environment.LINUX, Expected: "/usr/home/baz"},
{Input: "~/baz", GOOS: environment.WINDOWS, Expected: "/usr/home/baz"},
{Input: "C:\\Users\\Bob\\Foo", GOOS: environment.LINUX, Expected: "C:/Users/Bob/Foo/"},
{Input: "C:\\Users\\Bob\\Foo", GOOS: environment.WINDOWS, Expected: "c:/users/bob/foo/"},
{Input: "~\\Bob\\Foo", GOOS: environment.LINUX, Expected: "/usr/home/Bob/Foo/"},
{Input: "~\\Bob\\Foo", GOOS: environment.WINDOWS, Expected: "/usr/home/bob/foo/"},
{Input: "/foo/~/bar", GOOS: environment.LINUX, Expected: "/foo/~/bar/"},
{Input: "/foo/~/bar", GOOS: environment.WINDOWS, Expected: "/foo/~/bar/"},
{Input: "~/baz", GOOS: environment.LINUX, Expected: "/usr/home/baz/"},
{Input: "~/baz", GOOS: environment.WINDOWS, Expected: "/usr/home/baz/"},
}
for _, tc := range cases {
@ -863,10 +827,10 @@ func TestGetPwd(t *testing.T) {
Pswd string
Expected string
}{
{MappedLocationsEnabled: true, Pwd: "/usr/home", Expected: "~"},
{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: "~"},
{MappedLocationsEnabled: true, Pwd: "/usr/home/abc", Expected: "~/abc"},
{MappedLocationsEnabled: true, Pwd: "/a/b/c/d", Expected: "#"},
{MappedLocationsEnabled: true, Pwd: "/a/b/c/d/e/f/g", Expected: "#/e/f/g"},