2018-01-12 17:30:54 +01:00
|
|
|
package operations_test
|
|
|
|
|
|
|
|
import (
|
2019-06-17 10:34:30 +02:00
|
|
|
"context"
|
2018-01-12 17:30:54 +01:00
|
|
|
"testing"
|
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/filter"
|
|
|
|
"github.com/rclone/rclone/fs/list"
|
|
|
|
"github.com/rclone/rclone/fstest"
|
2018-01-12 17:30:54 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestListDirSorted is integration testing code in fs/list/list.go
|
|
|
|
// which can't be tested there due to import loops.
|
|
|
|
func TestListDirSorted(t *testing.T) {
|
|
|
|
r := fstest.NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
|
2020-11-26 18:10:41 +01:00
|
|
|
ctx := context.Background()
|
|
|
|
fi := filter.GetConfig(ctx)
|
|
|
|
fi.Opt.MaxSize = 10
|
2018-01-12 17:30:54 +01:00
|
|
|
defer func() {
|
2020-11-26 18:10:41 +01:00
|
|
|
fi.Opt.MaxSize = -1
|
2018-01-12 17:30:54 +01:00
|
|
|
}()
|
|
|
|
|
|
|
|
files := []fstest.Item{
|
2019-06-17 10:34:30 +02:00
|
|
|
r.WriteObject(context.Background(), "a.txt", "hello world", t1),
|
|
|
|
r.WriteObject(context.Background(), "zend.txt", "hello", t1),
|
|
|
|
r.WriteObject(context.Background(), "sub dir/hello world", "hello world", t1),
|
|
|
|
r.WriteObject(context.Background(), "sub dir/hello world2", "hello world", t1),
|
2019-06-29 03:17:18 +02:00
|
|
|
r.WriteObject(context.Background(), "sub dir/ignore dir/.ignore", "-", t1),
|
2019-06-17 10:34:30 +02:00
|
|
|
r.WriteObject(context.Background(), "sub dir/ignore dir/should be ignored", "to ignore", t1),
|
|
|
|
r.WriteObject(context.Background(), "sub dir/sub sub dir/hello world3", "hello world", t1),
|
2018-01-12 17:30:54 +01:00
|
|
|
}
|
2021-11-09 12:43:36 +01:00
|
|
|
r.CheckRemoteItems(t, files...)
|
2018-01-12 17:30:54 +01:00
|
|
|
var items fs.DirEntries
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Turn the DirEntry into a name, ending with a / if it is a
|
|
|
|
// dir
|
|
|
|
str := func(i int) string {
|
|
|
|
item := items[i]
|
|
|
|
name := item.Remote()
|
|
|
|
switch item.(type) {
|
|
|
|
case fs.Object:
|
|
|
|
case fs.Directory:
|
|
|
|
name += "/"
|
|
|
|
default:
|
|
|
|
t.Fatalf("Unknown type %+v", item)
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2019-06-17 10:34:30 +02:00
|
|
|
items, err = list.DirSorted(context.Background(), r.Fremote, true, "")
|
2018-01-12 17:30:54 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, items, 3)
|
|
|
|
assert.Equal(t, "a.txt", str(0))
|
|
|
|
assert.Equal(t, "sub dir/", str(1))
|
|
|
|
assert.Equal(t, "zend.txt", str(2))
|
|
|
|
|
2019-06-17 10:34:30 +02:00
|
|
|
items, err = list.DirSorted(context.Background(), r.Fremote, false, "")
|
2018-01-12 17:30:54 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, items, 2)
|
|
|
|
assert.Equal(t, "sub dir/", str(0))
|
|
|
|
assert.Equal(t, "zend.txt", str(1))
|
|
|
|
|
2019-06-17 10:34:30 +02:00
|
|
|
items, err = list.DirSorted(context.Background(), r.Fremote, true, "sub dir")
|
2018-01-12 17:30:54 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, items, 4)
|
|
|
|
assert.Equal(t, "sub dir/hello world", str(0))
|
|
|
|
assert.Equal(t, "sub dir/hello world2", str(1))
|
|
|
|
assert.Equal(t, "sub dir/ignore dir/", str(2))
|
|
|
|
assert.Equal(t, "sub dir/sub sub dir/", str(3))
|
|
|
|
|
2019-06-17 10:34:30 +02:00
|
|
|
items, err = list.DirSorted(context.Background(), r.Fremote, false, "sub dir")
|
2018-01-12 17:30:54 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, items, 2)
|
|
|
|
assert.Equal(t, "sub dir/ignore dir/", str(0))
|
|
|
|
assert.Equal(t, "sub dir/sub sub dir/", str(1))
|
|
|
|
|
|
|
|
// testing ignore file
|
2020-11-26 18:10:41 +01:00
|
|
|
fi.Opt.ExcludeFile = ".ignore"
|
2018-01-12 17:30:54 +01:00
|
|
|
|
2019-06-17 10:34:30 +02:00
|
|
|
items, err = list.DirSorted(context.Background(), r.Fremote, false, "sub dir")
|
2018-01-12 17:30:54 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, items, 1)
|
|
|
|
assert.Equal(t, "sub dir/sub sub dir/", str(0))
|
|
|
|
|
2019-06-17 10:34:30 +02:00
|
|
|
items, err = list.DirSorted(context.Background(), r.Fremote, false, "sub dir/ignore dir")
|
2018-01-12 17:30:54 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, items, 0)
|
|
|
|
|
2019-06-17 10:34:30 +02:00
|
|
|
items, err = list.DirSorted(context.Background(), r.Fremote, true, "sub dir/ignore dir")
|
2018-01-12 17:30:54 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, items, 2)
|
|
|
|
assert.Equal(t, "sub dir/ignore dir/.ignore", str(0))
|
|
|
|
assert.Equal(t, "sub dir/ignore dir/should be ignored", str(1))
|
|
|
|
|
2020-11-26 18:10:41 +01:00
|
|
|
fi.Opt.ExcludeFile = ""
|
2019-06-17 10:34:30 +02:00
|
|
|
items, err = list.DirSorted(context.Background(), r.Fremote, false, "sub dir/ignore dir")
|
2018-01-12 17:30:54 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, items, 2)
|
|
|
|
assert.Equal(t, "sub dir/ignore dir/.ignore", str(0))
|
|
|
|
assert.Equal(t, "sub dir/ignore dir/should be ignored", str(1))
|
|
|
|
}
|