mirror of
https://github.com/rclone/rclone.git
synced 2024-11-22 08:23:47 +01:00
Add test for cache renaming functionality
This commit is contained in:
parent
5928704e1b
commit
53d55ae760
@ -6,7 +6,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sort"
|
||||
@ -120,7 +119,7 @@ func newCache(ctx context.Context, f fs.Fs, opt *Options) (*cache, error) {
|
||||
|
||||
// findParent returns the parent directory of name, or "" for the root
|
||||
func findParent(name string) string {
|
||||
parent := path.Dir(name)
|
||||
parent := filepath.Dir(name)
|
||||
if parent == "." || parent == "/" {
|
||||
parent = ""
|
||||
}
|
||||
@ -130,7 +129,7 @@ func findParent(name string) string {
|
||||
// clean returns the cleaned version of name for use in the index map
|
||||
func clean(name string) string {
|
||||
name = strings.Trim(name, "/")
|
||||
name = path.Clean(name)
|
||||
name = filepath.Clean(name)
|
||||
if name == "." || name == "/" {
|
||||
name = ""
|
||||
}
|
||||
@ -146,7 +145,7 @@ func (c *cache) toOSPath(name string) string {
|
||||
// path for the file
|
||||
func (c *cache) mkdir(name string) (string, error) {
|
||||
parent := findParent(name)
|
||||
leaf := path.Base(name)
|
||||
leaf := filepath.Base(name)
|
||||
parentPath := c.toOSPath(parent)
|
||||
err := os.MkdirAll(parentPath, 0700)
|
||||
if err != nil {
|
||||
|
@ -51,7 +51,7 @@ func itemAsString(c *cache) []string {
|
||||
defer c.itemMu.Unlock()
|
||||
var out []string
|
||||
for name, item := range c.item {
|
||||
out = append(out, fmt.Sprintf("name=%q isFile=%v opens=%d size=%d", name, item.isFile, item.opens, item.size))
|
||||
out = append(out, fmt.Sprintf("name=%q isFile=%v opens=%d size=%d", filepath.ToSlash(name), item.isFile, item.opens, item.size))
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out
|
||||
@ -386,11 +386,11 @@ func TestCachePurgeOld(t *testing.T) {
|
||||
var removed []string
|
||||
removedDir := true
|
||||
removeFile := func(name string) {
|
||||
removed = append(removed, name)
|
||||
removed = append(removed, filepath.ToSlash(name))
|
||||
}
|
||||
removeDir := func(name string) bool {
|
||||
if removedDir {
|
||||
removed = append(removed, name+"/")
|
||||
removed = append(removed, filepath.ToSlash(name)+"/")
|
||||
}
|
||||
return removedDir
|
||||
}
|
||||
@ -483,7 +483,7 @@ func TestCachePurgeOverQuota(t *testing.T) {
|
||||
// Test funcs
|
||||
var removed []string
|
||||
remove := func(name string) {
|
||||
removed = append(removed, name)
|
||||
removed = append(removed, filepath.ToSlash(name))
|
||||
c.remove(name)
|
||||
}
|
||||
|
||||
|
@ -609,3 +609,30 @@ func TestRWFileModTimeWithOpenWriters(t *testing.T) {
|
||||
assert.Equal(t, info.ModTime().Unix(), mtime.Unix())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCacheRename(t *testing.T) {
|
||||
r := fstest.NewRun(t)
|
||||
defer r.Finalise()
|
||||
|
||||
opt := DefaultOpt
|
||||
opt.CacheMode = CacheModeFull
|
||||
vfs := New(r.Fremote, &opt)
|
||||
|
||||
h, err := vfs.OpenFile("rename_me", os.O_WRONLY|os.O_CREATE, 0777)
|
||||
require.NoError(t, err)
|
||||
fh, ok := h.(*RWFileHandle)
|
||||
require.True(t, ok)
|
||||
|
||||
err = fh.Sync()
|
||||
require.NoError(t, err)
|
||||
err = fh.Close()
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.True(t, vfs.cache.exists("rename_me"))
|
||||
|
||||
err = vfs.Rename("rename_me", "i_was_renamed")
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.False(t, vfs.cache.exists("rename_me"))
|
||||
assert.True(t, vfs.cache.exists("i_was_renamed"))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user