forked from TrueCloudLab/rclone
fstest/mockfs: allow fs.Objects to be added to the root
This commit is contained in:
parent
6959c997e2
commit
ea9b6087cf
1 changed files with 28 additions and 4 deletions
|
@ -5,6 +5,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"path"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rclone/rclone/fs"
|
"github.com/rclone/rclone/fs"
|
||||||
|
@ -13,9 +14,10 @@ import (
|
||||||
|
|
||||||
// Fs is a minimal mock Fs
|
// Fs is a minimal mock Fs
|
||||||
type Fs struct {
|
type Fs struct {
|
||||||
name string // the name of the remote
|
name string // the name of the remote
|
||||||
root string // The root directory (OS path)
|
root string // The root directory (OS path)
|
||||||
features *fs.Features // optional features
|
features *fs.Features // optional features
|
||||||
|
rootDir fs.DirEntries // directory listing of root
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrNotImplemented is returned by unimplemented methods
|
// ErrNotImplemented is returned by unimplemented methods
|
||||||
|
@ -31,6 +33,17 @@ func NewFs(name, root string) *Fs {
|
||||||
return f
|
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)
|
// Name of the remote (as passed into NewFs)
|
||||||
func (f *Fs) Name() string {
|
func (f *Fs) Name() string {
|
||||||
return f.name
|
return f.name
|
||||||
|
@ -71,12 +84,23 @@ func (f *Fs) Features() *fs.Features {
|
||||||
// This should return ErrDirNotFound if the directory isn't
|
// This should return ErrDirNotFound if the directory isn't
|
||||||
// found.
|
// found.
|
||||||
func (f *Fs) List(ctx context.Context, dir string) (entries fs.DirEntries, err error) {
|
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
|
// NewObject finds the Object at remote. If it can't be found
|
||||||
// it returns the error ErrorObjectNotFound.
|
// it returns the error ErrorObjectNotFound.
|
||||||
func (f *Fs) NewObject(ctx context.Context, remote string) (fs.Object, error) {
|
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
|
return nil, fs.ErrorObjectNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue