github.com/xhghs/rclone@v1.51.1-0.20200430155106-e186a28cced8/backend/alias/alias_internal_test.go (about) 1 package alias 2 3 import ( 4 "context" 5 "fmt" 6 "path" 7 "path/filepath" 8 "sort" 9 "testing" 10 11 _ "github.com/rclone/rclone/backend/local" // pull in test backend 12 "github.com/rclone/rclone/fs" 13 "github.com/rclone/rclone/fs/config" 14 "github.com/stretchr/testify/require" 15 ) 16 17 var ( 18 remoteName = "TestAlias" 19 ) 20 21 func prepare(t *testing.T, root string) { 22 config.LoadConfig() 23 24 // Configure the remote 25 config.FileSet(remoteName, "type", "alias") 26 config.FileSet(remoteName, "remote", root) 27 } 28 29 func TestNewFS(t *testing.T) { 30 type testEntry struct { 31 remote string 32 size int64 33 isDir bool 34 } 35 for testi, test := range []struct { 36 remoteRoot string 37 fsRoot string 38 fsList string 39 wantOK bool 40 entries []testEntry 41 }{ 42 {"", "", "", true, []testEntry{ 43 {"four", -1, true}, 44 {"one%.txt", 6, false}, 45 {"three", -1, true}, 46 {"two.html", 7, false}, 47 }}, 48 {"", "four", "", true, []testEntry{ 49 {"five", -1, true}, 50 {"under four.txt", 9, false}, 51 }}, 52 {"", "", "four", true, []testEntry{ 53 {"four/five", -1, true}, 54 {"four/under four.txt", 9, false}, 55 }}, 56 {"four", "..", "", true, []testEntry{ 57 {"four", -1, true}, 58 {"one%.txt", 6, false}, 59 {"three", -1, true}, 60 {"two.html", 7, false}, 61 }}, 62 {"four", "../three", "", true, []testEntry{ 63 {"underthree.txt", 9, false}, 64 }}, 65 } { 66 what := fmt.Sprintf("test %d remoteRoot=%q, fsRoot=%q, fsList=%q", testi, test.remoteRoot, test.fsRoot, test.fsList) 67 68 remoteRoot, err := filepath.Abs(filepath.FromSlash(path.Join("test/files", test.remoteRoot))) 69 require.NoError(t, err, what) 70 prepare(t, remoteRoot) 71 f, err := fs.NewFs(fmt.Sprintf("%s:%s", remoteName, test.fsRoot)) 72 require.NoError(t, err, what) 73 gotEntries, err := f.List(context.Background(), test.fsList) 74 require.NoError(t, err, what) 75 76 sort.Sort(gotEntries) 77 78 require.Equal(t, len(test.entries), len(gotEntries), what) 79 for i, gotEntry := range gotEntries { 80 what := fmt.Sprintf("%s, entry=%d", what, i) 81 wantEntry := test.entries[i] 82 83 require.Equal(t, wantEntry.remote, gotEntry.Remote(), what) 84 require.Equal(t, wantEntry.size, gotEntry.Size(), what) 85 _, isDir := gotEntry.(fs.Directory) 86 require.Equal(t, wantEntry.isDir, isDir, what) 87 } 88 } 89 } 90 91 func TestNewFSNoRemote(t *testing.T) { 92 prepare(t, "") 93 f, err := fs.NewFs(fmt.Sprintf("%s:", remoteName)) 94 95 require.Error(t, err) 96 require.Nil(t, f) 97 } 98 99 func TestNewFSInvalidRemote(t *testing.T) { 100 prepare(t, "not_existing_test_remote:") 101 f, err := fs.NewFs(fmt.Sprintf("%s:", remoteName)) 102 103 require.Error(t, err) 104 require.Nil(t, f) 105 }