2018-01-13 00:30:54 +08:00
|
|
|
package walk
|
2017-02-25 06:51:01 +08:00
|
|
|
|
|
|
|
import (
|
2019-06-17 16:34:30 +08:00
|
|
|
"context"
|
2021-11-04 18:12:57 +08:00
|
|
|
"errors"
|
2017-06-06 23:40:00 +08:00
|
|
|
"fmt"
|
2019-01-21 18:02:23 +08:00
|
|
|
"io"
|
|
|
|
"strings"
|
2017-02-25 06:51:01 +08:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
2019-11-18 22:13:02 +08:00
|
|
|
_ "github.com/rclone/rclone/fs/accounting"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs/filter"
|
2019-11-18 22:13:02 +08:00
|
|
|
"github.com/rclone/rclone/fs/fserrors"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fstest/mockdir"
|
|
|
|
"github.com/rclone/rclone/fstest/mockfs"
|
|
|
|
"github.com/rclone/rclone/fstest/mockobject"
|
2017-02-25 06:51:01 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-06-06 23:40:00 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-02-25 06:51:01 +08:00
|
|
|
)
|
|
|
|
|
2019-11-18 22:13:02 +08:00
|
|
|
var errDirNotFound, errorBoom error
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
errDirNotFound = fserrors.FsError(fs.ErrorDirNotFound)
|
|
|
|
fserrors.Count(errDirNotFound)
|
|
|
|
errorBoom = fserrors.FsError(errors.New("boom"))
|
|
|
|
fserrors.Count(errorBoom)
|
|
|
|
}
|
|
|
|
|
2017-02-25 06:51:01 +08:00
|
|
|
type (
|
|
|
|
listResult struct {
|
2018-01-13 00:30:54 +08:00
|
|
|
entries fs.DirEntries
|
2017-02-25 06:51:01 +08:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
listResults map[string]listResult
|
|
|
|
|
|
|
|
errorMap map[string]error
|
|
|
|
|
|
|
|
listDirs struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
t *testing.T
|
2018-01-13 00:30:54 +08:00
|
|
|
fs fs.Fs
|
2017-02-25 06:51:01 +08:00
|
|
|
includeAll bool
|
|
|
|
results listResults
|
|
|
|
walkResults listResults
|
|
|
|
walkErrors errorMap
|
|
|
|
finalError error
|
|
|
|
checkMaps bool
|
|
|
|
maxLevel int
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
func newListDirs(t *testing.T, f fs.Fs, includeAll bool, results listResults, walkErrors errorMap, finalError error) *listDirs {
|
2017-02-25 06:51:01 +08:00
|
|
|
return &listDirs{
|
|
|
|
t: t,
|
|
|
|
fs: f,
|
|
|
|
includeAll: includeAll,
|
|
|
|
results: results,
|
|
|
|
walkErrors: walkErrors,
|
|
|
|
walkResults: listResults{},
|
|
|
|
finalError: finalError,
|
|
|
|
checkMaps: true,
|
|
|
|
maxLevel: -1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NoCheckMaps marks the maps as to be ignored at the end
|
|
|
|
func (ls *listDirs) NoCheckMaps() *listDirs {
|
|
|
|
ls.checkMaps = false
|
|
|
|
return ls
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLevel(1) turns off recursion
|
|
|
|
func (ls *listDirs) SetLevel(maxLevel int) *listDirs {
|
|
|
|
ls.maxLevel = maxLevel
|
|
|
|
return ls
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListDir returns the expected listing for the directory
|
2019-06-17 16:34:30 +08:00
|
|
|
func (ls *listDirs) ListDir(ctx context.Context, f fs.Fs, includeAll bool, dir string) (entries fs.DirEntries, err error) {
|
2017-02-25 06:51:01 +08:00
|
|
|
ls.mu.Lock()
|
|
|
|
defer ls.mu.Unlock()
|
|
|
|
assert.Equal(ls.t, ls.fs, f)
|
|
|
|
assert.Equal(ls.t, ls.includeAll, includeAll)
|
|
|
|
|
|
|
|
// Fetch results for this path
|
|
|
|
result, ok := ls.results[dir]
|
|
|
|
if !ok {
|
|
|
|
ls.t.Errorf("Unexpected list of %q", dir)
|
|
|
|
return nil, errors.New("unexpected list")
|
|
|
|
}
|
|
|
|
delete(ls.results, dir)
|
|
|
|
|
|
|
|
// Put expected results for call of WalkFn
|
|
|
|
ls.walkResults[dir] = result
|
|
|
|
|
|
|
|
return result.entries, result.err
|
|
|
|
}
|
|
|
|
|
2017-06-06 23:40:00 +08:00
|
|
|
// ListR returns the expected listing for the directory using ListR
|
2019-06-17 16:34:30 +08:00
|
|
|
func (ls *listDirs) ListR(ctx context.Context, dir string, callback fs.ListRCallback) (err error) {
|
2017-06-06 23:40:00 +08:00
|
|
|
ls.mu.Lock()
|
|
|
|
defer ls.mu.Unlock()
|
|
|
|
|
|
|
|
var errorReturn error
|
|
|
|
for dirPath, result := range ls.results {
|
|
|
|
// Put expected results for call of WalkFn
|
|
|
|
// Note that we don't call the function at all if we got an error
|
|
|
|
if result.err != nil {
|
|
|
|
errorReturn = result.err
|
|
|
|
}
|
|
|
|
if errorReturn == nil {
|
|
|
|
err = callback(result.entries)
|
|
|
|
require.NoError(ls.t, err)
|
|
|
|
ls.walkResults[dirPath] = result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ls.results = listResults{}
|
|
|
|
return errorReturn
|
|
|
|
}
|
|
|
|
|
2017-02-25 06:51:01 +08:00
|
|
|
// IsFinished checks everything expected was used up
|
|
|
|
func (ls *listDirs) IsFinished() {
|
|
|
|
if ls.checkMaps {
|
|
|
|
assert.Equal(ls.t, errorMap{}, ls.walkErrors)
|
|
|
|
assert.Equal(ls.t, listResults{}, ls.results)
|
|
|
|
assert.Equal(ls.t, listResults{}, ls.walkResults)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WalkFn is called by the walk to test the expectations
|
2018-01-13 00:30:54 +08:00
|
|
|
func (ls *listDirs) WalkFn(dir string, entries fs.DirEntries, err error) error {
|
2017-02-25 06:51:01 +08:00
|
|
|
ls.mu.Lock()
|
|
|
|
defer ls.mu.Unlock()
|
2017-06-06 23:40:00 +08:00
|
|
|
// ls.t.Logf("WalkFn(%q, %v, %q)", dir, entries, err)
|
2017-02-25 06:51:01 +08:00
|
|
|
|
|
|
|
// Fetch expected entries and err
|
|
|
|
result, ok := ls.walkResults[dir]
|
|
|
|
if !ok {
|
|
|
|
ls.t.Errorf("Unexpected walk of %q (result not found)", dir)
|
|
|
|
return errors.New("result not found")
|
|
|
|
}
|
|
|
|
delete(ls.walkResults, dir)
|
|
|
|
|
|
|
|
// Check arguments are as expected
|
|
|
|
assert.Equal(ls.t, result.entries, entries)
|
|
|
|
assert.Equal(ls.t, result.err, err)
|
|
|
|
|
|
|
|
// Fetch return value
|
|
|
|
returnErr, ok := ls.walkErrors[dir]
|
|
|
|
if !ok {
|
|
|
|
ls.t.Errorf("Unexpected walk of %q (error not found)", dir)
|
|
|
|
return errors.New("error not found")
|
|
|
|
}
|
|
|
|
delete(ls.walkErrors, dir)
|
|
|
|
|
|
|
|
return returnErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk does the walk and tests the expectations
|
|
|
|
func (ls *listDirs) Walk() {
|
2019-06-17 16:34:30 +08:00
|
|
|
err := walk(context.Background(), nil, "", ls.includeAll, ls.maxLevel, ls.WalkFn, ls.ListDir)
|
2017-02-25 06:51:01 +08:00
|
|
|
assert.Equal(ls.t, ls.finalError, err)
|
|
|
|
ls.IsFinished()
|
|
|
|
}
|
|
|
|
|
2017-06-06 23:40:00 +08:00
|
|
|
// WalkR does the walkR and tests the expectations
|
|
|
|
func (ls *listDirs) WalkR() {
|
2019-06-17 16:34:30 +08:00
|
|
|
err := walkR(context.Background(), nil, "", ls.includeAll, ls.maxLevel, ls.WalkFn, ls.ListR)
|
2017-06-06 23:40:00 +08:00
|
|
|
assert.Equal(ls.t, ls.finalError, err)
|
|
|
|
if ls.finalError == nil {
|
|
|
|
ls.IsFinished()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testWalkEmpty(t *testing.T) *listDirs {
|
|
|
|
return newListDirs(t, nil, false,
|
2017-02-25 06:51:01 +08:00
|
|
|
listResults{
|
2018-01-13 00:30:54 +08:00
|
|
|
"": {entries: fs.DirEntries{}, err: nil},
|
2017-02-25 06:51:01 +08:00
|
|
|
},
|
|
|
|
errorMap{
|
|
|
|
"": nil,
|
|
|
|
},
|
|
|
|
nil,
|
2017-06-06 23:40:00 +08:00
|
|
|
)
|
2017-02-25 06:51:01 +08:00
|
|
|
}
|
2017-06-06 23:40:00 +08:00
|
|
|
func TestWalkEmpty(t *testing.T) { testWalkEmpty(t).Walk() }
|
|
|
|
func TestWalkREmpty(t *testing.T) { testWalkEmpty(t).WalkR() }
|
2017-02-25 06:51:01 +08:00
|
|
|
|
2017-06-06 23:40:00 +08:00
|
|
|
func testWalkEmptySkip(t *testing.T) *listDirs {
|
|
|
|
return newListDirs(t, nil, true,
|
2017-02-25 06:51:01 +08:00
|
|
|
listResults{
|
2018-01-13 00:30:54 +08:00
|
|
|
"": {entries: fs.DirEntries{}, err: nil},
|
2017-02-25 06:51:01 +08:00
|
|
|
},
|
|
|
|
errorMap{
|
|
|
|
"": ErrorSkipDir,
|
|
|
|
},
|
|
|
|
nil,
|
2017-06-06 23:40:00 +08:00
|
|
|
)
|
2017-02-25 06:51:01 +08:00
|
|
|
}
|
2017-06-06 23:40:00 +08:00
|
|
|
func TestWalkEmptySkip(t *testing.T) { testWalkEmptySkip(t).Walk() }
|
|
|
|
func TestWalkREmptySkip(t *testing.T) { testWalkEmptySkip(t).WalkR() }
|
2017-02-25 06:51:01 +08:00
|
|
|
|
2017-06-06 23:40:00 +08:00
|
|
|
func testWalkNotFound(t *testing.T) *listDirs {
|
|
|
|
return newListDirs(t, nil, true,
|
2017-02-25 06:51:01 +08:00
|
|
|
listResults{
|
2019-11-18 22:13:02 +08:00
|
|
|
"": {err: errDirNotFound},
|
2017-02-25 06:51:01 +08:00
|
|
|
},
|
|
|
|
errorMap{
|
2019-11-18 22:13:02 +08:00
|
|
|
"": errDirNotFound,
|
2017-02-25 06:51:01 +08:00
|
|
|
},
|
2019-11-18 22:13:02 +08:00
|
|
|
errDirNotFound,
|
2017-06-06 23:40:00 +08:00
|
|
|
)
|
2017-02-25 06:51:01 +08:00
|
|
|
}
|
2017-06-06 23:40:00 +08:00
|
|
|
func TestWalkNotFound(t *testing.T) { testWalkNotFound(t).Walk() }
|
|
|
|
func TestWalkRNotFound(t *testing.T) { testWalkNotFound(t).WalkR() }
|
2017-02-25 06:51:01 +08:00
|
|
|
|
|
|
|
func TestWalkNotFoundMaskError(t *testing.T) {
|
2017-06-06 23:40:00 +08:00
|
|
|
// this doesn't work for WalkR
|
2017-02-25 06:51:01 +08:00
|
|
|
newListDirs(t, nil, true,
|
|
|
|
listResults{
|
2019-11-18 22:13:02 +08:00
|
|
|
"": {err: errDirNotFound},
|
2017-02-25 06:51:01 +08:00
|
|
|
},
|
|
|
|
errorMap{
|
|
|
|
"": nil,
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
).Walk()
|
|
|
|
}
|
|
|
|
|
2019-04-30 20:06:24 +08:00
|
|
|
func TestWalkNotFoundSkipError(t *testing.T) {
|
2017-06-06 23:40:00 +08:00
|
|
|
// this doesn't work for WalkR
|
2017-02-25 06:51:01 +08:00
|
|
|
newListDirs(t, nil, true,
|
|
|
|
listResults{
|
2019-11-18 22:13:02 +08:00
|
|
|
"": {err: errDirNotFound},
|
2017-02-25 06:51:01 +08:00
|
|
|
},
|
|
|
|
errorMap{
|
|
|
|
"": ErrorSkipDir,
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
).Walk()
|
|
|
|
}
|
|
|
|
|
2017-06-06 23:40:00 +08:00
|
|
|
func testWalkLevels(t *testing.T, maxLevel int) *listDirs {
|
2018-01-13 00:30:54 +08:00
|
|
|
da := mockdir.New("a")
|
|
|
|
oA := mockobject.Object("A")
|
|
|
|
db := mockdir.New("a/b")
|
|
|
|
oB := mockobject.Object("a/B")
|
|
|
|
dc := mockdir.New("a/b/c")
|
|
|
|
oC := mockobject.Object("a/b/C")
|
|
|
|
dd := mockdir.New("a/b/c/d")
|
|
|
|
oD := mockobject.Object("a/b/c/D")
|
2017-06-06 23:40:00 +08:00
|
|
|
return newListDirs(t, nil, false,
|
2017-02-25 06:51:01 +08:00
|
|
|
listResults{
|
2018-01-13 00:30:54 +08:00
|
|
|
"": {entries: fs.DirEntries{oA, da}, err: nil},
|
|
|
|
"a": {entries: fs.DirEntries{oB, db}, err: nil},
|
|
|
|
"a/b": {entries: fs.DirEntries{oC, dc}, err: nil},
|
|
|
|
"a/b/c": {entries: fs.DirEntries{oD, dd}, err: nil},
|
|
|
|
"a/b/c/d": {entries: fs.DirEntries{}, err: nil},
|
2017-02-25 06:51:01 +08:00
|
|
|
},
|
|
|
|
errorMap{
|
|
|
|
"": nil,
|
|
|
|
"a": nil,
|
|
|
|
"a/b": nil,
|
|
|
|
"a/b/c": nil,
|
|
|
|
"a/b/c/d": nil,
|
|
|
|
},
|
|
|
|
nil,
|
2017-06-06 23:40:00 +08:00
|
|
|
).SetLevel(maxLevel)
|
2017-02-25 06:51:01 +08:00
|
|
|
}
|
2017-06-06 23:40:00 +08:00
|
|
|
func TestWalkLevels(t *testing.T) { testWalkLevels(t, -1).Walk() }
|
|
|
|
func TestWalkRLevels(t *testing.T) { testWalkLevels(t, -1).WalkR() }
|
|
|
|
func TestWalkLevelsNoRecursive10(t *testing.T) { testWalkLevels(t, 10).Walk() }
|
|
|
|
func TestWalkRLevelsNoRecursive10(t *testing.T) { testWalkLevels(t, 10).WalkR() }
|
2017-02-25 06:51:01 +08:00
|
|
|
|
2017-07-25 05:52:18 +08:00
|
|
|
func TestWalkNDirTree(t *testing.T) {
|
|
|
|
ls := testWalkLevels(t, -1)
|
2019-06-17 16:34:30 +08:00
|
|
|
entries, err := walkNDirTree(context.Background(), nil, "", ls.includeAll, ls.maxLevel, ls.ListDir)
|
2017-07-25 05:52:18 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, `/
|
|
|
|
A
|
|
|
|
a/
|
|
|
|
a/
|
|
|
|
B
|
|
|
|
b/
|
|
|
|
a/b/
|
|
|
|
C
|
|
|
|
c/
|
|
|
|
a/b/c/
|
|
|
|
D
|
|
|
|
d/
|
|
|
|
a/b/c/d/
|
|
|
|
`, entries.String())
|
|
|
|
}
|
|
|
|
|
2017-06-06 23:40:00 +08:00
|
|
|
func testWalkLevelsNoRecursive(t *testing.T) *listDirs {
|
2018-01-13 00:30:54 +08:00
|
|
|
da := mockdir.New("a")
|
|
|
|
oA := mockobject.Object("A")
|
2017-06-06 23:40:00 +08:00
|
|
|
return newListDirs(t, nil, false,
|
2017-02-25 06:51:01 +08:00
|
|
|
listResults{
|
2018-01-13 00:30:54 +08:00
|
|
|
"": {entries: fs.DirEntries{oA, da}, err: nil},
|
2017-02-25 06:51:01 +08:00
|
|
|
},
|
|
|
|
errorMap{
|
|
|
|
"": nil,
|
|
|
|
},
|
|
|
|
nil,
|
2017-06-06 23:40:00 +08:00
|
|
|
).SetLevel(1)
|
2017-02-25 06:51:01 +08:00
|
|
|
}
|
2017-06-06 23:40:00 +08:00
|
|
|
func TestWalkLevelsNoRecursive(t *testing.T) { testWalkLevelsNoRecursive(t).Walk() }
|
|
|
|
func TestWalkRLevelsNoRecursive(t *testing.T) { testWalkLevelsNoRecursive(t).WalkR() }
|
2017-02-25 06:51:01 +08:00
|
|
|
|
2017-06-06 23:40:00 +08:00
|
|
|
func testWalkLevels2(t *testing.T) *listDirs {
|
2018-01-13 00:30:54 +08:00
|
|
|
da := mockdir.New("a")
|
|
|
|
oA := mockobject.Object("A")
|
|
|
|
db := mockdir.New("a/b")
|
|
|
|
oB := mockobject.Object("a/B")
|
2017-06-06 23:40:00 +08:00
|
|
|
return newListDirs(t, nil, false,
|
2017-02-25 06:51:01 +08:00
|
|
|
listResults{
|
2018-01-13 00:30:54 +08:00
|
|
|
"": {entries: fs.DirEntries{oA, da}, err: nil},
|
|
|
|
"a": {entries: fs.DirEntries{oB, db}, err: nil},
|
2017-02-25 06:51:01 +08:00
|
|
|
},
|
|
|
|
errorMap{
|
|
|
|
"": nil,
|
|
|
|
"a": nil,
|
|
|
|
},
|
|
|
|
nil,
|
2017-06-06 23:40:00 +08:00
|
|
|
).SetLevel(2)
|
2017-02-25 06:51:01 +08:00
|
|
|
}
|
2017-06-06 23:40:00 +08:00
|
|
|
func TestWalkLevels2(t *testing.T) { testWalkLevels2(t).Walk() }
|
|
|
|
func TestWalkRLevels2(t *testing.T) { testWalkLevels2(t).WalkR() }
|
2017-02-25 06:51:01 +08:00
|
|
|
|
2017-06-06 23:40:00 +08:00
|
|
|
func testWalkSkip(t *testing.T) *listDirs {
|
2018-01-13 00:30:54 +08:00
|
|
|
da := mockdir.New("a")
|
|
|
|
db := mockdir.New("a/b")
|
|
|
|
dc := mockdir.New("a/b/c")
|
2017-06-06 23:40:00 +08:00
|
|
|
return newListDirs(t, nil, false,
|
2017-02-25 06:51:01 +08:00
|
|
|
listResults{
|
2018-01-13 00:30:54 +08:00
|
|
|
"": {entries: fs.DirEntries{da}, err: nil},
|
|
|
|
"a": {entries: fs.DirEntries{db}, err: nil},
|
|
|
|
"a/b": {entries: fs.DirEntries{dc}, err: nil},
|
2017-02-25 06:51:01 +08:00
|
|
|
},
|
|
|
|
errorMap{
|
|
|
|
"": nil,
|
|
|
|
"a": nil,
|
|
|
|
"a/b": ErrorSkipDir,
|
|
|
|
},
|
|
|
|
nil,
|
2017-06-06 23:40:00 +08:00
|
|
|
)
|
2017-02-25 06:51:01 +08:00
|
|
|
}
|
2017-06-06 23:40:00 +08:00
|
|
|
func TestWalkSkip(t *testing.T) { testWalkSkip(t).Walk() }
|
|
|
|
func TestWalkRSkip(t *testing.T) { testWalkSkip(t).WalkR() }
|
2017-02-25 06:51:01 +08:00
|
|
|
|
2019-11-18 22:13:02 +08:00
|
|
|
func walkErrors(t *testing.T, expectedErr error) *listDirs {
|
2017-02-25 06:51:01 +08:00
|
|
|
lr := listResults{}
|
|
|
|
em := errorMap{}
|
2018-01-13 00:30:54 +08:00
|
|
|
de := make(fs.DirEntries, 10)
|
2017-02-25 06:51:01 +08:00
|
|
|
for i := range de {
|
2020-07-24 23:16:45 +08:00
|
|
|
path := string('0' + rune(i))
|
2018-01-13 00:30:54 +08:00
|
|
|
de[i] = mockdir.New(path)
|
|
|
|
lr[path] = listResult{entries: nil, err: fs.ErrorDirNotFound}
|
|
|
|
em[path] = fs.ErrorDirNotFound
|
2017-02-25 06:51:01 +08:00
|
|
|
}
|
|
|
|
lr[""] = listResult{entries: de, err: nil}
|
|
|
|
em[""] = nil
|
2017-06-06 23:40:00 +08:00
|
|
|
return newListDirs(t, nil, true,
|
2017-02-25 06:51:01 +08:00
|
|
|
lr,
|
|
|
|
em,
|
2019-11-18 22:13:02 +08:00
|
|
|
expectedErr,
|
2017-06-06 23:40:00 +08:00
|
|
|
).NoCheckMaps()
|
2017-02-25 06:51:01 +08:00
|
|
|
}
|
|
|
|
|
2019-11-18 22:13:02 +08:00
|
|
|
func testWalkErrors(t *testing.T) *listDirs {
|
|
|
|
return walkErrors(t, errDirNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testWalkRErrors(t *testing.T) *listDirs {
|
|
|
|
return walkErrors(t, fs.ErrorDirNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWalkErrors(t *testing.T) { testWalkErrors(t).Walk() }
|
|
|
|
func TestWalkRErrors(t *testing.T) { testWalkRErrors(t).WalkR() }
|
2017-02-25 06:51:01 +08:00
|
|
|
|
|
|
|
func makeTree(level int, terminalErrors bool) (listResults, errorMap) {
|
|
|
|
lr := listResults{}
|
|
|
|
em := errorMap{}
|
|
|
|
var fill func(path string, level int)
|
|
|
|
fill = func(path string, level int) {
|
2018-01-13 00:30:54 +08:00
|
|
|
de := fs.DirEntries{}
|
2017-02-25 06:51:01 +08:00
|
|
|
if level > 0 {
|
|
|
|
for _, a := range "0123456789" {
|
|
|
|
subPath := string(a)
|
|
|
|
if path != "" {
|
|
|
|
subPath = path + "/" + subPath
|
|
|
|
}
|
2018-01-13 00:30:54 +08:00
|
|
|
de = append(de, mockdir.New(subPath))
|
2017-02-25 06:51:01 +08:00
|
|
|
fill(subPath, level-1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lr[path] = listResult{entries: de, err: nil}
|
|
|
|
em[path] = nil
|
|
|
|
if level == 0 && terminalErrors {
|
|
|
|
em[path] = errorBoom
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fill("", level)
|
|
|
|
return lr, em
|
|
|
|
}
|
|
|
|
|
2017-06-06 23:40:00 +08:00
|
|
|
func testWalkMulti(t *testing.T) *listDirs {
|
2017-02-25 06:51:01 +08:00
|
|
|
lr, em := makeTree(3, false)
|
2017-06-06 23:40:00 +08:00
|
|
|
return newListDirs(t, nil, true,
|
2017-02-25 06:51:01 +08:00
|
|
|
lr,
|
|
|
|
em,
|
|
|
|
nil,
|
2017-06-06 23:40:00 +08:00
|
|
|
)
|
2017-02-25 06:51:01 +08:00
|
|
|
}
|
2017-06-06 23:40:00 +08:00
|
|
|
func TestWalkMulti(t *testing.T) { testWalkMulti(t).Walk() }
|
|
|
|
func TestWalkRMulti(t *testing.T) { testWalkMulti(t).WalkR() }
|
2017-02-25 06:51:01 +08:00
|
|
|
|
2017-06-06 23:40:00 +08:00
|
|
|
func testWalkMultiErrors(t *testing.T) *listDirs {
|
2017-02-25 06:51:01 +08:00
|
|
|
lr, em := makeTree(3, true)
|
2017-06-06 23:40:00 +08:00
|
|
|
return newListDirs(t, nil, true,
|
2017-02-25 06:51:01 +08:00
|
|
|
lr,
|
|
|
|
em,
|
|
|
|
errorBoom,
|
2017-06-06 23:40:00 +08:00
|
|
|
).NoCheckMaps()
|
|
|
|
}
|
|
|
|
func TestWalkMultiErrors(t *testing.T) { testWalkMultiErrors(t).Walk() }
|
|
|
|
func TestWalkRMultiErrors(t *testing.T) { testWalkMultiErrors(t).Walk() }
|
|
|
|
|
|
|
|
// a very simple listRcallback function
|
2018-01-13 00:30:54 +08:00
|
|
|
func makeListRCallback(entries fs.DirEntries, err error) fs.ListRFn {
|
2019-06-17 16:34:30 +08:00
|
|
|
return func(ctx context.Context, dir string, callback fs.ListRCallback) error {
|
2017-06-06 23:40:00 +08:00
|
|
|
if err == nil {
|
|
|
|
err = callback(entries)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWalkRDirTree(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
2018-01-13 00:30:54 +08:00
|
|
|
entries fs.DirEntries
|
2017-06-06 23:40:00 +08:00
|
|
|
want string
|
|
|
|
err error
|
|
|
|
root string
|
|
|
|
level int
|
2022-05-20 21:54:04 +08:00
|
|
|
exclude string
|
2017-06-06 23:40:00 +08:00
|
|
|
}{
|
2022-05-20 21:54:04 +08:00
|
|
|
{
|
|
|
|
entries: fs.DirEntries{},
|
|
|
|
want: "/\n",
|
|
|
|
level: -1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
entries: fs.DirEntries{mockobject.Object("a")},
|
|
|
|
want: `/
|
2017-06-06 23:40:00 +08:00
|
|
|
a
|
2022-05-20 21:54:04 +08:00
|
|
|
`,
|
|
|
|
level: -1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
entries: fs.DirEntries{mockobject.Object("a/b")},
|
|
|
|
want: `/
|
2017-06-06 23:40:00 +08:00
|
|
|
a/
|
|
|
|
a/
|
|
|
|
b
|
2022-05-20 21:54:04 +08:00
|
|
|
`,
|
|
|
|
level: -1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
entries: fs.DirEntries{mockobject.Object("a/b/c/d")},
|
|
|
|
want: `/
|
2017-06-06 23:40:00 +08:00
|
|
|
a/
|
|
|
|
a/
|
|
|
|
b/
|
|
|
|
a/b/
|
|
|
|
c/
|
|
|
|
a/b/c/
|
|
|
|
d
|
2022-05-20 21:54:04 +08:00
|
|
|
`,
|
|
|
|
level: -1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
entries: fs.DirEntries{mockobject.Object("a")},
|
|
|
|
err: errorBoom,
|
|
|
|
level: -1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
entries: fs.DirEntries{
|
|
|
|
mockobject.Object("0/1/2/3"),
|
|
|
|
mockobject.Object("4/5/6/7"),
|
|
|
|
mockobject.Object("8/9/a/b"),
|
|
|
|
mockobject.Object("c/d/e/f"),
|
|
|
|
mockobject.Object("g/h/i/j"),
|
|
|
|
mockobject.Object("k/l/m/n"),
|
|
|
|
mockobject.Object("o/p/q/r"),
|
|
|
|
mockobject.Object("s/t/u/v"),
|
|
|
|
mockobject.Object("w/x/y/z"),
|
|
|
|
},
|
|
|
|
want: `/
|
2017-06-06 23:40:00 +08:00
|
|
|
0/
|
|
|
|
4/
|
|
|
|
8/
|
|
|
|
c/
|
|
|
|
g/
|
|
|
|
k/
|
|
|
|
o/
|
|
|
|
s/
|
|
|
|
w/
|
|
|
|
0/
|
|
|
|
1/
|
|
|
|
0/1/
|
|
|
|
2/
|
|
|
|
0/1/2/
|
|
|
|
3
|
|
|
|
4/
|
|
|
|
5/
|
|
|
|
4/5/
|
|
|
|
6/
|
|
|
|
4/5/6/
|
|
|
|
7
|
|
|
|
8/
|
|
|
|
9/
|
|
|
|
8/9/
|
|
|
|
a/
|
|
|
|
8/9/a/
|
|
|
|
b
|
|
|
|
c/
|
|
|
|
d/
|
|
|
|
c/d/
|
|
|
|
e/
|
|
|
|
c/d/e/
|
|
|
|
f
|
|
|
|
g/
|
|
|
|
h/
|
|
|
|
g/h/
|
|
|
|
i/
|
|
|
|
g/h/i/
|
|
|
|
j
|
|
|
|
k/
|
|
|
|
l/
|
|
|
|
k/l/
|
|
|
|
m/
|
|
|
|
k/l/m/
|
|
|
|
n
|
|
|
|
o/
|
|
|
|
p/
|
|
|
|
o/p/
|
|
|
|
q/
|
|
|
|
o/p/q/
|
|
|
|
r
|
|
|
|
s/
|
|
|
|
t/
|
|
|
|
s/t/
|
|
|
|
u/
|
|
|
|
s/t/u/
|
|
|
|
v
|
|
|
|
w/
|
|
|
|
x/
|
|
|
|
w/x/
|
|
|
|
y/
|
|
|
|
w/x/y/
|
|
|
|
z
|
2022-05-20 21:54:04 +08:00
|
|
|
`,
|
|
|
|
level: -1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
entries: fs.DirEntries{
|
|
|
|
mockobject.Object("a/b/c/d/e/f1"),
|
|
|
|
mockobject.Object("a/b/c/d/e/f2"),
|
|
|
|
mockobject.Object("a/b/c/d/e/f3"),
|
|
|
|
},
|
|
|
|
want: `a/b/c/
|
2017-06-06 23:40:00 +08:00
|
|
|
d/
|
|
|
|
a/b/c/d/
|
|
|
|
e/
|
|
|
|
a/b/c/d/e/
|
|
|
|
f1
|
|
|
|
f2
|
|
|
|
f3
|
2022-05-20 21:54:04 +08:00
|
|
|
`,
|
|
|
|
root: "a/b/c",
|
|
|
|
level: -1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
entries: fs.DirEntries{
|
|
|
|
mockobject.Object("A"),
|
|
|
|
mockobject.Object("a/B"),
|
|
|
|
mockobject.Object("a/b/C"),
|
|
|
|
mockobject.Object("a/b/c/D"),
|
|
|
|
mockobject.Object("a/b/c/d/E"),
|
|
|
|
},
|
|
|
|
want: `/
|
2017-06-06 23:40:00 +08:00
|
|
|
A
|
|
|
|
a/
|
|
|
|
a/
|
|
|
|
B
|
|
|
|
b/
|
2022-05-20 21:54:04 +08:00
|
|
|
a/b/
|
|
|
|
`,
|
|
|
|
level: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
entries: fs.DirEntries{
|
|
|
|
mockobject.Object("a/b/c"),
|
|
|
|
mockobject.Object("a/b/c/d/e"),
|
|
|
|
},
|
|
|
|
want: `/
|
2017-06-06 23:40:00 +08:00
|
|
|
a/
|
|
|
|
a/
|
|
|
|
b/
|
2022-05-20 21:54:04 +08:00
|
|
|
a/b/
|
|
|
|
`,
|
|
|
|
level: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
entries: fs.DirEntries{
|
|
|
|
mockobject.Object("a/.bzEmpty"),
|
|
|
|
mockobject.Object("a/b1/.bzEmpty"),
|
|
|
|
mockobject.Object("a/b2/.bzEmpty"),
|
|
|
|
},
|
|
|
|
want: `/
|
|
|
|
a/
|
|
|
|
a/
|
|
|
|
.bzEmpty
|
|
|
|
b1/
|
|
|
|
b2/
|
|
|
|
a/b1/
|
|
|
|
.bzEmpty
|
|
|
|
a/b2/
|
|
|
|
.bzEmpty
|
|
|
|
`,
|
|
|
|
level: -1,
|
|
|
|
exclude: ""},
|
|
|
|
{
|
|
|
|
entries: fs.DirEntries{
|
|
|
|
mockobject.Object("a/.bzEmpty"),
|
|
|
|
mockobject.Object("a/b1/.bzEmpty"),
|
|
|
|
mockobject.Object("a/b2/.bzEmpty"),
|
|
|
|
},
|
|
|
|
want: `/
|
|
|
|
a/
|
|
|
|
a/
|
|
|
|
b1/
|
|
|
|
b2/
|
|
|
|
a/b1/
|
|
|
|
a/b2/
|
|
|
|
`,
|
|
|
|
level: -1,
|
|
|
|
exclude: ".bzEmpty",
|
|
|
|
},
|
2017-06-06 23:40:00 +08:00
|
|
|
} {
|
2022-05-20 21:54:04 +08:00
|
|
|
ctx := context.Background()
|
|
|
|
if test.exclude != "" {
|
|
|
|
fi, err := filter.NewFilter(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, fi.Add(false, test.exclude))
|
|
|
|
// Change the active filter
|
|
|
|
ctx = filter.ReplaceConfig(ctx, fi)
|
|
|
|
|
|
|
|
}
|
|
|
|
r, err := walkRDirTree(ctx, nil, test.root, test.exclude == "", test.level, makeListRCallback(test.entries, test.err))
|
|
|
|
what := fmt.Sprintf("%+v", test)
|
|
|
|
assert.Equal(t, test.err, err, what)
|
|
|
|
assert.Equal(t, test.want, r.String(), what)
|
2017-06-06 23:40:00 +08:00
|
|
|
}
|
2017-02-25 06:51:01 +08:00
|
|
|
}
|
2017-11-10 21:57:07 +08:00
|
|
|
|
|
|
|
func TestWalkRDirTreeExclude(t *testing.T) {
|
2020-11-27 01:10:41 +08:00
|
|
|
ctx := context.Background()
|
|
|
|
fi := filter.GetConfig(ctx)
|
2017-11-10 21:57:07 +08:00
|
|
|
for _, test := range []struct {
|
2018-01-13 00:30:54 +08:00
|
|
|
entries fs.DirEntries
|
2017-11-10 21:57:07 +08:00
|
|
|
want string
|
|
|
|
err error
|
|
|
|
root string
|
|
|
|
level int
|
|
|
|
excludeFile string
|
|
|
|
includeAll bool
|
|
|
|
}{
|
2018-01-13 00:30:54 +08:00
|
|
|
{fs.DirEntries{mockobject.Object("a"), mockobject.Object("ignore")}, "", nil, "", -1, "ignore", false},
|
|
|
|
{fs.DirEntries{mockobject.Object("a")}, `/
|
2017-11-10 21:57:07 +08:00
|
|
|
a
|
|
|
|
`, nil, "", -1, "ignore", false},
|
2018-01-13 00:30:54 +08:00
|
|
|
{fs.DirEntries{
|
|
|
|
mockobject.Object("a"),
|
|
|
|
mockobject.Object("b/b"),
|
|
|
|
mockobject.Object("b/.ignore"),
|
2017-11-10 21:57:07 +08:00
|
|
|
}, `/
|
|
|
|
a
|
|
|
|
`, nil, "", -1, ".ignore", false},
|
2018-01-13 00:30:54 +08:00
|
|
|
{fs.DirEntries{
|
|
|
|
mockobject.Object("a"),
|
|
|
|
mockobject.Object("b/.ignore"),
|
|
|
|
mockobject.Object("b/b"),
|
2017-11-10 21:57:07 +08:00
|
|
|
}, `/
|
|
|
|
a
|
|
|
|
b/
|
|
|
|
b/
|
|
|
|
.ignore
|
|
|
|
b
|
|
|
|
`, nil, "", -1, ".ignore", true},
|
2018-01-13 00:30:54 +08:00
|
|
|
{fs.DirEntries{
|
|
|
|
mockobject.Object("a"),
|
|
|
|
mockobject.Object("b/b"),
|
|
|
|
mockobject.Object("b/c/d/e"),
|
|
|
|
mockobject.Object("b/c/ign"),
|
|
|
|
mockobject.Object("b/c/x"),
|
2017-11-10 21:57:07 +08:00
|
|
|
}, `/
|
|
|
|
a
|
|
|
|
b/
|
|
|
|
b/
|
|
|
|
b
|
|
|
|
`, nil, "", -1, "ign", false},
|
2018-01-13 00:30:54 +08:00
|
|
|
{fs.DirEntries{
|
|
|
|
mockobject.Object("a"),
|
|
|
|
mockobject.Object("b/b"),
|
|
|
|
mockobject.Object("b/c/d/e"),
|
|
|
|
mockobject.Object("b/c/ign"),
|
|
|
|
mockobject.Object("b/c/x"),
|
2017-11-10 21:57:07 +08:00
|
|
|
}, `/
|
|
|
|
a
|
|
|
|
b/
|
|
|
|
b/
|
|
|
|
b
|
|
|
|
c/
|
|
|
|
b/c/
|
|
|
|
d/
|
|
|
|
ign
|
|
|
|
x
|
|
|
|
b/c/d/
|
|
|
|
e
|
|
|
|
`, nil, "", -1, "ign", true},
|
|
|
|
} {
|
2022-06-08 15:29:01 +08:00
|
|
|
fi.Opt.ExcludeFile = []string{test.excludeFile}
|
2019-06-17 16:34:30 +08:00
|
|
|
r, err := walkRDirTree(context.Background(), nil, test.root, test.includeAll, test.level, makeListRCallback(test.entries, test.err))
|
2017-11-10 21:57:07 +08:00
|
|
|
assert.Equal(t, test.err, err, fmt.Sprintf("%+v", test))
|
|
|
|
assert.Equal(t, test.want, r.String(), fmt.Sprintf("%+v", test))
|
|
|
|
}
|
|
|
|
// Set to default value, to avoid side effects
|
2022-06-08 15:29:01 +08:00
|
|
|
fi.Opt.ExcludeFile = nil
|
2017-11-10 21:57:07 +08:00
|
|
|
}
|
2019-01-21 18:02:23 +08:00
|
|
|
|
|
|
|
func TestListType(t *testing.T) {
|
|
|
|
assert.Equal(t, true, ListObjects.Objects())
|
|
|
|
assert.Equal(t, false, ListObjects.Dirs())
|
|
|
|
assert.Equal(t, false, ListDirs.Objects())
|
|
|
|
assert.Equal(t, true, ListDirs.Dirs())
|
|
|
|
assert.Equal(t, true, ListAll.Objects())
|
|
|
|
assert.Equal(t, true, ListAll.Dirs())
|
|
|
|
|
|
|
|
var (
|
|
|
|
a = mockobject.Object("a")
|
|
|
|
b = mockobject.Object("b")
|
|
|
|
dir = mockdir.New("dir")
|
|
|
|
adir = mockobject.Object("dir/a")
|
|
|
|
dir2 = mockdir.New("dir2")
|
|
|
|
origEntries = fs.DirEntries{
|
|
|
|
a, b, dir, adir, dir2,
|
|
|
|
}
|
|
|
|
dirEntries = fs.DirEntries{
|
|
|
|
dir, dir2,
|
|
|
|
}
|
|
|
|
objEntries = fs.DirEntries{
|
|
|
|
a, b, adir,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
copyOrigEntries := func() (out fs.DirEntries) {
|
|
|
|
out = make(fs.DirEntries, len(origEntries))
|
|
|
|
copy(out, origEntries)
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
got := copyOrigEntries()
|
|
|
|
ListAll.Filter(&got)
|
|
|
|
assert.Equal(t, origEntries, got)
|
|
|
|
|
|
|
|
got = copyOrigEntries()
|
|
|
|
ListObjects.Filter(&got)
|
|
|
|
assert.Equal(t, objEntries, got)
|
|
|
|
|
|
|
|
got = copyOrigEntries()
|
|
|
|
ListDirs.Filter(&got)
|
|
|
|
assert.Equal(t, dirEntries, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListR(t *testing.T) {
|
2020-11-27 01:10:41 +08:00
|
|
|
ctx := context.Background()
|
2019-01-21 18:02:23 +08:00
|
|
|
objects := fs.DirEntries{
|
|
|
|
mockobject.Object("a"),
|
|
|
|
mockobject.Object("b"),
|
|
|
|
mockdir.New("dir"),
|
|
|
|
mockobject.Object("dir/a"),
|
|
|
|
mockobject.Object("dir/b"),
|
|
|
|
mockobject.Object("dir/c"),
|
|
|
|
}
|
2023-04-28 19:01:04 +08:00
|
|
|
f, err := mockfs.NewFs(ctx, "mock", "/", nil)
|
|
|
|
require.NoError(t, err)
|
2019-01-21 18:02:23 +08:00
|
|
|
var got []string
|
|
|
|
clearCallback := func() {
|
|
|
|
got = nil
|
|
|
|
}
|
|
|
|
callback := func(entries fs.DirEntries) error {
|
|
|
|
for _, entry := range entries {
|
|
|
|
got = append(got, entry.Remote())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
doListR := func(ctx context.Context, dir string, callback fs.ListRCallback) error {
|
2019-01-21 18:02:23 +08:00
|
|
|
var os fs.DirEntries
|
|
|
|
for _, o := range objects {
|
|
|
|
if dir == "" || strings.HasPrefix(o.Remote(), dir+"/") {
|
|
|
|
os = append(os, o)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return callback(os)
|
|
|
|
}
|
|
|
|
|
2020-11-27 01:10:41 +08:00
|
|
|
fi, err := filter.NewFilter(nil)
|
2019-01-21 18:02:23 +08:00
|
|
|
require.NoError(t, err)
|
2020-11-27 01:10:41 +08:00
|
|
|
require.NoError(t, fi.AddRule("+ b"))
|
|
|
|
require.NoError(t, fi.AddRule("- *"))
|
|
|
|
|
|
|
|
// Change the active filter
|
|
|
|
ctx = filter.ReplaceConfig(ctx, fi)
|
2019-01-21 18:02:23 +08:00
|
|
|
|
|
|
|
// Base case
|
|
|
|
clearCallback()
|
2020-11-27 01:10:41 +08:00
|
|
|
err = listR(ctx, f, "", true, ListAll, callback, doListR, false)
|
2019-01-21 18:02:23 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []string{"a", "b", "dir", "dir/a", "dir/b", "dir/c"}, got)
|
|
|
|
|
|
|
|
// Base case - with Objects
|
|
|
|
clearCallback()
|
2020-11-27 01:10:41 +08:00
|
|
|
err = listR(ctx, f, "", true, ListObjects, callback, doListR, false)
|
2019-01-21 18:02:23 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []string{"a", "b", "dir/a", "dir/b", "dir/c"}, got)
|
|
|
|
|
|
|
|
// Base case - with Dirs
|
|
|
|
clearCallback()
|
2020-11-27 01:10:41 +08:00
|
|
|
err = listR(ctx, f, "", true, ListDirs, callback, doListR, false)
|
2019-01-21 18:02:23 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []string{"dir"}, got)
|
|
|
|
|
|
|
|
// With filter
|
|
|
|
clearCallback()
|
2020-11-27 01:10:41 +08:00
|
|
|
err = listR(ctx, f, "", false, ListAll, callback, doListR, false)
|
2019-01-21 18:02:23 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []string{"b", "dir", "dir/b"}, got)
|
|
|
|
|
|
|
|
// With filter - with Objects
|
|
|
|
clearCallback()
|
2020-11-27 01:10:41 +08:00
|
|
|
err = listR(ctx, f, "", false, ListObjects, callback, doListR, false)
|
2019-01-21 18:02:23 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []string{"b", "dir/b"}, got)
|
|
|
|
|
|
|
|
// With filter - with Dir
|
|
|
|
clearCallback()
|
2020-11-27 01:10:41 +08:00
|
|
|
err = listR(ctx, f, "", false, ListDirs, callback, doListR, false)
|
2019-01-21 18:02:23 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []string{"dir"}, got)
|
|
|
|
|
|
|
|
// With filter and subdir
|
|
|
|
clearCallback()
|
2020-11-27 01:10:41 +08:00
|
|
|
err = listR(ctx, f, "dir", false, ListAll, callback, doListR, false)
|
2019-01-21 18:02:23 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []string{"dir/b"}, got)
|
|
|
|
|
2021-11-04 19:50:43 +08:00
|
|
|
// Now bucket-based
|
2019-01-21 18:02:23 +08:00
|
|
|
objects = fs.DirEntries{
|
|
|
|
mockobject.Object("a"),
|
|
|
|
mockobject.Object("b"),
|
|
|
|
mockobject.Object("dir/a"),
|
|
|
|
mockobject.Object("dir/b"),
|
|
|
|
mockobject.Object("dir/subdir/c"),
|
|
|
|
mockdir.New("dir/subdir"),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Base case
|
|
|
|
clearCallback()
|
2020-11-27 01:10:41 +08:00
|
|
|
err = listR(ctx, f, "", true, ListAll, callback, doListR, true)
|
2019-01-21 18:02:23 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []string{"a", "b", "dir/a", "dir/b", "dir/subdir/c", "dir/subdir", "dir"}, got)
|
|
|
|
|
|
|
|
// With filter
|
|
|
|
clearCallback()
|
2020-11-27 01:10:41 +08:00
|
|
|
err = listR(ctx, f, "", false, ListAll, callback, doListR, true)
|
2019-01-21 18:02:23 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []string{"b", "dir/b", "dir/subdir", "dir"}, got)
|
|
|
|
|
|
|
|
// With filter and subdir
|
|
|
|
clearCallback()
|
2020-11-27 01:10:41 +08:00
|
|
|
err = listR(ctx, f, "dir", false, ListAll, callback, doListR, true)
|
2019-01-21 18:02:23 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []string{"dir/b", "dir/subdir"}, got)
|
|
|
|
|
|
|
|
// With filter and subdir - with Objects
|
|
|
|
clearCallback()
|
2020-11-27 01:10:41 +08:00
|
|
|
err = listR(ctx, f, "dir", false, ListObjects, callback, doListR, true)
|
2019-01-21 18:02:23 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []string{"dir/b"}, got)
|
|
|
|
|
|
|
|
// With filter and subdir - with Dirs
|
|
|
|
clearCallback()
|
2020-11-27 01:10:41 +08:00
|
|
|
err = listR(ctx, f, "dir", false, ListDirs, callback, doListR, true)
|
2019-01-21 18:02:23 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []string{"dir/subdir"}, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDirMapAdd(t *testing.T) {
|
|
|
|
type add struct {
|
|
|
|
dir string
|
|
|
|
sent bool
|
|
|
|
}
|
|
|
|
for i, test := range []struct {
|
|
|
|
root string
|
|
|
|
in []add
|
|
|
|
want map[string]bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
root: "",
|
|
|
|
in: []add{
|
|
|
|
{"", true},
|
|
|
|
},
|
|
|
|
want: map[string]bool{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
root: "",
|
|
|
|
in: []add{
|
|
|
|
{"a/b/c", true},
|
|
|
|
},
|
|
|
|
want: map[string]bool{
|
|
|
|
"a/b/c": true,
|
|
|
|
"a/b": false,
|
|
|
|
"a": false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
root: "",
|
|
|
|
in: []add{
|
|
|
|
{"a/b/c", true},
|
|
|
|
{"a/b", true},
|
|
|
|
},
|
|
|
|
want: map[string]bool{
|
|
|
|
"a/b/c": true,
|
|
|
|
"a/b": true,
|
|
|
|
"a": false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
root: "",
|
|
|
|
in: []add{
|
|
|
|
{"a/b", true},
|
|
|
|
{"a/b/c", false},
|
|
|
|
},
|
|
|
|
want: map[string]bool{
|
|
|
|
"a/b/c": false,
|
|
|
|
"a/b": true,
|
|
|
|
"a": false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
root: "root",
|
|
|
|
in: []add{
|
|
|
|
{"root/a/b", true},
|
|
|
|
{"root/a/b/c", false},
|
|
|
|
},
|
|
|
|
want: map[string]bool{
|
|
|
|
"root/a/b/c": false,
|
|
|
|
"root/a/b": true,
|
|
|
|
"root/a": false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
|
|
|
dm := newDirMap(test.root)
|
|
|
|
for _, item := range test.in {
|
|
|
|
dm.add(item.dir, item.sent)
|
|
|
|
}
|
|
|
|
assert.Equal(t, test.want, dm.m)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDirMapAddEntries(t *testing.T) {
|
|
|
|
dm := newDirMap("")
|
|
|
|
entries := fs.DirEntries{
|
|
|
|
mockobject.Object("dir/a"),
|
|
|
|
mockobject.Object("dir/b"),
|
|
|
|
mockdir.New("dir"),
|
|
|
|
mockobject.Object("dir2/a"),
|
|
|
|
mockobject.Object("dir2/b"),
|
|
|
|
}
|
|
|
|
require.NoError(t, dm.addEntries(entries))
|
|
|
|
assert.Equal(t, map[string]bool{"dir": true, "dir2": false}, dm.m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDirMapSendEntries(t *testing.T) {
|
|
|
|
var got []string
|
|
|
|
clearCallback := func() {
|
|
|
|
got = nil
|
|
|
|
}
|
|
|
|
callback := func(entries fs.DirEntries) error {
|
|
|
|
for _, entry := range entries {
|
|
|
|
got = append(got, entry.Remote())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// general test
|
|
|
|
dm := newDirMap("")
|
|
|
|
entries := fs.DirEntries{
|
|
|
|
mockobject.Object("dir/a"),
|
|
|
|
mockobject.Object("dir/b"),
|
|
|
|
mockdir.New("dir"),
|
|
|
|
mockobject.Object("dir2/a"),
|
|
|
|
mockobject.Object("dir2/b"),
|
|
|
|
mockobject.Object("dir1/a"),
|
|
|
|
mockobject.Object("dir3/b"),
|
|
|
|
}
|
|
|
|
require.NoError(t, dm.addEntries(entries))
|
|
|
|
clearCallback()
|
|
|
|
err := dm.sendEntries(callback)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, []string{
|
|
|
|
"dir1",
|
|
|
|
"dir2",
|
|
|
|
"dir3",
|
|
|
|
}, got)
|
|
|
|
|
|
|
|
// return error from callback
|
|
|
|
callback2 := func(entries fs.DirEntries) error {
|
|
|
|
return io.EOF
|
|
|
|
}
|
|
|
|
err = dm.sendEntries(callback2)
|
|
|
|
require.Equal(t, io.EOF, err)
|
|
|
|
|
|
|
|
// empty
|
|
|
|
dm = newDirMap("")
|
|
|
|
clearCallback()
|
|
|
|
err = dm.sendEntries(callback)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, []string(nil), got)
|
|
|
|
}
|