github.com/sandwich-go/boost@v1.3.29/xos/copy_test.go (about) 1 package xos 2 3 import ( 4 "github.com/sandwich-go/boost/xhash/md5" 5 "github.com/sandwich-go/boost/xmap" 6 . "github.com/smartystreets/goconvey/convey" 7 "io/fs" 8 "os" 9 "path/filepath" 10 "sort" 11 "testing" 12 ) 13 14 func TestCopy(t *testing.T) { 15 Convey("copy a file", t, func() { 16 var file = "binary.go" 17 var dest = filepath.Join(os.TempDir(), file) 18 err := Copy(file, dest) 19 So(err, ShouldBeNil) 20 So(ExistsFile(dest), ShouldBeTrue) 21 var md50, md51 string 22 md50, err = md5.File(file) 23 So(err, ShouldBeNil) 24 md51, err = md5.File(dest) 25 So(err, ShouldBeNil) 26 So(md50, ShouldEqual, md51) 27 So(os.Remove(dest), ShouldBeNil) 28 }) 29 30 Convey("copy a dir", t, func() { 31 var files []string 32 var md5s = make(map[string]string) 33 var dir = "../xencoding/protobuf/test_perf" 34 err := FilePathWalkFollowLink(dir, func(path string, info fs.FileInfo, err error) error { 35 if !info.IsDir() { 36 files = append(files, info.Name()) 37 md5s[info.Name()], _ = md5.File(path) 38 } 39 return nil 40 }) 41 sort.Strings(files) 42 So(err, ShouldBeNil) 43 var destFiles []string 44 var destMd5s = make(map[string]string) 45 var dest = filepath.Join(os.TempDir(), "test_perf") 46 err = Copy(dir, dest) 47 So(err, ShouldBeNil) 48 So(Exists(dest), ShouldBeTrue) 49 err = FilePathWalkFollowLink(dir, func(path string, info fs.FileInfo, err error) error { 50 if !info.IsDir() { 51 destFiles = append(destFiles, info.Name()) 52 destMd5s[info.Name()], _ = md5.File(path) 53 } 54 return nil 55 }) 56 sort.Strings(destFiles) 57 So(files, ShouldResemble, destFiles) 58 So(len(md5s), ShouldEqual, len(destMd5s)) 59 So(xmap.EqualStringStringMap(md5s, destMd5s), ShouldBeTrue) 60 So(os.RemoveAll(dest), ShouldBeNil) 61 }) 62 }