From 53d55ae7608cdd1e07827aaa9dbaecf32764126a Mon Sep 17 00:00:00 2001 From: Brett Dutro Date: Mon, 28 Oct 2019 12:05:41 -0500 Subject: [PATCH] Add test for cache renaming functionality --- vfs/cache.go | 7 +++---- vfs/cache_test.go | 8 ++++---- vfs/read_write_test.go | 27 +++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/vfs/cache.go b/vfs/cache.go index c84d2c76b..41ace3005 100644 --- a/vfs/cache.go +++ b/vfs/cache.go @@ -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 { diff --git a/vfs/cache_test.go b/vfs/cache_test.go index 4f29d46dc..d6affea3c 100644 --- a/vfs/cache_test.go +++ b/vfs/cache_test.go @@ -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) } diff --git a/vfs/read_write_test.go b/vfs/read_write_test.go index 257d120ce..3d1df648a 100644 --- a/vfs/read_write_test.go +++ b/vfs/read_write_test.go @@ -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")) +}