fix(path): construct currentpath correctly on Windows

relates to #4727
This commit is contained in:
Jan De Dobbeleer 2024-04-22 08:54:18 +02:00 committed by Jan De Dobbeleer
parent d10d568c15
commit 92031bdf51
2 changed files with 30 additions and 3 deletions

View file

@ -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 {

View file

@ -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: "<b>%s</b>",
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: "<b>%s</b>",
Expected: Folders{
{Name: "a", Path: "C:/a"},
{Name: "b", Path: "C:/a/b"},
{Name: "<b>c</b>", 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{