fix(path): add last element when only 1 level deep

resolves #4162
This commit is contained in:
Jan De Dobbeleer 2023-08-17 06:38:22 +00:00
parent 6dbfcc7c1e
commit 75e098b20b
2 changed files with 34 additions and 3 deletions

View file

@ -317,6 +317,7 @@ func (pt *Path) getLetterPath() string {
func (pt *Path) getUniqueLettersPath(maxWidth int) string {
separator := pt.getFolderSeparator()
splitted := strings.Split(pt.relative, pt.env.PathSeparator())
if pt.root == pt.env.PathSeparator() {
pt.root = splitted[0]
splitted = splitted[1:]
@ -360,7 +361,7 @@ func (pt *Path) getUniqueLettersPath(maxWidth int) string {
}
}
if len(elements) > 0 {
if len(splitted) > 0 {
elements = append(elements, splitted[n-1])
}

View file

@ -908,11 +908,11 @@ func TestFullPathCustomMappedLocations(t *testing.T) {
env := new(mock.MockedEnvironment)
env.On("Home").Return(homeDir)
env.On("Pwd").Return(tc.Pwd)
if tc.GOOS == "" {
if len(tc.GOOS) == 0 {
tc.GOOS = platform.DARWIN
}
env.On("GOOS").Return(tc.GOOS)
if tc.PathSeparator == "" {
if len(tc.PathSeparator) == 0 {
tc.PathSeparator = "/"
}
env.On("PathSeparator").Return(tc.PathSeparator)
@ -941,6 +941,36 @@ func TestFullPathCustomMappedLocations(t *testing.T) {
}
}
func TestPowerlevelMappedLocations(t *testing.T) {
cases := []struct {
Pwd string
MappedLocations map[string]string
Expected string
}{
{Pwd: "/Users/michal/Applications", MappedLocations: map[string]string{"~": "#"}, Expected: "#/Applications"},
}
for _, tc := range cases {
env := new(mock.MockedEnvironment)
env.On("Home").Return("/Users/michal")
env.On("Pwd").Return(tc.Pwd)
env.On("GOOS").Return(platform.DARWIN)
env.On("PathSeparator").Return("/")
env.On("Shell").Return(shell.GENERIC)
path := &Path{
env: env,
props: properties.Map{
properties.Style: Powerlevel,
MappedLocations: tc.MappedLocations,
},
}
path.setPaths()
path.setStyle()
got := renderTemplateNoTrimSpace(env, "{{ .Path }}", path)
assert.Equal(t, tc.Expected, got)
}
}
func TestFolderPathCustomMappedLocations(t *testing.T) {
pwd := "/a/b/c/d"
env := new(mock.MockedEnvironment)