rclone/fs/cache/cache_test.go

107 lines
2 KiB
Go
Raw Normal View History

package cache
import (
"errors"
"fmt"
"testing"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fstest/mockfs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
called = 0
errSentinel = errors.New("an error")
)
2019-07-31 21:19:07 +00:00
func mockNewFs(t *testing.T) (func(), func(path string) (fs.Fs, error)) {
called = 0
2019-07-31 21:19:07 +00:00
create := func(path string) (fs.Fs, error) {
assert.Equal(t, 0, called)
called++
switch path {
case "/":
return mockfs.NewFs("mock", "mock"), nil
case "/file.txt":
return mockfs.NewFs("mock", "mock"), fs.ErrorIsFile
case "/error":
return nil, errSentinel
}
panic(fmt.Sprintf("Unknown path %q", path))
}
2019-07-31 21:19:07 +00:00
cleanup := func() {
c.Clear()
}
2019-07-31 21:19:07 +00:00
return cleanup, create
}
func TestGet(t *testing.T) {
2019-07-31 21:19:07 +00:00
cleanup, create := mockNewFs(t)
defer cleanup()
2019-07-31 21:19:07 +00:00
assert.Equal(t, 0, c.Entries())
2019-07-31 21:19:07 +00:00
f, err := GetFn("/", create)
require.NoError(t, err)
2019-07-31 21:19:07 +00:00
assert.Equal(t, 1, c.Entries())
2019-07-31 21:19:07 +00:00
f2, err := GetFn("/", create)
require.NoError(t, err)
assert.Equal(t, f, f2)
}
func TestGetFile(t *testing.T) {
2019-07-31 21:19:07 +00:00
cleanup, create := mockNewFs(t)
defer cleanup()
2019-07-31 21:19:07 +00:00
assert.Equal(t, 0, c.Entries())
2019-07-31 21:19:07 +00:00
f, err := GetFn("/file.txt", create)
require.Equal(t, fs.ErrorIsFile, err)
require.NotNil(t, f)
2019-07-31 21:19:07 +00:00
assert.Equal(t, 1, c.Entries())
2019-07-31 21:19:07 +00:00
f2, err := GetFn("/file.txt", create)
require.Equal(t, fs.ErrorIsFile, err)
require.NotNil(t, f2)
assert.Equal(t, f, f2)
}
func TestGetError(t *testing.T) {
2019-07-31 21:19:07 +00:00
cleanup, create := mockNewFs(t)
defer cleanup()
2019-07-31 21:19:07 +00:00
assert.Equal(t, 0, c.Entries())
2019-07-31 21:19:07 +00:00
f, err := GetFn("/error", create)
require.Equal(t, errSentinel, err)
require.Equal(t, nil, f)
2019-07-31 21:19:07 +00:00
assert.Equal(t, 0, c.Entries())
}
func TestPut(t *testing.T) {
2019-07-31 21:19:07 +00:00
cleanup, create := mockNewFs(t)
defer cleanup()
f := mockfs.NewFs("mock", "mock")
2019-07-31 21:19:07 +00:00
assert.Equal(t, 0, c.Entries())
Put("/alien", f)
2019-07-31 21:19:07 +00:00
assert.Equal(t, 1, c.Entries())
2019-07-31 21:19:07 +00:00
fNew, err := GetFn("/alien", create)
require.NoError(t, err)
require.Equal(t, f, fNew)
2019-07-31 21:19:07 +00:00
assert.Equal(t, 1, c.Entries())
}