Skip to content

Commit

Permalink
fix: make sure all the PATH entries follow cygpath -u style
Browse files Browse the repository at this point in the history
fix #172
  • Loading branch information
aooohan committed Apr 3, 2024
1 parent ff66677 commit 7d1abf3
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 6 deletions.
11 changes: 8 additions & 3 deletions internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
package env

import (
"github.com/version-fox/vfox/internal/util"
"io"
"os"
"strings"

"github.com/version-fox/vfox/internal/util"
)

type Manager interface {
Expand Down Expand Up @@ -77,7 +78,11 @@ func NewPaths(from PathFrom) *Paths {
default:

}
return &Paths{
util.NewSortedSetWithSlice(paths),
p := &Paths{
util.NewSortedSet[string](),
}
for _, v := range paths {
p.Add(v)
}
return p
}
74 changes: 74 additions & 0 deletions internal/env/path_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package env

import (
"os"
"runtime"
"testing"
)

func TestPathFormat(t *testing.T) {
if runtime.GOOS == "windows" {
testdata := []struct {
path string
want string
}{
{
path: "C:\\Program Files\\Git\\bin",
want: "/c/Program Files/Git/bin",
},
{
path: "D:\\b\\c",
want: "/d/b/c",
},
}

paths := NewPaths(EmptyPaths)
for _, v := range testdata {
paths.Add(v.path)
}
result := paths.Slice()
for i, v := range testdata {
if result[i] != v.path {
t.Errorf("want: %s, got: %s", v.want, result[i])
}
}

os.Setenv(HookFlag, "bash")
paths = NewPaths(EmptyPaths)
for _, v := range testdata {
paths.Add(v.path)
}
result = paths.Slice()
for i, v := range testdata {
if result[i] != v.want {
t.Errorf("want: %s, got: %s", v.want, result[i])
}
}

} else {
testdata := []struct {
path string
want string
}{
{
path: "/bin/bash",
want: "/bin/bash",
},
{
path: "/usr/bin",
want: "/usr/bin",
},
}

paths := NewPaths(EmptyPaths)
for _, v := range testdata {
paths.Add(v.path)
}
result := paths.Slice()
for i, v := range testdata {
if result[i] != v.want {
t.Errorf("want: %s, got: %s", v.want, result[i])
}
}
}
}
14 changes: 11 additions & 3 deletions internal/env/windows_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,15 @@ func (p *Paths) String() string {
}
}

func (p *Paths) Add(str string) bool {
str = filepath.FromSlash(str)
return p.Set.Add(str)
func (p *Paths) Add(path string) bool {
if os.Getenv(HookFlag) == "bash" {
path = filepath.ToSlash(path)
// Convert drive letter (e.g., "C:") to "/c"
if len(path) > 1 && path[1] == ':' {
path = "/" + strings.ToLower(string(path[0])) + path[2:]
}
} else {
path = filepath.FromSlash(path)
}
return p.Set.Add(path)
}

0 comments on commit 7d1abf3

Please sign in to comment.