Skip to content

Commit

Permalink
🐛 fix: fs - ToAbsPath if input is empty will return current work dir
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 24, 2024
1 parent 45cdda2 commit deb03c0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
16 changes: 13 additions & 3 deletions fsutil/fsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,21 @@ func UnixPath(path string) string {
return strings.ReplaceAll(path, "\\", "/")
}

// ToAbsPath convert process. will expand home dir
// ToAbsPath convert path to absolute path.
// Will expand home dir, if empty will return current work dir
//
// TIP: will don't check path
// TIP: will don't check path is really exists
func ToAbsPath(p string) string {
if len(p) == 0 || IsAbsPath(p) {
// return current work dir
if len(p) == 0 {
wd, err := os.Getwd()
if err != nil {
return p
}
return wd
}

if IsAbsPath(p) {
return p
}

Expand Down
2 changes: 1 addition & 1 deletion fsutil/fsutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ func TestSplitPath(t *testing.T) {
}

func TestToAbsPath(t *testing.T) {
assert.Eq(t, "", fsutil.ToAbsPath(""))
assert.Eq(t, "/path/to/dir/", fsutil.ToAbsPath("/path/to/dir/"))
assert.Neq(t, "~/path/to/dir", fsutil.ToAbsPath("~/path/to/dir"))
assert.NotEmpty(t, fsutil.ToAbsPath(""))
assert.Neq(t, ".", fsutil.ToAbsPath("."))
assert.Neq(t, "..", fsutil.ToAbsPath(".."))
assert.Neq(t, "./", fsutil.ToAbsPath("./"))
Expand Down

0 comments on commit deb03c0

Please sign in to comment.