github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/build/testing.go (about) 1 package build 2 3 import ( 4 "errors" 5 "io" 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 ) 10 11 var ( 12 // SiaTestingDir is the directory that contains all of the files and 13 // folders created during testing. 14 SiaTestingDir = filepath.Join(os.TempDir(), "SiaTesting") 15 ) 16 17 // TempDir joins the provided directories and prefixes them with the Sia 18 // testing directory. 19 func TempDir(dirs ...string) string { 20 path := filepath.Join(SiaTestingDir, filepath.Join(dirs...)) 21 os.RemoveAll(path) // remove old test data 22 return path 23 } 24 25 // CopyFile copies a file from a source to a destination. 26 func CopyFile(source, dest string) error { 27 sf, err := os.Open(source) 28 if err != nil { 29 return err 30 } 31 defer sf.Close() 32 33 df, err := os.Create(dest) 34 if err != nil { 35 return err 36 } 37 defer df.Close() 38 39 _, err = io.Copy(df, sf) 40 if err != nil { 41 return err 42 } 43 return nil 44 } 45 46 // CopyDir copies a directory and all of its contents to the destination 47 // directory. 48 func CopyDir(source, dest string) error { 49 stat, err := os.Stat(source) 50 if err != nil { 51 return err 52 } 53 if !stat.IsDir() { 54 return errors.New("source is not a directory") 55 } 56 57 err = os.MkdirAll(dest, stat.Mode()) 58 if err != nil { 59 return err 60 } 61 files, err := ioutil.ReadDir(source) 62 for _, file := range files { 63 newSource := filepath.Join(source, file.Name()) 64 newDest := filepath.Join(dest, file.Name()) 65 if file.IsDir() { 66 err = CopyDir(newSource, newDest) 67 if err != nil { 68 return err 69 } 70 } else { 71 err = CopyFile(newSource, newDest) 72 if err != nil { 73 return err 74 } 75 } 76 } 77 78 return nil 79 }