From 92031bdf5180cc2b657dda5b4351d5aeebb0908d Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Mon, 22 Apr 2024 08:54:18 +0200 Subject: [PATCH] fix(path): construct currentpath correctly on Windows relates to #4727 --- src/segments/path.go | 15 ++++++++++++--- src/segments/path_test.go | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/segments/path.go b/src/segments/path.go index c3d0a674..122c0ee6 100644 --- a/src/segments/path.go +++ b/src/segments/path.go @@ -710,11 +710,20 @@ func (pt *Path) splitPath() Folders { folderFormatMap := pt.makeFolderFormatMap() - currentPath := pt.root - if currentPath == "~" { - currentPath = pt.env.Home() + pt.env.PathSeparator() + getCurrentPath := func() string { + if pt.root == "~" { + return pt.env.Home() + pt.env.PathSeparator() + } + + if pt.env.GOOS() == platform.WINDOWS { + return pt.root + pt.env.PathSeparator() + } + + return pt.root } + currentPath := getCurrentPath() + var display bool for _, folder := range folders { diff --git a/src/segments/path_test.go b/src/segments/path_test.go index b4854293..414d61a9 100644 --- a/src/segments/path_test.go +++ b/src/segments/path_test.go @@ -1506,6 +1506,7 @@ func TestReplaceMappedLocations(t *testing.T) { func TestSplitPath(t *testing.T) { cases := []struct { Case string + GOOS string Relative string Root string GitDir *platform.FileInfo @@ -1517,6 +1518,7 @@ func TestSplitPath(t *testing.T) { Case: "Regular directory", Root: "/", Relative: "c/d", + GOOS: platform.DARWIN, Expected: Folders{ {Name: "c", Path: "/c"}, {Name: "d", Path: "/c/d"}, @@ -1526,6 +1528,7 @@ func TestSplitPath(t *testing.T) { Case: "Home directory - git folder", Root: "~", Relative: "c/d", + GOOS: platform.DARWIN, GitDir: &platform.FileInfo{IsDir: true, ParentFolder: "/a/b/c"}, GitDirFormat: "%s", Expected: Folders{ @@ -1533,6 +1536,20 @@ func TestSplitPath(t *testing.T) { {Name: "d", Path: "/a/b/c/d"}, }, }, + { + Case: "Home directory - git folder on Windows", + Root: "C:", + Relative: "a/b/c/d", + GOOS: platform.WINDOWS, + GitDir: &platform.FileInfo{IsDir: true, ParentFolder: "C:/a/b/c"}, + GitDirFormat: "%s", + Expected: Folders{ + {Name: "a", Path: "C:/a"}, + {Name: "b", Path: "C:/a/b"}, + {Name: "c", Path: "C:/a/b/c", Display: true}, + {Name: "d", Path: "C:/a/b/c/d"}, + }, + }, } for _, tc := range cases { @@ -1540,6 +1557,7 @@ func TestSplitPath(t *testing.T) { env.On("PathSeparator").Return("/") env.On("Home").Return("/a/b") env.On("HasParentFilePath", ".git").Return(tc.GitDir, nil) + env.On("GOOS").Return(tc.GOOS) path := &Path{ env: env, props: properties.Map{