aboutsummaryrefslogtreecommitdiff
path: root/fs/operations/listdirsorted_test.go
blob: 4719d6c5531ccb809f6b3e18714b6c4e253039ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package operations_test

import (
	"context"
	"testing"

	"github.com/rclone/rclone/fs"
	"github.com/rclone/rclone/fs/filter"
	"github.com/rclone/rclone/fs/list"
	"github.com/rclone/rclone/fstest"
	"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, listFn func(ctx context.Context, f fs.Fs, includeAll bool, dir string) (entries fs.DirEntries, err error)) {
	r := fstest.NewRun(t)

	ctx := context.Background()
	fi := filter.GetConfig(ctx)
	fi.Opt.MaxSize = 10
	defer func() {
		fi.Opt.MaxSize = -1
	}()

	files := []fstest.Item{
		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),
		r.WriteObject(context.Background(), "sub dir/ignore dir/.ignore", "-", t1),
		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),
	}
	r.CheckRemoteItems(t, files...)
	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
	}

	items, err = listFn(context.Background(), r.Fremote, true, "")
	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))

	items, err = listFn(context.Background(), r.Fremote, false, "")
	require.NoError(t, err)
	require.Len(t, items, 2)
	assert.Equal(t, "sub dir/", str(0))
	assert.Equal(t, "zend.txt", str(1))

	items, err = listFn(context.Background(), r.Fremote, true, "sub dir")
	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))

	items, err = listFn(context.Background(), r.Fremote, false, "sub dir")
	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
	fi.Opt.ExcludeFile = []string{".ignore"}

	items, err = listFn(context.Background(), r.Fremote, false, "sub dir")
	require.NoError(t, err)
	require.Len(t, items, 1)
	assert.Equal(t, "sub dir/sub sub dir/", str(0))

	items, err = listFn(context.Background(), r.Fremote, false, "sub dir/ignore dir")
	require.NoError(t, err)
	require.Len(t, items, 0)

	items, err = listFn(context.Background(), r.Fremote, true, "sub dir/ignore dir")
	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))

	fi.Opt.ExcludeFile = nil
	items, err = listFn(context.Background(), r.Fremote, false, "sub dir/ignore dir")
	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))
}

// 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) {
	testListDirSorted(t, list.DirSorted)
}

// TestListDirSortedFn is integration testing code in fs/list/list.go
// which can't be tested there due to import loops.
func TestListDirSortedFn(t *testing.T) {
	listFn := func(ctx context.Context, f fs.Fs, includeAll bool, dir string) (entries fs.DirEntries, err error) {
		callback := func(newEntries fs.DirEntries) error {
			entries = append(entries, newEntries...)
			return nil
		}
		err = list.DirSortedFn(ctx, f, includeAll, dir, callback, nil)
		return entries, err
	}
	testListDirSorted(t, listFn)
}