From 6b670bd43978d7f835794f8e5d4cf6436125917b Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Fri, 28 Apr 2023 12:01:04 +0100 Subject: [PATCH] mockfs: make it so it can be registered as an Fs --- fs/cache/cache_test.go | 12 ++++++++---- fs/fingerprint_test.go | 5 ++++- fs/operations/multithread_test.go | 6 ++++-- fs/rc/cache_test.go | 3 ++- fs/walk/walk_test.go | 3 ++- fstest/mockfs/mockfs.go | 19 +++++++++++++++++-- vfs/file_test.go | 4 +++- 7 files changed, 40 insertions(+), 12 deletions(-) diff --git a/fs/cache/cache_test.go b/fs/cache/cache_test.go index f4e5eb9c2..210fcddbf 100644 --- a/fs/cache/cache_test.go +++ b/fs/cache/cache_test.go @@ -23,9 +23,11 @@ func mockNewFs(t *testing.T) func(ctx context.Context, path string) (fs.Fs, erro called++ switch path { case "mock:/": - return mockfs.NewFs(ctx, "mock", "/"), nil + return mockfs.NewFs(ctx, "mock", "/", nil) 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": return nil, errSentinel } @@ -117,7 +119,8 @@ func TestGetError(t *testing.T) { func TestPut(t *testing.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()) @@ -147,7 +150,8 @@ func TestPin(t *testing.T) { create := mockNewFs(t) // 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) Unpin(f) diff --git a/fs/fingerprint_test.go b/fs/fingerprint_test.go index 0a69ceddf..713baf4cb 100644 --- a/fs/fingerprint_test.go +++ b/fs/fingerprint_test.go @@ -10,11 +10,14 @@ import ( "github.com/rclone/rclone/fstest/mockfs" "github.com/rclone/rclone/fstest/mockobject" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestFingerprint(t *testing.T) { 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)) for i, test := range []struct { diff --git a/fs/operations/multithread_test.go b/fs/operations/multithread_test.go index a036139e5..fa982cf59 100644 --- a/fs/operations/multithread_test.go +++ b/fs/operations/multithread_test.go @@ -19,9 +19,11 @@ import ( func TestDoMultiThreadCopy(t *testing.T) { ctx := context.Background() 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) - srcFs := mockfs.NewFs(ctx, "sausage", "") + srcFs, err := mockfs.NewFs(ctx, "sausage", "", nil) + require.NoError(t, err) src.SetFs(srcFs) oldStreams := ci.MultiThreadStreams diff --git a/fs/rc/cache_test.go b/fs/rc/cache_test.go index bbda7e0b6..6d7c7730e 100644 --- a/fs/rc/cache_test.go +++ b/fs/rc/cache_test.go @@ -12,7 +12,8 @@ import ( ) 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("mock:/", f) cache.Put(":mock:/", f) diff --git a/fs/walk/walk_test.go b/fs/walk/walk_test.go index cbfa82ce8..d47991486 100644 --- a/fs/walk/walk_test.go +++ b/fs/walk/walk_test.go @@ -798,7 +798,8 @@ func TestListR(t *testing.T) { mockobject.Object("dir/b"), mockobject.Object("dir/c"), } - f := mockfs.NewFs(ctx, "mock", "/") + f, err := mockfs.NewFs(ctx, "mock", "/", nil) + require.NoError(t, err) var got []string clearCallback := func() { got = nil diff --git a/fstest/mockfs/mockfs.go b/fstest/mockfs/mockfs.go index 62489c2f6..68df1f6b4 100644 --- a/fstest/mockfs/mockfs.go +++ b/fstest/mockfs/mockfs.go @@ -10,9 +10,24 @@ import ( "time" "github.com/rclone/rclone/fs" + "github.com/rclone/rclone/fs/config/configmap" "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 type Fs struct { name string // the name of the remote @@ -26,13 +41,13 @@ type Fs struct { var ErrNotImplemented = errors.New("not implemented") // 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{ name: name, root: root, } f.features = (&fs.Features{}).Fill(ctx, f) - return f + return f, nil } // AddObject adds an Object for List to return diff --git a/vfs/file_test.go b/vfs/file_test.go index 728d8c699..6688749d2 100644 --- a/vfs/file_test.go +++ b/vfs/file_test.go @@ -193,7 +193,9 @@ func TestFileOpenReadUnknownSize(t *testing.T) { assert.Equal(t, int64(-1), o.Size()) // 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) testObj, err := f.NewObject(ctx, remote) require.NoError(t, err)