mirror of
https://github.com/rclone/rclone.git
synced 2025-08-20 01:58:54 +02:00
.github
backend
bin
cmd
cmdtest
contrib
docs
fs
accounting
asyncreader
cache
cache.go
cache_test.go
chunkedreader
config
dirtree
driveletter
filter
fserrors
fshttp
fspath
hash
list
log
march
object
operations
rc
sync
walk
backend_config.go
backend_config_test.go
bwtimetable.go
bwtimetable_test.go
config.go
config_list.go
config_list_test.go
config_test.go
configmap.go
countsuffix.go
countsuffix_test.go
cutoffmode.go
cutoffmode_test.go
daemon_other.go
daemon_unix.go
deletemode.go
dir.go
direntries.go
direntries_test.go
dump.go
dump_test.go
features.go
fingerprint.go
fingerprint_test.go
fs.go
fs_test.go
log.go
log_test.go
mimetype.go
mount_helper.go
mount_helper_test.go
newfs.go
open_options.go
open_options_test.go
pacer.go
parseduration.go
parseduration_test.go
registry.go
sizesuffix.go
sizesuffix_test.go
tristate.go
tristate_test.go
types.go
version.go
versioncheck.go
fstest
graphics
lib
librclone
vfs
.gitattributes
.gitignore
.golangci.yml
CONTRIBUTING.md
COPYING
Dockerfile
MAINTAINERS.md
MANUAL.html
MANUAL.md
MANUAL.txt
Makefile
README.md
RELEASE.md
VERSION
go.mod
go.sum
notes.txt
rclone.1
rclone.go
This alters some comments in source files, but is interested mainly in documentation files and help messages.
212 lines
4.3 KiB
Go
212 lines
4.3 KiB
Go
package cache
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"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")
|
|
)
|
|
|
|
func mockNewFs(t *testing.T) (func(), func(ctx context.Context, path string) (fs.Fs, error)) {
|
|
called = 0
|
|
create := func(ctx context.Context, path string) (f fs.Fs, err error) {
|
|
assert.Equal(t, 0, called)
|
|
called++
|
|
switch path {
|
|
case "mock:/":
|
|
return mockfs.NewFs(ctx, "mock", "/"), nil
|
|
case "mock:/file.txt", "mock:file.txt":
|
|
return mockfs.NewFs(ctx, "mock", "/"), fs.ErrorIsFile
|
|
case "mock:/error":
|
|
return nil, errSentinel
|
|
}
|
|
t.Fatalf("Unknown path %q", path)
|
|
panic("unreachable")
|
|
}
|
|
cleanup := func() {
|
|
Clear()
|
|
}
|
|
return cleanup, create
|
|
}
|
|
|
|
func TestGet(t *testing.T) {
|
|
cleanup, create := mockNewFs(t)
|
|
defer cleanup()
|
|
|
|
assert.Equal(t, 0, Entries())
|
|
|
|
f, err := GetFn(context.Background(), "mock:/", create)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, Entries())
|
|
|
|
f2, err := GetFn(context.Background(), "mock:/", create)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, f, f2)
|
|
}
|
|
|
|
func TestGetFile(t *testing.T) {
|
|
cleanup, create := mockNewFs(t)
|
|
defer cleanup()
|
|
|
|
assert.Equal(t, 0, Entries())
|
|
|
|
f, err := GetFn(context.Background(), "mock:/file.txt", create)
|
|
require.Equal(t, fs.ErrorIsFile, err)
|
|
require.NotNil(t, f)
|
|
|
|
assert.Equal(t, 2, Entries())
|
|
|
|
f2, err := GetFn(context.Background(), "mock:/file.txt", create)
|
|
require.Equal(t, fs.ErrorIsFile, err)
|
|
require.NotNil(t, f2)
|
|
|
|
assert.Equal(t, f, f2)
|
|
|
|
// check parent is there too
|
|
f2, err = GetFn(context.Background(), "mock:/", create)
|
|
require.Nil(t, err)
|
|
require.NotNil(t, f2)
|
|
|
|
assert.Equal(t, f, f2)
|
|
}
|
|
|
|
func TestGetFile2(t *testing.T) {
|
|
cleanup, create := mockNewFs(t)
|
|
defer cleanup()
|
|
|
|
assert.Equal(t, 0, Entries())
|
|
|
|
f, err := GetFn(context.Background(), "mock:file.txt", create)
|
|
require.Equal(t, fs.ErrorIsFile, err)
|
|
require.NotNil(t, f)
|
|
|
|
assert.Equal(t, 2, Entries())
|
|
|
|
f2, err := GetFn(context.Background(), "mock:file.txt", create)
|
|
require.Equal(t, fs.ErrorIsFile, err)
|
|
require.NotNil(t, f2)
|
|
|
|
assert.Equal(t, f, f2)
|
|
|
|
// check parent is there too
|
|
f2, err = GetFn(context.Background(), "mock:/", create)
|
|
require.Nil(t, err)
|
|
require.NotNil(t, f2)
|
|
|
|
assert.Equal(t, f, f2)
|
|
}
|
|
|
|
func TestGetError(t *testing.T) {
|
|
cleanup, create := mockNewFs(t)
|
|
defer cleanup()
|
|
|
|
assert.Equal(t, 0, Entries())
|
|
|
|
f, err := GetFn(context.Background(), "mock:/error", create)
|
|
require.Equal(t, errSentinel, err)
|
|
require.Equal(t, nil, f)
|
|
|
|
assert.Equal(t, 0, Entries())
|
|
}
|
|
|
|
func TestPut(t *testing.T) {
|
|
cleanup, create := mockNewFs(t)
|
|
defer cleanup()
|
|
|
|
f := mockfs.NewFs(context.Background(), "mock", "/alien")
|
|
|
|
assert.Equal(t, 0, Entries())
|
|
|
|
Put("mock:/alien", f)
|
|
|
|
assert.Equal(t, 1, Entries())
|
|
|
|
fNew, err := GetFn(context.Background(), "mock:/alien", create)
|
|
require.NoError(t, err)
|
|
require.Equal(t, f, fNew)
|
|
|
|
assert.Equal(t, 1, Entries())
|
|
|
|
// Check canonicalisation
|
|
|
|
Put("mock:/alien/", f)
|
|
|
|
fNew, err = GetFn(context.Background(), "mock:/alien/", create)
|
|
require.NoError(t, err)
|
|
require.Equal(t, f, fNew)
|
|
|
|
assert.Equal(t, 1, Entries())
|
|
|
|
}
|
|
|
|
func TestPin(t *testing.T) {
|
|
cleanup, create := mockNewFs(t)
|
|
defer cleanup()
|
|
|
|
// Test pinning and unpinning non-existent
|
|
f := mockfs.NewFs(context.Background(), "mock", "/alien")
|
|
Pin(f)
|
|
Unpin(f)
|
|
|
|
// Now test pinning an existing
|
|
f2, err := GetFn(context.Background(), "mock:/", create)
|
|
require.NoError(t, err)
|
|
Pin(f2)
|
|
Unpin(f2)
|
|
}
|
|
|
|
func TestClearConfig(t *testing.T) {
|
|
cleanup, create := mockNewFs(t)
|
|
defer cleanup()
|
|
|
|
assert.Equal(t, 0, Entries())
|
|
|
|
_, err := GetFn(context.Background(), "mock:/file.txt", create)
|
|
require.Equal(t, fs.ErrorIsFile, err)
|
|
|
|
assert.Equal(t, 2, Entries()) // file + parent
|
|
|
|
assert.Equal(t, 2, ClearConfig("mock"))
|
|
|
|
assert.Equal(t, 0, Entries())
|
|
}
|
|
|
|
func TestClear(t *testing.T) {
|
|
cleanup, create := mockNewFs(t)
|
|
defer cleanup()
|
|
|
|
// Create something
|
|
_, err := GetFn(context.Background(), "mock:/", create)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, Entries())
|
|
|
|
Clear()
|
|
|
|
assert.Equal(t, 0, Entries())
|
|
}
|
|
|
|
func TestEntries(t *testing.T) {
|
|
cleanup, create := mockNewFs(t)
|
|
defer cleanup()
|
|
|
|
assert.Equal(t, 0, Entries())
|
|
|
|
// Create something
|
|
_, err := GetFn(context.Background(), "mock:/", create)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, Entries())
|
|
}
|