fstest/mockfs: allow fs.Objects to be added to the root

This commit is contained in:
Nick Craig-Wood 2019-09-14 13:05:36 +01:00
parent 6959c997e2
commit ea9b6087cf

View File

@ -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
}