github.com/xhghs/rclone@v1.51.1-0.20200430155106-e186a28cced8/fs/list/list_test.go (about) 1 package list 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/rclone/rclone/fs" 9 "github.com/rclone/rclone/fstest/mockdir" 10 "github.com/rclone/rclone/fstest/mockobject" 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 ) 14 15 // NB integration tests for DirSorted are in 16 // fs/operations/listdirsorted_test.go 17 18 func TestFilterAndSortIncludeAll(t *testing.T) { 19 da := mockdir.New("a") 20 oA := mockobject.Object("A") 21 db := mockdir.New("b") 22 oB := mockobject.Object("B") 23 dc := mockdir.New("c") 24 oC := mockobject.Object("C") 25 dd := mockdir.New("d") 26 oD := mockobject.Object("D") 27 entries := fs.DirEntries{da, oA, db, oB, dc, oC, dd, oD} 28 includeObject := func(ctx context.Context, o fs.Object) bool { 29 return o != oB 30 } 31 includeDirectory := func(remote string) (bool, error) { 32 return remote != "c", nil 33 } 34 // no filter 35 newEntries, err := filterAndSortDir(context.Background(), entries, true, "", includeObject, includeDirectory) 36 require.NoError(t, err) 37 assert.Equal(t, 38 newEntries, 39 fs.DirEntries{oA, oB, oC, oD, da, db, dc, dd}, 40 ) 41 // filter 42 newEntries, err = filterAndSortDir(context.Background(), entries, false, "", includeObject, includeDirectory) 43 require.NoError(t, err) 44 assert.Equal(t, 45 newEntries, 46 fs.DirEntries{oA, oC, oD, da, db, dd}, 47 ) 48 } 49 50 func TestFilterAndSortCheckDir(t *testing.T) { 51 // Check the different kinds of error when listing "dir" 52 da := mockdir.New("dir/") 53 oA := mockobject.Object("diR/a") 54 db := mockdir.New("dir/b") 55 oB := mockobject.Object("dir/B/sub") 56 dc := mockdir.New("dir/c") 57 oC := mockobject.Object("dir/C") 58 dd := mockdir.New("dir/d") 59 oD := mockobject.Object("dir/D") 60 entries := fs.DirEntries{da, oA, db, oB, dc, oC, dd, oD} 61 newEntries, err := filterAndSortDir(context.Background(), entries, true, "dir", nil, nil) 62 require.NoError(t, err) 63 assert.Equal(t, 64 newEntries, 65 fs.DirEntries{oC, oD, db, dc, dd}, 66 ) 67 } 68 69 func TestFilterAndSortCheckDirRoot(t *testing.T) { 70 // Check the different kinds of error when listing the root "" 71 da := mockdir.New("") 72 oA := mockobject.Object("A") 73 db := mockdir.New("b") 74 oB := mockobject.Object("B/sub") 75 dc := mockdir.New("c") 76 oC := mockobject.Object("C") 77 dd := mockdir.New("d") 78 oD := mockobject.Object("D") 79 entries := fs.DirEntries{da, oA, db, oB, dc, oC, dd, oD} 80 newEntries, err := filterAndSortDir(context.Background(), entries, true, "", nil, nil) 81 require.NoError(t, err) 82 assert.Equal(t, 83 newEntries, 84 fs.DirEntries{oA, oC, oD, db, dc, dd}, 85 ) 86 } 87 88 type unknownDirEntry string 89 90 func (o unknownDirEntry) String() string { return string(o) } 91 func (o unknownDirEntry) Remote() string { return string(o) } 92 func (o unknownDirEntry) ModTime(ctx context.Context) (t time.Time) { return t } 93 func (o unknownDirEntry) Size() int64 { return 0 } 94 95 func TestFilterAndSortUnknown(t *testing.T) { 96 // Check that an unknown entry produces an error 97 da := mockdir.New("") 98 oA := mockobject.Object("A") 99 ub := unknownDirEntry("b") 100 oB := mockobject.Object("B/sub") 101 entries := fs.DirEntries{da, oA, ub, oB} 102 newEntries, err := filterAndSortDir(context.Background(), entries, true, "", nil, nil) 103 assert.Error(t, err, "error") 104 assert.Nil(t, newEntries) 105 }