util: Patch CopyFs and add tests
Fix CopyFs bug that resulted in a flattened destination directory. Added tests catch this bug, and ensure the data is in fact copied to the destination directory.
This commit is contained in:
@@ -22,7 +22,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
)
|
)
|
||||||
@@ -89,19 +88,20 @@ func stringify(fs afero.Fs, name string, indent []bool) (string, error) {
|
|||||||
// currently undefined.
|
// currently undefined.
|
||||||
// TODO: this should be made more rsync like and robust!
|
// TODO: this should be made more rsync like and robust!
|
||||||
func CopyFs(srcFs, dstFs afero.Fs, src, dst string, force bool) error {
|
func CopyFs(srcFs, dstFs afero.Fs, src, dst string, force bool) error {
|
||||||
if src == "" {
|
src = path.Join("/", src)
|
||||||
src = "/"
|
dst = path.Join("/", dst)
|
||||||
}
|
|
||||||
if dst == "" {
|
// TODO: clean this up with function that gets parent dir?
|
||||||
dst = "/"
|
src = path.Clean(src)
|
||||||
}
|
parentDir, _ := path.Split(src)
|
||||||
|
srcFsLen := len(parentDir)
|
||||||
|
|
||||||
walkFn := func(name string, info os.FileInfo, err error) error {
|
walkFn := func(name string, info os.FileInfo, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
//perm := info.Perm()
|
perm := info.Mode() // get file permissions
|
||||||
perm := info.Mode() // TODO: is this correct?
|
p := path.Join(dst, name[srcFsLen:])
|
||||||
p := path.Join(dst, filepath.Base(name))
|
|
||||||
if info.IsDir() {
|
if info.IsDir() {
|
||||||
err := dstFs.Mkdir(p, perm)
|
err := dstFs.Mkdir(p, perm)
|
||||||
if os.IsExist(err) && (name == "/" || force) {
|
if os.IsExist(err) && (name == "/" || force) {
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -27,10 +28,96 @@ import (
|
|||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCopyDiskToFs1(t *testing.T) {
|
var dirInputs = []struct {
|
||||||
if true {
|
srcDirFull string
|
||||||
return // XXX: remove me once this test passes
|
srcCopyRoot string
|
||||||
|
dstCopyRoot string
|
||||||
|
dstExpected string
|
||||||
|
force bool
|
||||||
|
}{
|
||||||
|
{"/tmp/foo/bar/baz/", "/tmp/foo/", "/", "/foo/bar/baz", false},
|
||||||
|
{"/tmp/zoo/zar/zaz/", "/tmp/zoo/zar", "/start/dir", "/start/dir/zar/zaz", false},
|
||||||
|
{"/foo", "/foo", "/", "/foo", false},
|
||||||
|
{"/foo", "/foo", "/", "/foo", true},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCopyFs1(t *testing.T) {
|
||||||
|
for _, tt := range dirInputs {
|
||||||
|
src := afero.NewMemMapFs()
|
||||||
|
dst := afero.NewMemMapFs()
|
||||||
|
|
||||||
|
t.Run(tt.srcDirFull, func(t *testing.T) {
|
||||||
|
err := src.MkdirAll(tt.srcDirFull, 0700)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("could not MkdirAll %+v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = CopyFs(src, dst, tt.srcCopyRoot, tt.dstCopyRoot, tt.force)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("error copying source %s to dest %s", tt.srcCopyRoot, tt.dstCopyRoot)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
isDir, err := afero.IsDir(dst, tt.dstExpected)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("could not check IsDir: %+v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !isDir {
|
||||||
|
t.Errorf("expected directory tree %s to exist in dest", tt.dstExpected)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCopyFs2(t *testing.T) {
|
||||||
|
tree := "/foo/bar/baz/"
|
||||||
|
var files = []struct {
|
||||||
|
path string
|
||||||
|
content []byte
|
||||||
|
}{
|
||||||
|
{"/foo/foo.txt", []byte("foo")},
|
||||||
|
{"/foo/bar/bar.txt", []byte("bar")},
|
||||||
|
{"/foo/bar/baz/baz.txt", []byte("baz")},
|
||||||
|
}
|
||||||
|
|
||||||
|
src := afero.NewMemMapFs()
|
||||||
|
dst := afero.NewMemMapFs()
|
||||||
|
|
||||||
|
err := src.MkdirAll(tree, 0700)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("could not MkdirAll: %+v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, f := range files {
|
||||||
|
err = afero.WriteFile(src, f.path, f.content, 0600)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("could not WriteFile: %+v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = CopyFs(src, dst, "", "", false); err != nil {
|
||||||
|
t.Errorf("could not CopyFs: %+v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, f := range files {
|
||||||
|
content, err := afero.ReadFile(dst, f.path)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("could not ReadFile: %+v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !bytes.Equal(content, f.content) {
|
||||||
|
t.Errorf("expected: %s, actual: %s, for file %s", string(f.content), string(content), f.path)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCopyDiskToFs1(t *testing.T) {
|
||||||
dir, err := TestDirFull()
|
dir, err := TestDirFull()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("could not get tests directory: %+v", err)
|
t.Errorf("could not get tests directory: %+v", err)
|
||||||
|
|||||||
Reference in New Issue
Block a user