123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- package cstest
- import (
- "fmt"
- "os"
- "path/filepath"
- "testing"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- )
- func Copy(sourceFile string, destinationFile string) error {
- input, err := os.ReadFile(sourceFile)
- if err != nil {
- return err
- }
- err = os.WriteFile(destinationFile, input, 0644)
- if err != nil {
- return err
- }
- return nil
- }
- // checkPathNotContained returns an error if 'subpath' is inside 'path'
- func checkPathNotContained(path string, subpath string) error {
- absPath, err := filepath.Abs(path)
- if err != nil {
- return err
- }
- absSubPath, err := filepath.Abs(subpath)
- if err != nil {
- return err
- }
- current := absSubPath
- for {
- if current == absPath {
- return fmt.Errorf("cannot copy a folder onto itself")
- }
- up := filepath.Dir(current)
- if current == up {
- break
- }
- current = up
- }
- return nil
- }
- func CopyDir(src string, dest string) error {
- err := checkPathNotContained(src, dest)
- if err != nil {
- return err
- }
- f, err := os.Open(src)
- if err != nil {
- return err
- }
- file, err := f.Stat()
- if err != nil {
- return err
- }
- if !file.IsDir() {
- return fmt.Errorf("Source " + file.Name() + " is not a directory!")
- }
- err = os.MkdirAll(dest, 0755)
- if err != nil {
- return err
- }
- files, err := os.ReadDir(src)
- if err != nil {
- return err
- }
- for _, f := range files {
- if f.IsDir() {
- err = CopyDir(src+"/"+f.Name(), dest+"/"+f.Name())
- if err != nil {
- return err
- }
- }
- if !f.IsDir() {
- content, err := os.ReadFile(src + "/" + f.Name())
- if err != nil {
- return err
- }
- err = os.WriteFile(dest+"/"+f.Name(), content, 0755)
- if err != nil {
- return err
- }
- }
- }
- return nil
- }
- func AssertErrorContains(t *testing.T, err error, expectedErr string) {
- t.Helper()
- if expectedErr != "" {
- assert.ErrorContains(t, err, expectedErr)
- return
- }
- assert.NoError(t, err)
- }
- func RequireErrorContains(t *testing.T, err error, expectedErr string) {
- t.Helper()
- if expectedErr != "" {
- require.ErrorContains(t, err, expectedErr)
- return
- }
- require.NoError(t, err)
- }
|