2018-02-06 19:23:47 +01:00
|
|
|
package alias
|
|
|
|
|
|
|
|
import (
|
2019-06-17 10:34:30 +02:00
|
|
|
"context"
|
2018-02-06 19:23:47 +01:00
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
_ "github.com/rclone/rclone/backend/local" // pull in test backend
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/config"
|
2021-03-10 16:40:34 +01:00
|
|
|
"github.com/rclone/rclone/fs/config/configfile"
|
2018-02-06 19:23:47 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
remoteName = "TestAlias"
|
|
|
|
)
|
|
|
|
|
|
|
|
func prepare(t *testing.T, root string) {
|
2021-04-26 23:37:49 +02:00
|
|
|
configfile.Install()
|
2018-02-06 19:23:47 +01:00
|
|
|
|
|
|
|
// Configure the remote
|
|
|
|
config.FileSet(remoteName, "type", "alias")
|
|
|
|
config.FileSet(remoteName, "remote", root)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewFS(t *testing.T) {
|
|
|
|
type testEntry struct {
|
|
|
|
remote string
|
|
|
|
size int64
|
|
|
|
isDir bool
|
|
|
|
}
|
|
|
|
for testi, test := range []struct {
|
|
|
|
remoteRoot string
|
|
|
|
fsRoot string
|
|
|
|
fsList string
|
|
|
|
wantOK bool
|
|
|
|
entries []testEntry
|
|
|
|
}{
|
|
|
|
{"", "", "", true, []testEntry{
|
|
|
|
{"four", -1, true},
|
|
|
|
{"one%.txt", 6, false},
|
|
|
|
{"three", -1, true},
|
|
|
|
{"two.html", 7, false},
|
|
|
|
}},
|
|
|
|
{"", "four", "", true, []testEntry{
|
|
|
|
{"five", -1, true},
|
|
|
|
{"under four.txt", 9, false},
|
|
|
|
}},
|
|
|
|
{"", "", "four", true, []testEntry{
|
|
|
|
{"four/five", -1, true},
|
|
|
|
{"four/under four.txt", 9, false},
|
|
|
|
}},
|
|
|
|
{"four", "..", "", true, []testEntry{
|
2020-12-21 19:23:16 +01:00
|
|
|
{"five", -1, true},
|
|
|
|
{"under four.txt", 9, false},
|
2018-02-06 19:23:47 +01:00
|
|
|
}},
|
2020-12-21 19:23:16 +01:00
|
|
|
{"", "../../three", "", true, []testEntry{
|
2018-02-06 19:23:47 +01:00
|
|
|
{"underthree.txt", 9, false},
|
|
|
|
}},
|
2020-12-21 19:23:16 +01:00
|
|
|
{"four", "../../five", "", true, []testEntry{
|
|
|
|
{"underfive.txt", 6, false},
|
|
|
|
}},
|
2018-02-06 19:23:47 +01:00
|
|
|
} {
|
|
|
|
what := fmt.Sprintf("test %d remoteRoot=%q, fsRoot=%q, fsList=%q", testi, test.remoteRoot, test.fsRoot, test.fsList)
|
|
|
|
|
|
|
|
remoteRoot, err := filepath.Abs(filepath.FromSlash(path.Join("test/files", test.remoteRoot)))
|
|
|
|
require.NoError(t, err, what)
|
|
|
|
prepare(t, remoteRoot)
|
2020-11-05 16:18:51 +01:00
|
|
|
f, err := fs.NewFs(context.Background(), fmt.Sprintf("%s:%s", remoteName, test.fsRoot))
|
2018-02-06 19:23:47 +01:00
|
|
|
require.NoError(t, err, what)
|
2019-06-17 10:34:30 +02:00
|
|
|
gotEntries, err := f.List(context.Background(), test.fsList)
|
2018-02-06 19:23:47 +01:00
|
|
|
require.NoError(t, err, what)
|
|
|
|
|
|
|
|
sort.Sort(gotEntries)
|
|
|
|
|
|
|
|
require.Equal(t, len(test.entries), len(gotEntries), what)
|
|
|
|
for i, gotEntry := range gotEntries {
|
|
|
|
what := fmt.Sprintf("%s, entry=%d", what, i)
|
|
|
|
wantEntry := test.entries[i]
|
|
|
|
|
|
|
|
require.Equal(t, wantEntry.remote, gotEntry.Remote(), what)
|
2019-01-11 18:17:46 +01:00
|
|
|
require.Equal(t, wantEntry.size, gotEntry.Size(), what)
|
2018-02-06 19:23:47 +01:00
|
|
|
_, isDir := gotEntry.(fs.Directory)
|
|
|
|
require.Equal(t, wantEntry.isDir, isDir, what)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewFSNoRemote(t *testing.T) {
|
|
|
|
prepare(t, "")
|
2020-11-05 16:18:51 +01:00
|
|
|
f, err := fs.NewFs(context.Background(), fmt.Sprintf("%s:", remoteName))
|
2018-02-06 19:23:47 +01:00
|
|
|
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewFSInvalidRemote(t *testing.T) {
|
|
|
|
prepare(t, "not_existing_test_remote:")
|
2020-11-05 16:18:51 +01:00
|
|
|
f, err := fs.NewFs(context.Background(), fmt.Sprintf("%s:", remoteName))
|
2018-02-06 19:23:47 +01:00
|
|
|
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, f)
|
|
|
|
}
|