Skip to content

Commit

Permalink
mockfs: make it so it can be registered as an Fs
Browse files Browse the repository at this point in the history
  • Loading branch information
ncw committed Apr 28, 2023
1 parent 335ca6d commit 6b670bd
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 12 deletions.
12 changes: 8 additions & 4 deletions fs/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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())

Expand Down Expand Up @@ -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)

Expand Down
5 changes: 4 additions & 1 deletion fs/fingerprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions fs/operations/multithread_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion fs/rc/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion fs/walk/walk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions fstest/mockfs/mockfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion vfs/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6b670bd

Please sign in to comment.