fspath: factor Split into SplitFs and Split #4996

This commit is contained in:
Nick Craig-Wood 2021-02-10 14:26:30 +00:00
parent 8a46dd1b57
commit 71554c1371
2 changed files with 63 additions and 5 deletions

View File

@ -259,6 +259,27 @@ loop:
return parsed, nil return parsed, nil
} }
// SplitFs splits a remote a remoteName and an remotePath.
//
// SplitFs("remote:path/to/file") -> ("remote:", "path/to/file")
// SplitFs("/to/file") -> ("", "/to/file")
//
// If it returns remoteName as "" then remotePath is a local path
//
// The returned values have the property that remoteName + remotePath ==
// remote (except under Windows where \ will be translated into /)
func SplitFs(remote string) (remoteName string, remotePath string, err error) {
parsed, err := Parse(remote)
if err != nil {
return "", "", err
}
remoteName, remotePath = parsed.ConfigString, parsed.Path
if remoteName != "" {
remoteName += ":"
}
return remoteName, remotePath, nil
}
// Split splits a remote into a parent and a leaf // Split splits a remote into a parent and a leaf
// //
// if it returns leaf as an empty string then remote is a directory // if it returns leaf as an empty string then remote is a directory
@ -268,14 +289,10 @@ loop:
// The returned values have the property that parent + leaf == remote // The returned values have the property that parent + leaf == remote
// (except under Windows where \ will be translated into /) // (except under Windows where \ will be translated into /)
func Split(remote string) (parent string, leaf string, err error) { func Split(remote string) (parent string, leaf string, err error) {
parsed, err := Parse(remote) remoteName, remotePath, err := SplitFs(remote)
if err != nil { if err != nil {
return "", "", err return "", "", err
} }
remoteName, remotePath := parsed.ConfigString, parsed.Path
if remoteName != "" {
remoteName += ":"
}
// Construct new remote name without last segment // Construct new remote name without last segment
parent, leaf = path.Split(remotePath) parent, leaf = path.Split(remotePath)
return remoteName + parent, leaf, nil return remoteName + parent, leaf, nil

View File

@ -324,6 +324,47 @@ func TestParse(t *testing.T) {
} }
} }
func TestSplitFs(t *testing.T) {
for _, test := range []struct {
remote, wantRemoteName, wantRemotePath string
wantErr error
}{
{"", "", "", errCantBeEmpty},
{"remote:", "remote:", "", nil},
{"remote:potato", "remote:", "potato", nil},
{"remote:/", "remote:", "/", nil},
{"remote:/potato", "remote:", "/potato", nil},
{"remote:/potato/potato", "remote:", "/potato/potato", nil},
{"remote:potato/sausage", "remote:", "potato/sausage", nil},
{"rem.ote:potato/sausage", "", "", errInvalidCharacters},
{":remote:", ":remote:", "", nil},
{":remote:potato", ":remote:", "potato", nil},
{":remote:/", ":remote:", "/", nil},
{":remote:/potato", ":remote:", "/potato", nil},
{":remote:/potato/potato", ":remote:", "/potato/potato", nil},
{":remote:potato/sausage", ":remote:", "potato/sausage", nil},
{":rem[ote:potato/sausage", "", "", errInvalidCharacters},
{"/", "", "/", nil},
{"/root", "", "/root", nil},
{"/a/b", "", "/a/b", nil},
{"root", "", "root", nil},
{"a/b", "", "a/b", nil},
{"root/", "", "root/", nil},
{"a/b/", "", "a/b/", nil},
} {
gotRemoteName, gotRemotePath, gotErr := SplitFs(test.remote)
assert.Equal(t, test.wantErr, gotErr)
assert.Equal(t, test.wantRemoteName, gotRemoteName, test.remote)
assert.Equal(t, test.wantRemotePath, gotRemotePath, test.remote)
if gotErr == nil {
assert.Equal(t, test.remote, gotRemoteName+gotRemotePath, fmt.Sprintf("%s: %q + %q != %q", test.remote, gotRemoteName, gotRemotePath, test.remote))
}
}
}
func TestSplit(t *testing.T) { func TestSplit(t *testing.T) {
for _, test := range []struct { for _, test := range []struct {
remote, wantParent, wantLeaf string remote, wantParent, wantLeaf string