mirror of
https://github.com/rclone/rclone.git
synced 2024-11-22 08:46:24 +08:00
fstest/mockfs: allow fs.Objects to be added to the root
This commit is contained in:
parent
6959c997e2
commit
ea9b6087cf
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user