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++
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)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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