mockfs: make it so it can be registered as an Fs

This commit is contained in:
Nick Craig-Wood 2023-04-28 12:01:04 +01:00
parent 335ca6d572
commit 6b670bd439
7 changed files with 40 additions and 12 deletions

View file

@ -23,9 +23,11 @@ func mockNewFs(t *testing.T) func(ctx context.Context, path string) (fs.Fs, erro
called++ called++
switch path { switch path {
case "mock:/": case "mock:/":
return mockfs.NewFs(ctx, "mock", "/"), nil return mockfs.NewFs(ctx, "mock", "/", nil)
case "mock:/file.txt", "mock:file.txt": case "mock:/file.txt", "mock:file.txt":
return mockfs.NewFs(ctx, "mock", "/"), fs.ErrorIsFile fMock, err := mockfs.NewFs(ctx, "mock", "/", nil)
require.NoError(t, err)
return fMock, fs.ErrorIsFile
case "mock:/error": case "mock:/error":
return nil, errSentinel return nil, errSentinel
} }
@ -117,7 +119,8 @@ func TestGetError(t *testing.T) {
func TestPut(t *testing.T) { func TestPut(t *testing.T) {
create := mockNewFs(t) create := mockNewFs(t)
f := mockfs.NewFs(context.Background(), "mock", "/alien") f, err := mockfs.NewFs(context.Background(), "mock", "/alien", nil)
require.NoError(t, err)
assert.Equal(t, 0, Entries()) assert.Equal(t, 0, Entries())
@ -147,7 +150,8 @@ func TestPin(t *testing.T) {
create := mockNewFs(t) create := mockNewFs(t)
// Test pinning and unpinning nonexistent // Test pinning and unpinning nonexistent
f := mockfs.NewFs(context.Background(), "mock", "/alien") f, err := mockfs.NewFs(context.Background(), "mock", "/alien", nil)
require.NoError(t, err)
Pin(f) Pin(f)
Unpin(f) Unpin(f)

View file

@ -10,11 +10,14 @@ import (
"github.com/rclone/rclone/fstest/mockfs" "github.com/rclone/rclone/fstest/mockfs"
"github.com/rclone/rclone/fstest/mockobject" "github.com/rclone/rclone/fstest/mockobject"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestFingerprint(t *testing.T) { func TestFingerprint(t *testing.T) {
ctx := context.Background() ctx := context.Background()
f := mockfs.NewFs(ctx, "test", "root") fMock, err := mockfs.NewFs(ctx, "test", "root", nil)
require.NoError(t, err)
f := fMock.(*mockfs.Fs)
f.SetHashes(hash.NewHashSet(hash.MD5)) f.SetHashes(hash.NewHashSet(hash.MD5))
for i, test := range []struct { for i, test := range []struct {

View file

@ -19,9 +19,11 @@ import (
func TestDoMultiThreadCopy(t *testing.T) { func TestDoMultiThreadCopy(t *testing.T) {
ctx := context.Background() ctx := context.Background()
ci := fs.GetConfig(ctx) ci := fs.GetConfig(ctx)
f := mockfs.NewFs(ctx, "potato", "") f, err := mockfs.NewFs(ctx, "potato", "", nil)
require.NoError(t, err)
src := mockobject.New("file.txt").WithContent([]byte(random.String(100)), mockobject.SeekModeNone) src := mockobject.New("file.txt").WithContent([]byte(random.String(100)), mockobject.SeekModeNone)
srcFs := mockfs.NewFs(ctx, "sausage", "") srcFs, err := mockfs.NewFs(ctx, "sausage", "", nil)
require.NoError(t, err)
src.SetFs(srcFs) src.SetFs(srcFs)
oldStreams := ci.MultiThreadStreams oldStreams := ci.MultiThreadStreams

View file

@ -12,7 +12,8 @@ import (
) )
func mockNewFs(t *testing.T) func() { func mockNewFs(t *testing.T) func() {
f := mockfs.NewFs(context.Background(), "mock", "mock") f, err := mockfs.NewFs(context.Background(), "mock", "mock", nil)
require.NoError(t, err)
cache.Put("/", f) cache.Put("/", f)
cache.Put("mock:/", f) cache.Put("mock:/", f)
cache.Put(":mock:/", f) cache.Put(":mock:/", f)

View file

@ -798,7 +798,8 @@ func TestListR(t *testing.T) {
mockobject.Object("dir/b"), mockobject.Object("dir/b"),
mockobject.Object("dir/c"), mockobject.Object("dir/c"),
} }
f := mockfs.NewFs(ctx, "mock", "/") f, err := mockfs.NewFs(ctx, "mock", "/", nil)
require.NoError(t, err)
var got []string var got []string
clearCallback := func() { clearCallback := func() {
got = nil got = nil

View file

@ -10,9 +10,24 @@ import (
"time" "time"
"github.com/rclone/rclone/fs" "github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config/configmap"
"github.com/rclone/rclone/fs/hash" "github.com/rclone/rclone/fs/hash"
) )
// Register with Fs
func Register() {
fs.Register(&fs.RegInfo{
Name: "mockfs",
Description: "Mock FS",
NewFs: NewFs,
Options: []fs.Option{{
Name: "potato",
Help: "Does it have a potato?.",
Required: true,
}},
})
}
// 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
@ -26,13 +41,13 @@ type Fs struct {
var ErrNotImplemented = errors.New("not implemented") var ErrNotImplemented = errors.New("not implemented")
// NewFs returns a new mock Fs // NewFs returns a new mock Fs
func NewFs(ctx context.Context, name, root string) *Fs { func NewFs(ctx context.Context, name string, root string, config configmap.Mapper) (fs.Fs, error) {
f := &Fs{ f := &Fs{
name: name, name: name,
root: root, root: root,
} }
f.features = (&fs.Features{}).Fill(ctx, f) f.features = (&fs.Features{}).Fill(ctx, f)
return f return f, nil
} }
// AddObject adds an Object for List to return // AddObject adds an Object for List to return

View file

@ -193,7 +193,9 @@ func TestFileOpenReadUnknownSize(t *testing.T) {
assert.Equal(t, int64(-1), o.Size()) assert.Equal(t, int64(-1), o.Size())
// add it to a mock fs // add it to a mock fs
f := mockfs.NewFs(context.Background(), "test", "root") fMock, err := mockfs.NewFs(context.Background(), "test", "root", nil)
require.NoError(t, err)
f := fMock.(*mockfs.Fs)
f.AddObject(o) f.AddObject(o)
testObj, err := f.NewObject(ctx, remote) testObj, err := f.NewObject(ctx, remote)
require.NoError(t, err) require.NoError(t, err)