Add test for cache renaming functionality

This commit is contained in:
Brett Dutro 2019-10-28 12:05:41 -05:00 committed by Nick Craig-Wood
parent 5928704e1b
commit 53d55ae760
3 changed files with 34 additions and 8 deletions

View file

@ -6,7 +6,6 @@ import (
"context" "context"
"fmt" "fmt"
"os" "os"
"path"
"path/filepath" "path/filepath"
"runtime" "runtime"
"sort" "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 // findParent returns the parent directory of name, or "" for the root
func findParent(name string) string { func findParent(name string) string {
parent := path.Dir(name) parent := filepath.Dir(name)
if parent == "." || parent == "/" { if parent == "." || 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 // clean returns the cleaned version of name for use in the index map
func clean(name string) string { func clean(name string) string {
name = strings.Trim(name, "/") name = strings.Trim(name, "/")
name = path.Clean(name) name = filepath.Clean(name)
if name == "." || name == "/" { if name == "." || name == "/" {
name = "" name = ""
} }
@ -146,7 +145,7 @@ func (c *cache) toOSPath(name string) string {
// path for the file // path for the file
func (c *cache) mkdir(name string) (string, error) { func (c *cache) mkdir(name string) (string, error) {
parent := findParent(name) parent := findParent(name)
leaf := path.Base(name) leaf := filepath.Base(name)
parentPath := c.toOSPath(parent) parentPath := c.toOSPath(parent)
err := os.MkdirAll(parentPath, 0700) err := os.MkdirAll(parentPath, 0700)
if err != nil { if err != nil {

View file

@ -51,7 +51,7 @@ func itemAsString(c *cache) []string {
defer c.itemMu.Unlock() defer c.itemMu.Unlock()
var out []string var out []string
for name, item := range c.item { 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) sort.Strings(out)
return out return out
@ -386,11 +386,11 @@ func TestCachePurgeOld(t *testing.T) {
var removed []string var removed []string
removedDir := true removedDir := true
removeFile := func(name string) { removeFile := func(name string) {
removed = append(removed, name) removed = append(removed, filepath.ToSlash(name))
} }
removeDir := func(name string) bool { removeDir := func(name string) bool {
if removedDir { if removedDir {
removed = append(removed, name+"/") removed = append(removed, filepath.ToSlash(name)+"/")
} }
return removedDir return removedDir
} }
@ -483,7 +483,7 @@ func TestCachePurgeOverQuota(t *testing.T) {
// Test funcs // Test funcs
var removed []string var removed []string
remove := func(name string) { remove := func(name string) {
removed = append(removed, name) removed = append(removed, filepath.ToSlash(name))
c.remove(name) c.remove(name)
} }

View file

@ -609,3 +609,30 @@ func TestRWFileModTimeWithOpenWriters(t *testing.T) {
assert.Equal(t, info.ModTime().Unix(), mtime.Unix()) 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"))
}