github.com/ncw/rclone@v1.48.1-0.20190724201158-a35aa1360e3e/fs/fspath/path_test.go (about)

     1  package fspath
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestParse(t *testing.T) {
    11  	for _, test := range []struct {
    12  		in, wantConfigName, wantFsPath string
    13  	}{
    14  		{"", "", ""},
    15  		{"/path/to/file", "", "/path/to/file"},
    16  		{"path/to/file", "", "path/to/file"},
    17  		{"remote:path/to/file", "remote", "path/to/file"},
    18  		{"remote:/path/to/file", "remote", "/path/to/file"},
    19  		{":backend:/path/to/file", ":backend", "/path/to/file"},
    20  	} {
    21  		gotConfigName, gotFsPath := Parse(test.in)
    22  		assert.Equal(t, test.wantConfigName, gotConfigName)
    23  		assert.Equal(t, test.wantFsPath, gotFsPath)
    24  	}
    25  }
    26  
    27  func TestSplit(t *testing.T) {
    28  	for _, test := range []struct {
    29  		remote, wantParent, wantLeaf string
    30  	}{
    31  		{"", "", ""},
    32  
    33  		{"remote:", "remote:", ""},
    34  		{"remote:potato", "remote:", "potato"},
    35  		{"remote:/", "remote:/", ""},
    36  		{"remote:/potato", "remote:/", "potato"},
    37  		{"remote:/potato/potato", "remote:/potato/", "potato"},
    38  		{"remote:potato/sausage", "remote:potato/", "sausage"},
    39  
    40  		{":remote:", ":remote:", ""},
    41  		{":remote:potato", ":remote:", "potato"},
    42  		{":remote:/", ":remote:/", ""},
    43  		{":remote:/potato", ":remote:/", "potato"},
    44  		{":remote:/potato/potato", ":remote:/potato/", "potato"},
    45  		{":remote:potato/sausage", ":remote:potato/", "sausage"},
    46  
    47  		{"/", "/", ""},
    48  		{"/root", "/", "root"},
    49  		{"/a/b", "/a/", "b"},
    50  		{"root", "", "root"},
    51  		{"a/b", "a/", "b"},
    52  		{"root/", "root/", ""},
    53  		{"a/b/", "a/b/", ""},
    54  	} {
    55  		gotParent, gotLeaf := Split(test.remote)
    56  		assert.Equal(t, test.wantParent, gotParent, test.remote)
    57  		assert.Equal(t, test.wantLeaf, gotLeaf, test.remote)
    58  		assert.Equal(t, test.remote, gotParent+gotLeaf, fmt.Sprintf("%s: %q + %q != %q", test.remote, gotParent, gotLeaf, test.remote))
    59  	}
    60  }
    61  func TestJoinRootPath(t *testing.T) {
    62  	for _, test := range []struct {
    63  		elements []string
    64  		want     string
    65  	}{
    66  		{nil, ""},
    67  		{[]string{""}, ""},
    68  		{[]string{"/"}, "/"},
    69  		{[]string{"/", "/"}, "/"},
    70  		{[]string{"/", "//"}, "/"},
    71  		{[]string{"/root", ""}, "/root"},
    72  		{[]string{"/root", "/"}, "/root"},
    73  		{[]string{"/root", "//"}, "/root"},
    74  		{[]string{"/a/b"}, "/a/b"},
    75  		{[]string{"//", "/"}, "//"},
    76  		{[]string{"//server", "path"}, "//server/path"},
    77  		{[]string{"//server/sub", "path"}, "//server/sub/path"},
    78  		{[]string{"//server", "//path"}, "//server/path"},
    79  		{[]string{"//server/sub", "//path"}, "//server/sub/path"},
    80  		{[]string{"", "//", "/"}, "//"},
    81  		{[]string{"", "//server", "path"}, "//server/path"},
    82  		{[]string{"", "//server/sub", "path"}, "//server/sub/path"},
    83  		{[]string{"", "//server", "//path"}, "//server/path"},
    84  		{[]string{"", "//server/sub", "//path"}, "//server/sub/path"},
    85  	} {
    86  		got := JoinRootPath(test.elements...)
    87  		assert.Equal(t, test.want, got)
    88  	}
    89  }