Skip to content

Commit

Permalink
👔 up(fs): add new fs util JoinSubPaths for join paths
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 22, 2023
1 parent bc33394 commit a56a93e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
8 changes: 8 additions & 0 deletions fsutil/fsutil.go
Expand Up @@ -87,6 +87,14 @@ func JoinPaths(elem ...string) string {
return filepath.Join(elem...)
}

// JoinSubPaths elements, like the filepath.Join()
func JoinSubPaths(basePath string, elem ...string) string {
paths := make([]string, len(elem)+1)
paths[0] = basePath
copy(paths[1:], elem)
return filepath.Join(paths...)
}

// SlashPath alias of filepath.ToSlash
func SlashPath(path string) string {
return filepath.ToSlash(path)
Expand Down
1 change: 1 addition & 0 deletions fsutil/fsutil_nonwin_test.go
Expand Up @@ -11,6 +11,7 @@ import (

func TestSlashPath_nw(t *testing.T) {
assert.Eq(t, "path/to/dir", fsutil.JoinPaths("path", "to", "dir"))
assert.Eq(t, "path/to/dir", fsutil.JoinSubPaths("path", "to", "dir"))
}

func TestRealpath_nw(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions fsutil/fsutil_windows_test.go
Expand Up @@ -11,6 +11,7 @@ import (

func TestSlashPath_win(t *testing.T) {
assert.Eq(t, "path\\to\\dir", fsutil.JoinPaths("path", "to", "dir"))
assert.Eq(t, "path\\to\\dir", fsutil.JoinSubPaths("path", "to", "dir"))
}

func TestRealpath_win(t *testing.T) {
Expand Down

0 comments on commit a56a93e

Please sign in to comment.