diff --git a/fsutil/fsutil.go b/fsutil/fsutil.go index 0eabd6fc4..a55e04485 100644 --- a/fsutil/fsutil.go +++ b/fsutil/fsutil.go @@ -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 @@ -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) } diff --git a/fsutil/fsutil_nonwin_test.go b/fsutil/fsutil_nonwin_test.go index 2a9ac2c83..eb757e992 100644 --- a/fsutil/fsutil_nonwin_test.go +++ b/fsutil/fsutil_nonwin_test.go @@ -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")) } diff --git a/fsutil/fsutil_test.go b/fsutil/fsutil_test.go index 539c4b476..7c85bed16 100644 --- a/fsutil/fsutil_test.go +++ b/fsutil/fsutil_test.go @@ -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) { diff --git a/fsutil/fsutil_windows_test.go b/fsutil/fsutil_windows_test.go index 665eb5ed5..19c12446e 100644 --- a/fsutil/fsutil_windows_test.go +++ b/fsutil/fsutil_windows_test.go @@ -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")) } diff --git a/internal/comfunc/fsfunc.go b/internal/comfunc/fsfunc.go new file mode 100644 index 000000000..df17c9520 --- /dev/null +++ b/internal/comfunc/fsfunc.go @@ -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...) +}