From ea9b6087cf70135203621bb78db8282b5ac5020c Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Sat, 14 Sep 2019 13:05:36 +0100 Subject: [PATCH] fstest/mockfs: allow fs.Objects to be added to the root --- fstest/mockfs/mockfs.go | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/fstest/mockfs/mockfs.go b/fstest/mockfs/mockfs.go index 476864e73..ed2d1b5e6 100644 --- a/fstest/mockfs/mockfs.go +++ b/fstest/mockfs/mockfs.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io" + "path" "time" "github.com/rclone/rclone/fs" @@ -13,9 +14,10 @@ import ( // Fs is a minimal mock Fs type Fs struct { - name string // the name of the remote - root string // The root directory (OS path) - features *fs.Features // optional features + name string // the name of the remote + root string // The root directory (OS path) + features *fs.Features // optional features + rootDir fs.DirEntries // directory listing of root } // ErrNotImplemented is returned by unimplemented methods @@ -31,6 +33,17 @@ func NewFs(name, root string) *Fs { return f } +// AddObject adds an Object for List to return +// Only works for the root for the moment +func (f *Fs) AddObject(o fs.Object) { + f.rootDir = append(f.rootDir, o) + // Make this object part of mockfs if possible + do, ok := o.(interface{ SetFs(f fs.Fs) }) + if ok { + do.SetFs(f) + } +} + // Name of the remote (as passed into NewFs) func (f *Fs) Name() string { return f.name @@ -71,12 +84,23 @@ func (f *Fs) Features() *fs.Features { // This should return ErrDirNotFound if the directory isn't // found. func (f *Fs) List(ctx context.Context, dir string) (entries fs.DirEntries, err error) { - return nil, nil + if dir == "" { + return f.rootDir, nil + } + return entries, fs.ErrorDirNotFound } // NewObject finds the Object at remote. If it can't be found // it returns the error ErrorObjectNotFound. func (f *Fs) NewObject(ctx context.Context, remote string) (fs.Object, error) { + dirPath := path.Dir(remote) + if dirPath == "" || dirPath == "." { + for _, entry := range f.rootDir { + if entry.Remote() == remote { + return entry.(fs.Object), nil + } + } + } return nil, fs.ErrorObjectNotFound }