Unverified Commit 94b38c2f authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #76 from ipfs/fix/73-mv-fails-to-clean-up

Fix directory mv and add tests
parents 30aee5fc 2b77b0a3
......@@ -83,6 +83,14 @@ func mkdirP(t *testing.T, root *Directory, pth string) *Directory {
return cur
}
func assertDirNotAtPath(root *Directory, pth string) error {
_, err := DirLookup(root, pth)
if err == nil {
return fmt.Errorf("%s exists in %s", pth, root.name)
}
return nil
}
func assertDirAtPath(root *Directory, pth string, children []string) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
......@@ -370,6 +378,114 @@ func TestDirectoryLoadFromDag(t *testing.T) {
}
}
func TestMvFile(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dagService, rt := setupRoot(ctx, t)
rootDir := rt.GetDirectory()
fi := getRandFile(t, dagService, 1000)
err := rootDir.AddChild("afile", fi)
if err != nil {
t.Fatal(err)
}
err = Mv(rt, "/afile", "/bfile")
if err != nil {
t.Fatal(err)
}
err = assertFileAtPath(dagService, rootDir, fi, "bfile")
if err != nil {
t.Fatal(err)
}
}
func TestMvFileToSubdir(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dagService, rt := setupRoot(ctx, t)
rootDir := rt.GetDirectory()
_ = mkdirP(t, rootDir, "test1")
fi := getRandFile(t, dagService, 1000)
err := rootDir.AddChild("afile", fi)
if err != nil {
t.Fatal(err)
}
err = Mv(rt, "/afile", "/test1")
if err != nil {
t.Fatal(err)
}
err = assertFileAtPath(dagService, rootDir, fi, "test1/afile")
if err != nil {
t.Fatal(err)
}
}
func TestMvFileToSubdirWithRename(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dagService, rt := setupRoot(ctx, t)
rootDir := rt.GetDirectory()
_ = mkdirP(t, rootDir, "test1")
fi := getRandFile(t, dagService, 1000)
err := rootDir.AddChild("afile", fi)
if err != nil {
t.Fatal(err)
}
err = Mv(rt, "/afile", "/test1/bfile")
if err != nil {
t.Fatal(err)
}
err = assertFileAtPath(dagService, rootDir, fi, "test1/bfile")
if err != nil {
t.Fatal(err)
}
}
func TestMvDir(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dagService, rt := setupRoot(ctx, t)
rootDir := rt.GetDirectory()
_ = mkdirP(t, rootDir, "test1")
d2 := mkdirP(t, rootDir, "test2")
fi := getRandFile(t, dagService, 1000)
err := d2.AddChild("afile", fi)
if err != nil {
t.Fatal(err)
}
err = Mv(rt, "/test2", "/test1")
if err != nil {
t.Fatal(err)
}
err = assertDirNotAtPath(rootDir, "test2")
if err != nil {
t.Fatal(err)
}
err = assertFileAtPath(dagService, rootDir, fi, "test1/test2/afile")
if err != nil {
t.Fatal(err)
}
}
func TestMfsFile(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
......
......@@ -21,29 +21,29 @@ import (
// Mv moves the file or directory at 'src' to 'dst'
// TODO: Document what the strings 'src' and 'dst' represent.
func Mv(r *Root, src, dst string) error {
srcDir, srcFname := gopath.Split(src)
srcDirName, srcFname := gopath.Split(src)
var dstDirStr string
var filename string
var dstDirName string
var dstFname string
if dst[len(dst)-1] == '/' {
dstDirStr = dst
filename = srcFname
dstDirName = dst
dstFname = srcFname
} else {
dstDirStr, filename = gopath.Split(dst)
dstDirName, dstFname = gopath.Split(dst)
}
// get parent directories of both src and dest first
dstDir, err := lookupDir(r, dstDirStr)
dstDir, err := lookupDir(r, dstDirName)
if err != nil {
return err
}
srcDirObj, err := lookupDir(r, srcDir)
srcDir, err := lookupDir(r, srcDirName)
if err != nil {
return err
}
srcObj, err := srcDirObj.Child(srcFname)
srcObj, err := srcDir.Child(srcFname)
if err != nil {
return err
}
......@@ -53,14 +53,14 @@ func Mv(r *Root, src, dst string) error {
return err
}
fsn, err := dstDir.Child(filename)
fsn, err := dstDir.Child(dstFname)
if err == nil {
switch n := fsn.(type) {
case *File:
_ = dstDir.Unlink(filename)
_ = dstDir.Unlink(dstFname)
case *Directory:
dstDir = n
filename = srcFname
dstFname = srcFname
default:
return fmt.Errorf("unexpected type at path: %s", dst)
}
......@@ -68,16 +68,16 @@ func Mv(r *Root, src, dst string) error {
return err
}
err = dstDir.AddChild(filename, nd)
err = dstDir.AddChild(dstFname, nd)
if err != nil {
return err
}
if srcDir == dstDirStr && srcFname == filename {
if srcDir.name == dstDir.name && srcFname == dstFname {
return nil
}
return srcDirObj.Unlink(srcFname)
return srcDir.Unlink(srcFname)
}
func lookupDir(r *Root, path string) (*Directory, error) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment