Skip to content

Commit

Permalink
👔 up: fsutil - add new func: JoinPaths3, update some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 3, 2024
1 parent ebc89cd commit 1ef5549
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
14 changes: 8 additions & 6 deletions fsutil/fsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ func JoinPaths(elem ...string) string {
return filepath.Join(elem...)
}

// JoinPaths3 elements, like the filepath.Join()
func JoinPaths3(basePath, secPath string, elems ...string) string {
return comfunc.JoinPaths3(basePath, secPath, elems)
}

// 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...)
func JoinSubPaths(basePath string, elems ...string) string {
return comfunc.JoinPaths2(basePath, elems)
}

// SlashPath alias of filepath.ToSlash
Expand Down Expand Up @@ -69,5 +71,5 @@ func ToAbsPath(p string) string {
return filepath.Join(wd, p)
}

// Must2 ok for (any, error) result. if has error, will panic
// Must2 ok for (any, error) result. if it has error, will panic
func Must2(_ any, err error) { basefn.MustOK(err) }
3 changes: 2 additions & 1 deletion fsutil/fsutil_nonwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
"github.com/gookit/goutil/testutil/assert"
)

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

Expand Down
2 changes: 2 additions & 0 deletions fsutil/fsutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func TestSplitPath(t *testing.T) {
dir, file := fsutil.SplitPath("/path/to/dir/some.txt")
assert.Eq(t, "/path/to/dir/", dir)
assert.Eq(t, "some.txt", file)

assert.NotEmpty(t, fsutil.PathSep)
}

func TestToAbsPath(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion fsutil/fsutil_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
"github.com/gookit/goutil/testutil/assert"
)

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

Expand Down
20 changes: 20 additions & 0 deletions internal/comfunc/fsfunc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package comfunc

import "path/filepath"

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

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

0 comments on commit 1ef5549

Please sign in to comment.